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
d2afe7f0
Commit
d2afe7f0
authored
Oct 12, 2016
by
Pim Schellart
Browse files
Accept any sequence type as std::vector (or std::list)
parent
26df8523
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
6 deletions
+40
-6
include/pybind11/pytypes.h
include/pybind11/pytypes.h
+26
-0
include/pybind11/stl.h
include/pybind11/stl.h
+6
-6
tests/test_python_types.py
tests/test_python_types.py
+8
-0
No files found.
include/pybind11/pytypes.h
View file @
d2afe7f0
...
...
@@ -29,12 +29,14 @@ namespace accessor_policies {
struct
obj_attr
;
struct
str_attr
;
struct
generic_item
;
struct
sequence_item
;
struct
list_item
;
struct
tuple_item
;
}
using
obj_attr_accessor
=
accessor
<
accessor_policies
::
obj_attr
>
;
using
str_attr_accessor
=
accessor
<
accessor_policies
::
str_attr
>
;
using
item_accessor
=
accessor
<
accessor_policies
::
generic_item
>
;
using
sequence_accessor
=
accessor
<
accessor_policies
::
sequence_item
>
;
using
list_accessor
=
accessor
<
accessor_policies
::
list_item
>
;
using
tuple_accessor
=
accessor
<
accessor_policies
::
tuple_item
>
;
...
...
@@ -261,6 +263,23 @@ struct generic_item {
}
};
struct
sequence_item
{
using
key_type
=
size_t
;
static
object
get
(
handle
obj
,
size_t
index
)
{
PyObject
*
result
=
PySequence_GetItem
(
obj
.
ptr
(),
static_cast
<
ssize_t
>
(
index
));
if
(
!
result
)
{
throw
error_already_set
();
}
return
{
result
,
true
};
}
static
void
set
(
handle
obj
,
size_t
index
,
handle
val
)
{
// PySequence_SetItem does not steal a reference to 'val'
if
(
PySequence_SetItem
(
obj
.
ptr
(),
static_cast
<
ssize_t
>
(
index
),
val
.
ptr
())
!=
0
)
{
throw
error_already_set
();
}
}
};
struct
list_item
{
using
key_type
=
size_t
;
...
...
@@ -673,6 +692,13 @@ public:
bool
contains
(
const
char
*
key
)
const
{
return
PyDict_Contains
(
ptr
(),
pybind11
::
str
(
key
).
ptr
())
==
1
;
}
};
class
sequence
:
public
object
{
public:
PYBIND11_OBJECT
(
sequence
,
object
,
PySequence_Check
)
size_t
size
()
const
{
return
(
size_t
)
PySequence_Size
(
m_ptr
);
}
detail
::
sequence_accessor
operator
[](
size_t
index
)
const
{
return
{
*
this
,
index
};
}
};
class
list
:
public
object
{
public:
PYBIND11_OBJECT
(
list
,
object
,
PyList_Check
)
...
...
include/pybind11/stl.h
View file @
d2afe7f0
...
...
@@ -97,13 +97,13 @@ template <typename Type, typename Value> struct list_caster {
using
value_conv
=
make_caster
<
Value
>
;
bool
load
(
handle
src
,
bool
convert
)
{
list
l
(
src
,
true
);
if
(
!
l
.
check
())
sequence
s
(
src
,
true
);
if
(
!
s
.
check
())
return
false
;
value_conv
conv
;
value
.
clear
();
reserve_maybe
(
l
,
&
value
);
for
(
auto
it
:
l
)
{
reserve_maybe
(
s
,
&
value
);
for
(
auto
it
:
s
)
{
if
(
!
conv
.
load
(
it
,
convert
))
return
false
;
value
.
push_back
((
Value
)
conv
);
...
...
@@ -113,8 +113,8 @@ template <typename Type, typename Value> struct list_caster {
template
<
typename
T
=
Type
,
enable_if_t
<
std
::
is_same
<
decltype
(
std
::
declval
<
T
>().
reserve
(
0
)),
void
>::
value
,
int
>
=
0
>
void
reserve_maybe
(
list
l
,
Type
*
)
{
value
.
reserve
(
l
.
size
());
}
void
reserve_maybe
(
list
,
void
*
)
{
}
void
reserve_maybe
(
sequence
s
,
Type
*
)
{
value
.
reserve
(
s
.
size
());
}
void
reserve_maybe
(
sequence
,
void
*
)
{
}
static
handle
cast
(
const
Type
&
src
,
return_value_policy
policy
,
handle
parent
)
{
list
l
(
src
.
size
());
...
...
tests/test_python_types.py
View file @
d2afe7f0
...
...
@@ -71,6 +71,14 @@ def test_instance(capture):
list item 0: value
list item 1: value2
"""
with
capture
:
list_result
=
instance
.
get_list_2
()
list_result
.
append
(
'value2'
)
instance
.
print_list_2
(
tuple
(
list_result
))
assert
capture
.
unordered
==
"""
list item 0: value
list item 1: value2
"""
array_result
=
instance
.
get_array
()
assert
array_result
==
[
'array entry 1'
,
'array entry 2'
]
with
capture
:
...
...
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