From be19c1f32e4d430092c029f17984f0087a2b2087 Mon Sep 17 00:00:00 2001 From: Jim Fulton Date: Mon, 19 May 2014 19:52:43 -0400 Subject: [PATCH 1/2] Fixed: integer overflow on 32-bit machines wasn't detected correctly under Python 3. --- BTrees/intkeymacros.h | 7 ++++--- BTrees/intvaluemacros.h | 3 ++- BTrees/tests/testBTrees.py | 11 +++++++++-- BTrees/tests/test_IIBTree.py | 2 ++ CHANGES.rst | 2 ++ 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/BTrees/intkeymacros.h b/BTrees/intkeymacros.h index d439aa0..f9244b5 100644 --- a/BTrees/intkeymacros.h +++ b/BTrees/intkeymacros.h @@ -19,9 +19,10 @@ #define KEY_CHECK INT_CHECK #define COPY_KEY_TO_OBJECT(O, K) O=INT_FROM_LONG(K) #define COPY_KEY_FROM_ARG(TARGET, ARG, STATUS) \ - if (INT_CHECK(ARG)) { \ - long vcopy = INT_AS_LONG(ARG); \ - if ((int)vcopy != vcopy) { \ + if (INT_CHECK(ARG)) { \ + long vcopy = INT_AS_LONG(ARG); \ + if (PyErr_Occurred()) { (STATUS)=0; (TARGET)=0; } \ + else if ((int)vcopy != vcopy) { \ PyErr_SetString(PyExc_TypeError, "integer out of range"); \ (STATUS)=0; (TARGET)=0; \ } \ diff --git a/BTrees/intvaluemacros.h b/BTrees/intvaluemacros.h index b77a5c9..3072eea 100644 --- a/BTrees/intvaluemacros.h +++ b/BTrees/intvaluemacros.h @@ -23,7 +23,8 @@ #define COPY_VALUE_FROM_ARG(TARGET, ARG, STATUS) \ if (INT_CHECK(ARG)) { \ long vcopy = INT_AS_LONG(ARG); \ - if ((int)vcopy != vcopy) { \ + if (PyErr_Occurred()) { (STATUS)=0; (TARGET)=0; } \ + else if ((int)vcopy != vcopy) { \ PyErr_SetString(PyExc_TypeError, "integer out of range"); \ (STATUS)=0; (TARGET)=0; \ } \ diff --git a/BTrees/tests/testBTrees.py b/BTrees/tests/testBTrees.py index 50f5b43..31d641d 100644 --- a/BTrees/tests/testBTrees.py +++ b/BTrees/tests/testBTrees.py @@ -11,8 +11,11 @@ # FOR A PARTICULAR PURPOSE # ############################################################################## +import sys import unittest +python3 = sys.version_info >= (3, ) + from BTrees.tests.common import permutations @@ -451,8 +454,12 @@ def test32(self): # the characteristics change to match the 64 bit version, please # feel free to change. big = BTrees.family32.maxint + 1 - self.assertRaises(TypeError, s.insert, big) - self.assertRaises(TypeError, s.insert, BTrees.family32.minint - 1) + if python3: + expected_exception = OverflowError + else: + expected_exception = TypeError + self.assertRaises(expected_exception, s.insert, + BTrees.family32.minint - 1) self.check_pickling(BTrees.family32) def test64(self): diff --git a/BTrees/tests/test_IIBTree.py b/BTrees/tests/test_IIBTree.py index 72e95b2..fe776b8 100644 --- a/BTrees/tests/test_IIBTree.py +++ b/BTrees/tests/test_IIBTree.py @@ -113,6 +113,8 @@ def trial(i): i = int(i) try: b[i] = 0 + except OverflowError: + self.assertRaises(OverflowError, b.__setitem__, 0, i) except TypeError: self.assertRaises(TypeError, b.__setitem__, 0, i) else: diff --git a/CHANGES.rst b/CHANGES.rst index 4696be3..e3869ff 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,8 @@ ``BTrees`` Changelog ==================== +- Fixed: integer overflow on 32-bit machines wasn't detected correctly + under Python 3. 4.0.9 (unreleased) ------------------ -- 2.0.4 From 11a51d2a12bb9904e96349ff86e78e24a0ebe51a Mon Sep 17 00:00:00 2001 From: Jim Fulton Date: Wed, 21 May 2014 07:33:06 -0400 Subject: [PATCH 2/2] added back test mistakedly removed. We have to check both TypeError and OverflowError. On Python3 32-bit, we'll get an OverflowError, otherwise, we get type error. --- BTrees/tests/testBTrees.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/BTrees/tests/testBTrees.py b/BTrees/tests/testBTrees.py index 31d641d..d9be43a 100644 --- a/BTrees/tests/testBTrees.py +++ b/BTrees/tests/testBTrees.py @@ -453,13 +453,13 @@ def test32(self): # this next bit illustrates an, um, "interesting feature". If # the characteristics change to match the 64 bit version, please # feel free to change. - big = BTrees.family32.maxint + 1 - if python3: - expected_exception = OverflowError - else: - expected_exception = TypeError - self.assertRaises(expected_exception, s.insert, - BTrees.family32.minint - 1) + try: s.insert(BTrees.family32.maxint + 1) + except (TypeError, OverflowError): pass + else: self.assert_(False) + + try: s.insert(BTrees.family32.minint - 1) + except (TypeError, OverflowError): pass + else: self.assert_(False) self.check_pickling(BTrees.family32) def test64(self): -- 2.0.4