[Patches] [ python-Patches-696193 ] Enable __slots__ for meta-types

SourceForge.net noreply@sourceforge.net
Fri, 07 Mar 2003 07:51:35 -0800


Patches item #696193, was opened at 2003-03-02 22:02
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=696193&group_id=5470

Category: Core (C code)
Group: Python 2.3
Status: Closed
Resolution: Accepted
Priority: 5
Submitted By: Christian Tismer (tismer)
Assigned to: Guido van Rossum (gvanrossum)
Summary: Enable __slots__ for meta-types

Initial Comment:
The new type system allows non-empty __slots__ only
for fixed-size objects.

Meta-types are types which instances are also types.
types are variable-sized, because they take the slot
definitions for their instances, so the cannot have
extra members from their meta-type.

The proposed solution allows for two things:
a) meta-types can have slots
b) extensions get access to the whole type object and
    can create extended types with private fields.

The changes providing this are quite simple:
- replace the internal hidden "etype" and turn it into
  an explicit PyHeapTypeObject in object.h
- instead of a fixed offset into the former etype, the
slots
  calculation is based upon tp_basicsize.

To keep things easy, I added a macro which does this
calculation, and member access read now like so:

before:
	type->tp_members = et->members;
after:
	type->tp_members = PyHeapType_GET_MEMBERS(et);

This patch has been tested thoroughly in my own code since
Python 2.2, and I think it is ripe to get into the
distribution.
It has almost no impact on speed or simlicity.


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

>Comment By: Christian Tismer (tismer)
Date: 2003-03-07 16:51

Message:
Logged In: YES 
user_id=105700

Oops! You are right.
I forgot to back-port that change into the future. My
2.2.2 version already reads like this:

/* access macro to the members which are floating "behind"
the object */
#define PyHeapType_GET_MEMBERS(etype) \
	((PyMemberDef *)(((char *)etype) +
(etype)->type.ob_type->tp_basicsize))

Thanks for taking care -- chris

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

Comment By: Guido van Rossum (gvanrossum)
Date: 2003-03-07 16:24

Message:
Logged In: YES 
user_id=6380

Checked in, with that one fix.

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

Comment By: Guido van Rossum (gvanrossum)
Date: 2003-03-07 16:04

Message:
Logged In: YES 
user_id=6380

Everything looks fine, except subtracting 1 from the
expression in the PyHeapType_GET_MEMBERS() macro. Thart
makes the first members slot overlap with the 'name' and
'slots' struct members. I'll get rid of the "-1" part.

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

Comment By: Guido van Rossum (gvanrossum)
Date: 2003-03-03 15:57

Message:
Logged In: YES 
user_id=6380

I'll look at this on Friday.

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

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=696193&group_id=5470