Skip to content

Commit

Permalink
Merge pull request #1 from nbraem/master
Browse files Browse the repository at this point in the history
fix for unicode filenames
  • Loading branch information
lejafar authored Oct 18, 2018
2 parents 08bdc16 + fc5cbbd commit 5681d11
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
21 changes: 18 additions & 3 deletions openctm/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __eq__(self, other):
def import_mesh(_filename):
ctm_context = ctmNewContext(CTM_IMPORT)
try:
ctmLoad(ctm_context, str(_filename).encode('utf-8'))
ctmLoad(ctm_context, _encode(_filename))
err = ctmGetError(ctm_context)
if err != CTM_NONE:
raise IOError("Error loading file: %s" % str(ctmErrorString(err)))
Expand Down Expand Up @@ -55,7 +55,7 @@ def import_mesh(_filename):
def export_mesh(_ctm, _filename):
ctm_context = ctmNewContext(CTM_EXPORT)

if not _filename.lower().endswith('.ctm'):
if not str(_filename).lower().endswith('.ctm'):
_filename += '.ctm'

try:
Expand All @@ -81,6 +81,21 @@ def export_mesh(_ctm, _filename):
p_normals = None

ctmDefineMesh(ctm_context, p_vertices, CTMuint(vertex_count), p_faces, CTMuint(face_count), p_normals)
ctmSave(ctm_context, ctypes.c_char_p(_filename.encode('utf-8')))
ctmSave(ctm_context, ctypes.c_char_p(_encode(_filename)))
finally:
ctmFreeContext(ctm_context)

def _encode(_filename):
try:
return str(_filename).encode("utf-8")
except UnicodeEncodeError:
pass

try:
# works fine for pathlib.Path
return bytes(_filename)
except TypeError:
pass

return str(_filename).encode("utf-8", "surrogateescape")

2 changes: 1 addition & 1 deletion openctm/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.0.9'
__version__ = '1.0.10'

0 comments on commit 5681d11

Please sign in to comment.