This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: execfile() response to syntax errors
Type: Stage:
Components: None Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, jcgriffitts
Priority: low Keywords:

Created on 2007-03-05 23:38 by jcgriffitts, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg31433 - (view) Author: Jonathan Griffitts (jcgriffitts) Date: 2007-03-05 23:38
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.
msg31434 - (view) Author: Jonathan Griffitts (jcgriffitts) Date: 2007-03-06 01:17
Easy work-around: use exec(file('spam.py')) instead of execfile('spam.py')
msg31435 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-03-06 12:18
Indeed PyRun_FileEx only closed the file if compilation was successful, fixed in rev. 54159, 54158 (2.5).
History
Date User Action Args
2022-04-11 14:56:22adminsetgithub: 44664
2007-03-05 23:38:25jcgriffittscreate