self.assertRaises(OverflowError, int, Decimal('inf'))
self.assertRaises(OverflowError, int, Decimal('-inf'))
+ @cpython_only
+ def test_small_ints(self):
+ Decimal = self.decimal.Decimal
+ # bpo-46361
+ for x in range(-5, 257):
+ self.assertIs(int(Decimal(x)), x)
+
def test_trunc(self):
Decimal = self.decimal.Decimal
self.assertEqual(i, 1)
self.assertEqual(getattr(i, 'foo', 'none'), 'bar')
+ @support.cpython_only
+ def test_from_bytes_small(self):
+ # bpo-46361
+ for i in range(-5, 257):
+ b = i.to_bytes(2, signed=True)
+ self.assertIs(int.from_bytes(b, signed=True), i)
+
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
--- /dev/null
+Ensure that "small" integers created by :meth:`int.from_bytes` and
+:class:`decimal.Decimal` are properly cached.
return NULL;
}
+ if (n == 1) {
+ sdigit val = mpd_arith_sign(x) * ob_digit[0];
+ mpd_free(ob_digit);
+ mpd_del(x);
+ return PyLong_FromLong(val);
+ }
+
assert(n > 0);
pylong = _PyLong_New(n);
if (pylong == NULL) {
}
Py_SET_SIZE(v, is_signed ? -idigit : idigit);
- return (PyObject *)long_normalize(v);
+ return (PyObject *)maybe_small_long(long_normalize(v));
}
int