[ python-Bugs-1674503 ] execfile() response to syntax errors

SourceForge.net noreply at sourceforge.net
Tue Mar 6 13:18:07 CET 2007


Bugs item #1674503, was opened at 2007-03-05 23:38
Message generated for change (Comment added) made by gbrandl
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1674503&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: None
Group: None
>Status: Closed
>Resolution: Fixed
Priority: 3
Private: No
Submitted By: Jonathan Griffitts (jcgriffitts)
Assigned to: Nobody/Anonymous (nobody)
Summary: execfile() response to syntax errors

Initial Comment:
Python 2.5 seems to have an anomaly when execfile() encounters syntax errors.  

If I use execfile() to execute a file that contains a syntax error, it throws the expected SyntaxError exception but does not close the source file.  Since the file is still open, an external program can't modify it to fix the syntax error.

I have noticed this problem in Python 2.5 on Windows 2000 and Windows XP.  I tried to reproduce it on Python 2.3 on BSD Unix but it didn't seem to happen there.


---------------- spam.py ---------------------

[intentional-syntax-error)
----------------------------------------------

The following is my console session:

----------------------------------------------
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit 
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> execfile('spam.py')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "spam.py", line 1
    [intentional-syntax-error)
                             ^
SyntaxError: invalid syntax
>>>
----------------------------------------------

At this point file spam.py remains open until Python exits.  There's no obvious way to close the file from within the script because there is no file object.  Under Windows, this prevents spam.py from being edited, 
renamed, or deleted.


----------------------------------------------------------------------

>Comment By: Georg Brandl (gbrandl)
Date: 2007-03-06 12:18

Message:
Logged In: YES 
user_id=849994
Originator: NO

Indeed PyRun_FileEx only closed the file if compilation was successful,
fixed in rev. 54159, 54158 (2.5).

----------------------------------------------------------------------

Comment By: Jonathan Griffitts (jcgriffitts)
Date: 2007-03-06 01:17

Message:
Logged In: YES 
user_id=796443
Originator: YES

Easy work-around: use exec(file('spam.py')) instead of execfile('spam.py')

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1674503&group_id=5470


More information about the Python-bugs-list mailing list