Re: How to do a controlled exec compiled and dynamic-load files?

Steven D. Majewski (sdm7g@virginia.edu)
Wed, 25 Jan 1995 17:20:29 -0500 (EST)

For .pyc code try:

[ where xx.py is:
x = 1
y = range(10)
print __name__
and it has been previously imported to produce a xx.pyc file ]

>>> f = open( 'xx.pyc', 'r' )
>>> pyc = f.read()
>>> import marshal
>>> code = marshal.loads( pyc[8:] )
>>> code
<code object ? at 2009d548, file "./xx.py", line 0>
>>> D = {}
>>> D['__name__'] = 'SPAM'
>>> exec code in D, D
SPAM
>>> D
{'x': 1, 'y': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], '__name__': 'SPAM'}

If you look at the import code, a .pyc file has a (long) magic
version number and another long ( reserver for future use ??? )
before the code string.

[ Yeah - I know! there's a lot of stuff to be documented and explained
w.r.t. how to actually use the new features. None of this was expected
to be obvious, but I haven't had time to write this all up, so thanks
for bringing up the subject! ]

---| Steven D. Majewski (804-982-0831) <sdm7g@Virginia.EDU> |---
---| Computer Systems Engineer University of Virginia |---
---| Department of Molecular Physiology and Biological Physics |---
---| Box 449 Health Science Center Charlottesville,VA 22908 |---