Skip to content

Commit

Permalink
Fix mypyc failing to compile on CPython 3.10.0a6 (#10202)
Browse files Browse the repository at this point in the history
* Fix mypyc failing to compile on CPython 3.10.0a6

_PyObject_HasAttrId() has been removed in
python/cpython#22629

* Keep using _PyObject_HasAttrId when python version is too old

We could potentionally already use _PyObject_LookupAttrId starting with
python 3.7 instead of 3.10. I'm not sure if we should switch as soon or
as late as possible.

Alternatively we could also try backporting _PyObject_LookupAttrId to
<3.7.
  • Loading branch information
freundTech committed Mar 16, 2021
1 parent 4ecbd4d commit ac66403
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions mypyc/lib-rt/dict_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ int CPyDict_UpdateFromAny(PyObject *dict, PyObject *stuff) {
if (PyDict_CheckExact(dict)) {
// Argh this sucks
_Py_IDENTIFIER(keys);
if (PyDict_Check(stuff) || _PyObject_HasAttrId(stuff, &PyId_keys)) {
if (PyDict_Check(stuff) || _CPyObject_HasAttrId(stuff, &PyId_keys)) {
return PyDict_Update(dict, stuff);
} else {
return PyDict_MergeFromSeq2(dict, stuff, 1);
Expand All @@ -135,7 +135,7 @@ PyObject *CPyDict_FromAny(PyObject *obj) {
return NULL;
}
_Py_IDENTIFIER(keys);
if (_PyObject_HasAttrId(obj, &PyId_keys)) {
if (_CPyObject_HasAttrId(obj, &PyId_keys)) {
res = PyDict_Update(dict, obj);
} else {
res = PyDict_MergeFromSeq2(dict, obj, 1);
Expand Down
14 changes: 14 additions & 0 deletions mypyc/lib-rt/pythonsupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,4 +389,18 @@ _CPyDictView_New(PyObject *dict, PyTypeObject *type)
}
#endif

#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >=10
static int
_CPyObject_HasAttrId(PyObject *v, _Py_Identifier *name) {
PyObject *tmp = NULL;
int result = _PyObject_LookupAttrId(v, name, &tmp);
if (tmp) {
Py_DECREF(tmp);
}
return result;
}
#else
#define _CPyObject_HasAttrId _PyObject_HasAttrId
#endif

#endif
5 changes: 4 additions & 1 deletion mypyc/test-data/run-misc.test
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,10 @@ import sys

# We lie about the version we are running in tests if it is 3.5, so
# that hits a crash case.
if sys.version_info[:2] == (3, 9):
if sys.version_info[:2] == (3, 10):
def version() -> int:
return 10
elif sys.version_info[:2] == (3, 9):
def version() -> int:
return 9
elif sys.version_info[:2] == (3, 8):
Expand Down

0 comments on commit ac66403

Please sign in to comment.