Skip to content

Commit c4a05f9

Browse files
authored
Add support for GraalPy (#5380)
* Initial support for GraalPy * Mark tests that currently fail on GraalPy with xfail * Add graalpy to CI * Limit test deps on graalpy to available binary wheels * Skip cmake test installed_function on GraalPy CMake won't find libpython on GraalPy, it either fails or silently picks CPython's libpython. * Factor out setting function docstrings into a macro * Try to narrow down skipped tests
1 parent 7e418f4 commit c4a05f9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+211
-66
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ jobs:
3939
- 'pypy-3.8'
4040
- 'pypy-3.9'
4141
- 'pypy-3.10'
42+
- 'graalpy-24.1'
4243

4344
# Items in here will either be added to the build matrix (if not
4445
# present), or add new keys to an existing matrix element if all the
@@ -67,6 +68,10 @@ jobs:
6768
# Extra ubuntu latest job
6869
- runs-on: ubuntu-latest
6970
python: '3.11'
71+
exclude:
72+
# The setup-python action currently doesn't have graalpy for windows
73+
- python: 'graalpy-24.1'
74+
runs-on: 'windows-2022'
7075

7176

7277
name: "🐍 ${{ matrix.python }} • ${{ matrix.runs-on }} • x64 ${{ matrix.args }}"

include/pybind11/cast.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ class type_caster<bool> {
343343
#else
344344
// Alternate approach for CPython: this does the same as the above, but optimized
345345
// using the CPython API so as to avoid an unneeded attribute lookup.
346-
else if (auto *tp_as_number = src.ptr()->ob_type->tp_as_number) {
346+
else if (auto *tp_as_number = Py_TYPE(src.ptr())->tp_as_number) {
347347
if (PYBIND11_NB_BOOL(tp_as_number)) {
348348
res = (*PYBIND11_NB_BOOL(tp_as_number))(src.ptr());
349349
}

include/pybind11/detail/common.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ PYBIND11_WARNING_DISABLE_MSVC(4505)
299299
# define PYBIND11_INTERNAL_NUMPY_1_ONLY_DETECTED
300300
#endif
301301

302-
#if defined(PYPY_VERSION) && !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
302+
#if (defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)) && !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
303303
# define PYBIND11_SIMPLE_GIL_MANAGEMENT
304304
#endif
305305

@@ -387,6 +387,20 @@ PYBIND11_WARNING_POP
387387
#define PYBIND11_CONCAT(first, second) first##second
388388
#define PYBIND11_ENSURE_INTERNALS_READY pybind11::detail::get_internals();
389389

390+
#if !defined(GRAALVM_PYTHON)
391+
# define PYBIND11_PYCFUNCTION_GET_DOC(func) ((func)->m_ml->ml_doc)
392+
# define PYBIND11_PYCFUNCTION_SET_DOC(func, doc) \
393+
do { \
394+
(func)->m_ml->ml_doc = (doc); \
395+
} while (0)
396+
#else
397+
# define PYBIND11_PYCFUNCTION_GET_DOC(func) (GraalPyCFunction_GetDoc((PyObject *) (func)))
398+
# define PYBIND11_PYCFUNCTION_SET_DOC(func, doc) \
399+
do { \
400+
GraalPyCFunction_SetDoc((PyObject *) (func), (doc)); \
401+
} while (0)
402+
#endif
403+
390404
#define PYBIND11_CHECK_PYTHON_VERSION \
391405
{ \
392406
const char *compiled_ver \

include/pybind11/detail/internals.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ inline void translate_local_exception(std::exception_ptr p) {
454454

455455
inline object get_python_state_dict() {
456456
object state_dict;
457-
#if PYBIND11_INTERNALS_VERSION <= 4 || defined(PYPY_VERSION)
457+
#if PYBIND11_INTERNALS_VERSION <= 4 || defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)
458458
state_dict = reinterpret_borrow<object>(PyEval_GetBuiltins());
459459
#else
460460
# if PY_VERSION_HEX < 0x03090000
@@ -727,7 +727,8 @@ const char *c_str(Args &&...args) {
727727
}
728728

729729
inline const char *get_function_record_capsule_name() {
730-
#if PYBIND11_INTERNALS_VERSION > 4
730+
// On GraalPy, pointer equality of the names is currently not guaranteed
731+
#if PYBIND11_INTERNALS_VERSION > 4 && !defined(GRAALVM_PYTHON)
731732
return get_internals().function_record_capsule_name.c_str();
732733
#else
733734
return nullptr;

include/pybind11/detail/type_caster_base.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ PYBIND11_NOINLINE handle get_object_handle(const void *ptr, const detail::type_i
459459
}
460460

461461
inline PyThreadState *get_thread_state_unchecked() {
462-
#if defined(PYPY_VERSION)
462+
#if defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)
463463
return PyThreadState_GET();
464464
#elif PY_VERSION_HEX < 0x030D0000
465465
return _PyThreadState_UncheckedGet();

include/pybind11/eval.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,18 @@ void exec(const char (&s)[N], object global = globals(), object local = object()
9494
eval<eval_statements>(s, std::move(global), std::move(local));
9595
}
9696

97-
#if defined(PYPY_VERSION)
97+
#if defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)
9898
template <eval_mode mode = eval_statements>
9999
object eval_file(str, object, object) {
100-
pybind11_fail("eval_file not supported in PyPy3. Use eval");
100+
pybind11_fail("eval_file not supported in this interpreter. Use eval");
101101
}
102102
template <eval_mode mode = eval_statements>
103103
object eval_file(str, object) {
104-
pybind11_fail("eval_file not supported in PyPy3. Use eval");
104+
pybind11_fail("eval_file not supported in this interpreter. Use eval");
105105
}
106106
template <eval_mode mode = eval_statements>
107107
object eval_file(str) {
108-
pybind11_fail("eval_file not supported in PyPy3. Use eval");
108+
pybind11_fail("eval_file not supported in this interpreter. Use eval");
109109
}
110110
#else
111111
template <eval_mode mode = eval_statements>

include/pybind11/pybind11.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,7 @@ class cpp_function : public function {
573573
// chain.
574574
chain_start = rec;
575575
rec->next = chain;
576-
auto rec_capsule
577-
= reinterpret_borrow<capsule>(((PyCFunctionObject *) m_ptr)->m_self);
576+
auto rec_capsule = reinterpret_borrow<capsule>(PyCFunction_GET_SELF(m_ptr));
578577
rec_capsule.set_pointer(unique_rec.release());
579578
guarded_strdup.release();
580579
} else {
@@ -634,12 +633,11 @@ class cpp_function : public function {
634633
}
635634
}
636635

637-
/* Install docstring */
638636
auto *func = (PyCFunctionObject *) m_ptr;
639-
std::free(const_cast<char *>(func->m_ml->ml_doc));
640637
// Install docstring if it's non-empty (when at least one option is enabled)
641-
func->m_ml->ml_doc
642-
= signatures.empty() ? nullptr : PYBIND11_COMPAT_STRDUP(signatures.c_str());
638+
auto *doc = signatures.empty() ? nullptr : PYBIND11_COMPAT_STRDUP(signatures.c_str());
639+
std::free(const_cast<char *>(PYBIND11_PYCFUNCTION_GET_DOC(func)));
640+
PYBIND11_PYCFUNCTION_SET_DOC(func, doc);
643641

644642
if (rec->is_method) {
645643
m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->scope.ptr());
@@ -2780,8 +2778,8 @@ get_type_override(const void *this_ptr, const type_info *this_type, const char *
27802778
}
27812779

27822780
/* Don't call dispatch code if invoked from overridden function.
2783-
Unfortunately this doesn't work on PyPy. */
2784-
#if !defined(PYPY_VERSION)
2781+
Unfortunately this doesn't work on PyPy and GraalPy. */
2782+
#if !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON)
27852783
# if PY_VERSION_HEX >= 0x03090000
27862784
PyFrameObject *frame = PyThreadState_GetFrame(PyThreadState_Get());
27872785
if (frame != nullptr) {

include/pybind11/pytypes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ struct error_fetch_and_normalize {
643643

644644
bool have_trace = false;
645645
if (m_trace) {
646-
#if !defined(PYPY_VERSION)
646+
#if !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON)
647647
auto *tb = reinterpret_cast<PyTracebackObject *>(m_trace.ptr());
648648

649649
// Get the deepest trace possible.
@@ -1356,7 +1356,7 @@ inline bool PyUnicode_Check_Permissive(PyObject *o) {
13561356
# define PYBIND11_STR_CHECK_FUN PyUnicode_Check
13571357
#endif
13581358

1359-
inline bool PyStaticMethod_Check(PyObject *o) { return o->ob_type == &PyStaticMethod_Type; }
1359+
inline bool PyStaticMethod_Check(PyObject *o) { return Py_TYPE(o) == &PyStaticMethod_Type; }
13601360

13611361
class kwargs_proxy : public handle {
13621362
public:

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828

2929
@pytest.fixture(scope="session", autouse=True)
3030
def use_multiprocessing_forkserver_on_linux():
31-
if sys.platform != "linux":
32-
# The default on Windows and macOS is "spawn": If it's not broken, don't fix it.
31+
if sys.platform != "linux" or sys.implementation.name == "graalpy":
32+
# The default on Windows, macOS and GraalPy is "spawn": If it's not broken, don't fix it.
3333
return
3434

3535
# Full background: https://github.com/pybind/pybind11/issues/4105#issuecomment-1301004592

tests/env.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
CPYTHON = platform.python_implementation() == "CPython"
1414
PYPY = platform.python_implementation() == "PyPy"
15+
GRAALPY = sys.implementation.name == "graalpy"
1516
PY_GIL_DISABLED = bool(sysconfig.get_config_var("Py_GIL_DISABLED"))
1617

1718

0 commit comments

Comments
 (0)