gcode: Use regular str() types for g-code commands on Python2

Avoid using unicode() types on Python2 as it can lead to subtle
errors.

Also, accept utf8 on gcode input (instead of just ascii).

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2021-10-31 13:39:01 -04:00
parent 85d0ef974c
commit d6c3aaad8a
1 changed files with 3 additions and 3 deletions

View File

@ -381,13 +381,13 @@ class GCodeIO:
def _process_data(self, eventtime): def _process_data(self, eventtime):
# Read input, separate by newline, and add to pending_commands # Read input, separate by newline, and add to pending_commands
try: try:
data = os.read(self.fd, 4096) data = str(os.read(self.fd, 4096).decode('utf8'))
except os.error: except os.error, UnicodeDecodeError:
logging.exception("Read g-code") logging.exception("Read g-code")
return return
self.input_log.append((eventtime, data)) self.input_log.append((eventtime, data))
self.bytes_read += len(data) self.bytes_read += len(data)
lines = data.decode().split('\n') lines = data.split('\n')
lines[0] = self.partial_input + lines[0] lines[0] = self.partial_input + lines[0]
self.partial_input = lines.pop() self.partial_input = lines.pop()
pending_commands = self.pending_commands pending_commands = self.pending_commands