Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
12e8774b
"git@developer.sourcefind.cn:gaoqiong/pybind11.git" did not exist on "1a432b426fb9588789450fc4e7a3bf16f9c5d7f1"
Commit
12e8774b
authored
Aug 19, 2019
by
kingofpayne
Committed by
Wenzel Jakob
Aug 19, 2019
Browse files
Added support for list insertion. (#1888)
parent
19189b4c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
11 additions
and
3 deletions
+11
-3
include/pybind11/pytypes.h
include/pybind11/pytypes.h
+4
-0
tests/test_pytypes.cpp
tests/test_pytypes.cpp
+2
-0
tests/test_pytypes.py
tests/test_pytypes.py
+5
-3
No files found.
include/pybind11/pytypes.h
View file @
12e8774b
...
...
@@ -1265,6 +1265,10 @@ public:
template
<
typename
T
>
void
append
(
T
&&
val
)
const
{
PyList_Append
(
m_ptr
,
detail
::
object_or_cast
(
std
::
forward
<
T
>
(
val
)).
ptr
());
}
template
<
typename
T
>
void
insert
(
size_t
index
,
T
&&
val
)
const
{
PyList_Insert
(
m_ptr
,
static_cast
<
ssize_t
>
(
index
),
detail
::
object_or_cast
(
std
::
forward
<
T
>
(
val
)).
ptr
());
}
};
class
args
:
public
tuple
{
PYBIND11_OBJECT_DEFAULT
(
args
,
tuple
,
PyTuple_Check
)
};
...
...
tests/test_pytypes.cpp
View file @
12e8774b
...
...
@@ -17,6 +17,8 @@ TEST_SUBMODULE(pytypes, m) {
list
.
append
(
"value"
);
py
::
print
(
"Entry at position 0:"
,
list
[
0
]);
list
[
0
]
=
py
::
str
(
"overwritten"
);
list
.
insert
(
0
,
"inserted-0"
);
list
.
insert
(
2
,
"inserted-2"
);
return
list
;
});
m
.
def
(
"print_list"
,
[](
py
::
list
list
)
{
...
...
tests/test_pytypes.py
View file @
12e8774b
...
...
@@ -9,14 +9,16 @@ from pybind11_tests import debug_enabled
def
test_list
(
capture
,
doc
):
with
capture
:
lst
=
m
.
get_list
()
assert
lst
==
[
"
overwritten
"
]
assert
lst
==
[
"
inserted-0"
,
"overwritten"
,
"inserted-2
"
]
lst
.
append
(
"value2"
)
m
.
print_list
(
lst
)
assert
capture
.
unordered
==
"""
Entry at position 0: value
list item 0: overwritten
list item 1: value2
list item 0: inserted-0
list item 1: overwritten
list item 2: inserted-2
list item 3: value2
"""
assert
doc
(
m
.
get_list
)
==
"get_list() -> list"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment