Skip to content

Commit

Permalink
Merge pull request #124 from ufleisch/taglib2
Browse files Browse the repository at this point in the history
Fix building with TagLib 2.0 on macOS and Windows
  • Loading branch information
supermihi authored Mar 16, 2024
2 parents e0350f0 + d879acc commit 130b1e7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
build:
strategy:
matrix:
os: [ubuntu-latest, macos-latest] #, windows-latest]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
env:
#CIBW_SKIP: "*p36-* *p37-*"
Expand Down Expand Up @@ -41,7 +41,7 @@ jobs:
path: dist
retention-days: 5
- name: build binary wheels
uses: pypa/[email protected].2
uses: pypa/[email protected].5
- name: upload wheels
uses: actions/upload-artifact@v3
with:
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def extension_kwargs():
str(taglib_install_dir / "lib"),
str(taglib_install_dir / "lib64"),
],
extra_compile_args=["-std=c++17"],
extra_link_args=["-std=c++17"],
)


Expand Down
21 changes: 15 additions & 6 deletions src/ctypes.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@

"""This file contains the external C/C++ definitions used by taglib.pyx."""

from libc.stddef cimport wchar_t
from libcpp.list cimport list
from libcpp.map cimport map
from libcpp.string cimport string
from cpython.mem cimport PyMem_Free
from cpython.object cimport PyObject


cdef extern from "Python.h":
cdef wchar_t *PyUnicode_AsWideCharString(PyObject *path, Py_ssize_t *size)


cdef extern from 'taglib/tstring.h' namespace 'TagLib::String':
Expand Down Expand Up @@ -69,7 +76,7 @@ cdef extern from 'taglib/tiostream.h' namespace 'TagLib':
ctypedef char* FileName
ELSE:
cdef cppclass FileName:
FileName(const char*)
FileName(const wchar_t*)

cdef extern from 'taglib/fileref.h' namespace 'TagLib':
cdef cppclass FileRef:
Expand All @@ -82,13 +89,15 @@ cdef extern from 'taglib/fileref.h' namespace 'TagLib':
PropertyMap setProperties(PropertyMap&)
void removeUnsupportedProperties(StringList&)

cdef inline FileRef* create_wrapper(char* path) except +:
cdef inline FileRef* create_wrapper(unicode path) except +:
IF UNAME_SYSNAME != "Windows":
return new FileRef(path, True, ReadStyle.Average)
return new FileRef(path.encode('utf-8'), True, ReadStyle.Average)
ELSE:
cdef FileName fn = FileName(path)
return new FileRef(fn, True, ReadStyle.Average)
cdef wchar_t *wchar_path = PyUnicode_AsWideCharString(<PyObject*>path, NULL)
cdef FileRef *file_ref = new FileRef(FileName(wchar_path), True, ReadStyle.Average)
PyMem_Free(wchar_path)
return file_ref

cdef extern from 'taglib/taglib.h':
int TAGLIB_MAJOR_VERSION
int TAGLIB_MINOR_VERSION
int TAGLIB_MINOR_VERSION
6 changes: 3 additions & 3 deletions src/taglib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ cdef class File:
path = path.decode('utf-8')
path = Path(path)
self.path = path
self.cFile = ctypes.create_wrapper(str(path).encode('utf-8'))
self.cFile = ctypes.create_wrapper(str(path))
if self.cFile is NULL or self.cFile.file() is NULL or not self.cFile.file().isValid():
raise OSError(f'Could not read file {path}')

Expand All @@ -93,7 +93,7 @@ cdef class File:

cdef void readProperties(self):
"""Convert the Taglib::PropertyMap of the wrapped Taglib::File object into a python dict.
This method is not accessible from Python, and is called only once, immediately after
object creation.
"""
Expand Down Expand Up @@ -219,4 +219,4 @@ def taglib_version() -> tuple[int, int]:
circumstances (e.g. dynamic linking, or re-using the cythonized code after
upgrading Taglib) the actually running Taglib version might be different.
"""
return ctypes.TAGLIB_MAJOR_VERSION, ctypes.TAGLIB_MINOR_VERSION
return ctypes.TAGLIB_MAJOR_VERSION, ctypes.TAGLIB_MINOR_VERSION

0 comments on commit 130b1e7

Please sign in to comment.