Skip to content
Merged
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
19 changes: 19 additions & 0 deletions Lib/test/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,25 @@ def test_repeated_attribute_pops(self):

# frozen and namespace module reprs are tested in importlib.

def test_subclass_with_slots(self):
# In 3.11alpha this crashed, as the slots weren't NULLed.

class ModuleWithSlots(ModuleType):
__slots__ = ("a", "b")

def __init__(self, name):
super().__init__(name)

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.

What's the purpose of this method? Maybe assign a and/or b attribute if you want to keep it? Or remove it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The important bit is having __slots__ in the class definition, I just checked and leaving out the __init__ method still reproduces the crash that this PR fixes.

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.

Well, I approved the PR. @markshannon can decide to keep or remove the method, it doesn't really matter ;-)


m = ModuleWithSlots("name")
with self.assertRaises(AttributeError):
m.a
with self.assertRaises(AttributeError):
m.b
m.a, m.b = 1, 2
self.assertEqual(m.a, 1)
self.assertEqual(m.b, 2)



if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix bug introduced during 3.11alpha where subclasses of ``types.ModuleType``
with ``__slots__`` were not initialized correctly, resulting in an
interpreter crash.
3 changes: 2 additions & 1 deletion Objects/moduleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Python.h"
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_interp.h" // PyInterpreterState.importlib
#include "pycore_object.h" // _PyType_AllocNoTrack
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_moduleobject.h" // _PyModule_GetDef()
#include "structmember.h" // PyMemberDef
Expand Down Expand Up @@ -80,7 +81,7 @@ static PyModuleObject *
new_module_notrack(PyTypeObject *mt)
{
PyModuleObject *m;
m = PyObject_GC_New(PyModuleObject, mt);
m = (PyModuleObject *)_PyType_AllocNoTrack(mt, 0);
if (m == NULL)
return NULL;
m->md_def = NULL;
Expand Down