From: brian z. <bz...@zi...> - 2001-01-07 14:43:36
|
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 Additionally, in CPython 2.0, if the file is closed it is re-opened and executed. I also tested whether StringIO could be substited for a file and it appears to fail: >>> from StringIO import StringIO >>> s = StringIO("a = 'x'") >>> s <StringIO.StringIO instance at 007E1C1C> >>> t = {} >>> exec s in t Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: exec 1st arg must be string, code or file object >>> Given this, I've enclosed a working diff which mimics the exact behaviour of CPython 2.0. thanks, brian |