Is this a contradiction in the docs ?

Discussion in 'Python' started by Fuzzyman, Feb 2, 2005.

  1. Fuzzyman

    Fuzzyman Guest

    The following two passages from the python documentation *appear* to
    contradict each other. Equally possible (or more likely !) is that I
    misunderstand it :

    eval :
    This function can also be used to execute arbitrary code objects (such
    as those created by compile()). In this case pass a code object instead
    of a string. The code object must have been compiled passing 'eval' as
    the kind argument.


    compile:
    The kind argument specifies what kind of code must be compiled; it can
    be 'exec' if string consists of a sequence of statements, 'eval' if it
    consists of a single expression, or 'single' if it consists of a single
    interactive statement (in the latter case, expression statements that
    evaluate to something else than None will be printed).

    The docs for compile say that if you are creating a code object from a
    sequence of statements you must use the kind argument 'exec'. Eval says
    that if you are using the eval function you must use 'eval' as your
    kind argument.

    In practise I have found that using the eval function with code objects
    compiled with 'exec' as the kind argument works fine. Is this a 'bug'
    in the docs ?
    Regards,

    Fuzzy
    http://www.voidspace.org.uk/python/index.shtml
    Fuzzyman, Feb 2, 2005
    #1
    1. Advertising

  2. Fuzzyman a écrit :
    > The following two passages from the python documentation *appear* to
    > contradict each other. Equally possible (or more likely !) is that I
    > misunderstand it :
    >
    > eval :
    > This function can also be used to execute arbitrary code objects (such
    > as those created by compile()). In this case pass a code object instead
    > of a string. The code object must have been compiled passing 'eval' as
    > the kind argument.
    >
    >
    > compile:
    > The kind argument specifies what kind of code must be compiled; it can
    > be 'exec' if string consists of a sequence of statements, 'eval' if it
    > consists of a single expression, or 'single' if it consists of a single
    > interactive statement (in the latter case, expression statements that
    > evaluate to something else than None will be printed).
    >
    > The docs for compile say that if you are creating a code object from a
    > sequence of statements you must use the kind argument 'exec'. Eval says
    > that if you are using the eval function you must use 'eval' as your
    > kind argument.
    >
    > In practise I have found that using the eval function with code objects
    > compiled with 'exec' as the kind argument works fine. Is this a 'bug'
    > in the docs ?
    > Regards,
    >
    > Fuzzy
    > http://www.voidspace.org.uk/python/index.shtml
    >


    IMO, it's just logical the code sent to "eval" was compiled with "eval"
    as the kind argument. But don't forget the documentation is kind of
    "abstract" and that CPython is _just_ an implementation (ok, it's the
    reference implementation) of python. You'd better follow the doc if you
    want your code to work on other Python implementation (ie. JPython, ...).

    Pierre
    Pierre Barbier de Reuille, Feb 2, 2005
    #2
    1. Advertising

  3. Fuzzyman

    Fuzzyman Guest

    Pierre Barbier de Reuille wrote:
    > Fuzzyman a écrit :
    > > The following two passages from the python documentation *appear*

    to
    > > contradict each other. Equally possible (or more likely !) is that

    I
    > > misunderstand it :
    > >
    > > eval :
    > > This function can also be used to execute arbitrary code objects

    (such
    > > as those created by compile()). In this case pass a code object

    instead
    > > of a string. The code object must have been compiled passing 'eval'

    as
    > > the kind argument.
    > >
    > >
    > > compile:
    > > The kind argument specifies what kind of code must be compiled; it

    can
    > > be 'exec' if string consists of a sequence of statements, 'eval' if

    it
    > > consists of a single expression, or 'single' if it consists of a

    single
    > > interactive statement (in the latter case, expression statements

    that
    > > evaluate to something else than None will be printed).
    > >
    > > The docs for compile say that if you are creating a code object

    from a
    > > sequence of statements you must use the kind argument 'exec'. Eval

    says
    > > that if you are using the eval function you must use 'eval' as your
    > > kind argument.
    > >
    > > In practise I have found that using the eval function with code

    objects
    > > compiled with 'exec' as the kind argument works fine. Is this a

    'bug'
    > > in the docs ?
    > > Regards,
    > >
    > > Fuzzy
    > > http://www.voidspace.org.uk/python/index.shtml
    > >

    >
    > IMO, it's just logical the code sent to "eval" was compiled with

    "eval"
    > as the kind argument. But don't forget the documentation is kind of
    > "abstract" and that CPython is _just_ an implementation (ok, it's the


    > reference implementation) of python. You'd better follow the doc if

    you
    > want your code to work on other Python implementation (ie. JPython,

    ....).
    >


    Yes.. but that would mean that eval could only run code objects that
    "consist of a single expression".. which I doubt is the reality or
    the intention.

    Regards,
    Fuzzyman
    http://www.voidspace.org.uk/python/index.shtml



    > Pierre
    Fuzzyman, Feb 2, 2005
    #3
  4. Fuzzyman

    Steve Holden Guest

    Fuzzyman wrote:

    > The following two passages from the python documentation *appear* to
    > contradict each other. Equally possible (or more likely !) is that I
    > misunderstand it :
    >
    > eval :
    > This function can also be used to execute arbitrary code objects (such
    > as those created by compile()). In this case pass a code object instead
    > of a string. The code object must have been compiled passing 'eval' as
    > the kind argument.
    >
    >
    > compile:
    > The kind argument specifies what kind of code must be compiled; it can
    > be 'exec' if string consists of a sequence of statements, 'eval' if it
    > consists of a single expression, or 'single' if it consists of a single
    > interactive statement (in the latter case, expression statements that
    > evaluate to something else than None will be printed).
    >
    > The docs for compile say that if you are creating a code object from a
    > sequence of statements you must use the kind argument 'exec'. Eval says
    > that if you are using the eval function you must use 'eval' as your
    > kind argument.
    >
    > In practise I have found that using the eval function with code objects
    > compiled with 'exec' as the kind argument works fine. Is this a 'bug'
    > in the docs ?
    > Regards,
    >


    "Dear List:

    I live close to a railroad, and the tracks are surrounded by a fence
    with large signs saying 'Walking on the tracks can be dangerous'.

    I have walked on the tracks several times, however, and I don't see why
    the railway company put those signs there. In fact I'm walking along the
    tracks right now as I enter this post on my hand-held, and

    [message ends with sound of locomotive horn followed by an ugly squelch]."

    That's the kind of practice best avoided. Are you breaking the rules
    just for the fun of it?

    regards
    Steve
    --
    Meet the Python developers and your c.l.py favorites March 23-25
    Come to PyCon DC 2005 http://www.python.org/pycon/2005/
    Steve Holden http://www.holdenweb.com/
    Steve Holden, Feb 2, 2005
    #4
  5. Fuzzyman a écrit :
    >
    >
    > Yes.. but that would mean that eval could only run code objects that
    > "consist of a single expression".. which I doubt is the reality or
    > the intention.
    >
    > Regards,
    > Fuzzyman
    > http://www.voidspace.org.uk/python/index.shtml
    >


    [Sorry, I deleted the commented lines because they were completely
    unreadable anyway]

    It's exactly what eval is for : evaluate a single expression and return
    its result ! Try with strings : you can't even evaluate a statement !
    You can only evaluate an expression !

    And what would be the return result of a sequence of statement ?

    If you want to execute a sequence, you want to use exec, not eval ...

    Pierre
    Pierre Barbier de Reuille, Feb 2, 2005
    #5
  6. Fuzzyman

    Fuzzyman Guest

    Steve Holden wrote:
    > Fuzzyman wrote:
    >
    > > The following two passages from the python documentation *appear*

    to
    > > contradict each other. Equally possible (or more likely !) is that

    I
    > > misunderstand it :
    > >
    > > eval :
    > > This function can also be used to execute arbitrary code objects

    (such
    > > as those created by compile()). In this case pass a code object

    instead
    > > of a string. The code object must have been compiled passing 'eval'

    as
    > > the kind argument.
    > >
    > >
    > > compile:
    > > The kind argument specifies what kind of code must be compiled; it

    can
    > > be 'exec' if string consists of a sequence of statements, 'eval' if

    it
    > > consists of a single expression, or 'single' if it consists of a

    single
    > > interactive statement (in the latter case, expression statements

    that
    > > evaluate to something else than None will be printed).
    > >
    > > The docs for compile say that if you are creating a code object

    from a
    > > sequence of statements you must use the kind argument 'exec'. Eval

    says
    > > that if you are using the eval function you must use 'eval' as your
    > > kind argument.
    > >
    > > In practise I have found that using the eval function with code

    objects
    > > compiled with 'exec' as the kind argument works fine. Is this a

    'bug'
    > > in the docs ?
    > > Regards,
    > >

    >
    > "Dear List:
    >
    > I live close to a railroad, and the tracks are surrounded by a fence
    > with large signs saying 'Walking on the tracks can be dangerous'.
    >
    > I have walked on the tracks several times, however, and I don't see

    why
    > the railway company put those signs there. In fact I'm walking along

    the
    > tracks right now as I enter this post on my hand-held, and
    >
    > [message ends with sound of locomotive horn followed by an ugly

    squelch]."
    >
    > That's the kind of practice best avoided. Are you breaking the rules
    > just for the fun of it?
    >


    Hmm.... documentation on code objects says :

    Code objects are returned by the built-in compile() function and can be
    extracted from function objects through their func_code attribute.

    A code object can be executed or evaluated by passing it (instead of a
    source string) to the exec statement or the built-in eval() function.

    It mentions no such restriction. I didn't properly absorb the 'must' in
    the eval description......

    Hmmm... my code will *never* need to run on anything other than CPython
    because I use py2exe to turn it into a frozen python distribution. So
    if it was always safe in CPython then that would be fine for me.

    The reason I like eval is that "If both dictionaries are omitted, the
    expression is executed in the environment where eval is called".
    However I think exec might do the same thing anyway. I'll just switch
    to exec.

    Regards,

    Fuzzy
    http://www.voidspace.org.uk/python/index.shtml

    > regards
    > Steve
    > --
    > Meet the Python developers and your c.l.py favorites March 23-25
    > Come to PyCon DC 2005 http://www.python.org/pycon/2005/
    > Steve Holden http://www.holdenweb.com/
    Fuzzyman, Feb 2, 2005
    #6
  7. Fuzzyman

    Fuzzyman Guest

    Pierre Barbier de Reuille wrote:
    > Fuzzyman a écrit :
    > >
    > >
    > > Yes.. but that would mean that eval could only run code objects

    that
    > > "consist of a single expression".. which I doubt is the reality

    or
    > > the intention.
    > >
    > > Regards,
    > > Fuzzyman
    > > http://www.voidspace.org.uk/python/index.shtml
    > >

    >
    > [Sorry, I deleted the commented lines because they were completely
    > unreadable anyway]
    >


    That's alright - I *have* to read via google, which hides quoted lines
    anyway......

    > It's exactly what eval is for : evaluate a single expression and

    return
    > its result ! Try with strings : you can't even evaluate a statement !


    > You can only evaluate an expression !
    >


    Right.

    > And what would be the return result of a sequence of statement ?
    >


    :) I'm not interested in the return result. The code object is run in
    the current namespace, so I use it to run scripts.

    > If you want to execute a sequence, you want to use exec, not eval ...
    >


    Yup - I've swapped them over in my code.

    Thanks
    Fuzzyman
    http://www.voidspace.org.uk/python/index.shtml

    > Pierre
    Fuzzyman, Feb 2, 2005
    #7
  8. Fuzzyman schrieb:
    > Hmmm... my code will *never* need to run on anything other than CPython
    > because I use py2exe to turn it into a frozen python distribution. So
    > if it was always safe in CPython then that would be fine for me.


    No one will ever need more than 640K of RAM and BTW, my program will be
    replaced well before the year 10000, so four digits for the year should be
    enough...

    Stefan
    Stefan Behnel, Feb 2, 2005
    #8
  9. Fuzzyman

    Terry Reedy Guest

    "Fuzzyman" <> wrote in message
    news:...
    > Equally possible (or more likely !) is that I misunderstand it :


    Or that you did not sufficiently test the indicated difference ;-)

    > The docs for compile say that if you are creating a code object from a
    > sequence of statements you must use the kind argument 'exec'. Eval says
    > that if you are using the eval function you must use 'eval' as your
    > kind argument.


    And I believe that if you use the eval function as a function, rather than
    as a subroutine, the latter *is* true.

    > In practise I have found that using the eval function with code objects
    > compiled with 'exec' as the kind argument works fine.


    A minimal code example backing your claim would have been illuminative.
    Eval'ing exec-compiled expressions does NOT work fine, as least not in 2.2,
    and, I suspect, in all other versions:

    >>> def test():

    .... print 'in test'
    .... return 1
    ....
    >>> coex = compile('test()', '', 'exec')
    >>> coev = compile('test()', '', 'eval')
    >>> print eval(coex)

    in test
    None # wrong due to exex-compile-eval mismatch bug
    >>> print eval(coev)

    in test
    1

    The compile flag affects the code generation, but it does not get attached
    to the resulting code object. Eval runs the code object without knowing
    whether the correct flag was used. Therefore, 'must use the correct flag'
    means 'must, in order to get correct functioning' (in particular, the
    return of the expression value rather than None), and not 'must, in order
    to run at all'. A 'practise' that only tests running and ignores incorrect
    value returning is not a complete test.

    Terry J. Reedy
    Terry Reedy, Feb 3, 2005
    #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. nospawn

    the developer contradiction ?

    nospawn, Sep 29, 2005, in forum: Java
    Replies:
    17
    Views:
    667
    Kenneth P. Turvey
    Oct 4, 2005
  2. Oliver Wong
    Replies:
    9
    Views:
    391
    Roedy Green
    Feb 10, 2006
  3. ES Kim

    contradiction in TC++PL?

    ES Kim, Oct 19, 2004, in forum: C++
    Replies:
    44
    Views:
    1,220
    Ioannis Vranos
    Oct 20, 2004
  4. Scott M.

    Precompile Contradiction?

    Scott M., Nov 22, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    355
    Steven Cheng[MSFT]
    Nov 23, 2007
  5. Gavin Sinclair

    Regular expression contradiction

    Gavin Sinclair, Jul 11, 2003, in forum: Ruby
    Replies:
    1
    Views:
    139
    Daniel Carrera
    Jul 11, 2003
Loading...

Share This Page