diff -r 6762b943ee59 Lib/test/test_long.py --- a/Lib/test/test_long.py Tue Apr 17 21:42:07 2012 -0400 +++ b/Lib/test/test_long.py Fri Apr 20 18:20:55 2012 +0100 @@ -1228,6 +1228,20 @@ 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 -r 6762b943ee59 Misc/NEWS --- a/Misc/NEWS Tue Apr 17 21:42:07 2012 -0400 +++ b/Misc/NEWS Fri Apr 20 18:20:55 2012 +0100 @@ -10,6 +10,9 @@ Core and Builtins ----------------- +- Issue #14630: Fix a memory access bug for instances of a subclass of int + with value 0. + - Issue #12599: Be more strict in accepting None compared to a false-like object for importlib.util.module_for_loader and importlib.machinery.PathFinder. diff -r 6762b943ee59 Objects/longobject.c --- a/Objects/longobject.c Tue Apr 17 21:42:07 2012 -0400 +++ b/Objects/longobject.c Fri Apr 20 18:20:55 2012 +0100 @@ -156,9 +156,7 @@ 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);