add module::create (#306)

* add `module::create`

* some clean.
This commit is contained in:
ykiko 2024-09-08 21:37:57 +08:00 committed by GitHub
parent 389adc64d2
commit 2d4cf81356
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 69 additions and 39 deletions

View File

@ -11,11 +11,17 @@ class module : public object {
static module import(const char* name) { static module import(const char* name) {
raise_call<py_import>(name); raise_call<py_import>(name);
return module(py_retval(), object::realloc_t{}); return borrow<module>(py_retval());
}
static module create(const char* name) {
auto m = py_newmodule(name);
return steal<module>(m);
} }
module def_submodule(const char* name, const char* doc = nullptr) { module def_submodule(const char* name, const char* doc = nullptr) {
// auto package = (attr("__package__").cast<std::string>() += ".") += attr("__name__").cast<std::string_view>(); // auto package = (attr("__package__").cast<std::string>() += ".") +=
// attr("__name__").cast<std::string_view>();
auto fname = (attr("__name__").cast<std::string>() += ".") += name; auto fname = (attr("__name__").cast<std::string>() += ".") += name;
auto m = py_newmodule(fname.c_str()); auto m = py_newmodule(fname.c_str());
setattr(*this, name, m); setattr(*this, name, m);

View File

@ -53,7 +53,7 @@ struct Line {
}; };
TEST_F(PYBIND11_TEST, class) { TEST_F(PYBIND11_TEST, class) {
py::module_ m = py::module_::import("__main__"); py::module m = py::module::import("__main__");
py::class_<Point>(m, "Point") py::class_<Point>(m, "Point")
.def(py::init<>()) .def(py::init<>())
.def(py::init<int, int, int>()) .def(py::init<int, int, int>())
@ -121,7 +121,7 @@ TEST_F(PYBIND11_TEST, inheritance) {
Point3D(int x, int y, int z) : Point(x, y), z(z) { constructor_calls++; } Point3D(int x, int y, int z) : Point(x, y), z(z) { constructor_calls++; }
}; };
py::module_ m = py::module_::import("__main__"); py::module m = py::module::import("__main__");
py::class_<Point>(m, "Point") py::class_<Point>(m, "Point")
.def(py::init<>()) .def(py::init<>())
@ -160,7 +160,7 @@ assert p.z == 30
} }
TEST_F(PYBIND11_TEST, dynamic_attr) { TEST_F(PYBIND11_TEST, dynamic_attr) {
py::module_ m = py::module_::import("__main__"); py::module m = py::module::import("__main__");
struct Point { struct Point {
int x; int x;
@ -185,7 +185,7 @@ TEST_F(PYBIND11_TEST, dynamic_attr) {
TEST_F(PYBIND11_TEST, enum) { TEST_F(PYBIND11_TEST, enum) {
enum class Color { RED, Yellow, GREEN, BLUE }; enum class Color { RED, Yellow, GREEN, BLUE };
py::module_ m = py::module_::import("__main__"); py::module m = py::module::import("__main__");
py::enum_<Color> color(m, "Color"); py::enum_<Color> color(m, "Color");

View File

@ -17,7 +17,7 @@ TEST_F(PYBIND11_TEST, exception_python_to_cpp) {
} }
TEST_F(PYBIND11_TEST, exception_cpp_to_python) { TEST_F(PYBIND11_TEST, exception_cpp_to_python) {
auto m = py::module_::__main__(); auto m = py::module::__main__();
#define TEST_EXCEPTION(cppe, pye) \ #define TEST_EXCEPTION(cppe, pye) \
m.def("test_" #cppe, []() { \ m.def("test_" #cppe, []() { \

View File

@ -3,7 +3,7 @@
namespace { namespace {
TEST_F(PYBIND11_TEST, vectorcall) { TEST_F(PYBIND11_TEST, vectorcall) {
auto m = py::module_::__main__(); auto m = py::module::__main__();
py::exec(R"( py::exec(R"(
def add(a, b): def add(a, b):
@ -28,7 +28,7 @@ def add2(a, *args):
} }
TEST_F(PYBIND11_TEST, constructor) { TEST_F(PYBIND11_TEST, constructor) {
auto m = py::module_::__main__(); auto m = py::module::__main__();
struct Point { struct Point {
int x, y; int x, y;
@ -52,7 +52,7 @@ TEST_F(PYBIND11_TEST, constructor) {
} }
TEST_F(PYBIND11_TEST, args) { TEST_F(PYBIND11_TEST, args) {
auto m = py::module_::__main__(); auto m = py::module::__main__();
// test for binding function with args // test for binding function with args
m.def("sum", [](py::args args) { m.def("sum", [](py::args args) {
@ -67,7 +67,7 @@ TEST_F(PYBIND11_TEST, args) {
} }
TEST_F(PYBIND11_TEST, kwargs) { TEST_F(PYBIND11_TEST, kwargs) {
auto m = py::module_::__main__(); auto m = py::module::__main__();
// test for binding function with kwargs // test for binding function with kwargs
m.def("cal", [](py::kwargs kwargs) { m.def("cal", [](py::kwargs kwargs) {
@ -79,7 +79,7 @@ TEST_F(PYBIND11_TEST, kwargs) {
} }
TEST_F(PYBIND11_TEST, defaults) { TEST_F(PYBIND11_TEST, defaults) {
auto m = py::module_::__main__(); auto m = py::module::__main__();
// test for binding function with defaults // test for binding function with defaults
m.def( m.def(
@ -99,7 +99,7 @@ TEST_F(PYBIND11_TEST, defaults) {
} }
TEST_F(PYBIND11_TEST, defaults_with_args) { TEST_F(PYBIND11_TEST, defaults_with_args) {
auto m = py::module_::__main__(); auto m = py::module::__main__();
// test for binding function with defaults // test for binding function with defaults
m.def( m.def(
@ -123,7 +123,7 @@ TEST_F(PYBIND11_TEST, defaults_with_args) {
} }
TEST_F(PYBIND11_TEST, default_with_args_and_kwargs) { TEST_F(PYBIND11_TEST, default_with_args_and_kwargs) {
auto m = py::module_::__main__(); auto m = py::module::__main__();
// test for binding function with defaults // test for binding function with defaults
m.def( m.def(
@ -182,7 +182,7 @@ TEST_F(PYBIND11_TEST, default_with_args_and_kwargs) {
} }
TEST_F(PYBIND11_TEST, overload) { TEST_F(PYBIND11_TEST, overload) {
auto m = py::module_::__main__(); auto m = py::module::__main__();
// test for binding function with overloads // test for binding function with overloads
m.def("cal", [](int a, int b) { m.def("cal", [](int a, int b) {
@ -231,7 +231,7 @@ TEST_F(PYBIND11_TEST, return_value_policy) {
move_constructor_calls = 0; move_constructor_calls = 0;
destructor_calls = 0; destructor_calls = 0;
auto m = py::module_::__main__(); auto m = py::module::__main__();
py::class_<Point>(m, "Point") py::class_<Point>(m, "Point")
.def(py::init<int, int>()) .def(py::init<int, int>())
@ -292,7 +292,7 @@ TEST_F(PYBIND11_TEST, default_return_value_policy) {
bool operator== (const Point& p) const { return x == p.x && y == p.y; } bool operator== (const Point& p) const { return x == p.x && y == p.y; }
}; };
auto m = py::module_::__main__(); auto m = py::module::__main__();
py::class_<Point>(m, "Point") py::class_<Point>(m, "Point")
.def(py::init<int, int>()) .def(py::init<int, int>())
@ -333,7 +333,7 @@ TEST_F(PYBIND11_TEST, default_return_value_policy) {
} }
TEST_F(PYBIND11_TEST, lambda) { TEST_F(PYBIND11_TEST, lambda) {
auto m = py::module_::__main__(); auto m = py::module::__main__();
static int destructor_calls = 0; static int destructor_calls = 0;
@ -373,7 +373,7 @@ int add(int a, int b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; } int add(int a, int b, int c) { return a + b + c; }
TEST_F(PYBIND11_TEST, overload_cast) { TEST_F(PYBIND11_TEST, overload_cast) {
auto m = py::module_::__main__(); auto m = py::module::__main__();
m.def("add", py::overload_cast<int, int>(add)); m.def("add", py::overload_cast<int, int>(add));
m.def("add", py::overload_cast<int, int, int>(add)); m.def("add", py::overload_cast<int, int, int>(add));

View File

@ -1,4 +1,5 @@
#include "test.h" #include "test.h"
#include <gtest/gtest.h>
PYBIND11_EMBEDDED_MODULE(example, m) { PYBIND11_EMBEDDED_MODULE(example, m) {
m.def("add", [](int a, int b) { m.def("add", [](int a, int b) {
@ -23,9 +24,32 @@ TEST_F(PYBIND11_TEST, module) {
py::exec("from example.math import sub"); py::exec("from example.math import sub");
EXPECT_EVAL_EQ("sub(1, 2)", -1); EXPECT_EVAL_EQ("sub(1, 2)", -1);
auto math = py::module_::import("example.math"); auto math = py::module::import("example.math");
EXPECT_EQ(math.attr("sub")(4, 3).cast<int>(), 1); EXPECT_EQ(math.attr("sub")(4, 3).cast<int>(), 1);
} }
} // namespace TEST_F(PYBIND11_TEST, raw_module) {
auto m = py::module::create("example2");
m.def("add", [](int a, int b) {
return a + b;
});
auto math = m.def_submodule("math");
math.def("sub", [](int a, int b) {
return a - b;
});
py::exec("import example2");
EXPECT_EVAL_EQ("example2.add(1, 2)", 3);
py::exec("from example2 import math");
EXPECT_EVAL_EQ("math.sub(1, 2)", -1);
py::exec("from example2.math import sub");
EXPECT_EVAL_EQ("sub(1, 2)", -1);
auto math2 = py::module::import("example2.math");
EXPECT_EQ(math2.attr("sub")(4, 3).cast<int>(), 1);
}
} // namespace

View File

@ -58,7 +58,7 @@ class Point:
)"; )";
TEST_F(PYBIND11_TEST, object) { TEST_F(PYBIND11_TEST, object) {
py::module_ m = py::module_::import("__main__"); py::module m = py::module::import("__main__");
py::exec(source); py::exec(source);
py::exec("p = Point(3, 4)"); py::exec("p = Point(3, 4)");
py::object p = py::eval("p"); py::object p = py::eval("p");

View File

@ -54,7 +54,7 @@ struct Int {
} // namespace } // namespace
TEST_F(PYBIND11_TEST, arithmetic_operators) { TEST_F(PYBIND11_TEST, arithmetic_operators) {
py::module_ m = py::module_::import("__main__"); py::module m = py::module::import("__main__");
py::class_<Int>(m, "Int") py::class_<Int>(m, "Int")
.def(py::init<int>()) .def(py::init<int>())
.def(py::self + py::self) .def(py::self + py::self)
@ -126,7 +126,7 @@ TEST_F(PYBIND11_TEST, arithmetic_operators) {
} }
TEST_F(PYBIND11_TEST, logic_operators) { TEST_F(PYBIND11_TEST, logic_operators) {
py::module_ m = py::module_::import("__main__"); py::module m = py::module::import("__main__");
py::class_<Int>(m, "Int") py::class_<Int>(m, "Int")
.def(py::init<int>()) .def(py::init<int>())
.def_readwrite("x", &Int::x) .def_readwrite("x", &Int::x)
@ -149,7 +149,7 @@ TEST_F(PYBIND11_TEST, logic_operators) {
} }
TEST_F(PYBIND11_TEST, item_operators) { TEST_F(PYBIND11_TEST, item_operators) {
py::module_ m = py::module_::import("__main__"); py::module m = py::module::import("__main__");
py::class_<std::vector<int>>(m, "vector") py::class_<std::vector<int>>(m, "vector")
.def(py::init<>()) .def(py::init<>())

View File

@ -41,7 +41,7 @@ TEST_F(PYBIND11_TEST, vector_bool) {
} }
TEST_F(PYBIND11_TEST, list_like) { TEST_F(PYBIND11_TEST, list_like) {
py::class_<Point>(py::module_::__main__(), "Point") py::class_<Point>(py::module::__main__(), "Point")
.def(py::init<int, int>()) .def(py::init<int, int>())
.def_readwrite("x", &Point::x) .def_readwrite("x", &Point::x)
.def_readwrite("y", &Point::y) .def_readwrite("y", &Point::y)
@ -89,7 +89,7 @@ TEST_F(PYBIND11_TEST, list_like) {
} }
TEST_F(PYBIND11_TEST, dict_like) { TEST_F(PYBIND11_TEST, dict_like) {
py::class_<Point>(py::module_::__main__(), "Point") py::class_<Point>(py::module::__main__(), "Point")
.def(py::init<int, int>()) .def(py::init<int, int>())
.def_readwrite("x", &Point::x) .def_readwrite("x", &Point::x)
.def_readwrite("y", &Point::y) .def_readwrite("y", &Point::y)

View File

@ -152,7 +152,7 @@ TEST_F(PYBIND11_TEST, capsule) {
delete static_cast<NotTrivial*>(ptr); delete static_cast<NotTrivial*>(ptr);
}); });
auto m = py::module_::__main__(); auto m = py::module::__main__();
m.def("foo", [](int x) { m.def("foo", [](int x) {
return py::capsule(new int(x)); return py::capsule(new int(x));