[issue15870] PyType_FromSpec should take metaclass as an argument

Josh Haberman report at bugs.python.org
Tue Sep 28 15:11:53 EDT 2021


Josh Haberman <haberman at google.com> added the comment:

> Everything is copied by `_FromSpec` after all.

One thing I noticed isn't copied is the string pointed to by tp_name: https://github.com/python/cpython/blob/0c50b8c0b8274d54d6b71ed7bd21057d3642f138/Objects/typeobject.c#L3427

This isn't an issue if tp_name is initialized from a string literal.  But if tp_name is created dynamically, it could lead to a dangling pointer.  If the general message is that "everything is copied by _FromSpec", it might make sense to copy the tp_name string too.

> However, I suppose that would replace a safe-by-design API with a "best practice" to never define the spec/slots statically (a best practice that is probably not generally followed or even advertised currently, I guess).

Yes that seems reasonable.  I generally prefer static declarations, since they will end up in .data instead of .text and will avoid a copy to the stack at runtime.  But these are very minor differences, especially for code that only runs once at startup, and a safe-by-default recommendation of always initializing PyType_* on the stack makes sense.

----------

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


More information about the Python-bugs-list mailing list