@@ -6842,15 +6842,36 @@ PyLong_Export(PyObject *obj, PyLongExport *export_long)
68426842 return -1 ;
68436843 }
68446844
6845- PyLongObject * self = (PyLongObject * )obj ;
6846- export_long -> value = 0 ;
6847- export_long -> negative = _PyLong_IsNegative (self );
6848- export_long -> ndigits = _PyLong_DigitCount (self );
6849- if (export_long -> ndigits == 0 ) {
6850- export_long -> ndigits = 1 ;
6851- }
6852- export_long -> digits = self -> long_value .ob_digit ;
6853- export_long -> _reserved = (Py_uintptr_t )Py_NewRef (obj );
6845+ // Fast-path: try to convert to a int64_t
6846+ int overflow ;
6847+ #if SIZEOF_LONG == 8
6848+ long value = PyLong_AsLongAndOverflow (obj , & overflow );
6849+ #else
6850+ // Windows has 32-bit long, so use 64-bit long long instead
6851+ long long value = PyLong_AsLongLongAndOverflow (obj , & overflow );
6852+ #endif
6853+ Py_BUILD_ASSERT (sizeof (value ) == sizeof (int64_t ));
6854+ // the function cannot fail since obj is a PyLongObject
6855+ assert (!(value == -1 && PyErr_Occurred ()));
6856+
6857+ if (!overflow ) {
6858+ export_long -> value = value ;
6859+ export_long -> negative = 0 ;
6860+ export_long -> ndigits = 0 ;
6861+ export_long -> digits = NULL ;
6862+ export_long -> _reserved = 0 ;
6863+ }
6864+ else {
6865+ PyLongObject * self = (PyLongObject * )obj ;
6866+ export_long -> value = 0 ;
6867+ export_long -> negative = _PyLong_IsNegative (self );
6868+ export_long -> ndigits = _PyLong_DigitCount (self );
6869+ if (export_long -> ndigits == 0 ) {
6870+ export_long -> ndigits = 1 ;
6871+ }
6872+ export_long -> digits = self -> long_value .ob_digit ;
6873+ export_long -> _reserved = (Py_uintptr_t )Py_NewRef (obj );
6874+ }
68546875 return 0 ;
68556876}
68566877
0 commit comments