[Python-Dev] A Subtle Bug in Class Initializations

Ronald Oussoren ronaldoussoren at mac.com
Wed Aug 29 15:34:31 EDT 2018



> On 28 Aug 2018, at 22:09, Eddie Elizondo <eelizondo at fb.com> wrote:
> 
> Hi everyone, 
> 
> Sorry for the delay - I finally sent out a patch: https://bugs.python.org/issue34522.

As I mentioned on the tracker this patch is incorrect because it redefines PyVarObject_HEAD_INIT in a way that changes the semantics of existing code. This will break C extensions (I’m the author of one, and there may be others). 

> 
> Also, I'm +1 for modifying all extension modules to use PyType_FromSpec! I might lend a hand to start moving them :)

Somewhat off-topic, but I personally don’t like PyType_FromSpec and prefer the older PyTypeObject construction but using C99 initialisers. That gives short and readable code where the compiler can (and does) warn when initialising slots to values that don’t match the declared type of those slots.  The only advantage of using PyType_FromSpec is that this can result in C extensions that are binary compatible between python versions, which IMHO is less useful than it used to be in the past thanks to CI/CD systems. 

PyType_FromSpec also cannot deal with some esoteric use cases, such as defining types with a custom metatype in C code. 

Ronald


More information about the Python-Dev mailing list