Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Clean things up a bit
  • Loading branch information
brandtbucher committed Jan 13, 2022
commit 027551ec74e6c09c8fcd8d22da022734f95ab58a
5 changes: 2 additions & 3 deletions Lib/test/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2551,12 +2551,11 @@ def test_int(self):
self.assertRaises(ValueError, int, Decimal('snan'))
self.assertRaises(OverflowError, int, Decimal('inf'))
self.assertRaises(OverflowError, int, Decimal('-inf'))

@cpython_only
def test_small_ints(self):
# bpo-46361
Decimal = self.decimal.Decimal

# bpo-46361
for x in range(-5, 257):
self.assertIs(int(Decimal(x)), x)

Expand Down
1 change: 0 additions & 1 deletion Lib/test/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -1477,7 +1477,6 @@ def test_from_bytes_small(self):
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
Expand Down
32 changes: 20 additions & 12 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <Python.h>
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_long.h" // _PyLong_SMALL_INTS
#include "complexobject.h"
#include "mpdecimal.h"

Expand Down Expand Up @@ -3348,6 +3349,7 @@ dec_as_long(PyObject *dec, PyObject *context, int round)
PyLongObject *pylong;
digit *ob_digit;
size_t n;
Py_ssize_t i;
mpd_t *x;
mpd_context_t workctx;
uint32_t status = 0;
Expand Down Expand Up @@ -3387,36 +3389,42 @@ dec_as_long(PyObject *dec, PyObject *context, int round)
#error "PYLONG_BITS_IN_DIGIT should be 15 or 30"
#endif

Py_ssize_t sign = mpd_isnegative(x) && !mpd_iszero(x) ? -1 : 1;
mpd_del(x);

if (n == SIZE_MAX) {
PyErr_NoMemory();
mpd_del(x);
return NULL;
}

assert(n > 0);
while ((n > 0) && (ob_digit[n - 1] == 0)) {
n--;
}
Py_ssize_t sign = mpd_isnegative(x) && !mpd_iszero(x) ? -1 : 1;

if (n < 2) {
pylong = (PyLongObject *)PyLong_FromLong(sign * ob_digit[0]);
mpd_free(ob_digit);
return (PyObject *)pylong;
if (n == 1) {
sdigit value = sign * ob_digit[0];
// If this is a "small" int, use the cached instance:
if (-_PY_NSMALLNEGINTS <= value && value < _PY_NSMALLPOSINTS) {
mpd_free(ob_digit);
return Py_NewRef(&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS + value]);
}
}

assert(n > 0);
pylong = _PyLong_New(n);
if (pylong == NULL) {
mpd_free(ob_digit);
mpd_del(x);
return NULL;
}

memcpy(pylong->ob_digit, ob_digit, n * sizeof(digit));
mpd_free(ob_digit);

Py_SET_SIZE(pylong, sign * n);
i = n;
while ((i > 0) && (pylong->ob_digit[i-1] == 0)) {
i--;
}

Py_SET_SIZE(pylong, sign * i);

mpd_del(x);
return (PyObject *) pylong;
}

Expand Down