@@ -185,6 +185,7 @@ long_alloc(Py_ssize_t size)
185185 return NULL ;
186186 }
187187 _PyObject_Init ((PyObject * )result , & PyLong_Type );
188+ _PyLong_InitTag (result );
188189 }
189190 _PyLong_SetSignAndDigitCount (result , size != 0 , size );
190191 /* The digit has to be initialized explicitly to avoid
@@ -258,6 +259,7 @@ _PyLong_FromMedium(sdigit x)
258259 return NULL ;
259260 }
260261 _PyObject_Init ((PyObject * )v , & PyLong_Type );
262+ _PyLong_InitTag (v );
261263 }
262264 digit abs_x = x < 0 ? - x : x ;
263265 _PyLong_SetSignAndDigitCount (v , x < 0 ?-1 :1 , 1 );
@@ -337,6 +339,7 @@ medium_from_stwodigits(stwodigits x)
337339 return PyStackRef_NULL ;
338340 }
339341 _PyObject_Init ((PyObject * )v , & PyLong_Type );
342+ _PyLong_InitTag (v );
340343 }
341344 digit abs_x = x < 0 ? (digit )(- x ) : (digit )x ;
342345 _PyLong_SetSignAndDigitCount (v , x < 0 ?-1 :1 , 1 );
@@ -6011,29 +6014,34 @@ static PyObject *
60116014long_subtype_new (PyTypeObject * type , PyObject * x , PyObject * obase )
60126015{
60136016 PyLongObject * tmp , * newobj ;
6014- Py_ssize_t i , n ;
6017+ Py_ssize_t size , ndigits ;
6018+ int sign ;
60156019
60166020 assert (PyType_IsSubtype (type , & PyLong_Type ));
60176021 tmp = (PyLongObject * )long_new_impl (& PyLong_Type , x , obase );
60186022 if (tmp == NULL )
60196023 return NULL ;
60206024 assert (PyLong_Check (tmp ));
6021- n = _PyLong_DigitCount (tmp );
6025+ size = _PyLong_DigitCount (tmp );
60226026 /* Fast operations for single digit integers (including zero)
60236027 * assume that there is always at least one digit present. */
6024- if (n == 0 ) {
6025- n = 1 ;
6026- }
6027- newobj = (PyLongObject * )type -> tp_alloc (type , n );
6028+ ndigits = size ? size : 1 ;
6029+ newobj = (PyLongObject * )type -> tp_alloc (type , ndigits );
60286030 if (newobj == NULL ) {
60296031 Py_DECREF (tmp );
60306032 return NULL ;
60316033 }
60326034 assert (PyLong_Check (newobj ));
6033- newobj -> long_value .lv_tag = tmp -> long_value .lv_tag & ~IMMORTALITY_BIT_MASK ;
6034- for (i = 0 ; i < n ; i ++ ) {
6035- newobj -> long_value .ob_digit [i ] = tmp -> long_value .ob_digit [i ];
6035+ if (_PyLong_IsCompact (tmp )) {
6036+ sign = _PyLong_CompactSign (tmp );
6037+ }
6038+ else {
6039+ sign = _PyLong_NonCompactSign (tmp );
60366040 }
6041+ _PyLong_InitTag (newobj );
6042+ _PyLong_SetSignAndDigitCount (newobj , sign , size );
6043+ memcpy (newobj -> long_value .ob_digit , tmp -> long_value .ob_digit ,
6044+ ndigits * sizeof (digit ));
60376045 Py_DECREF (tmp );
60386046 return (PyObject * )newobj ;
60396047}
0 commit comments