From: Robert Bradshaw Date: Wed, 16 Jul 2008 04:56:39 +0000 (-0700) Subject: future division X-Git-Tag: 0.9.8.1~123^2~4 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=1cae03c08404f98682272f3b9467aa82b45099db;p=cython.git future division --- diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index bf94764a..3ce905c8 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -3133,7 +3133,7 @@ class NumBinopNode(BinopNode): "+": "PyNumber_Add", "-": "PyNumber_Subtract", "*": "PyNumber_Multiply", - "/": "PyNumber_Divide", + "/": "__Pyx_PyNumber_Divide", "//": "PyNumber_FloorDivide", "%": "PyNumber_Remainder", "**": "PyNumber_Power" diff --git a/Cython/Compiler/Future.py b/Cython/Compiler/Future.py index a517fea6..c42e595f 100644 --- a/Cython/Compiler/Future.py +++ b/Cython/Compiler/Future.py @@ -8,5 +8,6 @@ def _get_feature(name): unicode_literals = _get_feature("unicode_literals") with_statement = _get_feature("with_statement") +division = _get_feature("division") del _get_feature diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py index 3dbdf90d..68394529 100644 --- a/Cython/Compiler/ModuleNode.py +++ b/Cython/Compiler/ModuleNode.py @@ -5,6 +5,7 @@ import os, time from cStringIO import StringIO from PyrexTypes import CPtrType +import Future try: set @@ -467,8 +468,12 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): code.putln(" #define PyInt_AsSsize_t PyLong_AsSsize_t") code.putln(" #define PyInt_AsUnsignedLongMask PyLong_AsUnsignedLongMask") code.putln(" #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask") - code.putln(" #define PyNumber_Divide(x,y) PyNumber_TrueDivide(x,y)") + code.putln(" #define __Pyx_PyNumber_Divide(x,y) PyNumber_TrueDivide(x,y)") code.putln("#else") + if Future.division in env.context.future_directives: + code.putln(" #define __Pyx_PyNumber_Divide(x,y) PyNumber_TrueDivide(x,y)") + else: + code.putln(" #define __Pyx_PyNumber_Divide(x,y) PyNumber_Divide(x,y)") code.putln(" #define PyBytes_Type PyString_Type") code.putln("#endif") diff --git a/tests/run/future_division.pyx b/tests/run/future_division.pyx new file mode 100644 index 00000000..5f78dafc --- /dev/null +++ b/tests/run/future_division.pyx @@ -0,0 +1,16 @@ +from __future__ import division + +__doc__ = """ +>>> from future_division import doit +>>> doit(1,2) +(0.5, 0) +>>> doit(4,3) +(1.3333333333333333, 1) +>>> doit(4,3.0) +(1.3333333333333333, 1.0) +>>> doit(4,2) +(2.0, 2) +""" + +def doit(x,y): + return x/y, x//y