[Jython-checkins] jython: Fixed direct calls to __pow__() on int, long and float with a modulo of None

alex.gronholm jython-checkins at python.org
Tue Apr 14 16:19:51 CEST 2015


https://hg.python.org/jython/rev/41d761a58a53
changeset:   7667:41d761a58a53
user:        Alex Grönholm <alex.gronholm at nextday.fi>
date:        Tue Apr 14 10:18:50 2015 -0400
summary:
  Fixed direct calls to __pow__() on int, long and float with a modulo of None returning NotImplemented (fixes #2171)

files:
  Lib/test/test_float_jy.py          |   5 +++--
  Lib/test/test_int_jy.py            |   5 +++++
  Lib/test/test_long_jy.py           |   6 ++++++
  src/org/python/core/PyFloat.java   |   5 ++++-
  src/org/python/core/PyInteger.java |  13 +++++--------
  src/org/python/core/PyLong.java    |   5 ++++-
  6 files changed, 27 insertions(+), 12 deletions(-)


diff --git a/Lib/test/test_float_jy.py b/Lib/test/test_float_jy.py
--- a/Lib/test/test_float_jy.py
+++ b/Lib/test/test_float_jy.py
@@ -99,8 +99,9 @@
         class Foo(object):
             def __rpow__(self, other):
                 return other ** 2
-        # regression in 2.5 alphas
-        self.assertEqual(4.0 ** Foo(), 16.0)
+
+        self.assertEqual(4.0 ** Foo(), 16.0)  # regression in 2.5 alphas
+        self.assertEqual((4.0).__pow__(2, None), 16.0)
 
     def test_faux(self):
         class F(object):
diff --git a/Lib/test/test_int_jy.py b/Lib/test/test_int_jy.py
--- a/Lib/test/test_int_jy.py
+++ b/Lib/test/test_int_jy.py
@@ -11,6 +11,11 @@
     def test_type_matches(self):
         self.assert_(isinstance(1, types.IntType))
 
+    def test_int_pow(self):
+        self.assertEquals(pow(10, 10, None), 10000000000L)
+        self.assertEquals(int.__pow__(10, 10, None), 10000000000L)
+        self.assertEquals((10).__pow__(10, None), 10000000000L)
+
 def test_main():
     test_support.run_unittest(IntTestCase)
 
diff --git a/Lib/test/test_long_jy.py b/Lib/test/test_long_jy.py
--- a/Lib/test/test_long_jy.py
+++ b/Lib/test/test_long_jy.py
@@ -26,6 +26,12 @@
         # http://bugs.jython.org/issue1828
         self.assertTrue(bool(MyLong(42)))
 
+    def test_long_pow(self):
+        self.assertEquals(pow(10L, 10L, None), 10000000000L)
+        self.assertEquals(long.__pow__(10L, 10L, None), 10000000000L)
+        self.assertEquals((10L).__pow__(10L, None), 10000000000L)
+        self.assertEquals((10L).__pow__(10, None), 10000000000L)
+
 
 def test_main():
     test_support.run_unittest(LongTestCase)
diff --git a/src/org/python/core/PyFloat.java b/src/org/python/core/PyFloat.java
--- a/src/org/python/core/PyFloat.java
+++ b/src/org/python/core/PyFloat.java
@@ -736,7 +736,10 @@
     final PyObject float___pow__(PyObject right, PyObject modulo) {
         if (!canCoerce(right)) {
             return null;
-        } else if (modulo != null) {
+        }
+
+        modulo = (modulo == Py.None) ? null : modulo;
+        if (modulo != null) {
             throw Py.TypeError("pow() 3rd argument not allowed unless all arguments are integers");
         } else {
             return _pow(getValue(), coerce(right));
diff --git a/src/org/python/core/PyInteger.java b/src/org/python/core/PyInteger.java
--- a/src/org/python/core/PyInteger.java
+++ b/src/org/python/core/PyInteger.java
@@ -575,6 +575,7 @@
             return null;
         }
 
+        modulo = (modulo == Py.None) ? null : modulo;
         if (modulo != null && !canCoerce(modulo)) {
             return null;
         }
@@ -582,21 +583,17 @@
         return _pow(getValue(), coerce(right), modulo, this, right);
     }
 
-    public PyObject __rpow__(PyObject left, PyObject modulo) {
+    @Override
+    public PyObject __rpow__(PyObject left) {
         if (!canCoerce(left)) {
             return null;
         }
-
-        if (modulo != null && !canCoerce(modulo)) {
-            return null;
-        }
-
-        return _pow(coerce(left), getValue(), modulo, left, this);
+        return _pow(coerce(left), getValue(), null, left, this);
     }
 
     @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.int___rpow___doc)
     final PyObject int___rpow__(PyObject left) {
-        return __rpow__(left, null);
+        return __rpow__(left);
     }
 
     private static PyObject _pow(int value, int pow, PyObject modulo,//
diff --git a/src/org/python/core/PyLong.java b/src/org/python/core/PyLong.java
--- a/src/org/python/core/PyLong.java
+++ b/src/org/python/core/PyLong.java
@@ -660,9 +660,11 @@
             return null;
         }
 
-        if (modulo != null && !canCoerce(right)) {
+        modulo = (modulo == Py.None) ? null : modulo;
+        if (modulo != null && !canCoerce(modulo)) {
             return null;
         }
+
         return _pow(getValue(), coerce(right), modulo, this, right);
     }
 
@@ -676,6 +678,7 @@
         if (!canCoerce(left)) {
             return null;
         }
+
         return _pow(coerce(left), getValue(), null, left, this);
     }
 

-- 
Repository URL: https://hg.python.org/jython


More information about the Jython-checkins mailing list