Romern / syncMyMoodle

Synchronization client for RWTH Moodle
GNU General Public License v3.0
72 stars 18 forks source link

Codec error #80

Closed NiklasVousten closed 3 years ago

NiklasVousten commented 3 years ago

When syncing old data, errors with unicode or the codec are thrown:

ERROR:__main__:Failed to download the module Node(name=FoSAP Klausur 16-1 mit Lo\u0308sungen.pdf, id=https://moodle.rwth-aachen.de/webservice/pluginfile.php/467171/mod_folder/content/7/FoSAP%20Klausur%2016-1%20mit%20Lo%CC%88sungen.pdf?forcedownload=1, url=https://moodle.rwth-aachen.de/pluginfile.php/467171/mod_folder/content/7/FoSAP%20Klausur%2016-1%20mit%20Lo%CC%88sungen.pdf, type=Folder File)
Traceback (most recent call last):
  File "E:\Workspace\GIT\github.com\syncMyMoodle\syncmymoodle\__main__.py", line 734, in _download_all_files
    self.download_file(cur_node)
  File "E:\Workspace\GIT\github.com\syncMyMoodle\syncmymoodle\__main__.py", line 779, in download_file
    print(f"Downloading {downloadpath} [{node.type}]")
  File "C:\Users\Niklas\AppData\Local\Programs\Python\Python39\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u0308' in position 104: character maps to <undefined>
septatrix commented 3 years ago

Thanks for the traceback. I have already found the problem (though do not yet know what the best solution is). It seems like python cannot encode the name of the node. A minimal working example should be print("o\u0308").

The sequence o%CC%88 is the url encoded value for ö(o\xcc\x88). Note that this is different from the variant \xc3\xb6 which stands also for ö. These are two different unicode representations of the same character.

I made a small script to demonstrate this:

Script and output ```python3 import unicodedata from urllib.parse import unquote a = unquote("%C3%B6") b = unquote("o%CC%88") print(f"Variant A: {a}") print(f"Variant B: {b}") for form in ["NFC", "NFD"]: print() print(f"Form: {form}") print(f"A: {unicodedata.normalize(form, a).encode()}") print(f"B: {unicodedata.normalize(form, b).encode()}") print() print(f"Variant A (utf8): {a.encode('utf8')}") print(f"Variant B (utf8): {b.encode('utf8')}") print() print(f"Variant A (latin1): {a.encode('cp1252')}") print(f"Variant B (latin1): {b.encode('cp1252')}") ``` ``` Variant A: ö Variant B: ö Form: NFC A: b'\xc3\xb6' B: b'\xc3\xb6' Form: NFD A: b'o\xcc\x88' B: b'o\xcc\x88' Variant A (utf8): b'\xc3\xb6' Variant B (utf8): b'o\xcc\x88' Variant A (latin1): b'\xf6' Traceback (most recent call last): File "/tmp/septatrix-playground-2021-10-25T18-14-34-zhP/codec.py", line 22, in print(f"Variant B (latin1): {b.encode('cp1252')}") File "/usr/lib64/python3.9/encodings/cp1252.py", line 12, in encode UnicodeEncodeError: 'charmap' codec can't encode character '\u0308' in position 1: character maps to ```

However I am not sure how we should solve this. We could always normalize these to the NFC unicode form but that would also lead to different filenames and potentially redownloading them.

Sadly I currently do not have a windows machine at hand and this works under linux as this depends on the terminal encoding. Should you have access to another terminal emulator (and shell) you might try if the minimal example I gave above works in some of them e.g. the new Windows Terminal or an embedded terminal in some IDE (in combination with powershell.exe though it should depend on the terminal).

septatrix commented 3 years ago

Okay after I found out about PEP 528 it seems that this should no longer be the case if you are running a modern version of python. Could you please check and tell me which version you have installed?

PS: Based on your traceback it seems like you are using 3.9 - is that correct? Did you maybe redirect the output to a file?

NiklasVousten commented 3 years ago

Correct. Im using python 3.9 inside a virtual environment. But i think I have started it inside the git bash terminal. I'll try with a different terminal on windows. Maybe it is a git bash problem

Edit: It works in powershell, the files got downloaded. So the problem is the git bash on windows

septatrix commented 3 years ago

I am inclined to close this as a wontfix. While we could check the encoding of the terminal and encode every string with the backslashreplace fallback this is not really a clean solution. Given that this seems to only happen in the git-bash terminal (and for a colleague of mine not event there) and the workaround is rather simple this is not worth the effort. Additionally I have a branch laying around on which the remaining prints are replaced with logging statements which are written to stderr by default which should always use the backslashreplace.