Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python 3: exec arg 1

Reply
Thread Tools

Python 3: exec arg 1

 
 
Alan G Isaac
Guest
Posts: n/a
 
      01-17-2009
In Python 3, you can no longer ``exec(open(filename))``.

I guess the reason is that in 3.0 ``open`` returns a stream,
instead of open file, and exec wants
"a string, bytes, or code object" and not a "TextIOWrapper".
So it returns an error.

Is it intentional that ``exec`` cannot handle a TextIOWrapper?

Bottom line: has ``execfile(filename)`` really become
``exec(open(filename).read())``? Is this a good thing?

Thanks,
Alan Isaac
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      01-17-2009
Alan G Isaac wrote:
> In Python 3, you can no longer ``exec(open(filename))``.
>
> I guess the reason is that in 3.0 ``open`` returns a stream,
> instead of open file, and exec wants
> "a string, bytes, or code object" and not a "TextIOWrapper".
> So it returns an error.
>
> Is it intentional that ``exec`` cannot handle a TextIOWrapper?
>
> Bottom line: has ``execfile(filename)`` really become
> ``exec(open(filename).read())``? Is this a good thing?


Yes. Yes.

 
Reply With Quote
 
 
 
 
Alan G Isaac
Guest
Posts: n/a
 
      01-17-2009
> Alan G Isaac wrote:
>> Is it intentional that ``exec`` cannot handle a TextIOWrapper?


>> Bottom line: has ``execfile(filename)`` really become
>> ``exec(open(filename).read())``? Is this a good thing?



On 1/17/2009 4:20 PM Terry Reedy apparently wrote:
> Yes. Yes.



OK. Why?

Alan Isaac
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      01-18-2009
Alan G Isaac wrote:
>> Alan G Isaac wrote:
>>> Is it intentional that ``exec`` cannot handle a TextIOWrapper?

>
>>> Bottom line: has ``execfile(filename)`` really become
>>> ``exec(open(filename).read())``? Is this a good thing?

>
>
> On 1/17/2009 4:20 PM Terry Reedy apparently wrote:
>> Yes. Yes.

>
>
> OK. Why?


This: execfile(filename)
is a trivial (9 keystroke) abbreviation of
this: exec(open(filename).read())
which is similar in effect to
this: from filename import *
so it is really not needed.

 
Reply With Quote
 
Alan G Isaac
Guest
Posts: n/a
 
      01-18-2009
>>> Alan G Isaac wrote:
>>>> Is it intentional that ``exec`` cannot handle a TextIOWrapper?
>>>> Bottom line: has ``execfile(filename)`` really become
>>>> ``exec(open(filename).read())``? Is this a good thing?



>> On 1/17/2009 4:20 PM Terry Reedy apparently wrote:
>>> Yes. Yes.



> Alan G Isaac wrote:
>> OK. Why?



On 1/17/2009 10:08 PM Terry Reedy apparently wrote:
> This: execfile(filename)
> is a trivial (9 keystroke) abbreviation of
> this: exec(open(filename).read())
> which is similar in effect to
> this: from filename import *
> so it is really not needed.



Well, that does not really answer my question, imo.
I do not much care about the disappearance of ``execfile``.
I was asking, why is it a **good thing** that
``exec`` does not accept a TextIOWrapper?
Or is it just not implemented yet?
What is the gain from this particular backwards
incompatibility (in the sense that ``exec(open(fname))``
no longer works)?

Context: I used to be able to tell students they
could run their scripts from the interpreter
prompt with ``execfile(fname)``. I expected to
be able to tell them to ``exec( open(fname) )``,
which worked in Python 2. Now instead it is
``exec( open(filename).read() )`` which is a bit
more mysterious to a newbie. (Note: I teach
economics, not computer science.) And despite your
claim above, you know that ``import`` is not
really equivalent, and even more so now that
``reload`` must be imported.

Thanks,
Alan Isaac
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-18-2009
On Sun, 18 Jan 2009 14:36:15 +0000, Alan G Isaac wrote:

> Well, that does not really answer my question, imo. I do not much care
> about the disappearance of ``execfile``. I was asking, why is it a
> **good thing** that ``exec`` does not accept a TextIOWrapper?



I'm not sure if this is a stupid question or not, but what's a
TextIOWrapper? In the example you give:

exec(open(fname))

the argument to exec -- open(fname) -- is a file object:

>>> type(open('hello.py'))

<type 'file'>


BTW, exec is a statement. The brackets there are totally superfluous. You
can, and should, write:

exec open(fname)


> Or is it just not implemented yet?
> What is the gain from this particular backwards incompatibility (in the
> sense that ``exec(open(fname))`` no longer works)?


Simplicity of implementation?


> Context: I used to be able to tell students they could run their scripts
> from the interpreter prompt with ``execfile(fname)``. I expected to be
> able to tell them to ``exec( open(fname) )``, which worked in Python 2.
> Now instead it is ``exec( open(filename).read() )`` which is a bit more
> mysterious to a newbie. (Note: I teach economics, not computer
> science.)


Why don't you give your students a site.py module containing

def execfile(fname):
exec open(fname).read()

and tell them to do "from site import execfile"?

If you control the environment they are running their programs on (school
computers?) then you can put a startup file in their path and have
execfile imported automatically.


--
Steven
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-19-2009
On Sun, 18 Jan 2009 11:06:10 -0600, Rob Williscroft wrote:

> You must have missed the subject line: "Re: Python 3: exec arg 1"


Doh!



--
Steven
 
Reply With Quote
 
Alan G Isaac
Guest
Posts: n/a
 
      01-20-2009
On 1/18/2009 9:36 AM Alan G Isaac apparently wrote:
> I do not much care about the disappearance of ``execfile``.
> I was asking, why is it a **good thing** that
> ``exec`` does not accept a TextIOWrapper?
> Or is it just not implemented yet?
> What is the gain from this particular backwards
> incompatibility (in the sense that ``exec(open(fname))``
> no longer works)?


Still interested in an answer...
Alan Isaac
 
Reply With Quote
 
Alan G Isaac
Guest
Posts: n/a
 
      01-22-2009
On 1/20/2009 3:53 PM Rob Williscroft apparently wrote:
> http://bugs.python.org/issue1762972 (*)



Useful. Thanks.
Alan Isaac
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass a multiline arg to exec('some.exe arg')? n00m Python 5 05-05-2008 02:58 PM
Runtime.exec(String[]) Doesn't Always Work, bBut Runtime.exec(String) Does Hal Vaughan Java 11 05-22-2006 04:49 PM
Trouble with setTimeout(arg, arg) nat.hourt@gmail.com Javascript 7 11-12-2005 05:13 PM
exec "statement" VS. exec "statement" in globals(), locals() tedsuzman Python 2 07-21-2004 08:41 PM
Backup Exec 9.1: The Backup Exec job engine system service is not responding Christian Falch Computer Support 1 06-23-2004 02:22 AM



Advertisments