Simulating include statement

Gary Hubbard me at sandia.gov
Thu May 8 12:01:15 EDT 2003


I have a 15 year old application I am re-doing in python.  It is heavily
scripted in a VERY simple minded script language I created at the time which
has no concept of modules.  I am trying to ease the process of converting
the scripts (which I did not write) to python.

Here's the rub.  The existing scripts have only a single global namespace,
so a line like
    x = 2
in an imported module (using python terminology), act just as if the line
appeared in the calling module.  For example,

module a:
    x = 1
    call b
    print x

module b:
   x = 2

would print the value 2.


I kind of suspect that there is a cleaver way of making import work so that
the global dictionary in the imported module is really the global dictionary
in the caller, but I can't figure out how to do it.  I am writing a python
script to convert the modules, so anything lexical is relatively easy.  I am
nervous about doing anything that might change semantics, as some bugs will
be created.

Obviously, I could have the generated python code actually do an include,
but it would make the scripts unreadable.  They will all be conveted to
proper python later, but for now I would like a work around.

Any ideas?

Gary






More information about the Python-list mailing list