[Python-Dev] Re: Future division detection

Skip Montanaro skip@pobox.com (Skip Montanaro)
Mon, 05 Nov 2001 17:29:51 +0100


    Christopher> What I would like to do is have a function that can check
    Christopher> to see if the CO_FUTURE_DIVISION flag is set and if it is
    Christopher> and the denominator is 1 then return the numerator, else
    Christopher> return the rational.

Yeah, you can do this.  It's just a little awkward.

    % python
    Python 2.2b1+ (#9, Oct 29 2001, 14:53:15) 
    [GCC 2.96 20000731 (Linux-Mandrake 8.0 2.96-0.48mdk)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from __future__ import division
    >>> def f():
    ...   pass
    ... 
    >>> f.func_code.co_flags
    8195
    >>> 
    % python
    Python 2.2b1+ (#9, Oct 29 2001, 14:53:15) 
    [GCC 2.96 20000731 (Linux-Mandrake 8.0 2.96-0.48mdk)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> def f():
    ...   pass
    ... 
    >>> f.func_code.co_flags
    3

Note how the code object's co_flags field changed.  I think you just need to
& it with 2<<12.  During import you could have something like this:

    def _junk():
      pass
    future_div = not not (_junk.func_code.co_flags & (2<<12))
    del _junk

then test the value of future_div where you need it.

-- 
Skip Montanaro (skip@pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/

P.S.  I hope I guessed right about demangling your email address.  If
so, I've just exposed it to all the little email harvesting gremlins.
So sorry... ;-)