summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Dickinson <mdickinson@enthought.com>2012-04-20 21:44:09 +0100
committerMark Dickinson <mdickinson@enthought.com>2012-04-20 21:44:09 +0100
commitbafd86b424add695fad4d0e204907fc1a94cfef6 (patch)
tree4017cb66dac9bb3ccd3f7b5283556414cc9d7b50
parenta2a089f92e66b0c77c3118c0804e808d2cf8078b (diff)
parent5c338542fe52f9bddb715843ca797acb4acf0db5 (diff)
downloadcpython-bafd86b424add695fad4d0e204907fc1a94cfef6.tar.gz
Issue #14630: Merge fix from 3.2.
-rw-r--r--Lib/test/test_long.py14
-rw-r--r--Misc/NEWS3
-rw-r--r--Objects/longobject.c4
3 files changed, 18 insertions, 3 deletions
diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py
index 05b3e3e03f..b417bea215 100644
--- a/Lib/test/test_long.py
+++ b/Lib/test/test_long.py
@@ -1228,6 +1228,20 @@ class LongTest(unittest.TestCase):
self.assertRaises(TypeError, myint.from_bytes, 0, 'big')
self.assertRaises(TypeError, int.from_bytes, 0, 'big', True)
+ def test_access_to_nonexistent_digit_0(self):
+ # http://bugs.python.org/issue14630: A bug in _PyLong_Copy meant that
+ # ob_digit[0] was being incorrectly accessed for instances of a
+ # subclass of int, with value 0.
+ class Integer(int):
+ def __new__(cls, value=0):
+ self = int.__new__(cls, value)
+ self.foo = 'foo'
+ return self
+
+ integers = [Integer(0) for i in range(1000)]
+ for n in map(int, integers):
+ self.assertEqual(n, 0)
+
def test_main():
support.run_unittest(LongTest)
diff --git a/Misc/NEWS b/Misc/NEWS
index abd7c50fac..08a1dd3e4b 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.3.0 Alpha 3?
Core and Builtins
-----------------
+- Issue #14630: Fix a memory access bug for instances of a subclass of int
+ with value 0.
+
- Issue #14339: Speed improvements to bin, oct and hex functions. Patch by
Serhiy Storchaka.
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 0a5b9aa52c..3630ae4e19 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -156,9 +156,7 @@ _PyLong_Copy(PyLongObject *src)
if (i < 0)
i = -(i);
if (i < 2) {
- sdigit ival = src->ob_digit[0];
- if (Py_SIZE(src) < 0)
- ival = -ival;
+ sdigit ival = MEDIUM_VALUE(src);
CHECK_SMALL_INT(ival);
}
result = _PyLong_New(i);