Skip to content

Commit 0b94195

Browse files
committed
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.
1 parent 2e50abd commit 0b94195

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

mypyc/lib-rt/dict_ops.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,10 @@ int CPyDict_Update(PyObject *dict, PyObject *stuff) {
112112
}
113113

114114
int CPyDict_UpdateFromAny(PyObject *dict, PyObject *stuff) {
115-
PyObject *tmp;
116-
117115
if (PyDict_CheckExact(dict)) {
118116
// Argh this sucks
119117
_Py_IDENTIFIER(keys);
120-
int hasAttr = PyDict_Check(stuff) || _PyObject_LookupAttrId(stuff, &PyId_keys, &tmp);
121-
if (tmp) {
122-
Py_DECREF(tmp);
123-
}
124-
if (hasAttr) {
118+
if (PyDict_Check(stuff) || _CPyObject_HasAttrId(stuff, &PyId_keys)) {
125119
return PyDict_Update(dict, stuff);
126120
} else {
127121
return PyDict_MergeFromSeq2(dict, stuff, 1);
@@ -132,8 +126,6 @@ int CPyDict_UpdateFromAny(PyObject *dict, PyObject *stuff) {
132126
}
133127

134128
PyObject *CPyDict_FromAny(PyObject *obj) {
135-
PyObject *tmp;
136-
137129
if (PyDict_Check(obj)) {
138130
return PyDict_Copy(obj);
139131
} else {
@@ -143,14 +135,11 @@ PyObject *CPyDict_FromAny(PyObject *obj) {
143135
return NULL;
144136
}
145137
_Py_IDENTIFIER(keys);
146-
if (_PyObject_LookupAttrId(obj, &PyId_keys, &tmp)) {
138+
if (_CPyObject_HasAttrId(obj, &PyId_keys)) {
147139
res = PyDict_Update(dict, obj);
148140
} else {
149141
res = PyDict_MergeFromSeq2(dict, obj, 1);
150142
}
151-
if (tmp) {
152-
Py_DECREF(tmp);
153-
}
154143
if (res < 0) {
155144
Py_DECREF(dict);
156145
return NULL;

mypyc/lib-rt/pythonsupport.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,4 +389,18 @@ _CPyDictView_New(PyObject *dict, PyTypeObject *type)
389389
}
390390
#endif
391391

392+
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >=10
393+
static int
394+
_CPyObject_HasAttrId(PyObject *v, _Py_Identifier *name) {
395+
PyObject *tmp = NULL;
396+
int result = _PyObject_LookupAttrId(v, name, &tmp);
397+
if (tmp) {
398+
Py_DECREF(tmp);
399+
}
400+
return result;
401+
}
402+
#else
403+
#define _CPyObject_HasAttrId _PyObject_HasAttrId
404+
#endif
405+
392406
#endif

0 commit comments

Comments
 (0)