Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11
Commits
15a112f8
Commit
15a112f8
authored
Aug 30, 2016
by
Dean Moldovan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add py::dict() keyword constructor
parent
66aa2728
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
26 additions
and
16 deletions
+26
-16
include/pybind11/cast.h
+4
-0
include/pybind11/pytypes.h
+3
-0
tests/test_callbacks.cpp
+7
-16
tests/test_python_types.cpp
+6
-0
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
...
@@ -1148,6 +1148,10 @@ inline object handle::operator()(detail::args_proxy args, detail::kwargs_proxy k
return
result
;
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) \
#define PYBIND11_MAKE_OPAQUE(Type) \
namespace pybind11 { namespace detail { \
namespace pybind11 { namespace detail { \
template<> class type_caster<Type> : public type_caster_base<Type> { }; \
template<> class type_caster<Type> : public type_caster_base<Type> { }; \
...
...
include/pybind11/pytypes.h
View file @
15a112f8
...
@@ -589,6 +589,9 @@ public:
...
@@ -589,6 +589,9 @@ public:
dict
()
:
object
(
PyDict_New
(),
false
)
{
dict
()
:
object
(
PyDict_New
(),
false
)
{
if
(
!
m_ptr
)
pybind11_fail
(
"Could not allocate dict object!"
);
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
);
}
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
begin
()
const
{
return
(
++
detail
::
dict_iterator
(
*
this
,
0
));
}
detail
::
dict_iterator
end
()
const
{
return
detail
::
dict_iterator
();
}
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) {
...
@@ -89,12 +89,9 @@ test_initializer callbacks([](py::module &m) {
});
});
m
.
def
(
"test_dict_unpacking"
,
[](
py
::
function
f
)
{
m
.
def
(
"test_dict_unpacking"
,
[](
py
::
function
f
)
{
auto
d1
=
py
::
dict
();
auto
d1
=
py
::
dict
(
"key"
_a
=
"value"
,
"a"
_a
=
1
);
d1
[
"key"
]
=
py
::
cast
(
"value"
);
d1
[
"a"
]
=
py
::
cast
(
1
);
auto
d2
=
py
::
dict
();
auto
d2
=
py
::
dict
();
auto
d3
=
py
::
dict
();
auto
d3
=
py
::
dict
(
"b"
_a
=
2
);
d3
[
"b"
]
=
py
::
cast
(
2
);
return
f
(
"positional"
,
1
,
**
d1
,
**
d2
,
**
d3
);
return
f
(
"positional"
,
1
,
**
d1
,
**
d2
,
**
d3
);
});
});
...
@@ -104,30 +101,24 @@ test_initializer callbacks([](py::module &m) {
...
@@ -104,30 +101,24 @@ test_initializer callbacks([](py::module &m) {
m
.
def
(
"test_unpacking_and_keywords1"
,
[](
py
::
function
f
)
{
m
.
def
(
"test_unpacking_and_keywords1"
,
[](
py
::
function
f
)
{
auto
args
=
py
::
make_tuple
(
2
);
auto
args
=
py
::
make_tuple
(
2
);
auto
kwargs
=
py
::
dict
();
auto
kwargs
=
py
::
dict
(
"d"
_a
=
4
);
kwargs
[
"d"
]
=
py
::
cast
(
4
);
return
f
(
1
,
*
args
,
"c"
_a
=
3
,
**
kwargs
);
return
f
(
1
,
*
args
,
"c"
_a
=
3
,
**
kwargs
);
});
});
m
.
def
(
"test_unpacking_and_keywords2"
,
[](
py
::
function
f
)
{
m
.
def
(
"test_unpacking_and_keywords2"
,
[](
py
::
function
f
)
{
auto
kwargs1
=
py
::
dict
();
auto
kwargs1
=
py
::
dict
(
"a"
_a
=
1
);
kwargs1
[
"a"
]
=
py
::
cast
(
1
);
auto
kwargs2
=
py
::
dict
(
"c"
_a
=
3
,
"d"
_a
=
4
);
auto
kwargs2
=
py
::
dict
();
kwargs2
[
"c"
]
=
py
::
cast
(
3
);
kwargs2
[
"d"
]
=
py
::
cast
(
4
);
return
f
(
"positional"
,
*
py
::
make_tuple
(
1
),
2
,
*
py
::
make_tuple
(
3
,
4
),
5
,
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
);
"key"
_a
=
"value"
,
**
kwargs1
,
"b"
_a
=
2
,
**
kwargs2
,
"e"
_a
=
5
);
});
});
m
.
def
(
"test_unpacking_error1"
,
[](
py
::
function
f
)
{
m
.
def
(
"test_unpacking_error1"
,
[](
py
::
function
f
)
{
auto
kwargs
=
py
::
dict
();
auto
kwargs
=
py
::
dict
(
"x"
_a
=
3
);
kwargs
[
"x"
]
=
py
::
cast
(
3
);
return
f
(
"x"
_a
=
1
,
"y"
_a
=
2
,
**
kwargs
);
// duplicate ** after keyword
return
f
(
"x"
_a
=
1
,
"y"
_a
=
2
,
**
kwargs
);
// duplicate ** after keyword
});
});
m
.
def
(
"test_unpacking_error2"
,
[](
py
::
function
f
)
{
m
.
def
(
"test_unpacking_error2"
,
[](
py
::
function
f
)
{
auto
kwargs
=
py
::
dict
();
auto
kwargs
=
py
::
dict
(
"x"
_a
=
3
);
kwargs
[
"x"
]
=
py
::
cast
(
3
);
return
f
(
**
kwargs
,
"x"
_a
=
1
);
// duplicate keyword after **
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) {
...
@@ -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
);
auto
s2
=
"{a} + {b} = {c}"
_s
.
format
(
"a"
_a
=
1
,
"b"
_a
=
2
,
"c"
_a
=
3
);
return
py
::
make_tuple
(
s1
,
s2
);
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():
...
@@ -242,3 +242,9 @@ def test_str_api():
s1
,
s2
=
test_str_format
()
s1
,
s2
=
test_str_format
()
assert
s1
==
"1 + 2 = 3"
assert
s1
==
"1 + 2 = 3"
assert
s1
==
s2
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