Winter Madness - Passing Python objects as Strings

Hendrik van Rooyen mail at
Thu Jun 4 03:25:35 EDT 2009

When the days get colder and the nights longer,
then evil things are hatched.

A can is like a pickle, in that it is a string, but anything
can be canned.
Unlike a pickle, a can cannot leave the process, though,
unless the object it points to lives in shared memory.

Here is the output of a test session:
<test output start>
> python -i

dir(Can) yields:
['__doc__', '__file__', '__name__', 'can', 'uncan']

Testing string object
s is:          The quick brown fox jumps over the lazy dog
id(s) is:      47794404772392
ps = can(s) :  47794404772392
t = uncan(ps): The quick brown fox jumps over the lazy dog
t is s gives:  True

Testing q = Queue.Queue()
q is:          <Queue.Queue instance at 0x2b780085e830>
id(q) is:      47794404845616
pq = can(q) :  47794404845616
r = uncan(pq): <Queue.Queue instance at 0x2b780085e830>
r is q gives:  True

Testing banana class object
b = banana()   <__main__.banana object at 0x73d190>
id(b) is:      7590288
pb = can(c) :  7590288
c = uncan(pb): <__main__.banana object at 0x73d190>
c is b gives:  True

That's it, folks!
>>> pcan = can(Can.can)
>>> pcan
>>> griz = uncan(pcan)
>>> griz is can
>>> z = 'foo'
>>> griz(z)
>>> uncan(_)
>>> griz
<built-in function can>
>>> # uncanny!
<end of test output>

Now I have only tested it on my system, which is 64 bit SuSe Linux.
I suspect it is fragile.
I know it is dangerous - if you uncan a random
number, then if you are lucky, you will get a segfault.
If you are not lucky, your life will be completely

If you have any interest, contact me and I will
send you the source.

- Hendrik

More information about the Python-list mailing list