🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Python Script Storage

Started by
3 comments, last by c t o a n 20 years, 10 months ago
Recently, I''ve implemented a filesystem into my Python-enabled engine, and I''m currently able to read files into memory from any location on disk, either in an archive (''box'' file) or as plain-text/binary. I''ve now come to the problem of trying to import modules (I use the ..._ImportModule() functions to import scripts) from memory. You can do it by passing a filename to the function, and I believe there is also a method of passing a FILE* pointer. Is there a way that you guys can think of getting this to work? I''d like to eventually put the scripts in a box file (compressed...) and have Python read the scripts via my filesystem. The only way I can think of (not TOO bad...) is to read the files from the archives, then output them onto disk, and have Python read from them. I''ve also got some other ideas, but I''m curious what you guys can think of. Thanks! Chris Pergrossi My Realm | "Good Morning, Dave"
Chris PergrossiMy Realm | "Good Morning, Dave"
Advertisement
First thing that jumps to mind: Python2.3 can import from within zip files. You could store all your scripts in asdf.zip and then do ''sys.path.append(''./asdf.zip''); import ScriptInAsdf''.

Could you take a pointer to the memory location where the script is and cast it as (FILE*) and send that to Python? And typically you pass both the FILE* pointer and the filename to Python (the FILE* for doing work, the filename for printing useful tracebacks).

--
Dustin
You can replace __import__ in __builtins__ which is a Python function called whenever a module should be loaded. You can probably replace it with a C function object as well. If you do this, then your modules can go "import blah" and those modules will also be loaded recursively using the same mechanism.

I think to load a module without a FILE* you have to create an empty module with the given name (use imp.new_module(name) or the C equivalent), add it to the list in sys.modules (do this first to avoid circular dependency problems), load the source as a string (or code object if precompiled), and exec the string (or run the code object) using the module''s dictionary as global and local variables.
I can''t use v2.3 because boost.python doesn''t support it yet (at least, I don''t think it does). FILE is a struct that I''m not too sure how to use. I''ll research it a bit, but any assistance on the exact usage of the struct would be helpful. And AP, how do I replace the import function with a C++ object? And will it allow me to load the dictionary and namespaces as well as the script itself? Thanks guys.

Chris Pergrossi
My Realm | "Good Morning, Dave"
Chris PergrossiMy Realm | "Good Morning, Dave"
boost.python supports 2.3 in the latest cvs build

--
Dustin

This topic is closed to new replies.

Advertisement