Skip to content

Commit

Permalink
Make bases/mro accesses thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
DinoV committed Jan 12, 2024
1 parent 2d3363d commit 74d4985
Show file tree
Hide file tree
Showing 3 changed files with 277 additions and 66 deletions.
4 changes: 4 additions & 0 deletions Include/internal/pycore_typeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern "C" {
#endif

#include "pycore_moduleobject.h" // PyModuleObject
#include "pycore_lock.h" // PyMutex


/* state */
Expand All @@ -21,6 +22,9 @@ struct _types_runtime_state {
// bpo-42745: next_version_tag remains shared by all interpreters
// because of static types.
unsigned int next_version_tag;
#ifdef Py_GIL_DISABLED
PyMutex type_mutex;
#endif
};


Expand Down
15 changes: 4 additions & 11 deletions Modules/_abc.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "pycore_object.h" // _PyType_GetSubclasses()
#include "pycore_runtime.h" // _Py_ID()
#include "pycore_setobject.h" // _PySet_NextEntry()
#include "pycore_typeobject.h" // _PyType_GetMRO()
#include "pycore_weakref.h" // _PyWeakref_GET_REF()
#include "clinic/_abc.c.h"

Expand Down Expand Up @@ -744,18 +743,12 @@ _abc__abc_subclasscheck_impl(PyObject *module, PyObject *self,
Py_DECREF(ok);

/* 4. Check if it's a direct subclass. */
PyObject *mro = _PyType_GetMRO((PyTypeObject *)subclass);
assert(PyTuple_Check(mro));
for (pos = 0; pos < PyTuple_GET_SIZE(mro); pos++) {
PyObject *mro_item = PyTuple_GET_ITEM(mro, pos);
assert(mro_item != NULL);
if ((PyObject *)self == mro_item) {
if (_add_to_weak_set(&impl->_abc_cache, subclass) < 0) {
goto end;
}
result = Py_True;
if (PyType_IsSubtype((PyTypeObject *)subclass, (PyTypeObject *)self)) {
if (_add_to_weak_set(&impl->_abc_cache, subclass) < 0) {
goto end;
}
result = Py_True;
goto end;
}

/* 5. Check if it's a subclass of a registered class (recursive). */
Expand Down
Loading

0 comments on commit 74d4985

Please sign in to comment.