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
15a112f8
Commit
15a112f8
authored
Aug 30, 2016
by
Dean Moldovan
Browse files
Add py::dict() keyword constructor
parent
66aa2728
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
26 additions
and
16 deletions
+26
-16
include/pybind11/cast.h
include/pybind11/cast.h
+4
-0
include/pybind11/pytypes.h
include/pybind11/pytypes.h
+3
-0
tests/test_callbacks.cpp
tests/test_callbacks.cpp
+7
-16
tests/test_python_types.cpp
tests/test_python_types.cpp
+6
-0
tests/test_python_types.py
tests/test_python_types.py
+6
-0
No files found.
include/pybind11/cast.h
View file @
15a112f8
...
...
@@ -1148,6 +1148,10 @@ inline object handle::operator()(detail::args_proxy args, detail::kwargs_proxy k
return
result
;
}
template
<
typename
...
Args
,
typename
/*SFINAE*/
>
dict
::
dict
(
Args
&&
...
args
)
:
dict
(
detail
::
unpacking_collector
<>
(
std
::
forward
<
Args
>
(
args
)...).
kwargs
())
{
}
#define PYBIND11_MAKE_OPAQUE(Type) \
namespace pybind11 { namespace detail { \
template<> class type_caster<Type> : public type_caster_base<Type> { }; \
...
...
include/pybind11/pytypes.h
View file @
15a112f8
...
...
@@ -589,6 +589,9 @@ public:
dict
()
:
object
(
PyDict_New
(),
false
)
{
if
(
!
m_ptr
)
pybind11_fail
(
"Could not allocate dict object!"
);
}
template
<
typename
...
Args
,
typename
=
detail
::
enable_if_t
<
detail
::
all_of_t
<
detail
::
is_keyword_or_ds
,
Args
...>
::
value
>>
dict
(
Args
&&
...
args
);
size_t
size
()
const
{
return
(
size_t
)
PyDict_Size
(
m_ptr
);
}
detail
::
dict_iterator
begin
()
const
{
return
(
++
detail
::
dict_iterator
(
*
this
,
0
));
}
detail
::
dict_iterator
end
()
const
{
return
detail
::
dict_iterator
();
}
...
...
tests/test_callbacks.cpp
View file @
15a112f8
...
...
@@ -89,12 +89,9 @@ test_initializer callbacks([](py::module &m) {
});
m
.
def
(
"test_dict_unpacking"
,
[](
py
::
function
f
)
{
auto
d1
=
py
::
dict
();
d1
[
"key"
]
=
py
::
cast
(
"value"
);
d1
[
"a"
]
=
py
::
cast
(
1
);
auto
d1
=
py
::
dict
(
"key"
_a
=
"value"
,
"a"
_a
=
1
);
auto
d2
=
py
::
dict
();
auto
d3
=
py
::
dict
();
d3
[
"b"
]
=
py
::
cast
(
2
);
auto
d3
=
py
::
dict
(
"b"
_a
=
2
);
return
f
(
"positional"
,
1
,
**
d1
,
**
d2
,
**
d3
);
});
...
...
@@ -104,30 +101,24 @@ test_initializer callbacks([](py::module &m) {
m
.
def
(
"test_unpacking_and_keywords1"
,
[](
py
::
function
f
)
{
auto
args
=
py
::
make_tuple
(
2
);
auto
kwargs
=
py
::
dict
();
kwargs
[
"d"
]
=
py
::
cast
(
4
);
auto
kwargs
=
py
::
dict
(
"d"
_a
=
4
);
return
f
(
1
,
*
args
,
"c"
_a
=
3
,
**
kwargs
);
});
m
.
def
(
"test_unpacking_and_keywords2"
,
[](
py
::
function
f
)
{
auto
kwargs1
=
py
::
dict
();
kwargs1
[
"a"
]
=
py
::
cast
(
1
);
auto
kwargs2
=
py
::
dict
();
kwargs2
[
"c"
]
=
py
::
cast
(
3
);
kwargs2
[
"d"
]
=
py
::
cast
(
4
);
auto
kwargs1
=
py
::
dict
(
"a"
_a
=
1
);
auto
kwargs2
=
py
::
dict
(
"c"
_a
=
3
,
"d"
_a
=
4
);
return
f
(
"positional"
,
*
py
::
make_tuple
(
1
),
2
,
*
py
::
make_tuple
(
3
,
4
),
5
,
"key"
_a
=
"value"
,
**
kwargs1
,
"b"
_a
=
2
,
**
kwargs2
,
"e"
_a
=
5
);
});
m
.
def
(
"test_unpacking_error1"
,
[](
py
::
function
f
)
{
auto
kwargs
=
py
::
dict
();
kwargs
[
"x"
]
=
py
::
cast
(
3
);
auto
kwargs
=
py
::
dict
(
"x"
_a
=
3
);
return
f
(
"x"
_a
=
1
,
"y"
_a
=
2
,
**
kwargs
);
// duplicate ** after keyword
});
m
.
def
(
"test_unpacking_error2"
,
[](
py
::
function
f
)
{
auto
kwargs
=
py
::
dict
();
kwargs
[
"x"
]
=
py
::
cast
(
3
);
auto
kwargs
=
py
::
dict
(
"x"
_a
=
3
);
return
f
(
**
kwargs
,
"x"
_a
=
1
);
// duplicate keyword after **
});
...
...
tests/test_python_types.cpp
View file @
15a112f8
...
...
@@ -219,4 +219,10 @@ test_initializer python_types([](py::module &m) {
auto
s2
=
"{a} + {b} = {c}"
_s
.
format
(
"a"
_a
=
1
,
"b"
_a
=
2
,
"c"
_a
=
3
);
return
py
::
make_tuple
(
s1
,
s2
);
});
m
.
def
(
"test_dict_keyword_constructor"
,
[]()
{
auto
d1
=
py
::
dict
(
"x"
_a
=
1
,
"y"
_a
=
2
);
auto
d2
=
py
::
dict
(
"z"
_a
=
3
,
**
d1
);
return
d2
;
});
});
tests/test_python_types.py
View file @
15a112f8
...
...
@@ -242,3 +242,9 @@ def test_str_api():
s1
,
s2
=
test_str_format
()
assert
s1
==
"1 + 2 = 3"
assert
s1
==
s2
def
test_dict_api
():
from
pybind11_tests
import
test_dict_keyword_constructor
assert
test_dict_keyword_constructor
()
==
{
"x"
:
1
,
"y"
:
2
,
"z"
:
3
}
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