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

Ken Manheimer (klm@NIST.GOV)
Wed, 25 Jan 1995 16:23:48 -0500 (EST)

I probably should have mentioned in my posting that i had tried the kinds
of things that you suggested (and more). Perhaps i was doing something
wrong, but (1) execfile barfs trying to operate on .pyc files, and (2)
exec barfs on strings .read() from .pyc files. I guess it goes without
saying that they fail with the dynamic-load files.=20

1)=20
>>> execfile('/depot/python/lib/python/pdb.pyc')
Traceback (innermost last):
File "<stdin>", line 1, in ?
File "/depot/python/lib/python/pdb.pyc", line 1
=09=03=99=99-> s=03s=03=01d=09Without argument, print the list of available=
commands.
=09^
SyntaxError: invalid syntax
>>>=20

2)
>>> pycF =3D open('/depot/python/lib/python/pdb.pyc', 'r')
>>> pyc =3D pycF.read()
>>> exec pyc
Traceback (innermost last):
File "<stdin>", line 1, in ?
ValueError: embedded '\0' in exec string
>>>=20

> For "normal source" (.py) & .pyc files you can use __builtin__.execfile=
=20
> =09execfile( pathname, global_dict, local_dict ) =20
> [ And I expect now, optionally a builtin_dict, although I haven't tried=
=20
> it yet. This (execfile) is not documented in the hypertext docs (I think)=
;=20
> I haven't printed out the 1.2 beta docs yet - is it in there ? ]=20

It is in the libref, in the builtins node. As is eval().

> - or just read in the string ( text or byte-code ) and 'exec' it with
> the dictionary that provides the proper context.=20
>=20
> BUT: for compiled dynamic-loadable modules ? Hmmm...=20
> I don't think there is a general mechanism. The compiled module=20
> would probably require some hooks to allow this. This will take
> some thought!=20

The 'imp' module begins to provide the hooks =3D '.load_compiled()' and
'.load_dynamic()', but they do not enable me to inject a specific
binding, for __modpath__, so imports within the execing modules work
with respect to it.

> Can you give me a specific example or scenario ?=20

I am implementing, in my spare time (joke), import 'packages'. Among
other things, i need to be able to inject a binding for '__modpath__',
so that imports within the modules being loaded recursively inherit
the proper extensions to their load paths.

I'm not sure how better to paint the picture, other than to request
specifically what i said in my original message.

Ken
ken.manheimer@nist.gov, 301 975-3539