|
From: <bc...@wo...> - 2001-01-07 20:42:33
|
[brian zimmer]
>I was trying to use the following syntax in Jython but I get an exception:
>
>>>> t = {}
>>>> exec open(filename) in t
>
>Traceback (innermost last):
> File "install.py", line 381, in ?
> File "install.py", line 47, in run
> File "install.py", line 71, in detectComponents
> File "install.py", line 89, in readProperties
>TypeError: exec: argument 1 must be string or code object
>
>This works in CPython and appears to be part of the spec:
>
>"The first expression should evaluate to either a string, an open file
>object, or a code object. ... If it is an open file, the file is parsed
>until EOF and executed."
>
>http://python.org/doc/current/ref/exec.html
Yep, and jython should support this as well.
>Additionally, in CPython 2.0, if the file is closed it is re-opened and
>executed.
I can't reproduce this behaviour in CPython 2.0. Instead it appears that
CPython treats a closed file as an empty file (no exception is thrown).
Do you have an example that show that the file is reopened?
>Given this, I've enclosed a working diff which mimics the exact behaviour of
>CPython 2.0.
If I'm correct regarding the reopen thing, I would rather do something
like:
if (fp.closed)
return;
contents = fp.read().toString();
to avoid the "I/O operation on closed file" exception.
regards,
finn
|