[issue42418] PyType_GetModule() should warn/fail when type has Py_TPFLAGS_BASETYPE

Christian Heimes report at bugs.python.org
Fri Nov 20 05:28:13 EST 2020


New submission from Christian Heimes <lists at cheimes.de>:

PyType_GetModule() may return wrong or unexpected result when a type has 
the feature flag Py_TPFLAGS_BASETYPE. The base type flag permits subclassing of an extension class. The function will return NULL when a type is subclasses in Python space or a different module object when the type is subclasses and initialized in a differen C extension module.

All subclassable types must use _PyType_GetModuleByDef() to safely access  module and module state struct. It's easy to miss the problem. I missed it when I ported some of my code to heap types and multiphase init.

I propose to either a guard to PyType_GetModule() to prevent misuse of the function or to add a variant of the function that prevents misuse.

----------
components: Interpreter Core
messages: 381475
nosy: christian.heimes, corona10, petr.viktorin, vstinner
priority: normal
severity: normal
stage: needs patch
status: open
title: PyType_GetModule() should warn/fail when type has Py_TPFLAGS_BASETYPE
type: behavior
versions: Python 3.10

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42418>
_______________________________________


More information about the Python-bugs-list mailing list