Mailing List Archive

Annoying problem with rexec
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
Annoying problem with rexec [ In reply to ]
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@prakinf.tu-ilmenau.de> wrote in message
news:xzn1w99xwj.fsf@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
Annoying problem with rexec [ In reply to ]
Hi Ralf,

You create LOCAL name "open" and using it to open files.

Function *definition* is "executed" before run and I *guess* the local name
"open" is somehow interned. (See built-in function "intern" in library
reference.) Then during looking up for function "open" is first found interned
"open", not the built-in one. It is the same case as if you assign to "stdout"
instead of to "sys.stdout".

So you have to use either "global open" or "__builtins__.open=saved_open". The
last one is better, I thing.

David

8<----------Ralf Doering wrote:-------------
>
>def testrexec():
> temp = "/tmp/test.1"
> print "**** About to open:", temp
> f2 = open(temp,"w")
> f2.write("I WAS HERE.\n")
> f2.close()
> print "****",temp, "closed ."
> saved_open = open
> 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
>>>>
>
>
>However, if I insert a "global open" right after the start of the
>function, it works as expected.