Skip to content

Commit

Permalink
fix compilation for Python3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
supermihi committed Nov 17, 2023
1 parent e4c7f39 commit 783143f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/ctypes.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ cdef extern from 'taglib/tfile.h' namespace 'TagLib':

cdef extern from 'taglib/fileref.h' namespace 'TagLib::FileRef':
IF UNAME_SYSNAME == "Windows":
cdef File* create(const Py_UNICODE*) except +
cdef File* create(const wchar_t*) except +
ELSE:
cdef File* create(const char*) except +

Expand Down
15 changes: 9 additions & 6 deletions src/taglib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ cdef dict propertyMapToDict(ctypes.PropertyMap map):
dct[tag].append(toStr(value))
return dct

cdef ctypes.File* create_wrapper(unicode path):
IF UNAME_SYSNAME == "Windows":
cdef wchar_t *wchar_path = PyUnicode_AsWideCharString(path, NULL)
cdef ctypes.File* file = ctypes.create(wchar_path)
PyMem_Free(wchar_path)
return file
ELSE:
return ctypes.create(path)

cdef class File:
"""Class representing an audio file with metadata ("tags").
Expand Down Expand Up @@ -80,12 +88,7 @@ cdef class File:
path = path.decode('utf8')
path = Path(path)
self.path = path
IF UNAME_SYSNAME == "Windows":
# create on windows takes wchar_t* which Cython automatically converts to
# from unicode strings
self.cFile = ctypes.create(str(self.path))
ELSE:
self.cFile = ctypes.create(str(self.path).encode('utf8'))
self.cFile = create_wrapper(str(self.path))
if not self.cFile or not self.cFile.isValid():
raise OSError(f'Could not read file {path}')

Expand Down

0 comments on commit 783143f

Please sign in to comment.