classification
Title: exec(open(filename)) doesn't work
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.0
process
Status: closed Resolution: duplicate
Dependencies: Superseder: 'exec' does not accept what 'open' returns
View: 1762972
Assigned To: Nosy List: brett.cannon, leif.walsh, ms
Priority: normal Keywords:

Created on 2008-06-24 20:45 by ms, last changed 2008-06-26 08:58 by georg.brandl. This issue is now closed.

Messages (8)
msg68700 - (view) Author: Mike Speciner (ms) Date: 2008-06-24 20:45
In 2.5, exec(open(filename)), where filename refers to a file with
python code, executes the code.
In 2.5, open returns an open file, but in 3.0, open returns a stream,
and so exec (which wants "a string, file, or code object, not
TextIOWrapper") returns an error. exec should probably be able to handle
a stream.
msg68701 - (view) Author: Leif Walsh (leif.walsh) Date: 2008-06-24 21:00
There's 'execfile' in Lib/idlelib/PyShell.py...I'm looking for others.

There is also a class of functions 'exec*' (and a related class, 
'spawn*') in Lib/os.py that claims to do just what you want, but 
replacing the current process (or, in spawn*'s case, in a new thread).

That said, I agree that exec should handle streams, and if you could 
give me a heads up as to where the code is for exec(), I could try my 
hand at a patch.
msg68702 - (view) Author: Mike Speciner (ms) Date: 2008-06-24 22:05
I'm actually quite new to Python. My current interpreter of choice is
PostScript, followed by Matlab, but I've been discouraged from using
either one here at EMC. (PostScript because it's obscure, Matlab because
it's expensive!)

Meanwhile, I'm on a wintel machine, making perusing the Python source
tree difficult. So I'm not going to be much help in that regard. 

By the way, I was told that execfile is gone from python 3.

--ms
msg68703 - (view) Author: Leif Walsh (leif.walsh) Date: 2008-06-24 22:08
On Tue, Jun 24, 2008 at 3:05 PM, Mike Speciner <report@bugs.python.org> wrote:
> By the way, I was told that execfile is gone from python 3.

It certainly isn't in the branch I just checked out.
msg68704 - (view) Author: Leif Walsh (leif.walsh) Date: 2008-06-24 22:09
(isn't gone, that is)
msg68705 - (view) Author: Mike Speciner (ms) Date: 2008-06-24 22:33
Well, I think I found exec in bltinmodule.c,
but execfile isn't in that file. [In the 3.0 tree.]
msg68708 - (view) Author: Leif Walsh (leif.walsh) Date: 2008-06-24 22:56
execfile is in Lib/idlelib/PyShell.py, and is an instance function, so
it's not really standard for regular code, and would also be hard to
use correctly.  Sorry.
msg68735 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2008-06-25 17:20
issue 1762972 supercedes this.
History
Date User Action Args
2008-06-26 08:58:37georg.brandlsetstatus: open -> closed
resolution: duplicate
2008-06-25 17:20:23brett.cannonsetsuperseder: 'exec' does not accept what 'open' returns
messages: + msg68735
nosy: + brett.cannon
2008-06-24 22:56:25leif.walshsetmessages: + msg68708
2008-06-24 22:33:56mssetmessages: + msg68705
2008-06-24 22:09:18leif.walshsetmessages: + msg68704
2008-06-24 22:08:37leif.walshsetmessages: + msg68703
2008-06-24 22:05:43mssetmessages: + msg68702
2008-06-24 21:00:51leif.walshsetnosy: + leif.walsh
messages: + msg68701
2008-06-24 20:45:46mscreate