Compiling and running a flat Python file with args & keywds

martin z pxtl at hotmail.com
Fri May 30 11:19:04 EDT 2003


Allrighty - I have a python text file that I want to run as a function.
Ideally, I would like to store it in its fastest compiled form.

Now, here's the problem.  I have two structures I want to run with it.
First is a set of globals - no problem there - feed them in as globals.
Sure, using code requires a god-awful number of global statements, but it
works.

Second, and more messily, are the arguments.  Now, I've got the traditional
argument tuple and keyword dictionary, but they are not defined in a
tradtional def-statement.  They're stored in a header, elsewhere.  That's
only part of the problem.  Now, I have the name of the arguments stored
elsewhere, so the args/keywords can be merged into a single dictionary, but
then what?  Do I just shove it in as the locals?  That works, but it kills
the optimization, as I can't compile with CO_OPTIMIZED turned on under those
circumestances.

The second problem is that the number of keywords is unknown - the error
checking on the keywords is done externally.  One given code object could be
called from several different headers, each with their own set of keywords
(though identical argument sets).

So, I need to know how to shove these args/keywds into my code object on
run.  I'm somewhat new to Python, so I have no idea how the
cellvars/freevars work, but i suspect those may be useful.  Any insights?






More information about the Python-list mailing list