future division
authorRobert Bradshaw <robertwb@math.washington.edu>
Wed, 16 Jul 2008 04:56:39 +0000 (21:56 -0700)
committerRobert Bradshaw <robertwb@math.washington.edu>
Wed, 16 Jul 2008 04:56:39 +0000 (21:56 -0700)
Cython/Compiler/ExprNodes.py
Cython/Compiler/Future.py
Cython/Compiler/ModuleNode.py
tests/run/future_division.pyx [new file with mode: 0644]

index bf94764a8f9530d17be6d7229bd84324b216d7ec..3ce905c801644d48df68600b66f127829b2ff396 100644 (file)
@@ -3133,7 +3133,7 @@ class NumBinopNode(BinopNode):
         "+":           "PyNumber_Add",
         "-":           "PyNumber_Subtract",
         "*":           "PyNumber_Multiply",
-        "/":           "PyNumber_Divide",
+        "/":           "__Pyx_PyNumber_Divide",
         "//":          "PyNumber_FloorDivide",
         "%":           "PyNumber_Remainder",
         "**":       "PyNumber_Power"
index a517fea6ca8d97503259c1abc8136a83e2929343..c42e595f074c3e7d6dc60056cbc519fd42dd77cf 100644 (file)
@@ -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
index 3dbdf90db8b9a93be421064bc1543ea4239afbe7..68394529e46eb2d20eeff18d98758827e4725808 100644 (file)
@@ -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 (file)
index 0000000..5f78daf
--- /dev/null
@@ -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