[Python-Dev] Fun with numbers
Guido van Rossum
guido@python.org
Thu, 25 Apr 2002 21:02:53 -0400
> Tim, Jeremy, and I are having so much fun...
[...]
> >>> 23000 .__class__ = bool
> Debug memory block at address p=0x814f544:
> 485823496 bytes originally requested
> the 4 pad bytes at p-4 are not all FORBIDDENBYTE (0xfb):
> at p-4: 0x7a *** OUCH
> at p-3: 0x61 *** OUCH
> at p-2: 0xc8 *** OUCH
> at p-1: 0x3c *** OUCH
> the 4 pad bytes at tail=0x250a094c are Segmentation fault
Ouch. Fixed in CVS for this particular case, but I think you may
still be able provoke this with a built-in type that derives from
another built-in type without adding any new fields, if the base type
has a funky allocator that the derived type doesn't inherit. Also,
the fix means that if an extension defines a type that inherits from
int but doesn't override tp_alloc and tp_free, it inherits a tp_alloc
that's not matched to the tp_free. But why would anyone do that...?
Inspired by this, I tried something else.
>>> print True
True
>>> True.__class__ = int
>>> print True
1
>>>
There are no ill side effects. Handy for bool-haters. :-)
--Guido van Rossum (home page: http://www.python.org/~guido/)