Skip to content

Commit

Permalink
Add patch for missing PyInterpreterState_GetID on PyPy
Browse files Browse the repository at this point in the history
  • Loading branch information
althonos committed Aug 16, 2023
1 parent c6f3be1 commit 1e34754
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
10 changes: 10 additions & 0 deletions pyhmmer/_getid.pxi
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# --- Patch for PyPy 3.9 -----------------------------------------------------

cdef extern from *:
"""
#ifndef HAS_PYINTERPRETERSTATE_GETID
int64_t PyInterpreterState_GetID(PyInterpreterState *interp) {
return 0;
}
#endif
"""
2 changes: 2 additions & 0 deletions pyhmmer/daemon.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ from pyhmmer.easel cimport Sequence, Alphabet, MSA, KeyHash
from pyhmmer.errors import UnexpectedError, AllocationError, ServerError
from pyhmmer.plan7 cimport Builder, TopHits, Pipeline, HMM, Background

include "_getid.pxi"


# --- Python imports ---------------------------------------------------------

Expand Down
1 change: 1 addition & 0 deletions pyhmmer/easel.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ elif PLATFORM_UNAME_SYSTEM == "Darwin" or PLATFORM_UNAME_SYSTEM.endswith("BSD"):
from .fileobj.bsd cimport fileobj_bsd_open as fopen_obj

include "exceptions.pxi"
include "_getid.pxi"

# --- Python imports ---------------------------------------------------------

Expand Down
2 changes: 2 additions & 0 deletions pyhmmer/errors.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
cimport libeasel
cimport libeasel.alphabet

include "_getid.pxi"

statuscode = {
libeasel.eslOK: "eslOK",
libeasel.eslFAIL: "eslFAIL",
Expand Down
1 change: 1 addition & 0 deletions pyhmmer/plan7.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ elif PLATFORM_UNAME_SYSTEM == "Darwin" or PLATFORM_UNAME_SYSTEM.endswith("BSD"):
from .fileobj.bsd cimport fileobj_bsd_open as fopen_obj

include "exceptions.pxi"
include "_getid.pxi"


# --- Python imports ---------------------------------------------------------
Expand Down
43 changes: 42 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,45 @@ def finalize_options(self):
if self.parallel == 0:
self.parallel = os.cpu_count()

def _check_getid(self):
_eprint('checking whether `PyInterpreterState_GetID` is available')

base = "have_getid"
testfile = os.path.join(self.build_temp, "{}.c".format(base))
objects = []

self.mkpath(self.build_temp)
with open(testfile, "w") as f:
f.write("""
#include <stdint.h>
#include <stdlib.h>
#include <Python.h>
int main(int argc, char *argv[]) {{
PyInterpreterState_GetID(NULL);
return 0;
}}
""")

if self.compiler.compiler_type == "msvc":
flags = ["/WX"]
else:
flags = ["-Werror=implicit-function-declaration"]

try:
self.mkpath(self.build_temp)
objects = self.compiler.compile([testfile], extra_postargs=flags)
except CompileError:
_eprint("no")
return False
else:
_eprint("yes")
return True
finally:
os.remove(testfile)
for obj in filter(os.path.isfile, objects):
os.remove(obj)

def run(self):
# check `cythonize` is available
if isinstance(cythonize, ImportError):
Expand Down Expand Up @@ -160,10 +199,12 @@ def build_extension(self, ext):
ext.define_macros.append(("CYTHON_WITHOUT_ASSERTIONS", 1))
if self.compiler.compiler_type in {"unix", "cygwin", "mingw32"}:
ext.extra_compile_args.append("-Wno-unused-variable")

# remove universal binary CFLAGS from the compiler if any
if platform.system() == "Darwin":
_patch_osx_compiler(self.compiler)
# make sure the PyInterpreterState_GetID() function is available
if self._check_getid():
ext.define_macros.append(("HAS_PYINTERPRETERSTATE_GETID", 1))

# update link and include directories
ext.include_dirs.append(self._clib_cmd.build_clib)
Expand Down

0 comments on commit 1e34754

Please sign in to comment.