FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pybind11/tests/test_custom_type_setup.cpp at master · SimplexZhao/pybind11 · GitHub
SimplexZhao
/
pybind11
Public
forked from
pybind/pybind11
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pybind11
/
tests
/
test_custom_type_setup.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
104 lines (92 loc) · 3.9 KB
Breadcrumbs
pybind11
/
tests
/
test_custom_type_setup.cpp
Copy path
File metadata and controls
104 lines (92 loc) · 3.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
tests/test_custom_type_setup.cpp -- Tests `pybind11::custom_type_setup`
Copyright (c) Google LLC
All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE file.
*/
#
include
<
pybind11/detail/internals.h
>
#
include
<
pybind11/pybind11.h
>
#
include
"
pybind11_tests.h
"
#
include
<
vector
>
namespace
py
=
pybind11;
namespace
{
struct
ContainerOwnsPythonObjects
{
std::vector<py::object> list;
void
append
(
const
py::object &obj) { list.
emplace_back
(obj); }
py::object
at
(py::
ssize_t
index)
const
{
if
(index >=
size
() || index <
0
) {
throw
py::index_error
(
"
Index out of range
"
);
}
return
list.
at
(
py::size_t
(index));
}
py::
ssize_t
size
()
const
{
return
py::ssize_t_cast
(list.
size
()); }
void
clear
() { list.
clear
(); }
};
void
add_gc_checkers_with_weakrefs
(
const
py::object &obj) {
py::handle global_capsule =
py::detail::get_internals_capsule
();
if
(!global_capsule) {
throw
std::runtime_error
(
"
No global internals capsule found
"
);
}
(
void
)
py::weakref
(obj,
py::cpp_function
([global_capsule](py::handle weakref) ->
void
{
py::handle current_global_capsule =
py::detail::get_internals_capsule
();
if
(!current_global_capsule.
is
(global_capsule)) {
throw
std::runtime_error
(
"
Global internals capsule was destroyed prematurely
"
);
}
weakref.
dec_ref
();
}))
.
release
();
py::handle local_capsule =
py::detail::get_local_internals_capsule
();
if
(!local_capsule) {
throw
std::runtime_error
(
"
No local internals capsule found
"
);
}
(
void
)
py::weakref
(
obj,
py::cpp_function
([local_capsule](py::handle weakref) ->
void
{
py::handle current_local_capsule =
py::detail::get_local_internals_capsule
();
if
(!current_local_capsule.
is
(local_capsule)) {
throw
std::runtime_error
(
"
Local internals capsule was destroyed prematurely
"
);
}
weakref.
dec_ref
();
}))
.
release
();
}
}
//
namespace
TEST_SUBMODULE
(custom_type_setup, m) {
py::class_<ContainerOwnsPythonObjects>
cls
(
m,
"
ContainerOwnsPythonObjects
"
,
//
Please review/update docs/advanced/classes.rst after making changes here.
py::custom_type_setup
([](PyHeapTypeObject *heap_type) {
auto
*type = &heap_type->
ht_type
;
type->
tp_flags
|= Py_TPFLAGS_HAVE_GC;
type->
tp_traverse
= [](PyObject *self_base, visitproc visit,
void
*arg) {
//
https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_traverse
#
if
PY_VERSION_HEX >= 0x03090000
Py_VISIT
(
Py_TYPE
(self_base));
#
endif
if
(
py::detail::is_holder_constructed
(self_base)) {
auto
&self = py::cast<ContainerOwnsPythonObjects &>(
py::handle
(self_base));
for
(
auto
&item : self.
list
) {
Py_VISIT
(item.
ptr
());
}
}
return
0
;
};
type->
tp_clear
= [](PyObject *self_base) {
if
(
py::detail::is_holder_constructed
(self_base)) {
auto
&self = py::cast<ContainerOwnsPythonObjects &>(
py::handle
(self_base));
for
(
auto
&item : self.
list
) {
Py_CLEAR
(item.
ptr
());
}
self.
list
.
clear
();
}
return
0
;
};
}));
cls.
def
(py::init<>());
cls.
def
(
"
append
"
, &ContainerOwnsPythonObjects::append);
cls.
def
(
"
at
"
, &ContainerOwnsPythonObjects::at);
cls.
def
(
"
size
"
, &ContainerOwnsPythonObjects::size);
cls.
def
(
"
clear
"
, &ContainerOwnsPythonObjects::clear);
m.
def
(
"
add_gc_checkers_with_weakrefs
"
, &add_gc_checkers_with_weakrefs);
}
Back
|
FazBrowse Home
|
New Git URL