Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,22 @@ def __dir__(self):
# test that object has a __dir__()
self.assertEqual(sorted([].__dir__()), dir([]))

@support.skip_emscripten_stack_overflow()
@support.skip_wasi_stack_overflow()
def test_dir_cyclic_bases(self):
class Fake:
pass

a = Fake()
a.__bases__ = (a,)

class C:
@property
def __class__(self):
return a

self.assertRaises(RecursionError, dir, C())

def test___ne__(self):
self.assertFalse(None.__ne__(None))
self.assertIs(None.__ne__(0), NotImplemented)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash in :func:`dir` when an object's ``__class__`` has cyclic
``__bases__``.
34 changes: 20 additions & 14 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Python.h"
#include "pycore_abstract.h" // _PySequence_IterSearch()
#include "pycore_call.h" // _PyObject_VectorcallTstate()
#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
#include "pycore_code.h" // CO_FAST_FREE
#include "pycore_descrobject.h" // _PyMember_GetOffset()
#include "pycore_dict.h" // _PyDict_KeysSize()
Expand Down Expand Up @@ -6949,22 +6950,27 @@ merge_class_dict(PyObject *dict, PyObject *aclass)
Py_DECREF(bases);
return -1;
}
else {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can reduce patch, leaving "else" branch. Just add _Py_EnterRecursiveCall/_Py_LeaveRecursiveCall calls.

for (i = 0; i < n; i++) {
int status;
PyObject *base = PySequence_GetItem(bases, i);
if (base == NULL) {
Py_DECREF(bases);
return -1;
}
status = merge_class_dict(dict, base);
Py_DECREF(base);
if (status < 0) {
Py_DECREF(bases);
return -1;
}
if (_Py_EnterRecursiveCall(" in __bases__")) {
Py_DECREF(bases);
return -1;
}
for (i = 0; i < n; i++) {
int status;
PyObject *base = PySequence_GetItem(bases, i);
if (base == NULL) {
_Py_LeaveRecursiveCall();
Py_DECREF(bases);
return -1;
}
status = merge_class_dict(dict, base);
Py_DECREF(base);
if (status < 0) {
_Py_LeaveRecursiveCall();
Py_DECREF(bases);
return -1;
}
}
_Py_LeaveRecursiveCall();
Py_DECREF(bases);
}
return 0;
Expand Down
Loading