Annoying problem with rexec

Ralf Doering ralf.doering at
Tue Aug 3 02:33:00 EDT 1999

maybe someone can explain what happens here. Like stated in the
subject, the problem is about the rexec module. I tracked it down to
this simple example:

def testrexec():
    ml = ['test.1','test.2','test.3']
    for mod in ml:
        temp = "/tmp/"+mod
        print "**** About to open:", temp
        f2 = open(temp,"w")
        f2.write("I WAS HERE.\n")
        print "****",temp, "closed ."
        saved_open = open

        import rexec
        r = rexec.RExec(verbose=1)
        open = r.r_open
        #do some stuff here like load_modules ...
        open = saved_open

As a result, I get:

>>> testrexec.testrexec_ok()
**** About to open: /tmp/test.1
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "", line 8, in testrexec
    f2 = open(temp,"w")
NameError: open

I do not understand, why open becomes undefined, even *before* rexec
is imported and before I try to do nasty things with the function
reference. I did expect, that the first call to "open" in write mode
should succeed, even if something goes wrong with the references to
open later. But it seems, that I`m  wrong.
However, if I insert a "global open" right after the start of the
function, it works as expected.
Anyone out there how can enlight me?

No sig -- no fun

More information about the Python-list mailing list