Klipper3d / klipper

Klipper is a 3d-printer firmware
GNU General Public License v3.0
8.99k stars 5.18k forks source link

virtual_sdcard: Open file in utf-8 encoding #6430

Closed CODeRUS closed 4 months ago

CODeRUS commented 6 months ago

When printing file with utf-8 comments in it, pause/resume breaks printing process, it tries to resume at wrong position, caused by utf-8 (or utf-16) characters byte size.

00:12:07  // Unknown command:"F30000"
00:12:40  // Unknown command:"Y109.644"
00:13:04  // Unknown command:"ERIMETER"
00:13:33  // Unknown command:"Y117.633"
00:14:17  // Unknown command:"Y107.81"

This simple PR fixes issue

github-actions[bot] commented 5 months ago

Thank you for your contribution to Klipper. Unfortunately, a reviewer has not assigned themselves to this GitHub Pull Request. All Pull Requests are reviewed before merging, and a reviewer will need to volunteer. Further information is available at: https://www.klipper3d.org/CONTRIBUTING.html

There are some steps that you can take now:

  1. Perform a self-review of your Pull Request by following the steps at: https://www.klipper3d.org/CONTRIBUTING.html#what-to-expect-in-a-review If you have completed a self-review, be sure to state the results of that self-review explicitly in the Pull Request comments. A reviewer is more likely to participate if the bulk of a review has already been completed.
  2. Consider opening a topic on the Klipper Discourse server to discuss this work. The Discourse server is a good place to discuss development ideas and to engage users interested in testing. Reviewers are more likely to prioritize Pull Requests with an active community of users.
  3. Consider helping out reviewers by reviewing other Klipper Pull Requests. Taking the time to perform a careful and detailed review of others work is appreciated. Regular contributors are more likely to prioritize the contributions of other regular contributors.

Unfortunately, if a reviewer does not assign themselves to this GitHub Pull Request then it will be automatically closed. If this happens, then it is a good idea to move further discussion to the Klipper Discourse server. Reviewers can reach out on that forum to let you know if they are interested and when they are available.

Best regards, ~ Your friendly GitIssueBot

PS: I'm just an automated script, not a human being.

thijstriemstra commented 5 months ago

looks like a sensible change? 7 thumbs up on this PR, what do you think @KevinOConnor?

ksergey commented 5 months ago

Please review and accept.

Failed to complete printing several times after pause, because non-ascii comments inside gcode.

Thanks

KevinOConnor commented 5 months ago

Thanks. However, this change does not look correct to me (at least on Python2). When opening up a file in utf-8 encoding on python2 it changes the internal representation from a str() object to a unicode() object which has weird trickle down implications throughout the entire g-code processing stream. (This weird Python2 handling of unicode() objects is likely why Python3 got rid of that whole system.)

I think we'll need to find the root cause of the underlying file position tracking problem. It would help if you could report the steps used to recreate the problem and whether or not it was py2 or py3.

Thanks again, -Kevin

zxy16305 commented 5 months ago

The resume process begins at self.current_file.seek(self.file_position), where self.file_position represents a byte-length offset. However, file_position is incremented by len(line), which corresponds to the string length. If there are any UTF-8 characters preceding the 'PAUSE' command, the file position must be set before the occurrence of 'PAUSE'.

To address this issue, replacing the string length with the byte length is sufficient. Please find the modification below:

https://github.com/Klipper3d/klipper/pull/6469


# Before
# next_file_position = self.file_position + len(line) + 1

# After
# next_file_position = self.file_position + len(line.encode()) + 1
KevinOConnor commented 5 months ago

Hopefully this is fixed now with the merge of #6472 .

Thanks. -Kevin

github-actions[bot] commented 4 months ago

This ticket is being closed because the underlying issue is now thought to be resolved.

Best regards, ~ Your friendly GitIssueBot

PS: I'm just an automated script, not a human being.