Antoine Pitrou <pit...@free.fr> added the comment:

> My patch attempts to emulate 2.x PyInstance_NewRaw with a call to
> tp_alloc.

This is certainly the wrong thing to do. You could at least try 
PyBaseObject_Type.tp_new (see object_new() in typeobject.c), but even this is 
not necessarily right (for example if the class is derived from an extension 
type defining its own tp_new).

So, IMO, the right thing to do would be to choose the first base type that 
isn't a Python-defined class and use its tp_new:

    staticbase = subtype;
    while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
        staticbase = staticbase->tp_base;

(these two lines are from tp_new_wrapper() in typeobject.c)

That way you choose the right constructor function, yet don't call the 
Python-defined __new__ function. That's the only reasonable way of doing it I 
can imagine. It also follows the following behaviour:

>>> class C(int):
...   def __new__(cls, *a):
...     print "__new__", a
...     return int.__new__(cls, *a)
... 
>>> C(5)
__new__ (5,)
5
>>> s = pickle.dumps(C(5))
__new__ (5,)
>>> x = pickle.loads(s)
>>> x
5

As you can see, int.__new__ has been called on unpickling but not C.__new__ (no 
print).

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue5180>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to