Skip to content

Commit

Permalink
Python 3: Return strs (not bytes) from our cythonized module
Browse files Browse the repository at this point in the history
Also, import it properly, as it was not cythonized before and was not caught
by the lib2to3 import fixer.
  • Loading branch information
hroncok committed Feb 13, 2018
1 parent 20d3cd3 commit 32e2331
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion printrun/gcoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def __getattr__(self, name):
return None

try:
import gcoder_line
from . import gcoder_line
Line = gcoder_line.GLine
LightLine = gcoder_line.GLightLine
except Exception as e:
Expand Down
11 changes: 7 additions & 4 deletions printrun/gcoder_line.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#cython: language_level=3
#
# This file is copied from GCoder.
#
# GCoder is free software: you can redistribute it and/or modify
Expand All @@ -18,6 +20,7 @@ from libc.stdint cimport uint8_t, uint32_t
from libc.string cimport strlen, strncpy

cdef char* copy_string(object value):
value = value.encode('utf-8')
cdef char* orig = value
str_len = len(orig)
cdef char* array = <char *>malloc(str_len + 1)
Expand Down Expand Up @@ -192,7 +195,7 @@ cdef class GLine:
self._status = set_has_var(self._status, pos_gcview_end_vertex)
property raw:
def __get__(self):
if has_var(self._status, pos_raw): return self._raw
if has_var(self._status, pos_raw): return self._raw.decode('utf-8')
else: return None
def __set__(self, value):
# WARNING: memory leak could happen here, as we don't do the following :
Expand All @@ -201,7 +204,7 @@ cdef class GLine:
self._status = set_has_var(self._status, pos_raw)
property command:
def __get__(self):
if has_var(self._status, pos_command): return self._command
if has_var(self._status, pos_command): return self._command.decode('utf-8')
else: return None
def __set__(self, value):
# WARNING: memory leak could happen here, as we don't do the following :
Expand Down Expand Up @@ -231,7 +234,7 @@ cdef class GLightLine:

property raw:
def __get__(self):
if has_var(self._status, pos_raw): return self._raw
if has_var(self._status, pos_raw): return self._raw.decode('utf-8')
else: return None
def __set__(self, value):
# WARNING: memory leak could happen here, as we don't do the following :
Expand All @@ -240,7 +243,7 @@ cdef class GLightLine:
self._status = set_has_var(self._status, pos_raw)
property command:
def __get__(self):
if has_var(self._status, pos_command): return self._command
if has_var(self._status, pos_command): return self._command.decode('utf-8')
else: return None
def __set__(self, value):
# WARNING: memory leak could happen here, as we don't do the following :
Expand Down

0 comments on commit 32e2331

Please sign in to comment.