@@ -84,9 +84,9 @@ class autoclass_impl {
8484private:
8585 std::vector<PyType_Slot> m_slots;
8686 std::unique_ptr<detail::autoclass_storage> m_storage;
87- py::scoped_ref <PyTypeObject> m_type;
87+ py::owned_ref <PyTypeObject> m_type;
8888 PyType_Spec m_spec;
89- py::scoped_ref <PyTypeObject> m_py_basetype;
89+ py::owned_ref <PyTypeObject> m_py_basetype;
9090
9191 /* * Check if this type uses the `Py_TPFLAGS_HAVE_GC`, which requires that we implement
9292 at least `Py_tp_traverse`, and will use `PyObject_GC_New` and `PyObject_GC_Del`.
@@ -243,12 +243,12 @@ class autoclass_impl {
243243
244244 @return The already created type, or `nullptr` if the type wasn't yet created.
245245 */
246- static py::scoped_ref <PyTypeObject> lookup_type () {
246+ static py::owned_ref <PyTypeObject> lookup_type () {
247247 auto type_search = detail::autoclass_type_cache.get ().find (typeid (T));
248248 if (type_search != detail::autoclass_type_cache.get ().end ()) {
249249 PyTypeObject* type = type_search->second ->type ;
250250 Py_INCREF (type);
251- return py::scoped_ref (type);
251+ return py::owned_ref (type);
252252 }
253253 return nullptr ;
254254 }
@@ -262,7 +262,7 @@ class autoclass_impl {
262262 Python wrapped version of `T`, or nullptr on failure.
263263 */
264264 template <typename ... Args>
265- static std::tuple<py::scoped_ref <PyTypeObject>, py::scoped_ref <>>
265+ static std::tuple<py::owned_ref <PyTypeObject>, py::owned_ref <>>
266266 construct_with_type (Args&&... args) {
267267 auto cls = lookup_type ();
268268 if (!cls) {
@@ -280,7 +280,7 @@ class autoclass_impl {
280280 out = constructor_new_impl<false >(cls.get (), std::forward<Args>(args)...);
281281 }
282282
283- return {std::move (cls), py::scoped_ref (out)};
283+ return {std::move (cls), py::owned_ref (out)};
284284 }
285285
286286public:
@@ -291,7 +291,7 @@ class autoclass_impl {
291291 @return A new reference to a Python wrapped version of `T`, or nullptr on failure.
292292 */
293293 template <typename ... Args>
294- static py::scoped_ref <> construct (Args&&... args) {
294+ static py::owned_ref <> construct (Args&&... args) {
295295 auto [cls, ob] = construct_with_type (std::forward<Args>(args)...);
296296 return ob;
297297 }
@@ -344,7 +344,7 @@ class autoclass_impl {
344344 0 ,
345345 flags (extra_flags, base_type),
346346 nullptr }),
347- m_py_basetype(py::scoped_ref <PyTypeObject>::xnew_reference(base_type)) {
347+ m_py_basetype(py::owned_ref <PyTypeObject>::xnew_reference(base_type)) {
348348 if (base_type) {
349349 // Check to make sure that the static base type is not obviously
350350 // wrong. This check does not ensure that the static base type is
@@ -1091,11 +1091,11 @@ class autoclass_impl {
10911091 using end_type = decltype (std::declval<T>().end ());
10921092
10931093 struct iter {
1094- scoped_ref <> iterable;
1094+ owned_ref <> iterable;
10951095 begin_type it;
10961096 end_type end;
10971097
1098- iter (const scoped_ref <>& iterable, begin_type it, end_type end)
1098+ iter (const owned_ref <>& iterable, begin_type it, end_type end)
10991099 : iterable(iterable), it(it), end(end) {}
11001100
11011101 int traverse (visitproc visit, void * arg) {
@@ -1108,7 +1108,7 @@ class autoclass_impl {
11081108 try {
11091109 iter& unboxed = autoclass<iter>::unbox (self);
11101110 if (unboxed.it != unboxed.end ) {
1111- py::scoped_ref out = py::to_object (*unboxed.it );
1111+ py::owned_ref out = py::to_object (*unboxed.it );
11121112 ++unboxed.it ;
11131113 return std::move (out).escape ();
11141114 }
@@ -1133,15 +1133,15 @@ class autoclass_impl {
11331133
11341134 return [](PyObject* self) -> PyObject* {
11351135 try {
1136- py::scoped_ref <PyTypeObject> cls = autoclass<iter>::lookup_type ();
1136+ py::owned_ref <PyTypeObject> cls = autoclass<iter>::lookup_type ();
11371137 if (!cls) {
11381138 py::raise (PyExc_RuntimeError)
11391139 << " no iterator type found for " << util::type_name<T>();
11401140 return nullptr ;
11411141 }
11421142
11431143 Py_INCREF (self);
1144- py::scoped_ref self_ref (self);
1144+ py::owned_ref self_ref (self);
11451145 return autoclass<iter>::template constructor_new_impl<true >(
11461146 cls.get (), self_ref, unbox (self).begin (), unbox (self).end ());
11471147 }
@@ -1306,7 +1306,7 @@ class autoclass_impl {
13061306 @note If an exception is thrown, the state of the `autoclass` object is
13071307 unspecified.
13081308 */
1309- scoped_ref <PyTypeObject> type () {
1309+ owned_ref <PyTypeObject> type () {
13101310 if (m_type) {
13111311 return m_type;
13121312 }
@@ -1375,7 +1375,7 @@ class autoclass_impl {
13751375 finalize_slots ();
13761376 m_spec.slots = m_slots.data ();
13771377
1378- py::scoped_ref type (reinterpret_cast <PyTypeObject*>(PyType_FromSpec (&m_spec)));
1378+ py::owned_ref type (reinterpret_cast <PyTypeObject*>(PyType_FromSpec (&m_spec)));
13791379 if (!type) {
13801380 throw py::exception{};
13811381 }
@@ -1392,15 +1392,15 @@ class autoclass_impl {
13921392 // `PyCFunctionObject` has a pointer to the input `PyMethodDef` which is why
13931393 // we need to store the methoddef on the type cache storage itself.
13941394 storage->callback_method = automethod<cache_cleanup>(" cache_cleanup" );
1395- py::scoped_ref callback_func (
1395+ py::owned_ref callback_func (
13961396 PyCFunction_NewEx (&storage->callback_method , nullptr , nullptr ));
13971397 if (!callback_func) {
13981398 throw py::exception{};
13991399 }
14001400 // Create a weakref that calls `callback_func` (A Python function) when `type`
14011401 // dies. This will take a reference to `callback_func`, and after we leave this
14021402 // scope, it will be the sole owner of that function.
1403- storage->cleanup_wr = py::scoped_ref (
1403+ storage->cleanup_wr = py::owned_ref (
14041404 PyWeakref_NewRef (static_cast <PyObject*>(type), callback_func.get ()));
14051405 if (!storage->cleanup_wr ) {
14061406 throw py::exception{};
@@ -1427,7 +1427,7 @@ class autoclass_impl {
14271427 class to_object {
14281428 template <typename U>
14291429 static PyObject* f (U&& value) {
1430- py::scoped_ref <PyTypeObject> cls = lookup_type ();
1430+ py::owned_ref <PyTypeObject> cls = lookup_type ();
14311431 if (!cls) {
14321432 py::raise (PyExc_RuntimeError) << " autoclass type wasn't initialized yet" ;
14331433 return nullptr ;
@@ -1456,7 +1456,7 @@ class autoclass_impl {
14561456 you can write the following:
14571457
14581458 \code
1459- py::scoped_ref <PyTypeObject> t = py::autoclass<my_type>("modname.PythonName").type();
1459+ py::owned_ref <PyTypeObject> t = py::autoclass<my_type>("modname.PythonName").type();
14601460 \endcode
14611461
14621462 The resulting type will have a `__name__` of "PythonName", and a
@@ -1469,7 +1469,7 @@ class autoclass_impl {
14691469 accepts an int and a double:
14701470
14711471 \code
1472- py::scoped_ref <PyTypeObject> t = py::autoclass<my_type>("modname.PythonName")
1472+ py::owned_ref <PyTypeObject> t = py::autoclass<my_type>("modname.PythonName")
14731473 .new_<int, double>()
14741474 .type();
14751475 \endcode
@@ -1485,7 +1485,7 @@ class autoclass_impl {
14851485 as a Python method named `bar`:
14861486
14871487 \code
1488- py::scoped_ref <PyTypeObject> t = py::autoclass<my_type>("modname.PythonName")
1488+ py::owned_ref <PyTypeObject> t = py::autoclass<my_type>("modname.PythonName")
14891489 .new_<int, double>()
14901490 .def<&my_type::foo>("bar")
14911491 .type();
@@ -1611,7 +1611,7 @@ void initialize_interface_type(typename object::base* ptr) {
16111611 `py::autoclass_interface`:
16121612
16131613 \code
1614- py::scoped_ref interface_pytype =
1614+ py::owned_ref interface_pytype =
16151615 py::autoclass_interface<I>("Interface").type();
16161616 \endcode
16171617
@@ -1625,7 +1625,7 @@ void initialize_interface_type(typename object::base* ptr) {
16251625 interface. Adding methods is the same as `py::automethod`:
16261626
16271627 \code
1628- py::scoped_ref interface_pytype =
1628+ py::owned_ref interface_pytype =
16291629 py::autoclass_interface<I>("Interface")
16301630 .def<&interface::f>("f")
16311631 .type();
@@ -1638,12 +1638,12 @@ void initialize_interface_type(typename object::base* ptr) {
16381638 `py::autoclass_interface_instance`. For example:
16391639
16401640 \code
1641- py::scoped_ref concrete_a_pytype =
1641+ py::owned_ref concrete_a_pytype =
16421642 py::autoclass_interface_instance<concrete_a, interface>("ConcreteA")
16431643 .new_()
16441644 .type();
16451645
1646- py::scoped_ref concrete_b_pytype =
1646+ py::owned_ref concrete_b_pytype =
16471647 py::autoclass_interface_instance<concrete_b, interface>("ConcreteB")
16481648 .new_<std::string_view>()
16491649 .def<&concrete_b::non_interface_method>("non_interface_method")
@@ -1760,7 +1760,7 @@ struct autoclass_interface_instance final
17601760 static_assert (std::is_base_of_v<I, T>, " interface type is not a base of T" );
17611761
17621762private:
1763- static py::scoped_ref <PyTypeObject> resolve_pybase () {
1763+ static py::owned_ref <PyTypeObject> resolve_pybase () {
17641764 auto res = py::autoclass_interface<I>::lookup_type ();
17651765 if (!res) {
17661766 throw std::runtime_error{
0 commit comments