@@ -3651,32 +3651,25 @@ long_richcompare(PyObject *self, PyObject *other, int op)
36513651}
36523652
36533653static inline int
3654- compact_int_is_small (PyObject * self )
3654+ /// Return 1 if the object is one of the immortal small ints
3655+ _long_is_small_int (PyObject * op )
36553656{
3656- PyLongObject * pylong = (PyLongObject * )self ;
3657- assert (_PyLong_IsCompact (pylong ));
3658- stwodigits ival = medium_value (pylong );
3659- if (IS_SMALL_INT (ival )) {
3660- PyLongObject * small_pylong = (PyLongObject * )get_small_int ((sdigit )ival );
3661- if (pylong == small_pylong ) {
3662- return 1 ;
3663- }
3664- }
3665- return 0 ;
3657+ PyLongObject * long_object = (PyLongObject * )op ;
3658+ int is_small_int = (long_object -> long_value .lv_tag & IMMORTALITY_BIT_MASK ) != 0 ;
3659+ assert ((!is_small_int ) || PyLong_CheckExact (op ));
3660+ return is_small_int ;
36663661}
36673662
36683663void
36693664_PyLong_ExactDealloc (PyObject * self )
36703665{
36713666 assert (PyLong_CheckExact (self ));
3667+ if (_long_is_small_int (self )) {
3668+ // See PEP 683, section Accidental De-Immortalizing for details
3669+ _Py_SetImmortal (self );
3670+ return ;
3671+ }
36723672 if (_PyLong_IsCompact ((PyLongObject * )self )) {
3673- #ifndef Py_GIL_DISABLED
3674- if (compact_int_is_small (self )) {
3675- // See PEP 683, section Accidental De-Immortalizing for details
3676- _Py_SetImmortal (self );
3677- return ;
3678- }
3679- #endif
36803673 _Py_FREELIST_FREE (ints , self , PyObject_Free );
36813674 return ;
36823675 }
@@ -3686,24 +3679,20 @@ _PyLong_ExactDealloc(PyObject *self)
36863679static void
36873680long_dealloc (PyObject * self )
36883681{
3689- assert (self );
3690- if (_PyLong_IsCompact ((PyLongObject * )self )) {
3691- if (compact_int_is_small (self )) {
3692- /* This should never get called, but we also don't want to SEGV if
3693- * we accidentally decref small Ints out of existence. Instead,
3694- * since small Ints are immortal, re-set the reference count.
3695- *
3696- * See PEP 683, section Accidental De-Immortalizing for details
3697- */
3698- _Py_SetImmortal (self );
3699- return ;
3700- }
3701- if (PyLong_CheckExact (self )) {
3702- _Py_FREELIST_FREE (ints , self , PyObject_Free );
3703- return ;
3704- }
3682+ if (_long_is_small_int (self )) {
3683+ /* This should never get called, but we also don't want to SEGV if
3684+ * we accidentally decref small Ints out of existence. Instead,
3685+ * since small Ints are immortal, re-set the reference count.
3686+ *
3687+ * See PEP 683, section Accidental De-Immortalizing for details
3688+ */
3689+ _Py_SetImmortal (self );
3690+ return ;
3691+ }
3692+ if (PyLong_CheckExact (self ) && _PyLong_IsCompact ((PyLongObject * )self )) {
3693+ _Py_FREELIST_FREE (ints , self , PyObject_Free );
3694+ return ;
37053695 }
3706-
37073696 Py_TYPE (self )-> tp_free (self );
37083697}
37093698
@@ -6065,7 +6054,7 @@ long_subtype_new(PyTypeObject *type, PyObject *x, PyObject *obase)
60656054 return NULL ;
60666055 }
60676056 assert (PyLong_Check (newobj ));
6068- newobj -> long_value .lv_tag = tmp -> long_value .lv_tag ;
6057+ newobj -> long_value .lv_tag = tmp -> long_value .lv_tag & ~ IMMORTALITY_BIT_MASK ;
60696058 for (i = 0 ; i < n ; i ++ ) {
60706059 newobj -> long_value .ob_digit [i ] = tmp -> long_value .ob_digit [i ];
60716060 }
0 commit comments