Annoying problem with rexec
david.stegbauer at cz.opel.com
david.stegbauer at cz.opel.com
Tue Aug 3 10:54:55 EDT 1999
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.
More information about the Python-list
mailing list