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
13 changes: 13 additions & 0 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3206,6 +3206,19 @@ def test_type_firstlineno(self):
A.__firstlineno__ = 43
self.assertEqual(A.__dict__['__firstlineno__'], 43)

def test_type_firstlineno_metaclass_module(self):
# gh-162572: the metaclass's own __module__ is found before
# type.__module__ when setting the attribute.
class M(type):
pass
A = M('A', (), {'__firstlineno__': 42})
self.assertEqual(A.__dict__['__firstlineno__'], 42)
A.__module__ = 'testmodule'
self.assertEqual(A.__module__, 'testmodule')
self.assertNotIn('__firstlineno__', A.__dict__)
A.__firstlineno__ = 43
self.assertEqual(A.__dict__['__firstlineno__'], 43)

def test_type_typeparams(self):
class A[T]:
pass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Setting the :attr:`~type.__module__` attribute of a class now removes the
:attr:`~type.__firstlineno__` item from the class dictionary also when the
metaclass has a ``__module__`` attribute, for example a metaclass defined in
Python.
8 changes: 8 additions & 0 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6850,6 +6850,14 @@ type_setattro(PyObject *self, PyObject *name, PyObject *value)
Py_DECREF(refs.types[i]);
}
#endif
if (res == 0 && _PyUnicode_Equal(name, &_Py_ID(__module__))) {
// A __module__ attribute of the metaclass hides type_set_module(),
// so remove __firstlineno__ here as well.
PyType_Modified(type);
if (PyDict_Pop(dict, &_Py_ID(__firstlineno__), NULL) < 0) {
res = -1;
}
}

done:
Py_DECREF(name);
Expand Down
Loading