bpo-43973: Use Py_TPFLAGS_IMMUTABLETYPE to check for class assignments by erlend-aasland · Pull Request #25714 · python/cpython
erlend-aasland
changed the title
Use Py_TPFLAGS_IMMUTABLETYPE to check for class assignments
bpo-43973: Use Py_TPFLAGS_IMMUTABLETYPE to check for class assignments
Doesn't this also require a change?
| if (compatible_for_assignment(oldto, newto, "__class__")) { | |
| if (newto->tp_flags & Py_TPFLAGS_HEAPTYPE) { | |
| Py_INCREF(newto); | |
| } | |
| Py_SET_TYPE(self, newto); | |
| if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE) | |
| Py_DECREF(oldto); | |
| return 0; | |
| } | |
| else { | |
| return -1; | |
| } |
Suggested :-
if (compatible_for_assignment(oldto, newto, "__class__")) {
if (!(newto->tp_flags & Py_TPFLAGS_IMMUTABLETYPE)) {
Py_INCREF(newto);
}
Py_SET_TYPE(self, newto);
if (!(oldto->tp_flags & Py_TPFLAGS_IMMUTABLETYPE))
Py_DECREF(oldto);
return 0;
}
else {
return -1;
}
Doesn't this also require a change?
AFAICT, no. They adjust the ref. count if heap types are used:
If the old class was a heap type, decrement its ref. count. If the new class is a heap type, acquire a strong reference to it (increment the ref. count).
The comment above the check should have been fixed to reflect the new semantics, no?
Also, is the explicit check for PyModule_Type still needed?
The comment above the check should have been fixed to reflect the new semantics, no?
Yes, and I believe it can be considerably reduced.
Also, is the explicit check for
PyModule_Typestill needed?
I don't know. See bpo-24912.