Is this a contradiction in the docs ?

Terry Reedy tjreedy at udel.edu
Wed Feb 2 19:10:43 EST 2005


"Fuzzyman" <fuzzyman at gmail.com> wrote in message 
news:1107342032.273119.194120 at o13g2000cwo.googlegroups.com...
> 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








More information about the Python-list mailing list