@@ -2489,6 +2489,43 @@ test_try_incref(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
24892489 Py_RETURN_NONE ;
24902490}
24912491
2492+ #if 0x030C0000 <= PY_VERSION_HEX && !defined(PYPY_VERSION )
2493+ static PyObject *
2494+ test_set_immortal (PyObject * Py_UNUSED (module ), PyObject * Py_UNUSED (args ))
2495+ {
2496+ PyObject object = {0 };
2497+ #ifdef Py_GIL_DISABLED
2498+ object .ob_tid = _Py_ThreadId ();
2499+ object .ob_gc_bits = 0 ;
2500+ object .ob_ref_local = 1 ;
2501+ object .ob_ref_shared = 0 ;
2502+ #else
2503+ object .ob_refcnt = 1 ;
2504+ #endif
2505+ object .ob_type = & PyBaseObject_Type ;
2506+
2507+ assert (!PyUnstable_IsImmortal (& object ));
2508+ int rc = PyUnstable_SetImmortal (& object );
2509+ assert (rc == 1 );
2510+ assert (PyUnstable_IsImmortal (& object ));
2511+ Py_DECREF (& object ); // should not dealloc
2512+ assert (PyUnstable_IsImmortal (& object ));
2513+
2514+ // Check already immortal object
2515+ rc = PyUnstable_SetImmortal (& object );
2516+ assert (rc == 0 );
2517+
2518+ // Check unicode objects
2519+ PyObject * unicode = PyUnicode_FromString ("test" );
2520+ assert (!PyUnstable_IsImmortal (unicode ));
2521+ rc = PyUnstable_SetImmortal (unicode );
2522+ assert (rc == 0 );
2523+ assert (!PyUnstable_IsImmortal (unicode ));
2524+ Py_DECREF (unicode );
2525+ Py_RETURN_NONE ;
2526+ }
2527+ #endif
2528+
24922529
24932530static struct PyMethodDef methods [] = {
24942531 {"test_object" , test_object , METH_NOARGS , _Py_NULL },
@@ -2546,6 +2583,9 @@ static struct PyMethodDef methods[] = {
25462583 {"test_byteswriter" , test_byteswriter , METH_NOARGS , _Py_NULL },
25472584 {"test_tuple" , test_tuple , METH_NOARGS , _Py_NULL },
25482585 {"test_try_incref" , test_try_incref , METH_NOARGS , _Py_NULL },
2586+ #ifdef 0x030C0000 <= PY_VERSION_HEX && !defined (PYPY_VERSION )
2587+ {"test_set_immortal" , test_set_immortal , METH_NOARGS , _Py_NULL },
2588+ #endif
25492589 {_Py_NULL , _Py_NULL , 0 , _Py_NULL }
25502590};
25512591
0 commit comments