This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: PyType_GetModule() should warn/fail when type has Py_TPFLAGS_BASETYPE
Type: behavior Stage: needs patch
Components: Interpreter Core Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, corona10, petr.viktorin, shihai1991, vstinner
Priority: normal Keywords:

Created on 2020-11-20 10:28 by christian.heimes, last changed 2022-04-11 14:59 by admin.

Messages (4)
msg381475 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2020-11-20 10:28
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.
msg381493 - (view) Author: Hai Shi (shihai1991) * (Python triager) Date: 2020-11-20 18:25
>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.
+1. Looks like check Py_TPFLAGS_BASETYPE in PyType_GetModule() is an easy way:)
msg381532 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2020-11-21 09:13
I don't understand this issue. Why would Py_TPFLAGS_BASETYPE make the result of PyType_GetModule() wrong? Is the note in the documentation of PyType_GetModule() not clear?
A type and its subtypes may be defined in different modules; why would it be wrong to ask which module the superclass was defined in?

Note that PyType_GetModule() is needed if users want to implement something like _PyType_GetModuleByDef().
msg381740 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2020-11-24 13:43
> I missed it when I ported some of my code to heap types and multiphase init.

I guess PyType_GetModule() is not good for that use case.
If your code is open, could you add a link?
History
Date User Action Args
2022-04-11 14:59:38adminsetgithub: 86584
2020-11-24 13:43:57petr.viktorinsetmessages: + msg381740
2020-11-21 09:13:02petr.viktorinsetmessages: + msg381532
2020-11-20 18:25:06shihai1991setnosy: + shihai1991
messages: + msg381493
2020-11-20 10:28:13christian.heimescreate