How does CO_FUTURE_DIVISION compiler flag get propagated?

Discussion in 'Python' started by Terry, Jul 2, 2011.

  1. Terry

    Terry Guest

    I've built a Python app for the iPhone, http://www.sabonrai.com/PythonMath/.

    Like embedding Python in another app, it uses PyRun_SimpleString() to
    execute commands entered by the user. For evaluating expressions, it
    uses PyEval_EvalCode() with the dictionary from the __main__ module.

    Future division ("from __future__ import division") works within
    scripts executed by import or execfile(). However, it does not work
    when entered interactively in the interpreter like this:

    >>> from __future__ import division
    >>> a=2/3


    You get classic (integer) division, but if you enter it as follows,
    you get future (float) division.

    >>> from __future__ import division;a=2/3


    It appears that the CO_FUTURE_DIVISION compiler flag is not being
    retained in the interpreter so that later commands get compiled
    without that flag.

    I found a hint in
    http://groups.google.com/group/comp...lnk=gst&q=co_future_division#960e47f572a59711,
    but I don't see that PyRun_SimpleStringFlags returns the flags it
    uses. I guess I could watch for the user to enter the import command
    but that's really brittle.

    Thanks.
    Terry, Jul 2, 2011
    #1
    1. Advertising

  2. Terry <> writes:

    > Future division ("from __future__ import division") works within
    > scripts executed by import or execfile(). However, it does not work
    > when entered interactively in the interpreter like this:
    >
    >>>> from __future__ import division
    >>>> a=2/3


    Are you referring to the interactive interpreter normally invoked by
    just running "python"? That seems to work for me:

    Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
    [GCC 4.5.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 2/3

    0
    >>> from __future__ import division
    >>> 2/3

    0.6666666666666666
    Hrvoje Niksic, Jul 2, 2011
    #2
    1. Advertising

  3. Terry

    Terry Guest

    On Jul 2, 3:55 pm, Hrvoje Niksic <> wrote:
    > Terry <> writes:
    > > Future division ("from __future__ import division") works within
    > > scripts executed by import or execfile(). However, it does not work
    > > when entered interactively in the interpreter like this:

    >
    > >>>> from __future__ import division
    > >>>> a=2/3

    >
    > Are you referring to the interactive interpreter normally invoked by
    > just running "python"?  That seems to work for me:
    >
    > Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
    > [GCC 4.5.2] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.>>>2/3
    > 0
    > >>> from __future__ import division
    > >>> 2/3

    >
    > 0.6666666666666666


    Yes, that works for me on my Mac. The problem I'm having is in a
    Python interpreter that I built for the iPhone. It uses
    PyRun_SimpleString() to execute user entered commands. After you
    import future division, it does not seem to remember it on subsequent
    commands.
    Terry, Jul 3, 2011
    #3
    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. Replies:
    6
    Views:
    393
    Jim Cheshire [MSFT]
    Dec 17, 2003
  2. Bura Tino
    Replies:
    7
    Views:
    763
    Dale King
    Apr 15, 2006
  3. Dirk Gomez

    -server flag: what does it do?

    Dirk Gomez, Apr 27, 2006, in forum: Java
    Replies:
    1
    Views:
    3,261
    Remon van Vliet
    Apr 27, 2006
  4. Thomas Lotze

    Semantics of propagated exceptions

    Thomas Lotze, Jul 21, 2006, in forum: Python
    Replies:
    5
    Views:
    307
    Thomas Lotze
    Aug 1, 2006
  5. Replies:
    2
    Views:
    336
Loading...

Share This Page