@@ -75,6 +75,15 @@ const char *cpp_std() {
75
75
#endif
76
76
}
77
77
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
+
78
87
PYBIND11_MODULE (pybind11_tests, m, py::mod_gil_not_used()) {
79
88
m.doc () = " pybind11 test module" ;
80
89
@@ -90,7 +99,6 @@ PYBIND11_MODULE(pybind11_tests, m, py::mod_gil_not_used()) {
90
99
m.attr (" cpp_std" ) = cpp_std ();
91
100
m.attr (" PYBIND11_INTERNALS_ID" ) = PYBIND11_INTERNALS_ID;
92
101
// Free threaded Python uses UINT32_MAX for immortal objects.
93
- m.attr (" PYBIND11_REFCNT_IMMORTAL" ) = UINT32_MAX;
94
102
m.attr (" PYBIND11_SIMPLE_GIL_MANAGEMENT" ) =
95
103
#if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
96
104
true ;
@@ -106,6 +114,8 @@ PYBIND11_MODULE(pybind11_tests, m, py::mod_gil_not_used()) {
106
114
m.attr (" detailed_error_messages_enabled" ) = false ;
107
115
#endif
108
116
117
+ m.def (" is_immortal" , &is_immortal, " Returns true if Python supports it and the object is immortal" );
118
+
109
119
py::class_<UserType>(m, " UserType" , " A `py::class_` type for testing" )
110
120
.def (py::init<>())
111
121
.def (py::init<int >())
0 commit comments