diff options
author | shadchin <shadchin@yandex-team.ru> | 2022-02-10 16:44:30 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:44:30 +0300 |
commit | 2598ef1d0aee359b4b6d5fdd1758916d5907d04f (patch) | |
tree | 012bb94d777798f1f56ac1cec429509766d05181 /contrib/tools/python3/src/Objects/longobject.c | |
parent | 6751af0b0c1b952fede40b19b71da8025b5d8bcf (diff) | |
download | ydb-2598ef1d0aee359b4b6d5fdd1758916d5907d04f.tar.gz |
Restoring authorship annotation for <shadchin@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'contrib/tools/python3/src/Objects/longobject.c')
-rw-r--r-- | contrib/tools/python3/src/Objects/longobject.c | 1258 |
1 files changed, 629 insertions, 629 deletions
diff --git a/contrib/tools/python3/src/Objects/longobject.c b/contrib/tools/python3/src/Objects/longobject.c index cf13b2c430..567cfd1a91 100644 --- a/contrib/tools/python3/src/Objects/longobject.c +++ b/contrib/tools/python3/src/Objects/longobject.c @@ -3,8 +3,8 @@ /* XXX The functional organization of this file is terrible */ #include "Python.h" -#include "pycore_interp.h" // _PY_NSMALLPOSINTS -#include "pycore_pystate.h" // _Py_IsMainInterpreter() +#include "pycore_interp.h" // _PY_NSMALLPOSINTS +#include "pycore_pystate.h" // _Py_IsMainInterpreter() #include "longintrepr.h" #include <float.h> @@ -17,8 +17,8 @@ class int "PyObject *" "&PyLong_Type" [clinic start generated code]*/ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=ec0275e3422a36e3]*/ -#define NSMALLPOSINTS _PY_NSMALLPOSINTS -#define NSMALLNEGINTS _PY_NSMALLNEGINTS +#define NSMALLPOSINTS _PY_NSMALLPOSINTS +#define NSMALLNEGINTS _PY_NSMALLNEGINTS _Py_IDENTIFIER(little); _Py_IDENTIFIER(big); @@ -33,15 +33,15 @@ PyObject *_PyLong_Zero = NULL; PyObject *_PyLong_One = NULL; #if NSMALLNEGINTS + NSMALLPOSINTS > 0 -#define IS_SMALL_INT(ival) (-NSMALLNEGINTS <= (ival) && (ival) < NSMALLPOSINTS) -#define IS_SMALL_UINT(ival) ((ival) < NSMALLPOSINTS) +#define IS_SMALL_INT(ival) (-NSMALLNEGINTS <= (ival) && (ival) < NSMALLPOSINTS) +#define IS_SMALL_UINT(ival) ((ival) < NSMALLPOSINTS) static PyObject * get_small_int(sdigit ival) { - assert(IS_SMALL_INT(ival)); - PyThreadState *tstate = _PyThreadState_GET(); - PyObject *v = (PyObject*)tstate->interp->small_ints[ival + NSMALLNEGINTS]; + assert(IS_SMALL_INT(ival)); + PyThreadState *tstate = _PyThreadState_GET(); + PyObject *v = (PyObject*)tstate->interp->small_ints[ival + NSMALLNEGINTS]; Py_INCREF(v); return v; } @@ -51,7 +51,7 @@ maybe_small_long(PyLongObject *v) { if (v && Py_ABS(Py_SIZE(v)) <= 1) { sdigit ival = MEDIUM_VALUE(v); - if (IS_SMALL_INT(ival)) { + if (IS_SMALL_INT(ival)) { Py_DECREF(v); return (PyLongObject *)get_small_int(ival); } @@ -59,9 +59,9 @@ maybe_small_long(PyLongObject *v) return v; } #else -#define IS_SMALL_INT(ival) 0 -#define IS_SMALL_UINT(ival) 0 -#define get_small_int(ival) (Py_UNREACHABLE(), NULL) +#define IS_SMALL_INT(ival) 0 +#define IS_SMALL_UINT(ival) 0 +#define get_small_int(ival) (Py_UNREACHABLE(), NULL) #define maybe_small_long(val) (val) #endif @@ -74,7 +74,7 @@ _PyLong_Negate(PyLongObject **x_p) x = (PyLongObject *)*x_p; if (Py_REFCNT(x) == 1) { - Py_SET_SIZE(x, -Py_SIZE(x)); + Py_SET_SIZE(x, -Py_SIZE(x)); return; } @@ -113,9 +113,9 @@ long_normalize(PyLongObject *v) while (i > 0 && v->ob_digit[i-1] == 0) --i; - if (i != j) { - Py_SET_SIZE(v, (Py_SIZE(v) < 0) ? -(i) : i); - } + if (i != j) { + Py_SET_SIZE(v, (Py_SIZE(v) < 0) ? -(i) : i); + } return v; } @@ -124,7 +124,7 @@ long_normalize(PyLongObject *v) nb_int slot is not available or the result of the call to nb_int returns something not of type int. */ -PyObject * +PyObject * _PyLong_FromNbInt(PyObject *integral) { PyNumberMethods *nb; @@ -133,7 +133,7 @@ _PyLong_FromNbInt(PyObject *integral) /* Fast path for the case that we already have an int. */ if (PyLong_CheckExact(integral)) { Py_INCREF(integral); - return integral; + return integral; } nb = Py_TYPE(integral)->tp_as_number; @@ -148,11 +148,11 @@ _PyLong_FromNbInt(PyObject *integral) of exact type int. */ result = nb->nb_int(integral); if (!result || PyLong_CheckExact(result)) - return result; + return result; if (!PyLong_Check(result)) { PyErr_Format(PyExc_TypeError, "__int__ returned non-int (type %.200s)", - Py_TYPE(result)->tp_name); + Py_TYPE(result)->tp_name); Py_DECREF(result); return NULL; } @@ -161,81 +161,81 @@ _PyLong_FromNbInt(PyObject *integral) "__int__ returned non-int (type %.200s). " "The ability to return an instance of a strict subclass of int " "is deprecated, and may be removed in a future version of Python.", - Py_TYPE(result)->tp_name)) { + Py_TYPE(result)->tp_name)) { Py_DECREF(result); return NULL; } - return result; + return result; } -/* Convert the given object to a PyLongObject using the nb_index or - nb_int slots, if available (the latter is deprecated). - Raise TypeError if either nb_index and nb_int slots are not - available or the result of the call to nb_index or nb_int - returns something not of type int. - Should be replaced with PyNumber_Index after the end of the - deprecation period. -*/ -PyObject * -_PyLong_FromNbIndexOrNbInt(PyObject *integral) -{ - PyNumberMethods *nb; - PyObject *result; - - /* Fast path for the case that we already have an int. */ - if (PyLong_CheckExact(integral)) { - Py_INCREF(integral); - return integral; - } - - nb = Py_TYPE(integral)->tp_as_number; - if (nb == NULL || (nb->nb_index == NULL && nb->nb_int == NULL)) { - PyErr_Format(PyExc_TypeError, - "an integer is required (got type %.200s)", - Py_TYPE(integral)->tp_name); - return NULL; - } - - if (nb->nb_index) { - /* Convert using the nb_index slot, which should return something - of exact type int. */ - result = nb->nb_index(integral); - if (!result || PyLong_CheckExact(result)) - return result; - if (!PyLong_Check(result)) { - PyErr_Format(PyExc_TypeError, - "__index__ returned non-int (type %.200s)", - Py_TYPE(result)->tp_name); - Py_DECREF(result); - return NULL; - } - /* Issue #17576: warn if 'result' not of exact type int. */ - if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, - "__index__ returned non-int (type %.200s). " - "The ability to return an instance of a strict subclass of int " - "is deprecated, and may be removed in a future version of Python.", - Py_TYPE(result)->tp_name)) - { - Py_DECREF(result); - return NULL; - } - return result; - } - - result = _PyLong_FromNbInt(integral); - if (result && PyErr_WarnFormat(PyExc_DeprecationWarning, 1, - "an integer is required (got type %.200s). " - "Implicit conversion to integers using __int__ is deprecated, " - "and may be removed in a future version of Python.", - Py_TYPE(integral)->tp_name)) - { - Py_DECREF(result); - return NULL; - } - return result; -} - - +/* Convert the given object to a PyLongObject using the nb_index or + nb_int slots, if available (the latter is deprecated). + Raise TypeError if either nb_index and nb_int slots are not + available or the result of the call to nb_index or nb_int + returns something not of type int. + Should be replaced with PyNumber_Index after the end of the + deprecation period. +*/ +PyObject * +_PyLong_FromNbIndexOrNbInt(PyObject *integral) +{ + PyNumberMethods *nb; + PyObject *result; + + /* Fast path for the case that we already have an int. */ + if (PyLong_CheckExact(integral)) { + Py_INCREF(integral); + return integral; + } + + nb = Py_TYPE(integral)->tp_as_number; + if (nb == NULL || (nb->nb_index == NULL && nb->nb_int == NULL)) { + PyErr_Format(PyExc_TypeError, + "an integer is required (got type %.200s)", + Py_TYPE(integral)->tp_name); + return NULL; + } + + if (nb->nb_index) { + /* Convert using the nb_index slot, which should return something + of exact type int. */ + result = nb->nb_index(integral); + if (!result || PyLong_CheckExact(result)) + return result; + if (!PyLong_Check(result)) { + PyErr_Format(PyExc_TypeError, + "__index__ returned non-int (type %.200s)", + Py_TYPE(result)->tp_name); + Py_DECREF(result); + return NULL; + } + /* Issue #17576: warn if 'result' not of exact type int. */ + if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, + "__index__ returned non-int (type %.200s). " + "The ability to return an instance of a strict subclass of int " + "is deprecated, and may be removed in a future version of Python.", + Py_TYPE(result)->tp_name)) + { + Py_DECREF(result); + return NULL; + } + return result; + } + + result = _PyLong_FromNbInt(integral); + if (result && PyErr_WarnFormat(PyExc_DeprecationWarning, 1, + "an integer is required (got type %.200s). " + "Implicit conversion to integers using __int__ is deprecated, " + "and may be removed in a future version of Python.", + Py_TYPE(integral)->tp_name)) + { + Py_DECREF(result); + return NULL; + } + return result; +} + + /* Allocate a new int object with size digits. Return NULL and set exception if we run out of memory. */ @@ -277,16 +277,16 @@ _PyLong_Copy(PyLongObject *src) i = -(i); if (i < 2) { sdigit ival = MEDIUM_VALUE(src); - if (IS_SMALL_INT(ival)) { - return get_small_int(ival); - } + if (IS_SMALL_INT(ival)) { + return get_small_int(ival); + } } result = _PyLong_New(i); if (result != NULL) { - Py_SET_SIZE(result, Py_SIZE(src)); - while (--i >= 0) { + Py_SET_SIZE(result, Py_SIZE(src)); + while (--i >= 0) { result->ob_digit[i] = src->ob_digit[i]; - } + } } return (PyObject *)result; } @@ -302,9 +302,9 @@ PyLong_FromLong(long ival) int ndigits = 0; int sign; - if (IS_SMALL_INT(ival)) { - return get_small_int((sdigit)ival); - } + if (IS_SMALL_INT(ival)) { + return get_small_int((sdigit)ival); + } if (ival < 0) { /* negate: can't write this as abs_ival = -ival since that @@ -321,7 +321,7 @@ PyLong_FromLong(long ival) if (!(abs_ival >> PyLong_SHIFT)) { v = _PyLong_New(1); if (v) { - Py_SET_SIZE(v, sign); + Py_SET_SIZE(v, sign); v->ob_digit[0] = Py_SAFE_DOWNCAST( abs_ival, unsigned long, digit); } @@ -333,7 +333,7 @@ PyLong_FromLong(long ival) if (!(abs_ival >> 2*PyLong_SHIFT)) { v = _PyLong_New(2); if (v) { - Py_SET_SIZE(v, 2 * sign); + Py_SET_SIZE(v, 2 * sign); v->ob_digit[0] = Py_SAFE_DOWNCAST( abs_ival & PyLong_MASK, unsigned long, digit); v->ob_digit[1] = Py_SAFE_DOWNCAST( @@ -352,7 +352,7 @@ PyLong_FromLong(long ival) v = _PyLong_New(ndigits); if (v != NULL) { digit *p = v->ob_digit; - Py_SET_SIZE(v, ndigits * sign); + Py_SET_SIZE(v, ndigits * sign); t = abs_ival; while (t) { *p++ = Py_SAFE_DOWNCAST( @@ -363,74 +363,74 @@ PyLong_FromLong(long ival) return (PyObject *)v; } -#define PYLONG_FROM_UINT(INT_TYPE, ival) \ - do { \ - if (IS_SMALL_UINT(ival)) { \ - return get_small_int((sdigit)(ival)); \ - } \ - /* Count the number of Python digits. */ \ - Py_ssize_t ndigits = 0; \ - INT_TYPE t = (ival); \ - while (t) { \ - ++ndigits; \ - t >>= PyLong_SHIFT; \ - } \ - PyLongObject *v = _PyLong_New(ndigits); \ - if (v == NULL) { \ - return NULL; \ - } \ - digit *p = v->ob_digit; \ - while ((ival)) { \ - *p++ = (digit)((ival) & PyLong_MASK); \ - (ival) >>= PyLong_SHIFT; \ - } \ - return (PyObject *)v; \ - } while(0) - +#define PYLONG_FROM_UINT(INT_TYPE, ival) \ + do { \ + if (IS_SMALL_UINT(ival)) { \ + return get_small_int((sdigit)(ival)); \ + } \ + /* Count the number of Python digits. */ \ + Py_ssize_t ndigits = 0; \ + INT_TYPE t = (ival); \ + while (t) { \ + ++ndigits; \ + t >>= PyLong_SHIFT; \ + } \ + PyLongObject *v = _PyLong_New(ndigits); \ + if (v == NULL) { \ + return NULL; \ + } \ + digit *p = v->ob_digit; \ + while ((ival)) { \ + *p++ = (digit)((ival) & PyLong_MASK); \ + (ival) >>= PyLong_SHIFT; \ + } \ + return (PyObject *)v; \ + } while(0) + /* Create a new int object from a C unsigned long int */ PyObject * PyLong_FromUnsignedLong(unsigned long ival) { - PYLONG_FROM_UINT(unsigned long, ival); -} - -/* Create a new int object from a C unsigned long long int. */ - -PyObject * -PyLong_FromUnsignedLongLong(unsigned long long ival) -{ - PYLONG_FROM_UINT(unsigned long long, ival); -} - -/* Create a new int object from a C size_t. */ - -PyObject * -PyLong_FromSize_t(size_t ival) -{ - PYLONG_FROM_UINT(size_t, ival); + PYLONG_FROM_UINT(unsigned long, ival); +} + +/* Create a new int object from a C unsigned long long int. */ + +PyObject * +PyLong_FromUnsignedLongLong(unsigned long long ival) +{ + PYLONG_FROM_UINT(unsigned long long, ival); } +/* Create a new int object from a C size_t. */ + +PyObject * +PyLong_FromSize_t(size_t ival) +{ + PYLONG_FROM_UINT(size_t, ival); +} + /* Create a new int object from a C double */ PyObject * PyLong_FromDouble(double dval) { - /* Try to get out cheap if this fits in a long. When a finite value of real - * floating type is converted to an integer type, the value is truncated - * toward zero. If the value of the integral part cannot be represented by - * the integer type, the behavior is undefined. Thus, we must check that - * value is in range (LONG_MIN - 1, LONG_MAX + 1). If a long has more bits - * of precision than a double, casting LONG_MIN - 1 to double may yield an - * approximation, but LONG_MAX + 1 is a power of two and can be represented - * as double exactly (assuming FLT_RADIX is 2 or 16), so for simplicity - * check against [-(LONG_MAX + 1), LONG_MAX + 1). - */ - const double int_max = (unsigned long)LONG_MAX + 1; - if (-int_max < dval && dval < int_max) { - return PyLong_FromLong((long)dval); - } - + /* Try to get out cheap if this fits in a long. When a finite value of real + * floating type is converted to an integer type, the value is truncated + * toward zero. If the value of the integral part cannot be represented by + * the integer type, the behavior is undefined. Thus, we must check that + * value is in range (LONG_MIN - 1, LONG_MAX + 1). If a long has more bits + * of precision than a double, casting LONG_MIN - 1 to double may yield an + * approximation, but LONG_MAX + 1 is a power of two and can be represented + * as double exactly (assuming FLT_RADIX is 2 or 16), so for simplicity + * check against [-(LONG_MAX + 1), LONG_MAX + 1). + */ + const double int_max = (unsigned long)LONG_MAX + 1; + if (-int_max < dval && dval < int_max) { + return PyLong_FromLong((long)dval); + } + PyLongObject *v; double frac; int i, ndig, expo, neg; @@ -450,7 +450,7 @@ PyLong_FromDouble(double dval) dval = -dval; } frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */ - assert(expo > 0); + assert(expo > 0); ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */ v = _PyLong_New(ndig); if (v == NULL) @@ -462,9 +462,9 @@ PyLong_FromDouble(double dval) frac = frac - (double)bits; frac = ldexp(frac, PyLong_SHIFT); } - if (neg) { - Py_SET_SIZE(v, -(Py_SIZE(v))); - } + if (neg) { + Py_SET_SIZE(v, -(Py_SIZE(v))); + } return (PyObject *)v; } @@ -511,7 +511,7 @@ PyLong_AsLongAndOverflow(PyObject *vv, int *overflow) v = (PyLongObject *)vv; } else { - v = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(vv); + v = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(vv); if (v == NULL) return -1; do_decref = 1; @@ -791,7 +791,7 @@ PyLong_AsUnsignedLongMask(PyObject *op) return _PyLong_AsUnsignedLongMask(op); } - lo = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(op); + lo = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(op); if (lo == NULL) return (unsigned long)-1; @@ -828,7 +828,7 @@ _PyLong_NumBits(PyObject *vv) if ((size_t)(ndigits - 1) > SIZE_MAX / (size_t)PyLong_SHIFT) goto Overflow; result = (size_t)(ndigits - 1) * (size_t)PyLong_SHIFT; - msd_bits = _Py_bit_length(msd); + msd_bits = _Py_bit_length(msd); if (SIZE_MAX - msd_bits < result) goto Overflow; result += msd_bits; @@ -948,7 +948,7 @@ _PyLong_FromByteArray(const unsigned char* bytes, size_t n, } } - Py_SET_SIZE(v, is_signed ? -idigit : idigit); + Py_SET_SIZE(v, is_signed ? -idigit : idigit); return (PyObject *)long_normalize(v); } @@ -1137,7 +1137,7 @@ PyLong_AsVoidPtr(PyObject *vv) * rewritten to use the newer PyLong_{As,From}ByteArray API. */ -#define PY_ABS_LLONG_MIN (0-(unsigned long long)LLONG_MIN) +#define PY_ABS_LLONG_MIN (0-(unsigned long long)LLONG_MIN) /* Create a new int object from a C long long int. */ @@ -1150,10 +1150,10 @@ PyLong_FromLongLong(long long ival) int ndigits = 0; int negative = 0; - if (IS_SMALL_INT(ival)) { - return get_small_int((sdigit)ival); - } - + if (IS_SMALL_INT(ival)) { + return get_small_int((sdigit)ival); + } + if (ival < 0) { /* avoid signed overflow on negation; see comments in PyLong_FromLong above. */ @@ -1176,7 +1176,7 @@ PyLong_FromLongLong(long long ival) v = _PyLong_New(ndigits); if (v != NULL) { digit *p = v->ob_digit; - Py_SET_SIZE(v, negative ? -ndigits : ndigits); + Py_SET_SIZE(v, negative ? -ndigits : ndigits); t = abs_ival; while (t) { *p++ = (digit)(t & PyLong_MASK); @@ -1197,10 +1197,10 @@ PyLong_FromSsize_t(Py_ssize_t ival) int ndigits = 0; int negative = 0; - if (IS_SMALL_INT(ival)) { - return get_small_int((sdigit)ival); - } - + if (IS_SMALL_INT(ival)) { + return get_small_int((sdigit)ival); + } + if (ival < 0) { /* avoid signed overflow when ival = SIZE_T_MIN */ abs_ival = (size_t)(-1-ival)+1; @@ -1219,7 +1219,7 @@ PyLong_FromSsize_t(Py_ssize_t ival) v = _PyLong_New(ndigits); if (v != NULL) { digit *p = v->ob_digit; - Py_SET_SIZE(v, negative ? -ndigits : ndigits); + Py_SET_SIZE(v, negative ? -ndigits : ndigits); t = abs_ival; while (t) { *p++ = (digit)(t & PyLong_MASK); @@ -1249,7 +1249,7 @@ PyLong_AsLongLong(PyObject *vv) v = (PyLongObject *)vv; } else { - v = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(vv); + v = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(vv); if (v == NULL) return -1; do_decref = 1; @@ -1329,7 +1329,7 @@ _PyLong_AsUnsignedLongLongMask(PyObject *vv) if (vv == NULL || !PyLong_Check(vv)) { PyErr_BadInternalCall(); - return (unsigned long long) -1; + return (unsigned long long) -1; } v = (PyLongObject *)vv; switch(Py_SIZE(v)) { @@ -1357,14 +1357,14 @@ PyLong_AsUnsignedLongLongMask(PyObject *op) if (op == NULL) { PyErr_BadInternalCall(); - return (unsigned long long)-1; + return (unsigned long long)-1; } if (PyLong_Check(op)) { return _PyLong_AsUnsignedLongLongMask(op); } - lo = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(op); + lo = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(op); if (lo == NULL) return (unsigned long long)-1; @@ -1404,7 +1404,7 @@ PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow) v = (PyLongObject *)vv; } else { - v = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(vv); + v = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(vv); if (v == NULL) return -1; do_decref = 1; @@ -1441,11 +1441,11 @@ PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow) /* Haven't lost any bits, but casting to long requires extra * care (see comment above). */ - if (x <= (unsigned long long)LLONG_MAX) { + if (x <= (unsigned long long)LLONG_MAX) { res = (long long)x * sign; } else if (sign < 0 && x == PY_ABS_LLONG_MIN) { - res = LLONG_MIN; + res = LLONG_MIN; } else { *overflow = sign; @@ -1459,102 +1459,102 @@ PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow) return res; } -int -_PyLong_UnsignedShort_Converter(PyObject *obj, void *ptr) -{ - unsigned long uval; - - if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { - PyErr_SetString(PyExc_ValueError, "value must be positive"); - return 0; - } - uval = PyLong_AsUnsignedLong(obj); - if (uval == (unsigned long)-1 && PyErr_Occurred()) - return 0; - if (uval > USHRT_MAX) { - PyErr_SetString(PyExc_OverflowError, - "Python int too large for C unsigned short"); - return 0; - } - - *(unsigned short *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned short); - return 1; -} - -int -_PyLong_UnsignedInt_Converter(PyObject *obj, void *ptr) -{ - unsigned long uval; - - if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { - PyErr_SetString(PyExc_ValueError, "value must be positive"); - return 0; - } - uval = PyLong_AsUnsignedLong(obj); - if (uval == (unsigned long)-1 && PyErr_Occurred()) - return 0; - if (uval > UINT_MAX) { - PyErr_SetString(PyExc_OverflowError, - "Python int too large for C unsigned int"); - return 0; - } - - *(unsigned int *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned int); - return 1; -} - -int -_PyLong_UnsignedLong_Converter(PyObject *obj, void *ptr) -{ - unsigned long uval; - - if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { - PyErr_SetString(PyExc_ValueError, "value must be positive"); - return 0; - } - uval = PyLong_AsUnsignedLong(obj); - if (uval == (unsigned long)-1 && PyErr_Occurred()) - return 0; - - *(unsigned long *)ptr = uval; - return 1; -} - -int -_PyLong_UnsignedLongLong_Converter(PyObject *obj, void *ptr) -{ - unsigned long long uval; - - if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { - PyErr_SetString(PyExc_ValueError, "value must be positive"); - return 0; - } - uval = PyLong_AsUnsignedLongLong(obj); - if (uval == (unsigned long long)-1 && PyErr_Occurred()) - return 0; - - *(unsigned long long *)ptr = uval; - return 1; -} - -int -_PyLong_Size_t_Converter(PyObject *obj, void *ptr) -{ - size_t uval; - - if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { - PyErr_SetString(PyExc_ValueError, "value must be positive"); - return 0; - } - uval = PyLong_AsSize_t(obj); - if (uval == (size_t)-1 && PyErr_Occurred()) - return 0; - - *(size_t *)ptr = uval; - return 1; -} - - +int +_PyLong_UnsignedShort_Converter(PyObject *obj, void *ptr) +{ + unsigned long uval; + + if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { + PyErr_SetString(PyExc_ValueError, "value must be positive"); + return 0; + } + uval = PyLong_AsUnsignedLong(obj); + if (uval == (unsigned long)-1 && PyErr_Occurred()) + return 0; + if (uval > USHRT_MAX) { + PyErr_SetString(PyExc_OverflowError, + "Python int too large for C unsigned short"); + return 0; + } + + *(unsigned short *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned short); + return 1; +} + +int +_PyLong_UnsignedInt_Converter(PyObject *obj, void *ptr) +{ + unsigned long uval; + + if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { + PyErr_SetString(PyExc_ValueError, "value must be positive"); + return 0; + } + uval = PyLong_AsUnsignedLong(obj); + if (uval == (unsigned long)-1 && PyErr_Occurred()) + return 0; + if (uval > UINT_MAX) { + PyErr_SetString(PyExc_OverflowError, + "Python int too large for C unsigned int"); + return 0; + } + + *(unsigned int *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned int); + return 1; +} + +int +_PyLong_UnsignedLong_Converter(PyObject *obj, void *ptr) +{ + unsigned long uval; + + if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { + PyErr_SetString(PyExc_ValueError, "value must be positive"); + return 0; + } + uval = PyLong_AsUnsignedLong(obj); + if (uval == (unsigned long)-1 && PyErr_Occurred()) + return 0; + + *(unsigned long *)ptr = uval; + return 1; +} + +int +_PyLong_UnsignedLongLong_Converter(PyObject *obj, void *ptr) +{ + unsigned long long uval; + + if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { + PyErr_SetString(PyExc_ValueError, "value must be positive"); + return 0; + } + uval = PyLong_AsUnsignedLongLong(obj); + if (uval == (unsigned long long)-1 && PyErr_Occurred()) + return 0; + + *(unsigned long long *)ptr = uval; + return 1; +} + +int +_PyLong_Size_t_Converter(PyObject *obj, void *ptr) +{ + size_t uval; + + if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) { + PyErr_SetString(PyExc_ValueError, "value must be positive"); + return 0; + } + uval = PyLong_AsSize_t(obj); + if (uval == (size_t)-1 && PyErr_Occurred()) + return 0; + + *(size_t *)ptr = uval; + return 1; +} + + #define CHECK_BINOP(v,w) \ do { \ if (!PyLong_Check(v) || !PyLong_Check(w)) \ @@ -1938,7 +1938,7 @@ long_format_binary(PyObject *aa, int base, int alternate, return -1; } size_a_in_bits = (size_a - 1) * PyLong_SHIFT + - _Py_bit_length(a->ob_digit[size_a - 1]); + _Py_bit_length(a->ob_digit[size_a - 1]); /* Allow 1 character for a '-' sign. */ sz = negative + (size_a_in_bits + (bits - 1)) / bits; } @@ -2243,7 +2243,7 @@ PyLong_FromString(const char *str, char **pend, int base) "int() arg 2 must be >= 2 and <= 36"); return NULL; } - while (*str != '\0' && Py_ISSPACE(*str)) { + while (*str != '\0' && Py_ISSPACE(*str)) { str++; } if (*str == '+') { @@ -2461,7 +2461,7 @@ digit beyond the first. if (z == NULL) { return NULL; } - Py_SET_SIZE(z, 0); + Py_SET_SIZE(z, 0); /* `convwidth` consecutive input digits are treated as a single * digit in base `convmultmax`. @@ -2511,7 +2511,7 @@ digit beyond the first. assert(c < PyLong_BASE); if (Py_SIZE(z) < size_z) { *pz = (digit)c; - Py_SET_SIZE(z, Py_SIZE(z) + 1); + Py_SET_SIZE(z, Py_SIZE(z) + 1); } else { PyLongObject *tmp; @@ -2550,9 +2550,9 @@ digit beyond the first. goto onError; } if (sign < 0) { - Py_SET_SIZE(z, -(Py_SIZE(z))); + Py_SET_SIZE(z, -(Py_SIZE(z))); } - while (*str && Py_ISSPACE(*str)) { + while (*str && Py_ISSPACE(*str)) { str++; } if (*str != '\0') { @@ -2758,7 +2758,7 @@ x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem) /* normalize: shift w1 left so that its top digit is >= PyLong_BASE/2. shift v1 left by the same amount. Results go into w and v. */ - d = PyLong_SHIFT - _Py_bit_length(w1->ob_digit[size_w-1]); + d = PyLong_SHIFT - _Py_bit_length(w1->ob_digit[size_w-1]); carry = v_lshift(w->ob_digit, w1->ob_digit, size_w, d); assert(carry == 0); carry = v_lshift(v->ob_digit, v1->ob_digit, size_v, d); @@ -2866,8 +2866,8 @@ _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e) { Py_ssize_t a_size, a_bits, shift_digits, shift_bits, x_size; /* See below for why x_digits is always large enough. */ - digit rem; - digit x_digits[2 + (DBL_MANT_DIG + 1) / PyLong_SHIFT] = {0,}; + digit rem; + digit x_digits[2 + (DBL_MANT_DIG + 1) / PyLong_SHIFT] = {0,}; double dx; /* Correction term for round-half-to-even rounding. For a digit x, "x + half_even_correction[x & 7]" gives x rounded to the nearest @@ -2880,7 +2880,7 @@ _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e) *e = 0; return 0.0; } - a_bits = _Py_bit_length(a->ob_digit[a_size-1]); + a_bits = _Py_bit_length(a->ob_digit[a_size-1]); /* The following is an overflow-free version of the check "if ((a_size - 1) * PyLong_SHIFT + a_bits > PY_SSIZE_T_MAX) ..." */ if (a_size >= (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 && @@ -2917,7 +2917,7 @@ _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e) if (a_bits <= DBL_MANT_DIG + 2) { shift_digits = (DBL_MANT_DIG + 2 - a_bits) / PyLong_SHIFT; shift_bits = (DBL_MANT_DIG + 2 - a_bits) % PyLong_SHIFT; - x_size = shift_digits; + x_size = shift_digits; rem = v_lshift(x_digits + x_size, a->ob_digit, a_size, (int)shift_bits); x_size += a_size; @@ -3005,32 +3005,32 @@ PyLong_AsDouble(PyObject *v) /* Methods */ -/* if a < b, return a negative number - if a == b, return 0 - if a > b, return a positive number */ - -static Py_ssize_t +/* if a < b, return a negative number + if a == b, return 0 + if a > b, return a positive number */ + +static Py_ssize_t long_compare(PyLongObject *a, PyLongObject *b) { - Py_ssize_t sign = Py_SIZE(a) - Py_SIZE(b); - if (sign == 0) { + Py_ssize_t sign = Py_SIZE(a) - Py_SIZE(b); + if (sign == 0) { Py_ssize_t i = Py_ABS(Py_SIZE(a)); - sdigit diff = 0; - while (--i >= 0) { - diff = (sdigit) a->ob_digit[i] - (sdigit) b->ob_digit[i]; - if (diff) { - break; - } + sdigit diff = 0; + while (--i >= 0) { + diff = (sdigit) a->ob_digit[i] - (sdigit) b->ob_digit[i]; + if (diff) { + break; + } } - sign = Py_SIZE(a) < 0 ? -diff : diff; + sign = Py_SIZE(a) < 0 ? -diff : diff; } - return sign; + return sign; } static PyObject * long_richcompare(PyObject *self, PyObject *other, int op) { - Py_ssize_t result; + Py_ssize_t result; CHECK_BINOP(self, other); if (self == other) result = 0; @@ -3182,9 +3182,9 @@ x_sub(PyLongObject *a, PyLongObject *b) } assert(borrow == 0); if (sign < 0) { - Py_SET_SIZE(z, -Py_SIZE(z)); + Py_SET_SIZE(z, -Py_SIZE(z)); } - return maybe_small_long(long_normalize(z)); + return maybe_small_long(long_normalize(z)); } static PyObject * @@ -3206,7 +3206,7 @@ long_add(PyLongObject *a, PyLongObject *b) That also means z is not an element of small_ints, so negating it in-place is safe. */ assert(Py_REFCNT(z) == 1); - Py_SET_SIZE(z, -(Py_SIZE(z))); + Py_SET_SIZE(z, -(Py_SIZE(z))); } } else @@ -3232,15 +3232,15 @@ long_sub(PyLongObject *a, PyLongObject *b) return PyLong_FromLong(MEDIUM_VALUE(a) - MEDIUM_VALUE(b)); } if (Py_SIZE(a) < 0) { - if (Py_SIZE(b) < 0) { - z = x_sub(b, a); - } - else { + if (Py_SIZE(b) < 0) { + z = x_sub(b, a); + } + else { z = x_add(a, b); - if (z != NULL) { - assert(Py_SIZE(z) == 0 || Py_REFCNT(z) == 1); - Py_SET_SIZE(z, -(Py_SIZE(z))); - } + if (z != NULL) { + assert(Py_SIZE(z) == 0 || Py_REFCNT(z) == 1); + Py_SET_SIZE(z, -(Py_SIZE(z))); + } } } else { @@ -3632,7 +3632,7 @@ k_lopsided_mul(PyLongObject *a, PyLongObject *b) /* Multiply the next slice of b by a. */ memcpy(bslice->ob_digit, b->ob_digit + nbdone, nbtouse * sizeof(digit)); - Py_SET_SIZE(bslice, nbtouse); + Py_SET_SIZE(bslice, nbtouse); product = k_mul(a, bslice); if (product == NULL) goto fail; @@ -3973,8 +3973,8 @@ long_true_divide(PyObject *v, PyObject *w) /* Extreme underflow */ goto underflow_or_zero; /* Next line is now safe from overflowing a Py_ssize_t */ - diff = diff * PyLong_SHIFT + _Py_bit_length(a->ob_digit[a_size - 1]) - - _Py_bit_length(b->ob_digit[b_size - 1]); + diff = diff * PyLong_SHIFT + _Py_bit_length(a->ob_digit[a_size - 1]) - + _Py_bit_length(b->ob_digit[b_size - 1]); /* Now diff = a_bits - b_bits. */ if (diff > DBL_MAX_EXP) goto overflow; @@ -4050,7 +4050,7 @@ long_true_divide(PyObject *v, PyObject *w) } x_size = Py_ABS(Py_SIZE(x)); assert(x_size > 0); /* result of division is never zero */ - x_bits = (x_size-1)*PyLong_SHIFT+_Py_bit_length(x->ob_digit[x_size-1]); + x_bits = (x_size-1)*PyLong_SHIFT+_Py_bit_length(x->ob_digit[x_size-1]); /* The number of extra bits that have to be rounded away. */ extra_bits = Py_MAX(x_bits, DBL_MIN_EXP - shift) - DBL_MANT_DIG; @@ -4117,8 +4117,8 @@ long_divmod(PyObject *a, PyObject *b) } z = PyTuple_New(2); if (z != NULL) { - PyTuple_SET_ITEM(z, 0, (PyObject *) div); - PyTuple_SET_ITEM(z, 1, (PyObject *) mod); + PyTuple_SET_ITEM(z, 0, (PyObject *) div); + PyTuple_SET_ITEM(z, 1, (PyObject *) mod); } else { Py_DECREF(div); @@ -4127,98 +4127,98 @@ long_divmod(PyObject *a, PyObject *b) return z; } - -/* Compute an inverse to a modulo n, or raise ValueError if a is not - invertible modulo n. Assumes n is positive. The inverse returned - is whatever falls out of the extended Euclidean algorithm: it may - be either positive or negative, but will be smaller than n in - absolute value. - - Pure Python equivalent for long_invmod: - - def invmod(a, n): - b, c = 1, 0 - while n: - q, r = divmod(a, n) - a, b, c, n = n, c, b - q*c, r - - # at this point a is the gcd of the original inputs - if a == 1: - return b - raise ValueError("Not invertible") -*/ - -static PyLongObject * -long_invmod(PyLongObject *a, PyLongObject *n) -{ - PyLongObject *b, *c; - - /* Should only ever be called for positive n */ - assert(Py_SIZE(n) > 0); - - b = (PyLongObject *)PyLong_FromLong(1L); - if (b == NULL) { - return NULL; - } - c = (PyLongObject *)PyLong_FromLong(0L); - if (c == NULL) { - Py_DECREF(b); - return NULL; - } - Py_INCREF(a); - Py_INCREF(n); - - /* references now owned: a, b, c, n */ - while (Py_SIZE(n) != 0) { - PyLongObject *q, *r, *s, *t; - - if (l_divmod(a, n, &q, &r) == -1) { - goto Error; - } - Py_DECREF(a); - a = n; - n = r; - t = (PyLongObject *)long_mul(q, c); - Py_DECREF(q); - if (t == NULL) { - goto Error; - } - s = (PyLongObject *)long_sub(b, t); - Py_DECREF(t); - if (s == NULL) { - goto Error; - } - Py_DECREF(b); - b = c; - c = s; - } - /* references now owned: a, b, c, n */ - - Py_DECREF(c); - Py_DECREF(n); - if (long_compare(a, (PyLongObject *)_PyLong_One)) { - /* a != 1; we don't have an inverse. */ - Py_DECREF(a); - Py_DECREF(b); - PyErr_SetString(PyExc_ValueError, - "base is not invertible for the given modulus"); - return NULL; - } - else { - /* a == 1; b gives an inverse modulo n */ - Py_DECREF(a); - return b; - } - - Error: - Py_DECREF(a); - Py_DECREF(b); - Py_DECREF(c); - Py_DECREF(n); - return NULL; -} - - + +/* Compute an inverse to a modulo n, or raise ValueError if a is not + invertible modulo n. Assumes n is positive. The inverse returned + is whatever falls out of the extended Euclidean algorithm: it may + be either positive or negative, but will be smaller than n in + absolute value. + + Pure Python equivalent for long_invmod: + + def invmod(a, n): + b, c = 1, 0 + while n: + q, r = divmod(a, n) + a, b, c, n = n, c, b - q*c, r + + # at this point a is the gcd of the original inputs + if a == 1: + return b + raise ValueError("Not invertible") +*/ + +static PyLongObject * +long_invmod(PyLongObject *a, PyLongObject *n) +{ + PyLongObject *b, *c; + + /* Should only ever be called for positive n */ + assert(Py_SIZE(n) > 0); + + b = (PyLongObject *)PyLong_FromLong(1L); + if (b == NULL) { + return NULL; + } + c = (PyLongObject *)PyLong_FromLong(0L); + if (c == NULL) { + Py_DECREF(b); + return NULL; + } + Py_INCREF(a); + Py_INCREF(n); + + /* references now owned: a, b, c, n */ + while (Py_SIZE(n) != 0) { + PyLongObject *q, *r, *s, *t; + + if (l_divmod(a, n, &q, &r) == -1) { + goto Error; + } + Py_DECREF(a); + a = n; + n = r; + t = (PyLongObject *)long_mul(q, c); + Py_DECREF(q); + if (t == NULL) { + goto Error; + } + s = (PyLongObject *)long_sub(b, t); + Py_DECREF(t); + if (s == NULL) { + goto Error; + } + Py_DECREF(b); + b = c; + c = s; + } + /* references now owned: a, b, c, n */ + + Py_DECREF(c); + Py_DECREF(n); + if (long_compare(a, (PyLongObject *)_PyLong_One)) { + /* a != 1; we don't have an inverse. */ + Py_DECREF(a); + Py_DECREF(b); + PyErr_SetString(PyExc_ValueError, + "base is not invertible for the given modulus"); + return NULL; + } + else { + /* a == 1; b gives an inverse modulo n */ + Py_DECREF(a); + return b; + } + + Error: + Py_DECREF(a); + Py_DECREF(b); + Py_DECREF(c); + Py_DECREF(n); + return NULL; +} + + /* pow(v, w, x) */ static PyObject * long_pow(PyObject *v, PyObject *w, PyObject *x) @@ -4252,14 +4252,14 @@ long_pow(PyObject *v, PyObject *w, PyObject *x) Py_RETURN_NOTIMPLEMENTED; } - if (Py_SIZE(b) < 0 && c == NULL) { - /* if exponent is negative and there's no modulus: - return a float. This works because we know + if (Py_SIZE(b) < 0 && c == NULL) { + /* if exponent is negative and there's no modulus: + return a float. This works because we know that this calls float_pow() which converts its arguments to double. */ - Py_DECREF(a); - Py_DECREF(b); - return PyFloat_Type.tp_as_number->nb_power(v, w, x); + Py_DECREF(a); + Py_DECREF(b); + return PyFloat_Type.tp_as_number->nb_power(v, w, x); } if (c) { @@ -4294,27 +4294,27 @@ long_pow(PyObject *v, PyObject *w, PyObject *x) goto Done; } - /* if exponent is negative, negate the exponent and - replace the base with a modular inverse */ - if (Py_SIZE(b) < 0) { - temp = (PyLongObject *)_PyLong_Copy(b); - if (temp == NULL) - goto Error; - Py_DECREF(b); - b = temp; - temp = NULL; - _PyLong_Negate(&b); - if (b == NULL) - goto Error; - - temp = long_invmod(a, c); - if (temp == NULL) - goto Error; - Py_DECREF(a); - a = temp; - temp = NULL; - } - + /* if exponent is negative, negate the exponent and + replace the base with a modular inverse */ + if (Py_SIZE(b) < 0) { + temp = (PyLongObject *)_PyLong_Copy(b); + if (temp == NULL) + goto Error; + Py_DECREF(b); + b = temp; + temp = NULL; + _PyLong_Negate(&b); + if (b == NULL) + goto Error; + + temp = long_invmod(a, c); + if (temp == NULL) + goto Error; + Py_DECREF(a); + a = temp; + temp = NULL; + } + /* Reduce base by modulus in some cases: 1. If base < 0. Forcing the base non-negative makes things easier. 2. If base is obviously larger than the modulus. The "small @@ -4449,7 +4449,7 @@ long_neg(PyLongObject *v) return PyLong_FromLong(-MEDIUM_VALUE(v)); z = (PyLongObject *)_PyLong_Copy(v); if (z != NULL) - Py_SET_SIZE(z, -(Py_SIZE(v))); + Py_SET_SIZE(z, -(Py_SIZE(v))); return (PyObject *)z; } @@ -4470,9 +4470,9 @@ long_bool(PyLongObject *v) /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */ static int -divmod_shift(PyObject *shiftby, Py_ssize_t *wordshift, digit *remshift) +divmod_shift(PyObject *shiftby, Py_ssize_t *wordshift, digit *remshift) { - assert(PyLong_Check(shiftby)); + assert(PyLong_Check(shiftby)); assert(Py_SIZE(shiftby) >= 0); Py_ssize_t lshiftby = PyLong_AsSsize_t((PyObject *)shiftby); if (lshiftby >= 0) { @@ -4484,7 +4484,7 @@ divmod_shift(PyObject *shiftby, Py_ssize_t *wordshift, digit *remshift) be that PyLong_AsSsize_t raised an OverflowError. */ assert(PyErr_ExceptionMatches(PyExc_OverflowError)); PyErr_Clear(); - PyLongObject *wordshift_obj = divrem1((PyLongObject *)shiftby, PyLong_SHIFT, remshift); + PyLongObject *wordshift_obj = divrem1((PyLongObject *)shiftby, PyLong_SHIFT, remshift); if (wordshift_obj == NULL) { return -1; } @@ -4502,11 +4502,11 @@ divmod_shift(PyObject *shiftby, Py_ssize_t *wordshift, digit *remshift) } static PyObject * -long_rshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift) +long_rshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift) { PyLongObject *z = NULL; - Py_ssize_t newsize, hishift, i, j; - digit lomask, himask; + Py_ssize_t newsize, hishift, i, j; + digit lomask, himask; if (Py_SIZE(a) < 0) { /* Right shifting negative numbers is harder */ @@ -4514,7 +4514,7 @@ long_rshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift) a1 = (PyLongObject *) long_invert(a); if (a1 == NULL) return NULL; - a2 = (PyLongObject *) long_rshift1(a1, wordshift, remshift); + a2 = (PyLongObject *) long_rshift1(a1, wordshift, remshift); Py_DECREF(a1); if (a2 == NULL) return NULL; @@ -4525,14 +4525,14 @@ long_rshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift) newsize = Py_SIZE(a) - wordshift; if (newsize <= 0) return PyLong_FromLong(0); - hishift = PyLong_SHIFT - remshift; + hishift = PyLong_SHIFT - remshift; lomask = ((digit)1 << hishift) - 1; himask = PyLong_MASK ^ lomask; z = _PyLong_New(newsize); if (z == NULL) return NULL; for (i = 0, j = wordshift; i < newsize; i++, j++) { - z->ob_digit[i] = (a->ob_digit[j] >> remshift) & lomask; + z->ob_digit[i] = (a->ob_digit[j] >> remshift) & lomask; if (i+1 < newsize) z->ob_digit[i] |= (a->ob_digit[j+1] << hishift) & himask; } @@ -4542,9 +4542,9 @@ long_rshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift) } static PyObject * -long_rshift(PyObject *a, PyObject *b) +long_rshift(PyObject *a, PyObject *b) { - Py_ssize_t wordshift; + Py_ssize_t wordshift; digit remshift; CHECK_BINOP(a, b); @@ -4558,33 +4558,33 @@ long_rshift(PyObject *a, PyObject *b) } if (divmod_shift(b, &wordshift, &remshift) < 0) return NULL; - return long_rshift1((PyLongObject *)a, wordshift, remshift); -} - -/* Return a >> shiftby. */ -PyObject * -_PyLong_Rshift(PyObject *a, size_t shiftby) -{ - Py_ssize_t wordshift; - digit remshift; - - assert(PyLong_Check(a)); - if (Py_SIZE(a) == 0) { - return PyLong_FromLong(0); - } - wordshift = shiftby / PyLong_SHIFT; - remshift = shiftby % PyLong_SHIFT; - return long_rshift1((PyLongObject *)a, wordshift, remshift); -} - -static PyObject * -long_lshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift) -{ - /* This version due to Tim Peters */ - PyLongObject *z = NULL; - Py_ssize_t oldsize, newsize, i, j; - twodigits accum; - + return long_rshift1((PyLongObject *)a, wordshift, remshift); +} + +/* Return a >> shiftby. */ +PyObject * +_PyLong_Rshift(PyObject *a, size_t shiftby) +{ + Py_ssize_t wordshift; + digit remshift; + + assert(PyLong_Check(a)); + if (Py_SIZE(a) == 0) { + return PyLong_FromLong(0); + } + wordshift = shiftby / PyLong_SHIFT; + remshift = shiftby % PyLong_SHIFT; + return long_rshift1((PyLongObject *)a, wordshift, remshift); +} + +static PyObject * +long_lshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift) +{ + /* This version due to Tim Peters */ + PyLongObject *z = NULL; + Py_ssize_t oldsize, newsize, i, j; + twodigits accum; + oldsize = Py_ABS(Py_SIZE(a)); newsize = oldsize + wordshift; if (remshift) @@ -4594,7 +4594,7 @@ long_lshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift) return NULL; if (Py_SIZE(a) < 0) { assert(Py_REFCNT(z) == 1); - Py_SET_SIZE(z, -Py_SIZE(z)); + Py_SET_SIZE(z, -Py_SIZE(z)); } for (i = 0; i < wordshift; i++) z->ob_digit[i] = 0; @@ -4612,42 +4612,42 @@ long_lshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift) return (PyObject *) maybe_small_long(z); } -static PyObject * -long_lshift(PyObject *a, PyObject *b) -{ - Py_ssize_t wordshift; - digit remshift; - - CHECK_BINOP(a, b); - - if (Py_SIZE(b) < 0) { - PyErr_SetString(PyExc_ValueError, "negative shift count"); - return NULL; - } - if (Py_SIZE(a) == 0) { - return PyLong_FromLong(0); - } - if (divmod_shift(b, &wordshift, &remshift) < 0) - return NULL; - return long_lshift1((PyLongObject *)a, wordshift, remshift); -} - -/* Return a << shiftby. */ -PyObject * -_PyLong_Lshift(PyObject *a, size_t shiftby) -{ - Py_ssize_t wordshift; - digit remshift; - - assert(PyLong_Check(a)); - if (Py_SIZE(a) == 0) { - return PyLong_FromLong(0); - } - wordshift = shiftby / PyLong_SHIFT; - remshift = shiftby % PyLong_SHIFT; - return long_lshift1((PyLongObject *)a, wordshift, remshift); -} - +static PyObject * +long_lshift(PyObject *a, PyObject *b) +{ + Py_ssize_t wordshift; + digit remshift; + + CHECK_BINOP(a, b); + + if (Py_SIZE(b) < 0) { + PyErr_SetString(PyExc_ValueError, "negative shift count"); + return NULL; + } + if (Py_SIZE(a) == 0) { + return PyLong_FromLong(0); + } + if (divmod_shift(b, &wordshift, &remshift) < 0) + return NULL; + return long_lshift1((PyLongObject *)a, wordshift, remshift); +} + +/* Return a << shiftby. */ +PyObject * +_PyLong_Lshift(PyObject *a, size_t shiftby) +{ + Py_ssize_t wordshift; + digit remshift; + + assert(PyLong_Check(a)); + if (Py_SIZE(a) == 0) { + return PyLong_FromLong(0); + } + wordshift = shiftby / PyLong_SHIFT; + remshift = shiftby % PyLong_SHIFT; + return long_lshift1((PyLongObject *)a, wordshift, remshift); +} + /* Compute two's complement of digit vector a[0:m], writing result to z[0:m]. The digit vector a need not be normalized, but should not be entirely zero. a and z may point to the same digit vector. */ @@ -4738,7 +4738,7 @@ long_bitwise(PyLongObject *a, size_z = negb ? size_b : size_a; break; default: - Py_UNREACHABLE(); + Py_UNREACHABLE(); } /* We allow an extra digit if z is negative, to make sure that @@ -4765,7 +4765,7 @@ long_bitwise(PyLongObject *a, z->ob_digit[i] = a->ob_digit[i] ^ b->ob_digit[i]; break; default: - Py_UNREACHABLE(); + Py_UNREACHABLE(); } /* Copy any remaining digits of a, inverting if necessary. */ @@ -4778,7 +4778,7 @@ long_bitwise(PyLongObject *a, /* Complement result if negative. */ if (negz) { - Py_SET_SIZE(z, -(Py_SIZE(z))); + Py_SET_SIZE(z, -(Py_SIZE(z))); z->ob_digit[size_z] = PyLong_MASK; v_complement(z->ob_digit, z->ob_digit, size_z+1); } @@ -4865,7 +4865,7 @@ _PyLong_GCD(PyObject *aarg, PyObject *barg) alloc_b = Py_SIZE(b); /* reduce until a fits into 2 digits */ while ((size_a = Py_SIZE(a)) > 2) { - nbits = _Py_bit_length(a->ob_digit[size_a-1]); + nbits = _Py_bit_length(a->ob_digit[size_a-1]); /* extract top 2*PyLong_SHIFT bits of a into x, along with corresponding bits of b into y */ size_b = Py_SIZE(b); @@ -4925,9 +4925,9 @@ _PyLong_GCD(PyObject *aarg, PyObject *barg) T = -A; A = -B; B = T; T = -C; C = -D; D = T; } - if (c != NULL) { - Py_SET_SIZE(c, size_a); - } + if (c != NULL) { + Py_SET_SIZE(c, size_a); + } else if (Py_REFCNT(a) == 1) { Py_INCREF(a); c = a; @@ -4939,13 +4939,13 @@ _PyLong_GCD(PyObject *aarg, PyObject *barg) goto error; } - if (d != NULL) { - Py_SET_SIZE(d, size_a); - } + if (d != NULL) { + Py_SET_SIZE(d, size_a); + } else if (Py_REFCNT(b) == 1 && size_a <= alloc_b) { Py_INCREF(b); d = b; - Py_SET_SIZE(d, size_a); + Py_SET_SIZE(d, size_a); } else { alloc_b = size_a; @@ -5001,7 +5001,7 @@ simple: /* a fits into a long, so b must too */ x = PyLong_AsLong((PyObject *)a); y = PyLong_AsLong((PyObject *)b); -#elif LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT +#elif LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT x = PyLong_AsLongLong((PyObject *)a); y = PyLong_AsLongLong((PyObject *)b); #else @@ -5020,7 +5020,7 @@ simple: } #if LONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT return PyLong_FromLong(x); -#elif LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT +#elif LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT return PyLong_FromLongLong(x); #else # error "_PyLong_GCD" @@ -5086,7 +5086,7 @@ long_new_impl(PyTypeObject *type, PyObject *x, PyObject *obase) if (PyUnicode_Check(x)) return PyLong_FromUnicodeObject(x, (int)base); else if (PyByteArray_Check(x) || PyBytes_Check(x)) { - const char *string; + const char *string; if (PyByteArray_Check(x)) string = PyByteArray_AS_STRING(x); else @@ -5125,10 +5125,10 @@ long_subtype_new(PyTypeObject *type, PyObject *x, PyObject *obase) return NULL; } assert(PyLong_Check(newobj)); - Py_SET_SIZE(newobj, Py_SIZE(tmp)); - for (i = 0; i < n; i++) { + Py_SET_SIZE(newobj, Py_SIZE(tmp)); + for (i = 0; i < n; i++) { newobj->ob_digit[i] = tmp->ob_digit[i]; - } + } Py_DECREF(tmp); return (PyObject *)newobj; } @@ -5145,14 +5145,14 @@ int___getnewargs___impl(PyObject *self) } static PyObject * -long_get0(PyObject *Py_UNUSED(self), void *Py_UNUSED(context)) -{ +long_get0(PyObject *Py_UNUSED(self), void *Py_UNUSED(context)) +{ return PyLong_FromLong(0L); } static PyObject * -long_get1(PyObject *Py_UNUSED(self), void *Py_UNUSED(ignored)) -{ +long_get1(PyObject *Py_UNUSED(self), void *Py_UNUSED(ignored)) +{ return PyLong_FromLong(1L); } @@ -5192,8 +5192,8 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b) { PyLongObject *quo = NULL, *rem = NULL; PyObject *twice_rem, *result, *temp; - int quo_is_odd, quo_is_neg; - Py_ssize_t cmp; + int quo_is_odd, quo_is_neg; + Py_ssize_t cmp; /* Equivalent Python code: @@ -5386,7 +5386,7 @@ int_bit_length_impl(PyObject *self) return PyLong_FromLong(0); msd = ((PyLongObject *)self)->ob_digit[ndigits-1]; - msd_bits = _Py_bit_length(msd); + msd_bits = _Py_bit_length(msd); if (ndigits <= PY_SSIZE_T_MAX/PyLong_SHIFT) return PyLong_FromSsize_t((ndigits-1)*PyLong_SHIFT + msd_bits); @@ -5422,35 +5422,35 @@ int_bit_length_impl(PyObject *self) return NULL; } - -/*[clinic input] -int.as_integer_ratio - -Return integer ratio. - -Return a pair of integers, whose ratio is exactly equal to the original int -and with a positive denominator. - ->>> (10).as_integer_ratio() -(10, 1) ->>> (-10).as_integer_ratio() -(-10, 1) ->>> (0).as_integer_ratio() -(0, 1) -[clinic start generated code]*/ - + +/*[clinic input] +int.as_integer_ratio + +Return integer ratio. + +Return a pair of integers, whose ratio is exactly equal to the original int +and with a positive denominator. + +>>> (10).as_integer_ratio() +(10, 1) +>>> (-10).as_integer_ratio() +(-10, 1) +>>> (0).as_integer_ratio() +(0, 1) +[clinic start generated code]*/ + static PyObject * -int_as_integer_ratio_impl(PyObject *self) -/*[clinic end generated code: output=e60803ae1cc8621a input=55ce3058e15de393]*/ +int_as_integer_ratio_impl(PyObject *self) +/*[clinic end generated code: output=e60803ae1cc8621a input=55ce3058e15de393]*/ { - PyObject *ratio_tuple; - PyObject *numerator = long_long(self); - if (numerator == NULL) { - return NULL; - } - ratio_tuple = PyTuple_Pack(2, numerator, _PyLong_One); - Py_DECREF(numerator); - return ratio_tuple; + PyObject *ratio_tuple; + PyObject *numerator = long_long(self); + if (numerator == NULL) { + return NULL; + } + ratio_tuple = PyTuple_Pack(2, numerator, _PyLong_One); + Py_DECREF(numerator); + return ratio_tuple; } /*[clinic input] @@ -5562,30 +5562,30 @@ int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj, Py_DECREF(bytes); if (long_obj != NULL && type != &PyLong_Type) { - Py_SETREF(long_obj, PyObject_CallOneArg((PyObject *)type, long_obj)); + Py_SETREF(long_obj, PyObject_CallOneArg((PyObject *)type, long_obj)); } return long_obj; } -static PyObject * -long_long_meth(PyObject *self, PyObject *Py_UNUSED(ignored)) -{ - return long_long(self); -} - +static PyObject * +long_long_meth(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + return long_long(self); +} + static PyMethodDef long_methods[] = { - {"conjugate", long_long_meth, METH_NOARGS, + {"conjugate", long_long_meth, METH_NOARGS, "Returns self, the complex conjugate of any int."}, INT_BIT_LENGTH_METHODDEF INT_TO_BYTES_METHODDEF INT_FROM_BYTES_METHODDEF - INT_AS_INTEGER_RATIO_METHODDEF - {"__trunc__", long_long_meth, METH_NOARGS, + INT_AS_INTEGER_RATIO_METHODDEF + {"__trunc__", long_long_meth, METH_NOARGS, "Truncating an Integral returns itself."}, - {"__floor__", long_long_meth, METH_NOARGS, + {"__floor__", long_long_meth, METH_NOARGS, "Flooring an Integral returns itself."}, - {"__ceil__", long_long_meth, METH_NOARGS, + {"__ceil__", long_long_meth, METH_NOARGS, "Ceiling of an Integral returns itself."}, {"__round__", (PyCFunction)long_round, METH_VARARGS, "Rounding an Integral returns itself.\n" @@ -5598,19 +5598,19 @@ static PyMethodDef long_methods[] = { static PyGetSetDef long_getset[] = { {"real", - (getter)long_long_meth, (setter)NULL, + (getter)long_long_meth, (setter)NULL, "the real part of a complex number", NULL}, {"imag", - long_get0, (setter)NULL, + long_get0, (setter)NULL, "the imaginary part of a complex number", NULL}, {"numerator", - (getter)long_long_meth, (setter)NULL, + (getter)long_long_meth, (setter)NULL, "the numerator of a rational number in lowest terms", NULL}, {"denominator", - long_get1, (setter)NULL, + long_get1, (setter)NULL, "the denominator of a rational number in lowest terms", NULL}, {NULL} /* Sentinel */ @@ -5640,12 +5640,12 @@ static PyNumberMethods long_as_number = { long_divmod, /*nb_divmod*/ long_pow, /*nb_power*/ (unaryfunc)long_neg, /*nb_negative*/ - long_long, /*tp_positive*/ + long_long, /*tp_positive*/ (unaryfunc)long_abs, /*tp_absolute*/ (inquiry)long_bool, /*tp_bool*/ (unaryfunc)long_invert, /*nb_invert*/ long_lshift, /*nb_lshift*/ - long_rshift, /*nb_rshift*/ + long_rshift, /*nb_rshift*/ long_and, /*nb_and*/ long_xor, /*nb_xor*/ long_or, /*nb_or*/ @@ -5674,18 +5674,18 @@ PyTypeObject PyLong_Type = { "int", /* tp_name */ offsetof(PyLongObject, ob_digit), /* tp_basicsize */ sizeof(digit), /* tp_itemsize */ - 0, /* tp_dealloc */ - 0, /* tp_vectorcall_offset */ + 0, /* tp_dealloc */ + 0, /* tp_vectorcall_offset */ 0, /* tp_getattr */ 0, /* tp_setattr */ - 0, /* tp_as_async */ + 0, /* tp_as_async */ long_to_decimal_string, /* tp_repr */ &long_as_number, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ (hashfunc)long_hash, /* tp_hash */ 0, /* tp_call */ - 0, /* tp_str */ + 0, /* tp_str */ PyObject_GenericGetAttr, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ @@ -5717,7 +5717,7 @@ static PyTypeObject Int_InfoType; PyDoc_STRVAR(int_info__doc__, "sys.int_info\n\ \n\ -A named tuple that holds information about Python's\n\ +A named tuple that holds information about Python's\n\ internal representation of integers. The attributes are read only."); static PyStructSequence_Field int_info_fields[] = { @@ -5753,58 +5753,58 @@ PyLong_GetInfo(void) } int -_PyLong_Init(PyThreadState *tstate) +_PyLong_Init(PyThreadState *tstate) { #if NSMALLNEGINTS + NSMALLPOSINTS > 0 - for (Py_ssize_t i=0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) { - sdigit ival = (sdigit)i - NSMALLNEGINTS; - int size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1); + for (Py_ssize_t i=0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) { + sdigit ival = (sdigit)i - NSMALLNEGINTS; + int size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1); - PyLongObject *v = _PyLong_New(1); - if (!v) { - return -1; - } + PyLongObject *v = _PyLong_New(1); + if (!v) { + return -1; + } - Py_SET_SIZE(v, size); + Py_SET_SIZE(v, size); v->ob_digit[0] = (digit)abs(ival); - - tstate->interp->small_ints[i] = v; + + tstate->interp->small_ints[i] = v; } #endif - if (_Py_IsMainInterpreter(tstate)) { - _PyLong_Zero = PyLong_FromLong(0); - if (_PyLong_Zero == NULL) { - return 0; - } - - _PyLong_One = PyLong_FromLong(1); - if (_PyLong_One == NULL) { + if (_Py_IsMainInterpreter(tstate)) { + _PyLong_Zero = PyLong_FromLong(0); + if (_PyLong_Zero == NULL) { return 0; - } - - /* initialize int_info */ - if (Int_InfoType.tp_name == NULL) { - if (PyStructSequence_InitType2(&Int_InfoType, &int_info_desc) < 0) { - return 0; - } - } + } + + _PyLong_One = PyLong_FromLong(1); + if (_PyLong_One == NULL) { + return 0; + } + + /* initialize int_info */ + if (Int_InfoType.tp_name == NULL) { + if (PyStructSequence_InitType2(&Int_InfoType, &int_info_desc) < 0) { + return 0; + } + } } return 1; } void -_PyLong_Fini(PyThreadState *tstate) +_PyLong_Fini(PyThreadState *tstate) { - if (_Py_IsMainInterpreter(tstate)) { - Py_CLEAR(_PyLong_One); - Py_CLEAR(_PyLong_Zero); - } - + if (_Py_IsMainInterpreter(tstate)) { + Py_CLEAR(_PyLong_One); + Py_CLEAR(_PyLong_Zero); + } + #if NSMALLNEGINTS + NSMALLPOSINTS > 0 - for (Py_ssize_t i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) { - Py_CLEAR(tstate->interp->small_ints[i]); + for (Py_ssize_t i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) { + Py_CLEAR(tstate->interp->small_ints[i]); } #endif } |