From ee1fb1b04548e5f29e22410d44b7c6c1f61b59da Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Wed, 27 Oct 2021 15:54:23 -0400 Subject: [PATCH] fix: better PyPy support --- include/pybind11/eval.h | 4 ++-- tests/requirements.txt | 2 +- tests/test_buffers.py | 1 + tests/test_builtin_casters.py | 4 ++-- tests/test_embed/CMakeLists.txt | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/include/pybind11/eval.h b/include/pybind11/eval.h index 6cc672e2d5..4248551e9b 100644 --- a/include/pybind11/eval.h +++ b/include/pybind11/eval.h @@ -19,11 +19,11 @@ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(detail) inline void ensure_builtins_in_globals(object &global) { - #if PY_VERSION_HEX < 0x03080000 + #if defined(PYPY_VERSION) || PY_VERSION_HEX < 0x03080000 // Running exec and eval on Python 2 and 3 adds `builtins` module under // `__builtins__` key to globals if not yet present. // Python 3.8 made PyRun_String behave similarly. Let's also do that for - // older versions, for consistency. + // older versions, for consistency. This was missing from PyPy3.8 7.3.7. if (!global.contains("__builtins__")) global["__builtins__"] = module_::import(PYBIND11_BUILTINS_MODULE); #else diff --git a/tests/requirements.txt b/tests/requirements.txt index 8d2742a71c..98ca46d28a 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -2,7 +2,7 @@ numpy==1.16.6; python_version<"3.6" and sys_platform!="win32" and platform_pytho numpy==1.19.0; platform_python_implementation=="PyPy" and sys_platform=="linux" and python_version=="3.6" numpy==1.20.0; platform_python_implementation=="PyPy" and sys_platform=="linux" and python_version=="3.7" numpy==1.19.3; platform_python_implementation!="PyPy" and python_version=="3.6" -numpy==1.21.3; platform_python_implementation!="PyPy" and python_version>="3.7" +numpy==1.21.3; platform_python_implementation!="PyPy" and python_version>="3.7" and python_version<"3.11" py @ git+https://github.com/pytest-dev/py; python_version>="3.11" pytest==4.6.9; python_version<"3.5" pytest==6.1.2; python_version=="3.5" diff --git a/tests/test_buffers.py b/tests/test_buffers.py index adf7cadff2..41cc23f4f4 100644 --- a/tests/test_buffers.py +++ b/tests/test_buffers.py @@ -62,6 +62,7 @@ def test_to_python(): assert cstats.alive() == 1 del mat2 # holds a mat reference pytest.gc_collect() + pytest.gc_collect() # Needed for PyPy assert cstats.alive() == 0 assert cstats.values() == ["5x4 matrix"] assert cstats.copy_constructions == 0 diff --git a/tests/test_builtin_casters.py b/tests/test_builtin_casters.py index 2a061c193e..db5ab48dac 100644 --- a/tests/test_builtin_casters.py +++ b/tests/test_builtin_casters.py @@ -299,7 +299,7 @@ def cant_convert(v): assert noconvert(7) == 7 cant_convert(3.14159) # TODO: Avoid DeprecationWarning in `PyLong_AsLong` (and similar) - if (3, 8) <= env.PY < (3, 10): + if (3, 8) <= env.PY < (3, 10) and env.CPYTHON: with env.deprecated_call(): assert convert(Int()) == 42 else: @@ -334,7 +334,7 @@ def require_implicit(v): # The implicit conversion from np.float32 is undesirable but currently accepted. # TODO: Avoid DeprecationWarning in `PyLong_AsLong` (and similar) - if (3, 8) <= env.PY < (3, 10): + if (3, 8) <= env.PY < (3, 10) and env.CPYTHON: with env.deprecated_call(): assert convert(np.float32(3.14159)) == 3 else: diff --git a/tests/test_embed/CMakeLists.txt b/tests/test_embed/CMakeLists.txt index 3b89d6e584..41e4d22cbe 100644 --- a/tests/test_embed/CMakeLists.txt +++ b/tests/test_embed/CMakeLists.txt @@ -7,7 +7,7 @@ if("${PYTHON_MODULE_EXTENSION}" MATCHES "pypy" OR "${Python_INTERPRETER_ID}" STR return() endif() -find_package(Catch 2.13.2) +find_package(Catch 2.13.7) if(CATCH_FOUND) message(STATUS "Building interpreter tests using Catch v${CATCH_VERSION}")