Python 3: exec arg 1

Discussion in 'Python' started by Alan G Isaac, Jan 17, 2009.

  1. Alan G Isaac

    Alan G Isaac Guest

    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
    Alan G Isaac, Jan 17, 2009
    #1
    1. Advertising

  2. Alan G Isaac

    Terry Reedy Guest

    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.
    Terry Reedy, Jan 17, 2009
    #2
    1. Advertising

  3. Alan G Isaac

    Alan G Isaac Guest

    > 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
    Alan G Isaac, Jan 17, 2009
    #3
  4. Alan G Isaac

    Terry Reedy Guest

    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.
    Terry Reedy, Jan 18, 2009
    #4
  5. Alan G Isaac

    Alan G Isaac Guest

    >>> 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
    Alan G Isaac, Jan 18, 2009
    #5
  6. 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
    Steven D'Aprano, Jan 18, 2009
    #6
  7. 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
    Steven D'Aprano, Jan 19, 2009
    #7
  8. Alan G Isaac

    Alan G Isaac Guest

    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
    Alan G Isaac, Jan 20, 2009
    #8
  9. Alan G Isaac

    Alan G Isaac Guest

    Alan G Isaac, Jan 22, 2009
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hal Vaughan
    Replies:
    11
    Views:
    1,108
    Gordon Beaton
    May 22, 2006
  2. tedsuzman
    Replies:
    2
    Views:
    7,082
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  3. n00m
    Replies:
    5
    Views:
    396
  4. Replies:
    7
    Views:
    175
  5. Replies:
    21
    Views:
    298
    Barry Schwarz
    Mar 5, 2014
Loading...

Share This Page