Example of execfile() problem (was Re: [albatross-users] New release?)
Matt Goodall
matt at pollenation.net
Sun Sep 21 01:27:41 EST 2003
The following is not an Albatross page module but demonstrates the
problem using execfile() to dynamically load and execute code.
The code fails during pickle.loads(...) since the Test class cannot be
found. Strange but true ;-).
--- test.py ---
import cPickle as pickle
data = None
class Test:
def __init__(self):
self.count = 0
def run():
global data
if data:
t = pickle.loads(data)
else:
t = Test()
print 't is of type', t
t.count += 1
print 'count=%s' % t.count
data = pickle.dumps(t)
---------------
And then from inside an Python shell:
>>> d={}
>>> execfile('test.py',d)
>>> d['run']()
t is of type <__builtin__.Test instance at 0x402125ac>
count=1
>>> d['run']()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "test.py", line 12, in run
t = pickle.loads(data)
AttributeError: 'module' object has no attribute 'Test'
>>>
--
Matt Goodall, Pollenation Internet Ltd
w: http://www.pollenationinternet.com
e: matt at pollenation.net
t: +44 (0)113 2252500
More information about the Albatross-users
mailing list