importing pyc from memory?

Bengt Richter bokr at oz.net
Mon Jul 4 14:28:04 EDT 2005


On Mon, 04 Jul 2005 19:25:13 +0200, Thomas Heller <theller at python.net> wrote:

>"Derek van Vliet" <dvanvliet at 3dna.net> writes:
>
>> The second method you describe sounds like it is along the lines of
>> what I need to do. Is there a way to do this using the Python/C API?
>>
>> For instance, if I have the contents of a pyc file loaded entirely into
>> memory, and I have a pointer to the beginning of the file and the size
>> in bytes at my disposal, can I import that by any means?
>
>The .pyc file format is sort of 'documented' in lib/py_compile.py.
>IIRC, it is a magic number, a timestamp, plus a marshalled code
>object.
>
I'm wondering if what the OP has is really .pyc files "in memory" or code objects.
If he is using Py_CompileString to compile "scripts" extracted from XML,
wouldn't he get code objects? And if so, wouldn't he have to execute them
in a constructed module dict and be careful of normal imports in the "scripts"
etc etc to simulate import? Anyway, it feels like what he wants to do could be done,
but "the devil is in the details," which are missing ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list