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
c84b37b5
Commit
c84b37b5
authored
Sep 07, 2016
by
Wenzel Jakob
Browse files
fix bogus return value policy fallbacks (fixes #389)
parent
a3dbdc67
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
5 deletions
+34
-5
include/pybind11/cast.h
include/pybind11/cast.h
+7
-5
tests/test_issues.cpp
tests/test_issues.cpp
+19
-0
tests/test_issues.py
tests/test_issues.py
+8
-0
No files found.
include/pybind11/cast.h
View file @
c84b37b5
...
...
@@ -220,14 +220,16 @@ public:
policy
=
return_value_policy
::
reference
;
if
(
policy
==
return_value_policy
::
copy
)
{
wrapper
->
value
=
copy_constructor
(
wrapper
->
value
);
if
(
wrapper
->
value
==
nullptr
)
if
(
copy_constructor
)
wrapper
->
value
=
copy_constructor
(
wrapper
->
value
);
else
throw
cast_error
(
"return_value_policy = copy, but the object is non-copyable!"
);
}
else
if
(
policy
==
return_value_policy
::
move
)
{
wrapper
->
value
=
move_constructor
(
wrapper
->
value
);
if
(
wrapper
->
value
==
nullptr
)
if
(
move_constructor
)
wrapper
->
value
=
move_constructor
(
wrapper
->
value
);
else
if
(
copy_constructor
)
wrapper
->
value
=
copy_constructor
(
wrapper
->
value
);
if
(
wrapper
->
value
==
nullptr
)
else
throw
cast_error
(
"return_value_policy = move, but the object is neither movable nor copyable!"
);
}
else
if
(
policy
==
return_value_policy
::
reference
)
{
wrapper
->
owned
=
false
;
...
...
tests/test_issues.cpp
View file @
c84b37b5
...
...
@@ -172,6 +172,25 @@ void init_issues(py::module &m) {
m2
.
def
(
"get_NestA"
,
[](
const
NestA
&
a
)
{
return
a
.
value
;
});
m2
.
def
(
"get_NestB"
,
[](
const
NestB
&
b
)
{
return
b
.
value
;
});
m2
.
def
(
"get_NestC"
,
[](
const
NestC
&
c
)
{
return
c
.
value
;
});
// Issue 389: r_v_p::move should fall-through to copy on non-movable objects
class
MoveIssue1
{
public:
MoveIssue1
(
int
v
)
:
v
{
v
}
{}
MoveIssue1
(
const
MoveIssue1
&
c
)
{
std
::
cerr
<<
"copy ctor
\n
"
;
v
=
c
.
v
;
}
MoveIssue1
(
MoveIssue1
&&
)
=
delete
;
int
v
;
};
class
MoveIssue2
{
public:
MoveIssue2
(
int
v
)
:
v
{
v
}
{}
MoveIssue2
(
MoveIssue2
&&
)
=
default
;
int
v
;
};
py
::
class_
<
MoveIssue1
>
(
m2
,
"MoveIssue1"
).
def
(
py
::
init
<
int
>
()).
def_readwrite
(
"value"
,
&
MoveIssue1
::
v
);
py
::
class_
<
MoveIssue2
>
(
m2
,
"MoveIssue2"
).
def
(
py
::
init
<
int
>
()).
def_readwrite
(
"value"
,
&
MoveIssue2
::
v
);
m2
.
def
(
"get_moveissue1"
,
[](
int
i
)
->
MoveIssue1
*
{
return
new
MoveIssue1
(
i
);
},
py
::
return_value_policy
::
move
);
m2
.
def
(
"get_moveissue2"
,
[](
int
i
)
{
return
MoveIssue2
(
i
);
},
py
::
return_value_policy
::
move
);
}
// MSVC workaround: trying to use a lambda here crashes MSCV
...
...
tests/test_issues.py
View file @
c84b37b5
...
...
@@ -158,3 +158,11 @@ def test_nested():
assert
abase
.
value
==
42
del
abase
,
b
gc
.
collect
()
def
test_move_fallback
():
from
pybind11_tests.issues
import
get_moveissue1
,
get_moveissue2
m2
=
get_moveissue2
(
2
)
assert
m2
.
value
==
2
m1
=
get_moveissue1
(
1
)
assert
m1
.
value
==
1
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