saving pre-compiled scripts
Hi, I'm working on an application that reads python scripts from XML elements and compiles them during my app's startup process using. The application then runs the resulting compiled PyCodeObjects using PyEval_EvalCode as they are needed. Now I'm wondering if its possible to pre-compile the scripts and save the PyCodeObjects in a binary file (or in CDATA in an XML element) and have them ready to run when the application starts up. My only constraint is that I need to be able to load the scripts from memory (which is why using Py_CompileString has worked well for me). Is there any way I can get the size of a PyCodeObject so that I could save it to a binary file? If I did that would it run correctly if I reloaded it and tried to Eval it? Can I load a pyc file from memory somehow? Any guidance would be greatly appreciated. Thanks, Derek -- http://mail.python.org/mailman/listinfo/python-list
Re: saving pre-compiled scripts
Another thing which may be important to note re: my constraints is that each script is essentially being run as a function. In fact, every script element I parse in XML gets wrapped in a function def before I send it to Py_CompileString. I then PyEval the result of that function, and then run Py_CompileString again, passing only the automatically generated function call. The result of the second Py_CompileString is what I pass to PyEval_EvalCode whenever my app needs to run that script (function). -- http://mail.python.org/mailman/listinfo/python-list
Re: saving pre-compiled scripts
I probably should have also mentioned that my application is written in C++ and using the Python/C API. -- http://mail.python.org/mailman/listinfo/python-list
importing pyc from memory?
Using the Python/C API, is there a way I can import a pyc file that I have in memory (as opposed to loading from disk)? I'm trying to save compiled python code in a proprietary file format to cut reduce the overhead of compiling all my scripts when my app starts up. Derek -- http://mail.python.org/mailman/listinfo/python-list
Re: importing pyc from memory?
Up to now, I've had all my python scripts defined in XML elements, which were compiled when my program started up using Py_CompileString. This has worked great, but I'm finding that most of the time my app uses to start up is spent in that Py_CompileString, because of the large number of scripts that I use. Unfortunately, storing these scripts as .pyc files on disk is not an option for me. -- http://mail.python.org/mailman/listinfo/python-list
Re: importing pyc from memory?
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? -- http://mail.python.org/mailman/listinfo/python-list
Re: importing pyc from memory?
I do have to do everything you describe here in the current implementation of my system. I realize that importing a pyc file and compiling a string results in two different things. This is okay though as I'm fairly sure this will still suit my needs. As I said, the file format I plan to store these compiled scripts in is proprietary, but it would be very comparable storing them in XML elements as CDATA. I'm hoping that this would allow me to import and run them, thereby removing the need for my app to compile scripts. -- http://mail.python.org/mailman/listinfo/python-list