Skip to content

Commit b5f5a83

Browse files
author
gerrymanoim
authored
MAINT rename scoped_ref to owned_ref to better match cpython terms (#146)
1 parent 06302c6 commit b5f5a83

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+454
-453
lines changed

include/libpy/any.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ struct any_vtable_impl {
3333
void (*destruct)(void* addr);
3434
bool (*ne)(const void* lhs, const void* rhs);
3535
bool (*eq)(const void* lhs, const void* rhs);
36-
py::scoped_ref<> (*to_object)(const void* addr);
37-
py::scoped_ref<PyArray_Descr> (*new_dtype)();
36+
py::owned_ref<> (*to_object)(const void* addr);
37+
py::owned_ref<PyArray_Descr> (*new_dtype)();
3838
std::ostream& (*ostream_format)(std::ostream& stream, const void* addr);
3939
std::string (*type_name)();
4040
};
@@ -86,7 +86,7 @@ inline constexpr any_vtable_impl any_vtable_instance = {
8686
[](const void* lhs, const void* rhs) -> bool {
8787
return *static_cast<const T*>(lhs) == *static_cast<const T*>(rhs);
8888
},
89-
[]([[maybe_unused]] const void* addr) -> scoped_ref<> {
89+
[]([[maybe_unused]] const void* addr) -> owned_ref<> {
9090
if constexpr (py::has_to_object<T>) {
9191
return py::to_object(*static_cast<const T*>(addr));
9292
}
@@ -97,7 +97,7 @@ inline constexpr any_vtable_impl any_vtable_instance = {
9797
" into Python object");
9898
}
9999
},
100-
[]() -> scoped_ref<PyArray_Descr> {
100+
[]() -> owned_ref<PyArray_Descr> {
101101
if constexpr (py::has_new_dtype<T>) {
102102
return py::new_dtype<T>();
103103
}
@@ -145,8 +145,8 @@ inline constexpr any_vtable_impl any_vtable_instance<void> = {
145145
[](void*) { void_vtable(); },
146146
[](const void*, const void*) -> bool { void_vtable(); },
147147
[](const void*, const void*) -> bool { void_vtable(); },
148-
[](const void*) -> scoped_ref<> { void_vtable(); },
149-
[]() -> scoped_ref<PyArray_Descr> { void_vtable(); },
148+
[](const void*) -> owned_ref<> { void_vtable(); },
149+
[]() -> owned_ref<PyArray_Descr> { void_vtable(); },
150150
[](std::ostream&, const void*) -> std::ostream& { void_vtable(); },
151151
[]() { return py::util::type_name<void>(); },
152152
};
@@ -255,11 +255,11 @@ class any_vtable {
255255
return m_impl->eq(lhs, rhs);
256256
}
257257

258-
inline scoped_ref<> to_object(const void* addr) const {
258+
inline owned_ref<> to_object(const void* addr) const {
259259
return m_impl->to_object(addr);
260260
}
261261

262-
inline scoped_ref<PyArray_Descr> new_dtype() const {
262+
inline owned_ref<PyArray_Descr> new_dtype() const {
263263
return m_impl->new_dtype();
264264
}
265265

@@ -574,14 +574,14 @@ namespace dispatch {
574574
*/
575575
template<>
576576
struct to_object<py::any_ref> {
577-
static py::scoped_ref<> f(const py::any_ref& ref) {
577+
static py::owned_ref<> f(const py::any_ref& ref) {
578578
return ref.vtable().to_object(ref.addr());
579579
}
580580
};
581581

582582
template<>
583583
struct to_object<py::any_cref> {
584-
static py::scoped_ref<> f(const py::any_cref& ref) {
584+
static py::owned_ref<> f(const py::any_cref& ref) {
585585
return ref.vtable().to_object(ref.addr());
586586
}
587587
};
@@ -675,7 +675,7 @@ dtype_to_vtable(py::borrowed_ref<PyArray_Descr> dtype) {
675675
throw exception(PyExc_TypeError, "unknown datetime unit: ", unit);
676676
}
677677
case NPY_OBJECT:
678-
return any_vtable::make<scoped_ref<>>();
678+
return any_vtable::make<owned_ref<>>();
679679
case NPY_STRING:
680680
return detail::make_string_vtable(dtype->elsize);
681681
}

include/libpy/any_vector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ class any_vector {
612612
};
613613

614614

615-
inline scoped_ref<> move_to_numpy_array(py::any_vector&& values) {
615+
inline owned_ref<> move_to_numpy_array(py::any_vector&& values) {
616616

617617
auto descr = values.vtable().new_dtype();
618618
if (!descr) {

include/libpy/autoclass.h

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ class autoclass_impl {
8484
private:
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

286286
public:
@@ -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

17621762
private:
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{

include/libpy/automethod.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ struct argument_parser {
482482

483483
// flatten the args + kwargs into a single tuple to pass to
484484
// `positional_args`
485-
py::scoped_ref<> flat(PyTuple_New(arity));
485+
py::owned_ref<> flat(PyTuple_New(arity));
486486
if (!flat) {
487487
throw py::exception{};
488488
}

include/libpy/borrowed_ref.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44

55
namespace py {
66
template<typename T>
7-
class scoped_ref;
7+
class owned_ref;
88

99
/** A type that explicitly indicates that a Python object is a borrowed
1010
reference. This is implicitly convertible from a regular `PyObject*` or a
11-
`py::scoped_ref`. This type may be used as a Python object parameter like:
11+
`py::owned_ref`. This type may be used as a Python object parameter like:
1212
1313
\code
1414
int f(borrowed_ref a, borrowed_ref b);
1515
\endcode
1616
17-
This allows calling this function with either `py::scoped_ref` or
17+
This allows calling this function with either `py::owned_ref` or
1818
`PyObject*`.
1919
2020
@note A `borrowed_ref` may still hold a value of `nullptr`.
@@ -28,7 +28,7 @@ class borrowed_ref {
2828
constexpr borrowed_ref() : m_ref(nullptr) {}
2929
constexpr borrowed_ref(std::nullptr_t) : m_ref(nullptr) {}
3030
constexpr borrowed_ref(T* ref) : m_ref(ref) {}
31-
constexpr borrowed_ref(const py::scoped_ref<T>& ref) : m_ref(ref.get()) {}
31+
constexpr borrowed_ref(const py::owned_ref<T>& ref) : m_ref(ref.get()) {}
3232

3333
constexpr borrowed_ref(const borrowed_ref&) = default;
3434
constexpr borrowed_ref& operator=(const borrowed_ref& ob) = default;

include/libpy/build_tuple.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#pragma once
22

3-
#include "libpy/scoped_ref.h"
3+
#include "libpy/owned_ref.h"
44
#include "libpy/to_object.h"
55

66
namespace py {
77
template<typename... Args>
8-
py::scoped_ref<> build_tuple(const Args&... args) {
9-
py::scoped_ref out(PyTuple_New(sizeof...(args)));
8+
py::owned_ref<> build_tuple(const Args&... args) {
9+
py::owned_ref out(PyTuple_New(sizeof...(args)));
1010
if (!out) {
1111
return nullptr;
1212
}

0 commit comments

Comments
 (0)