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
76e99f1c
Commit
76e99f1c
authored
Dec 30, 2020
by
Ralf W. Grosse-Kunstleve
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pybind11 equivalent of Boost.Python test similar to reproducer under #1333
parent
8a393e68
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
95 additions
and
0 deletions
+95
-0
tests/test_cpp_base_py_derived.cpp
+57
-0
tests/test_cpp_base_py_derived.py
+38
-0
No files found.
tests/test_cpp_base_py_derived.cpp
0 → 100644
View file @
76e99f1c
// pybind11 equivalent of Boost.Python test:
// https://github.com/rwgk/rwgk_tbx/blob/6c9a6d6bc72d5c1b8609724433259c5b47178680/cpp_base_py_derived_ext.cpp
// See also: https://github.com/pybind/pybind11/issues/1333 (this was the starting point)
#include "pybind11_tests.h"
namespace
pybind11_tests
{
namespace
cpp_base_py_derived
{
struct
base
{
base
()
:
base_num
(
100
)
{}
virtual
int
get_num
()
const
{
return
base_num
;
}
virtual
std
::
shared_ptr
<
base
>
clone
()
const
{
return
std
::
shared_ptr
<
base
>
(
new
base
(
150
));
}
virtual
~
base
()
=
default
;
private
:
explicit
base
(
int
num
)
:
base_num
(
num
)
{}
int
base_num
;
};
inline
int
get_num
(
std
::
shared_ptr
<
base
>
b
)
{
return
b
->
get_num
();
}
inline
int
clone_get_num
(
std
::
shared_ptr
<
base
>
b
)
{
std
::
shared_ptr
<
base
>
c
=
b
->
clone
();
return
(
b
->
get_num
()
+
3
)
*
1000
+
(
c
->
get_num
()
+
7
);
}
struct
base_trampoline
:
public
base
{
using
base
::
base
;
int
get_num
()
const
override
{
PYBIND11_OVERRIDE
(
int
,
base
,
get_num
);
}
std
::
shared_ptr
<
base
>
clone
()
const
override
{
PYBIND11_OVERRIDE
(
std
::
shared_ptr
<
base
>
,
base
,
clone
);
}
};
TEST_SUBMODULE
(
cpp_base_py_derived
,
m
)
{
py
::
class_
<
base
,
base_trampoline
,
std
::
shared_ptr
<
base
>>
(
m
,
"base"
)
.
def
(
py
::
init
<>
())
.
def
(
"get_num"
,
&
base
::
get_num
)
.
def
(
"clone"
,
&
base
::
clone
)
;
m
.
def
(
"get_num"
,
get_num
);
m
.
def
(
"clone_get_num"
,
clone_get_num
);
}
}
// namespace cpp_base_py_derived
}
// namespace pybind11_tests
tests/test_cpp_base_py_derived.py
0 → 100644
View file @
76e99f1c
# -*- coding: utf-8 -*-
# pybind11 equivalent of Boost.Python test:
# https://github.com/rwgk/rwgk_tbx/blob/6c9a6d6bc72d5c1b8609724433259c5b47178680/tst_cpp_base_py_derived.py
# See also: https://github.com/pybind/pybind11/issues/1333 (this was the starting point)
import
pytest
from
pybind11_tests
import
cpp_base_py_derived
as
m
class
drvd
(
m
.
base
):
def
__init__
(
self
,
_num
=
200
):
super
()
.
__init__
()
self
.
_drvd_num
=
_num
def
get_num
(
self
):
return
self
.
_drvd_num
def
clone
(
self
):
return
drvd
(
250
)
def
test_base
():
b
=
m
.
base
()
assert
b
.
get_num
()
==
100
m
.
get_num
(
b
)
==
100
bc
=
b
.
clone
()
assert
bc
.
get_num
()
==
150
assert
m
.
clone_get_num
(
b
)
==
103157
def
test_drvd
():
d
=
drvd
()
assert
d
.
get_num
()
==
200
assert
m
.
get_num
(
d
)
==
200
dc
=
d
.
clone
()
assert
dc
.
get_num
()
==
250
assert
m
.
clone_get_num
(
d
)
==
203257
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