[Python-checkins] cpython: Issue #27792: force int return type for modulo operations involving bools.

mark.dickinson python-checkins at python.org
Mon Aug 22 07:24:57 EDT 2016


https://hg.python.org/cpython/rev/d998d87f0aa0
changeset:   102836:d998d87f0aa0
user:        Mark Dickinson <dickinsm at gmail.com>
date:        Mon Aug 22 12:24:46 2016 +0100
summary:
  Issue #27792: force int return type for modulo operations involving bools.

files:
  Lib/test/test_bool.py |  7 +++++++
  Misc/NEWS             |  4 ++++
  Objects/longobject.c  |  7 +++++--
  3 files changed, 16 insertions(+), 2 deletions(-)


diff --git a/Lib/test/test_bool.py b/Lib/test/test_bool.py
--- a/Lib/test/test_bool.py
+++ b/Lib/test/test_bool.py
@@ -96,6 +96,13 @@
         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)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@
 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
diff --git a/Objects/longobject.c b/Objects/longobject.c
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -2458,8 +2458,11 @@
         *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) {

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


More information about the Python-checkins mailing list