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() removed from Python3
Type: Stage:
Components: Versions: Python 3.0, Python 3.1
process
Status: closed Resolution:
Dependencies: 4628 Superseder:
Assigned To: Nosy List: jhylton, loewis, vstinner
Priority: normal Keywords: patch

Created on 2009-03-20 01:54 by vstinner, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
open_script.patch vstinner, 2009-03-20 01:54
Messages (6)
msg83845 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2009-03-20 01:54
In "What’s New In Python 3.0" document, I can read "Removed 
execfile(). Instead of execfile(fn) use exec(open(fn).read())". The 
new syntax has two problems:
 - if the file is not encoding in UTF-8, we get an unicode error. Eg. 
see issue #4282
 - exec() doesn't support newline different than \n, see issue #4628

We need a short function which opens the Python file with the right 
encoding. Get Python file encoding and open it with the right encoding 
is a command pattern.

Attached patch proposes a function open_script() to open a Python 
script with the correct encoding. Using it, execfile() can be replaced 
by exec(open_script(fn).read()) which doesn't have to two binary file 
problems.
msg83853 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2009-03-20 05:56
-1. There is a much simpler solution to the problem: use exec(open(fn,
"rb").read())
msg83858 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2009-03-20 08:53
martin> There is a much simpler solution to the problem: 
martin> use exec(open(fn,"rb").read())

Ok... but there is the newline issue: (self quote) "exec() doesn't support 
newline different than \n, see issue #4628".

And open_python() can be used for other usages than execfile() ;-)

Note: tokenize.open_python() is maybe not the best module and/or function 
name.
msg83885 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2009-03-20 23:27
> Ok... but there is the newline issue: (self quote) "exec() doesn't support 
> newline different than \n, see issue #4628".

So that issue should get fixed, then.

> And open_python() can be used for other usages than execfile() ;-)
> 
> Note: tokenize.open_python() is maybe not the best module and/or function 
> name.

I remain opposed to the entire concept.
msg83932 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2009-03-21 10:48
>> Ok... but there is the newline issue: (self quote) 
>> "exec() doesn't support newline different than \n, 
>> see issue #4628".

> So that issue should get fixed, then.

Ok, I will work in the other other issue. If #4628 is fixed, this 
issue becomes meaningless ;-)
msg84821 - (view) Author: Jeremy Hylton (jhylton) (Python triager) Date: 2009-03-31 16:15
It doesn't seem helpful to leave this issue open, particularly since the
title suggest there's a problem with execfile being removed and that's
not going to change.
History
Date User Action Args
2022-04-11 14:56:46adminsetgithub: 49774
2009-03-31 16:15:29jhyltonsetstatus: open -> closed
nosy: + jhylton
messages: + msg84821

2009-03-21 10:48:16vstinnersetdependencies: + No universal newline support for compile() when using bytes
messages: + msg83932
2009-03-20 23:27:28loewissetmessages: + msg83885
2009-03-20 08:53:10vstinnersetmessages: + msg83858
2009-03-20 05:56:21loewissetnosy: + loewis
messages: + msg83853
2009-03-20 01:54:03vstinnercreate