Annoying problem with rexec

Darrell news at dorb.com
Tue Aug 3 16:06:06 CEST 1999


I'll take a stab at the answer here.
You are assigning to the variable named "open" later in the function.
These variables are allocated on entry to this function.
The variable open hasn't been assigned to yet but the name
is reserved for later.

>>> def x():
...     fp=open('test')
...     open=None
...
>>> x()
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 2, in x
NameError: open
>>>
--
--Darrell
Ralf Doering <ralf.doering at prakinf.tu-ilmenau.de> wrote in message
news:xzn1w99xwj.fsf at prakinf.tu-ilmenau.de...
>
> Hello,
> 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")
>         f2.close()
>         print "****",temp, "closed ."
>         saved_open = open
>
>         import rexec
>         r = rexec.RExec(verbose=1)
>         open = r.r_open
>         #do some stuff here like load_modules ...
>         pass
>         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 "testrexec.py", 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?
>
> Ralf
> --
> No sig -- no fun






More information about the Python-list mailing list