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
d2b628bb
Commit
d2b628bb
authored
Apr 30, 2016
by
Wenzel Jakob
Committed by
Wenzel Jakob
Apr 30, 2016
Browse files
added testcase for issue #187
parent
81e0975b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
3 deletions
+47
-3
example/issues.cpp
example/issues.cpp
+35
-2
example/issues.py
example/issues.py
+11
-1
example/issues.ref
example/issues.ref
+1
-0
No files found.
example/issues.cpp
View file @
d2b628bb
...
...
@@ -10,6 +10,8 @@
#include "example.h"
#include <pybind11/stl.h>
PYBIND11_DECLARE_HOLDER_TYPE
(
T
,
std
::
shared_ptr
<
T
>
);
void
init_issues
(
py
::
module
&
m
)
{
py
::
module
m2
=
m
.
def_submodule
(
"issues"
);
...
...
@@ -20,7 +22,9 @@ void init_issues(py::module &m) {
m2
.
def
(
"print_char"
,
[](
char
c
)
{
std
::
cout
<<
c
<<
std
::
endl
;
});
// #159: virtual function dispatch has problems with similar-named functions
struct
Base
{
virtual
void
dispatch
(
void
)
const
=
0
;
};
struct
Base
{
virtual
void
dispatch
(
void
)
const
{
/* for some reason MSVC2015 can't compile this if the function is pure virtual */
};
};
struct
DispatchIssue
:
Base
{
virtual
void
dispatch
(
void
)
const
{
...
...
@@ -58,4 +62,33 @@ void init_issues(py::module &m) {
m2
.
def
(
"iterator_passthrough"
,
[](
py
::
iterator
s
)
->
py
::
iterator
{
return
py
::
make_iterator
(
std
::
begin
(
s
),
std
::
end
(
s
));
});
}
// #187: issue involving std::shared_ptr<> return value policy & garbage collection
struct
ElementBase
{
virtual
void
foo
()
{
}
/* Force creation of virtual table */
};
struct
ElementA
:
ElementBase
{
ElementA
(
int
v
)
:
v
(
v
)
{
}
int
value
()
{
return
v
;
}
int
v
;
};
struct
ElementList
{
void
add
(
std
::
shared_ptr
<
ElementBase
>
e
)
{
l
.
push_back
(
e
);
}
std
::
vector
<
std
::
shared_ptr
<
ElementBase
>>
l
;
};
py
::
class_
<
ElementBase
,
std
::
shared_ptr
<
ElementBase
>>
(
m2
,
"ElementBase"
);
py
::
class_
<
ElementA
,
std
::
shared_ptr
<
ElementA
>>
(
m2
,
"ElementA"
,
py
::
base
<
ElementBase
>
())
.
def
(
py
::
init
<
int
>
())
.
def
(
"value"
,
&
ElementA
::
value
);
py
::
class_
<
ElementList
,
std
::
shared_ptr
<
ElementList
>>
(
m2
,
"ElementList"
)
.
def
(
py
::
init
<>
())
.
def
(
"add"
,
&
ElementList
::
add
)
.
def
(
"get"
,
[](
ElementList
&
el
){
py
::
list
list
;
for
(
auto
&
e
:
el
.
l
)
list
.
append
(
py
::
cast
(
e
));
return
list
;
});
};
example/issues.py
View file @
d2b628bb
...
...
@@ -5,8 +5,10 @@ sys.path.append('.')
from
example.issues
import
print_cchar
,
print_char
from
example.issues
import
DispatchIssue
,
dispatch_issue_go
from
example.issues
import
Placeholder
,
return_vec_of_reference_wrapper
from
example.issues
import
Placeholder
,
return_vec_of_reference_wrapper
from
example.issues
import
iterator_passthrough
from
example.issues
import
ElementList
,
ElementA
import
gc
print_cchar
(
"const char *"
)
print_char
(
'c'
)
...
...
@@ -32,3 +34,11 @@ dispatch_issue_go(b)
print
(
return_vec_of_reference_wrapper
(
Placeholder
(
4
)))
print
(
list
(
iterator_passthrough
(
iter
([
3
,
5
,
7
,
9
,
11
,
13
,
15
]))))
el
=
ElementList
()
for
i
in
range
(
10
):
el
.
add
(
ElementA
(
i
))
gc
.
collect
()
for
i
,
v
in
enumerate
(
el
.
get
()):
print
(
"%i==%i, "
%
(
i
,
v
.
value
()),
end
=
''
)
print
()
example/issues.ref
View file @
d2b628bb
...
...
@@ -4,3 +4,4 @@ Failed as expected: Tried to call pure virtual function "dispatch"
Yay..
[Placeholder[1], Placeholder[2], Placeholder[3], Placeholder[4]]
[3, 5, 7, 9, 11, 13, 15]
0==0, 1==1, 2==2, 3==3, 4==4, 5==5, 6==6, 7==7, 8==8, 9==9,
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