Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > tp_base, tp_basesize, and __slots__ instance __class__ reassignment

Reply
Thread Tools

tp_base, tp_basesize, and __slots__ instance __class__ reassignment

 
 
Jp Calderone
Guest
Posts: n/a
 
      07-05-2003
In trying to update some code built on top of reload(), I've hit a point
of confusion in how it is determined whether or not __class__ is allowed to
be rebound.

Consider, for example:

>>> class Foo(object):

... __slots__ = 'a', 'b'
...
>>> class Bar(object):

... __slots__ = 'a', 'b'
...
>>> f = Foo()
>>> f.__class__ = Bar

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: __class__ assignment: 'Bar' object layout differs from 'Foo'

The cause of this particular error does not actually seem to be that the
object layouts of Bar and Foo are incompatible, but rather that the object
layouts of _object_ and Foo are incompatible.

I've tracked down the point of rejection to same_slots_added
(Objects/typeobject.c:2347 or thereabouts):

static int
same_slots_added(PyTypeObject *a, PyTypeObject *b)
{
PyTypeObject *base = a->tp_base;
int size;

if (base != b->tp_base)
return 0;
if (equiv_structs(a, base) && equiv_structs(b, base))
return 1;
size = base->tp_basicsize;
if (a->tp_dictoffset == size && b->tp_dictoffset == size)
size += sizeof(PyObject *);
if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
size += sizeof(PyObject *);
return size == a->tp_basicsize && size == b->tp_basicsize;
}

I do not understand why the final line is comparing size against each of a
and b's tp_basicsize, rather than perfoming some comparison between a and b
directly.

Can anyone enlighten me?

Jp

--
Where a calculator on the ENIAC is equipped with 18,000 vacuum tubes and
weighs 30 tons, computers in the future may have only 1,000 vacuum tubes and
weigh only 1.5 tons. -- Popular Mechanics, March 1949

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      07-06-2003
Jp Calderone wrote:

> Can anyone enlighten me?


The test doesn't recognize the addition of arbitrary same slots.
Instead, it only looks for the __dict__ slot and the weakrefs slot.
If any additional slots have been added to either subclass, the
classes are considered different.

To test whether just these two slots have been added in lock-step,
you check
1. the dictoffset is the same for both classes, and it follows
immediately the base slots, or neither class has a dictoffset.
2. the weakrefs offset is the same for both classes, and it follows
immediately the dictoffset (or the base slots if there was no
dictoffset).
3. there are no additional slots

Now, this *could* be generalized to treating to classes the same if
they have the same user-defined slots. If you want to do that, looking
at the size is not sufficient - you also have to verify that the slot
names and offsets are the same, lock-step-wise.

HTH,
Martin

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
import reassignment different at module and function scope Brendan Miller Python 0 01-31-2009 07:31 AM
Weird lambda rebinding/reassignment without me doing it ssecorp Python 5 07-12-2008 08:32 PM
const variable reassignment rahul8143@gmail.com C Programming 11 08-24-2005 08:05 AM
Newbie: OnClick reassignment Ted Singh ASP General 4 12-04-2004 05:41 PM
Reassignment to a const variable that was declared in an if() Oplec C++ 6 11-01-2003 03:06 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57