468,136 Members | 1,399 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,136 developers. It's quick & easy.

tp_base, tp_basesize, and __slots__ instance __class__ reassignment

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

Jul 18 '05 #1
1 1642
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

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by anabell | last post: by
9 posts views Thread by flori | last post: by
5 posts views Thread by Jean Brouwers | last post: by
3 posts views Thread by Nick Jacobson | last post: by
7 posts views Thread by Porky Pig Jr | last post: by
2 posts views Thread by Ewald R. de Wit | last post: by
4 posts views Thread by dan.j.weber | last post: by
3 posts views Thread by John Machin | last post: by
27 posts views Thread by Licheng Fang | last post: by
27 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.