@@ -75,6 +75,15 @@ const char *cpp_std() {
7575#endif
7676}
7777
78+ bool is_immortal (py::handle object) {
79+ // If Python doesn't support immortal objects, this returns False
80+ #if PY_VERSION_HEX >= 0x030E0000
81+ return PyUnstable_IsImmortal (object.ptr ()) != 0 ;
82+ #else
83+ return Py_REFCNT (object.ptr ()) == UINT32_MAX ;
84+ #endif
85+ }
86+
7887PYBIND11_MODULE (pybind11_tests, m, py::mod_gil_not_used()) {
7988 m.doc () = " pybind11 test module" ;
8089
@@ -90,7 +99,6 @@ PYBIND11_MODULE(pybind11_tests, m, py::mod_gil_not_used()) {
9099 m.attr (" cpp_std" ) = cpp_std ();
91100 m.attr (" PYBIND11_INTERNALS_ID" ) = PYBIND11_INTERNALS_ID ;
92101 // Free threaded Python uses UINT32_MAX for immortal objects.
93- m.attr (" PYBIND11_REFCNT_IMMORTAL" ) = UINT32_MAX ;
94102 m.attr (" PYBIND11_SIMPLE_GIL_MANAGEMENT" ) =
95103#if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
96104 true ;
@@ -106,6 +114,8 @@ PYBIND11_MODULE(pybind11_tests, m, py::mod_gil_not_used()) {
106114 m.attr (" detailed_error_messages_enabled" ) = false ;
107115#endif
108116
117+ m.def (" is_immortal" , &is_immortal, " Returns true if Python supports it and the object is immortal" );
118+
109119 py::class_<UserType>(m, " UserType" , " A `py::class_` type for testing" )
110120 .def (py::init<>())
111121 .def (py::init<int >())
0 commit comments