Skip to content

Commit 812a3c8

Browse files
committed
[mypyc] Revert PyObject_GetOptionalAttrString in module shims
PyObject_GetOptionalAttrString was added in CPython 3.13, but the shim templates #include only <Python.h> -- they don't see CPy.h or the pythoncapi_compat.h shim from lib-rt -- so on 3.10/3.11/3.12 CI all shim builds fail with "implicit declaration of function 'PyObject_GetOptionalAttrString'" under -Werror. Mypy supports 3.10+, so go back to the portable PyObject_GetAttrString + PyErr_Clear form. Leaving a comment in both templates explaining why we can't use the cleaner API here.
1 parent 6d488bf commit 812a3c8

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

mypyc/lib-rt/module_shim.tmpl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ PyInit_{modname}(void)
2020
// the per-module real init. Deferring this out of the shared lib's own
2121
// PyInit keeps separate-mode compiled modules from recursively triggering
2222
// sibling-package __init__.py mid-bootstrap.
23-
PyObject *deps_capsule;
24-
if (PyObject_GetOptionalAttrString(tmp, "ensure_deps", &deps_capsule) < 0) {{
25-
Py_DECREF(tmp);
26-
return NULL;
27-
}}
23+
// PyObject_GetOptionalAttrString would be cleaner, but it's 3.13+ and the
24+
// shim deliberately #includes only <Python.h>, so we can't pull in the
25+
// pythoncapi_compat.h shim from lib-rt here. AttributeError + PyErr_Clear
26+
// is the portable form.
27+
PyObject *deps_capsule = PyObject_GetAttrString(tmp, "ensure_deps");
2828
if (deps_capsule != NULL) {{
2929
int (*deps_func)(void) = (int (*)(void))PyCapsule_GetPointer(
3030
deps_capsule, "{libname}.ensure_deps");
@@ -37,6 +37,8 @@ PyInit_{modname}(void)
3737
Py_DECREF(tmp);
3838
return NULL;
3939
}}
40+
}} else {{
41+
PyErr_Clear();
4042
}}
4143
PyObject *capsule = PyObject_GetAttrString(tmp, "init_{full_modname}");
4244
Py_DECREF(tmp);

mypyc/lib-rt/module_shim_no_gil_multiphase.tmpl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ static int {modname}_exec(PyObject *module)
1919
// the per-module real init. Deferring this out of the shared lib's own
2020
// PyInit keeps separate-mode compiled modules from recursively triggering
2121
// sibling-package __init__.py mid-bootstrap.
22-
PyObject *deps_capsule;
23-
if (PyObject_GetOptionalAttrString(tmp, "ensure_deps", &deps_capsule) < 0) {{
24-
Py_DECREF(tmp);
25-
return -1;
26-
}}
22+
// PyObject_GetOptionalAttrString would be cleaner, but it's 3.13+ and the
23+
// shim deliberately #includes only <Python.h>, so we can't pull in the
24+
// pythoncapi_compat.h shim from lib-rt here. AttributeError + PyErr_Clear
25+
// is the portable form.
26+
PyObject *deps_capsule = PyObject_GetAttrString(tmp, "ensure_deps");
2727
if (deps_capsule != NULL) {{
2828
int (*deps_func)(void) = (int (*)(void))PyCapsule_GetPointer(
2929
deps_capsule, "{libname}.ensure_deps");
@@ -36,6 +36,8 @@ static int {modname}_exec(PyObject *module)
3636
Py_DECREF(tmp);
3737
return -1;
3838
}}
39+
}} else {{
40+
PyErr_Clear();
3941
}}
4042
PyObject *capsule = PyObject_GetAttrString(tmp, "exec_{full_modname}");
4143
Py_DECREF(tmp);

0 commit comments

Comments
 (0)