]> granicus.if.org Git - python/commitdiff
Issue #27792: force int return type for modulo operations involving bools.
authorMark Dickinson <dickinsm@gmail.com>
Mon, 22 Aug 2016 11:24:46 +0000 (12:24 +0100)
committerMark Dickinson <dickinsm@gmail.com>
Mon, 22 Aug 2016 11:24:46 +0000 (12:24 +0100)
Lib/test/test_bool.py
Misc/NEWS
Objects/longobject.c

index 5f7e842da2d50c7289d4060d59d595bc1b216080..9f8f0e122c8da44eeefdf6038786bd66e835ab0b 100644 (file)
@@ -96,6 +96,13 @@ class BoolTest(unittest.TestCase):
         self.assertEqual(False/1, 0)
         self.assertIsNot(False/1, False)
 
+        self.assertEqual(True%1, 0)
+        self.assertIsNot(True%1, False)
+        self.assertEqual(True%2, 1)
+        self.assertIsNot(True%2, True)
+        self.assertEqual(False%1, 0)
+        self.assertIsNot(False%1, False)
+
         for b in False, True:
             for i in 0, 1, 2:
                 self.assertEqual(b**i, int(b)**i)
index ff677e008ed63c90fc5aedae198aca8b13282a86..cbbb4242e4941d55a1605cb8a87f4780866a5d72 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@ What's New in Python 3.6.0 beta 1
 Core and Builtins
 -----------------
 
+- Issue #27792: The modulo operation applied to ``bool`` and other
+  ``int`` subclasses now always returns an ``int``. Previously
+  the return type depended on the input values. Patch by Xiang Zhang.
+
 - Issue #26984: int() now always returns an instance of exact int.
 
 - Issue #25604: Fix a minor bug in integer true division; this bug could
index 5b9bc67a48d053807f4569df7d3a09aca79a6c1a..38e707220a2ccfa401f8101149f2fffc7060a76d 100644 (file)
@@ -2458,8 +2458,11 @@ long_divrem(PyLongObject *a, PyLongObject *b,
         *pdiv = (PyLongObject*)PyLong_FromLong(0);
         if (*pdiv == NULL)
             return -1;
-        Py_INCREF(a);
-        *prem = (PyLongObject *) a;
+        *prem = (PyLongObject *)long_long((PyObject *)a);
+        if (*prem == NULL) {
+            Py_CLEAR(*pdiv);
+            return -1;
+        }
         return 0;
     }
     if (size_b == 1) {