"The typemap is there to make sure the resulting string is utf-8 encoded".
So that is not true.
Let me try to explain with simple examples. So let's look at "fileeraser" and "trashcan"
for root, dirs, files in os.walk(trash, topdown=False):
for name in files:
fn = os.path.join(root, name)
try:
fn = fn.encode(encoding="utf8", errors="ignore").decode(encoding="utf8") # ensure string is all utf-8, if dataset name changed, erase will handle not found.
enigma.eBackgroundFileEraser.getInstance().erase(fn)
except Exception as e:
print("[Trashcan] Failed to erase file:", name, " ", e)
fn = fn.encode(encoding="utf8", errors="ignore").decode(encoding="utf8")
So if we "ignore" that is not going to work and we end up with a filename that is wrong so impossible to erase... so... we want to read the filenames in bytes...
for root, dirs, files in os.walk(trash.encode(), topdown=False): # handle non utf-8 filenames
To do that we encode the input.
trash.encode()
Now python returns bytes, not strings from the directory walk.
So now we want to pass that bytestring to "file_eraser.cpp". The result is a crash. c++ is expecting type std::string&, not bytes.
To accept the bytestring we add the typemap (as an extra, the old code still works on string inputs).
So now the bytesring from os.walk is fed to "file_eraser.cpp" and the file is successfully erased.
-------------------------------------------------------------------
The chardet code you can play with in the Python interpreter.
It stops the Python crash and displays the correct characters in the GUI.