mirror of
https://github.com/python/cpython.git
synced 2025-08-28 12:45:07 +00:00
bpo-46891: Fix creating a new instance of a module subclass with slots (GH-31643)
This commit is contained in:
parent
3c4abfab0d
commit
751c9ed801
3 changed files with 24 additions and 1 deletions
|
@ -346,6 +346,25 @@ a = A(destroyed)"""
|
||||||
|
|
||||||
# frozen and namespace module reprs are tested in importlib.
|
# 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)
|
||||||
|
|
||||||
|
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__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -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.
|
|
@ -4,6 +4,7 @@
|
||||||
#include "Python.h"
|
#include "Python.h"
|
||||||
#include "pycore_call.h" // _PyObject_CallNoArgs()
|
#include "pycore_call.h" // _PyObject_CallNoArgs()
|
||||||
#include "pycore_interp.h" // PyInterpreterState.importlib
|
#include "pycore_interp.h" // PyInterpreterState.importlib
|
||||||
|
#include "pycore_object.h" // _PyType_AllocNoTrack
|
||||||
#include "pycore_pystate.h" // _PyInterpreterState_GET()
|
#include "pycore_pystate.h" // _PyInterpreterState_GET()
|
||||||
#include "pycore_moduleobject.h" // _PyModule_GetDef()
|
#include "pycore_moduleobject.h" // _PyModule_GetDef()
|
||||||
#include "structmember.h" // PyMemberDef
|
#include "structmember.h" // PyMemberDef
|
||||||
|
@ -80,7 +81,7 @@ static PyModuleObject *
|
||||||
new_module_notrack(PyTypeObject *mt)
|
new_module_notrack(PyTypeObject *mt)
|
||||||
{
|
{
|
||||||
PyModuleObject *m;
|
PyModuleObject *m;
|
||||||
m = PyObject_GC_New(PyModuleObject, mt);
|
m = (PyModuleObject *)_PyType_AllocNoTrack(mt, 0);
|
||||||
if (m == NULL)
|
if (m == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
m->md_def = NULL;
|
m->md_def = NULL;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue