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
cbebb6da
Commit
cbebb6da
authored
Jan 22, 2021
by
Ralf W. Grosse-Kunstleve
Browse files
Tests for classh py::module_local() feature.
parent
5608b71e
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
120 additions
and
2 deletions
+120
-2
include/pybind11/detail/classh_type_casters.h
include/pybind11/detail/classh_type_casters.h
+0
-2
tests/classh_module_local_0.cpp
tests/classh_module_local_0.cpp
+27
-0
tests/classh_module_local_1.cpp
tests/classh_module_local_1.cpp
+33
-0
tests/classh_module_local_2.cpp
tests/classh_module_local_2.cpp
+33
-0
tests/test_classh_module_local.py
tests/test_classh_module_local.py
+27
-0
No files found.
include/pybind11/detail/classh_type_casters.h
View file @
cbebb6da
...
...
@@ -84,7 +84,6 @@ public:
}
PYBIND11_NOINLINE
static
void
*
local_load
(
PyObject
*
src
,
const
type_info
*
ti
)
{
pybind11_fail
(
"classh_type_casters: local_load UNTESTED."
);
// Not thread safe. But the GIL needs to be held anyway in the context of this code.
static
modified_type_caster_generic_load_impl
caster
;
caster
=
modified_type_caster_generic_load_impl
(
ti
);
...
...
@@ -111,7 +110,6 @@ public:
void
*
void_ptr
=
foreign_typeinfo
->
module_local_load
(
src
.
ptr
(),
foreign_typeinfo
);
if
(
void_ptr
!=
nullptr
)
{
pybind11_fail
(
"classh_type_casters: try_load_foreign_module_local UNTESTED."
);
auto
foreign_load_impl
=
static_cast
<
modified_type_caster_generic_load_impl
*>
(
void_ptr
);
if
(
loaded_v_h_cpptype
!=
nullptr
)
{
pybind11_fail
(
"classh_type_casters: try_load_foreign_module_local failure."
);
...
...
tests/classh_module_local_0.cpp
0 → 100644
View file @
cbebb6da
#include <pybind11/classh.h>
#include <string>
namespace
pybind11_tests
{
namespace
classh_module_local
{
struct
bottle
{
std
::
string
msg
;
};
std
::
string
get_msg
(
const
bottle
&
b
)
{
return
b
.
msg
;
}
bottle
make_bottle
()
{
return
bottle
();
}
}
// namespace classh_module_local
}
// namespace pybind11_tests
PYBIND11_CLASSH_TYPE_CASTERS
(
pybind11_tests
::
classh_module_local
::
bottle
)
PYBIND11_MODULE
(
classh_module_local_0
,
m
)
{
using
namespace
pybind11_tests
::
classh_module_local
;
m
.
def
(
"get_msg"
,
get_msg
);
m
.
def
(
"make_bottle"
,
make_bottle
);
}
tests/classh_module_local_1.cpp
0 → 100644
View file @
cbebb6da
// Identical to classh_module_local_2.cpp, except 2 replaced with 1.
#include <pybind11/classh.h>
#include <string>
namespace
pybind11_tests
{
namespace
classh_module_local
{
struct
bottle
{
std
::
string
msg
;
};
std
::
string
get_msg
(
const
bottle
&
b
)
{
return
b
.
msg
;
}
}
// namespace classh_module_local
}
// namespace pybind11_tests
PYBIND11_CLASSH_TYPE_CASTERS
(
pybind11_tests
::
classh_module_local
::
bottle
)
PYBIND11_MODULE
(
classh_module_local_1
,
m
)
{
namespace
py
=
pybind11
;
using
namespace
pybind11_tests
::
classh_module_local
;
py
::
classh
<
bottle
>
(
m
,
"bottle"
,
py
::
module_local
())
.
def
(
py
::
init
([](
const
std
::
string
&
msg
)
{
bottle
obj
;
obj
.
msg
=
msg
;
return
obj
;
}))
.
def
(
"tag"
,
[](
const
bottle
&
)
{
return
1
;
});
m
.
def
(
"get_msg"
,
get_msg
);
}
tests/classh_module_local_2.cpp
0 → 100644
View file @
cbebb6da
// Identical to classh_module_local_1.cpp, except 1 replaced with 2.
#include <pybind11/classh.h>
#include <string>
namespace
pybind11_tests
{
namespace
classh_module_local
{
struct
bottle
{
std
::
string
msg
;
};
std
::
string
get_msg
(
const
bottle
&
b
)
{
return
b
.
msg
;
}
}
// namespace classh_module_local
}
// namespace pybind11_tests
PYBIND11_CLASSH_TYPE_CASTERS
(
pybind11_tests
::
classh_module_local
::
bottle
)
PYBIND11_MODULE
(
classh_module_local_2
,
m
)
{
namespace
py
=
pybind11
;
using
namespace
pybind11_tests
::
classh_module_local
;
py
::
classh
<
bottle
>
(
m
,
"bottle"
,
py
::
module_local
())
.
def
(
py
::
init
([](
const
std
::
string
&
msg
)
{
bottle
obj
;
obj
.
msg
=
msg
;
return
obj
;
}))
.
def
(
"tag"
,
[](
const
bottle
&
)
{
return
2
;
});
m
.
def
(
"get_msg"
,
get_msg
);
}
tests/test_classh_module_local.py
0 → 100644
View file @
cbebb6da
# -*- coding: utf-8 -*-
import
pytest
import
classh_module_local_0
as
m0
import
classh_module_local_1
as
m1
import
classh_module_local_2
as
m2
def
test_cross_module_get_msg
():
b1
=
m1
.
bottle
(
"A"
)
assert
b1
.
tag
()
==
1
b2
=
m2
.
bottle
(
"B"
)
assert
b2
.
tag
()
==
2
assert
m1
.
get_msg
(
b1
)
==
"A"
assert
m2
.
get_msg
(
b2
)
==
"B"
assert
m1
.
get_msg
(
b2
)
==
"B"
assert
m2
.
get_msg
(
b1
)
==
"A"
assert
m0
.
get_msg
(
b1
)
==
"A"
assert
m0
.
get_msg
(
b2
)
==
"B"
def
test_m0_make_bottle
():
with
pytest
.
raises
(
TypeError
)
as
exc_info
:
m0
.
make_bottle
()
assert
str
(
exc_info
.
value
).
startswith
(
"Unable to convert function return value to a Python type!"
)
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