[Python-bugs-list] [ python-Bugs-540965 ] PyType_GenericNew broken

noreply@sourceforge.net noreply@sourceforge.net
Mon, 15 Apr 2002 06:01:24 -0700


Bugs item #540965, was opened at 2002-04-08 08:24
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=540965&group_id=5470

Category: Type/class unification
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Ralf Juengling (rjuengling)
Assigned to: Guido van Rossum (gvanrossum)
Summary: PyType_GenericNew broken

Initial Comment:
A new Python type/class _A is implemented in C.
It needs a special allocator function, thus
tp_alloc=_A_alloc for this type. There is no need
for a special new-method, however, thus
tp_new=PyType_GenericNew for this type.

The class A inherits from _A and is implemented
in Python:

class A(_A):
	pass

When an instance of _A is created, its allocator
function _A_alloc gets invoked. When an instance 
of A is created, the _A_alloc does not get invoked.


PyType_GenericNew should also invoke all allocator 
functions of the argument type's superclasses (in 
reverse mro, I think).



----------------------------------------------------------------------

>Comment By: Guido van Rossum (gvanrossum)
Date: 2002-04-15 09:01

Message:
Logged In: YES 
user_id=6380

Your understanding of instance allocation doesn't match
reality, *or* you're expressing yourself poorly. Allocation
does not include initialization; allocation simply requests
the memory for the object from malloc (or some other memory
allocator) and fills it with zeros, except for the reference
count, which is set to 1, and the ob_type field, which is
set to the type argument to the allocator. The amount of
memory allocated for the object is calculated by taking the
tp_basicsize of the type object, and adding nitems times the
tp_itemsize from the type object.

If you want to initialize your object, that should be done
in the tp_new slot, not in tp_alloc.

Have you tried to read the source code (or go through an
allocation in a C debugger) to find out how it really works?

----------------------------------------------------------------------

Comment By: Ralf Juengling (rjuengling)
Date: 2002-04-15 04:06

Message:
Logged In: YES 
user_id=495820

The class A does not know how to allocate the 'base part' of instances of itself
(i.e. what makes an instance of class _A). Thus sometime during instantiation 
of class A objects, the allocater of the base class must be called!
This will not happen, however, if the tp_new-slot function merely calls the 
allocator of the argument type.

You pointed out, that e.g. subclasses of 'int' won't inherit int's special allocator.
Does this mean, int's allocator will not be invoked on instantiation of the subclass?
Then, I don't understand why this works; I can only imagine, that PyType_GenericAlloc
is designed to deal with Python built-in types (and probably compostitions of these).




----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2002-04-14 20:09

Message:
Logged In: YES 
user_id=6380

> PyType_GenericNew should also invoke all allocator 
> functions of the argument type's superclasses (in 
> reverse mro, I think).

No, this doesn't make sense; you can't invoke
more than one allocator. (Each allocator would
create a whole new object.)

The cause of your problem is in type_new(): it
always overrides tp_alloc with PyType_GenericAlloc.
You'd have to write a new metaclass (inheriting
from type) in C to change the tp_alloc (and tp_free)
settings.

But why do you want this? Why do you want
A instances to use the same allocator as _A?
This is not how subclassing built-in types works
elsewhere; e.g. int uses a special allocator,
but subclasses of int don't inherit that.

Before I do anything about this I'd like to
understand what you are trying to accomplish.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=540965&group_id=5470