@@ -301,6 +301,7 @@ typedef struct _PyEncoderObject {
301301 int iterable_as_array ;
302302 PyObject * max_long_size ;
303303 PyObject * min_long_size ;
304+ PyObject * large_int_bitcount ;
304305 PyObject * item_sort_key ;
305306 PyObject * item_sort_kw ;
306307 int for_json ;
@@ -327,6 +328,7 @@ typedef struct _PyEncoderObject {
327328 X(item_sort_key) \
328329 X(max_long_size) \
329330 X(min_long_size) \
331+ X(large_int_bitcount) \
330332 X(Decimal)
331333
332334static PyMemberDef encoder_members [] = {
@@ -708,6 +710,36 @@ maybe_quote_bigint(PyEncoderObject* s, PyObject *encoded, PyObject *obj)
708710 int ge , le ;
709711 PyObject * quoted ;
710712
713+ if (s -> large_int_bitcount != NULL ) {
714+ size_t nbits ;
715+ PyObject * integer , * bits ;
716+ integer = PyNumber_Long (obj );
717+ if (integer == NULL ) {
718+ Py_DECREF (encoded );
719+ return NULL ;
720+ }
721+ nbits = _PyLong_NumBits (integer );
722+ Py_DECREF (integer );
723+ if (nbits == (size_t )-1 && PyErr_Occurred ()) {
724+ Py_DECREF (encoded );
725+ return NULL ;
726+ }
727+ bits = PyLong_FromSize_t (nbits );
728+ if (bits == NULL ) {
729+ Py_DECREF (encoded );
730+ return NULL ;
731+ }
732+ ge = PyObject_RichCompareBool (bits , s -> large_int_bitcount , Py_GT );
733+ Py_DECREF (bits );
734+ if (ge < 0 ) {
735+ Py_DECREF (encoded );
736+ return NULL ;
737+ }
738+ if (!ge )
739+ return encoded ;
740+ goto quote ;
741+ }
742+
711743 /* int_as_string_bitcount is not set: fast path, return as-is. */
712744 if (s -> max_long_size == Py_None || s -> min_long_size == Py_None )
713745 return encoded ;
@@ -725,6 +757,7 @@ maybe_quote_bigint(PyEncoderObject* s, PyObject *encoded, PyObject *obj)
725757 if (!(ge || le ))
726758 return encoded ;
727759
760+ quote :
728761#if PY_MAJOR_VERSION >= 3
729762 quoted = PyUnicode_FromFormat ("\"%U\"" , encoded );
730763#else
@@ -2653,10 +2686,11 @@ encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
26532686 goto bail ;
26542687 if (PyInt_Check (int_as_string_bitcount ) || PyLong_Check (int_as_string_bitcount )) {
26552688 static const unsigned long long_long_bitsize = sizeof (long long ) * CHAR_BIT ;
2656- long int_as_string_bitcount_val = PyLong_AsLong (int_as_string_bitcount );
2689+ int overflow = 0 ;
2690+ long int_as_string_bitcount_val = PyLong_AsLongAndOverflow (int_as_string_bitcount , & overflow );
26572691 if (int_as_string_bitcount_val == -1 && PyErr_Occurred ())
26582692 goto bail ;
2659- if (int_as_string_bitcount_val > 0 && int_as_string_bitcount_val < (long )long_long_bitsize ) {
2693+ if (! overflow && int_as_string_bitcount_val > 0 && int_as_string_bitcount_val < (long )long_long_bitsize ) {
26602694 int n = (int )int_as_string_bitcount_val ;
26612695 /* Compute 2^n as unsigned (well-defined for n < 64) and
26622696 * -(2^n) as signed without UB. Naive "-1LL << n" is a
@@ -2672,10 +2706,19 @@ encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
26722706 goto bail ;
26732707 }
26742708 }
2709+ else if (overflow > 0 || int_as_string_bitcount_val > 0 ) {
2710+ /* Compare magnitudes without allocating a 2**n boundary. */
2711+ s -> large_int_bitcount = PyNumber_Long (int_as_string_bitcount );
2712+ if (s -> large_int_bitcount == NULL )
2713+ goto bail ;
2714+ Py_INCREF (Py_None );
2715+ s -> max_long_size = Py_None ;
2716+ Py_INCREF (Py_None );
2717+ s -> min_long_size = Py_None ;
2718+ }
26752719 else {
2676- PyErr_Format (PyExc_TypeError ,
2677- "int_as_string_bitcount (%ld) must be greater than 0 and less than the number of bits of a `long long` type (%lu bits)" ,
2678- int_as_string_bitcount_val , long_long_bitsize );
2720+ PyErr_SetString (PyExc_TypeError ,
2721+ "int_as_string_bitcount must be a positive integer" );
26792722 goto bail ;
26802723 }
26812724 }
0 commit comments