Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnicodeEncodeError 'ascii' codec can't encode character - ordinal not in range(128) #21

Open
Thinkersbluff opened this issue Feb 11, 2023 · 2 comments

Comments

@Thinkersbluff
Copy link
Owner

I encountered this problem, while trying to print a gcode file whose name included this non-ascii character: æ.

The attached snippet from Klipper.log shows what Klipper was able to report.

To troubleshoot this issue, I looked-up the ascii character xe6 that it was complaining about, and recognized the æ character in the filename. Sure enough, delete that character from the filename and t5uid1 is very happy to go ahead and print it.

I expect that the problem is incurred when the code that displays the current filename at the top of the Print Status screen tries to encode the message to the display.

It would probably be helpful if there is some way to substitute '_' for all non-ascii (128) characters in such strings, before they reach that routine.
Can someone in the Community please suggest how I might go about adding that?

KlippyLogExtract.txt

@Thinkersbluff
Copy link
Owner Author

Thinkersbluff commented Feb 12, 2023

Coders familiar with encoding and decoding unicode strings in python may recognize the root of my troubles being that t5uid1.py is assuming all strings to be composed of some combination of the 128 ascii characters, while python3 works in unicode or utf-8.
The æ character is of course not within that set of ascii characters.
This article offers some ideas, but I would rather not ignore the æ, or replace æ with a "?".

xmlcharrefreplace inserted \x.. character pointers, on the display. Party of my problem may lie in the DWIN_SET definition of this particular field...

I can use errors="ignore" for now, but looking for a better option...

@Thinkersbluff
Copy link
Owner Author

Thinkersbluff commented Feb 12, 2023

Since the current app seems to be having trouble executing vars_out.filename, I have implemented a local solution, with this mod to the filename string encoding instructions:

[t5uid1_var file_name]
type: output
address: 0x31c1
data_type: str
data_len: 32
script:
{% if printer.print_stats.filename %}
{"{:>32s}".format((printer.print_stats.filename.encode(encoding="ascii",errors="ignore").decode())|trim)}
{% else %}
{"No name passed by slicer"}
{% endif %}

Now any non-ascii character(s) in the filename are simply left out of the filename.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant