From 2d4cf81356d7b908820f474f4c957250cfcc110b Mon Sep 17 00:00:00 2001 From: ykiko Date: Sun, 8 Sep 2024 21:37:57 +0800 Subject: [PATCH] add `module::create` (#306) * add `module::create` * some clean. --- include/pybind11/internal/module.h | 32 +++++++++++++++++----------- include/pybind11/tests/class.cpp | 8 +++---- include/pybind11/tests/error.cpp | 2 +- include/pybind11/tests/function.cpp | 24 ++++++++++----------- include/pybind11/tests/module.cpp | 28 ++++++++++++++++++++++-- include/pybind11/tests/object.cpp | 2 +- include/pybind11/tests/operators.cpp | 6 +++--- include/pybind11/tests/stl.cpp | 4 ++-- include/pybind11/tests/types.cpp | 2 +- 9 files changed, 69 insertions(+), 39 deletions(-) diff --git a/include/pybind11/internal/module.h b/include/pybind11/internal/module.h index 859b6039..2dd1dad4 100644 --- a/include/pybind11/internal/module.h +++ b/include/pybind11/internal/module.h @@ -11,11 +11,17 @@ class module : public object { static module import(const char* name) { raise_call(name); - return module(py_retval(), object::realloc_t{}); + return borrow(py_retval()); + } + + static module create(const char* name) { + auto m = py_newmodule(name); + return steal(m); } module def_submodule(const char* name, const char* doc = nullptr) { - // auto package = (attr("__package__").cast() += ".") += attr("__name__").cast(); + // auto package = (attr("__package__").cast() += ".") += + // attr("__name__").cast(); auto fname = (attr("__name__").cast() += ".") += name; auto m = py_newmodule(fname.c_str()); setattr(*this, name, m); @@ -31,17 +37,17 @@ class module : public object { using module_ = module; -#define PYBIND11_EMBEDDED_MODULE(name, variable) \ - static void _pkbind_register_##name(::pkbind::module& variable); \ - namespace pkbind::impl { \ - auto _module_##name = [] { \ - ::pkbind::action::register_start([] { \ - auto m = ::pkbind::module(py_newmodule(#name), ::pkbind::object::ref_t{}); \ - _pkbind_register_##name(m); \ - }); \ - return 1; \ - }(); \ - } \ +#define PYBIND11_EMBEDDED_MODULE(name, variable) \ + static void _pkbind_register_##name(::pkbind::module& variable); \ + namespace pkbind::impl { \ + auto _module_##name = [] { \ + ::pkbind::action::register_start([] { \ + auto m = ::pkbind::module(py_newmodule(#name), ::pkbind::object::ref_t{}); \ + _pkbind_register_##name(m); \ + }); \ + return 1; \ + }(); \ + } \ static void _pkbind_register_##name(::pkbind::module& variable) } // namespace pkbind diff --git a/include/pybind11/tests/class.cpp b/include/pybind11/tests/class.cpp index e5a13c0a..b547a810 100644 --- a/include/pybind11/tests/class.cpp +++ b/include/pybind11/tests/class.cpp @@ -53,7 +53,7 @@ struct Line { }; TEST_F(PYBIND11_TEST, class) { - py::module_ m = py::module_::import("__main__"); + py::module m = py::module::import("__main__"); py::class_(m, "Point") .def(py::init<>()) .def(py::init()) @@ -121,7 +121,7 @@ TEST_F(PYBIND11_TEST, inheritance) { 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_(m, "Point") .def(py::init<>()) @@ -160,7 +160,7 @@ assert p.z == 30 } TEST_F(PYBIND11_TEST, dynamic_attr) { - py::module_ m = py::module_::import("__main__"); + py::module m = py::module::import("__main__"); struct Point { int x; @@ -185,7 +185,7 @@ TEST_F(PYBIND11_TEST, dynamic_attr) { TEST_F(PYBIND11_TEST, enum) { enum class Color { RED, Yellow, GREEN, BLUE }; - py::module_ m = py::module_::import("__main__"); + py::module m = py::module::import("__main__"); py::enum_ color(m, "Color"); diff --git a/include/pybind11/tests/error.cpp b/include/pybind11/tests/error.cpp index 605773d7..12ea3ee9 100644 --- a/include/pybind11/tests/error.cpp +++ b/include/pybind11/tests/error.cpp @@ -17,7 +17,7 @@ TEST_F(PYBIND11_TEST, exception_python_to_cpp) { } TEST_F(PYBIND11_TEST, exception_cpp_to_python) { - auto m = py::module_::__main__(); + auto m = py::module::__main__(); #define TEST_EXCEPTION(cppe, pye) \ m.def("test_" #cppe, []() { \ diff --git a/include/pybind11/tests/function.cpp b/include/pybind11/tests/function.cpp index 59f1c023..1b7d4891 100644 --- a/include/pybind11/tests/function.cpp +++ b/include/pybind11/tests/function.cpp @@ -3,7 +3,7 @@ namespace { TEST_F(PYBIND11_TEST, vectorcall) { - auto m = py::module_::__main__(); + auto m = py::module::__main__(); py::exec(R"( def add(a, b): @@ -28,7 +28,7 @@ def add2(a, *args): } TEST_F(PYBIND11_TEST, constructor) { - auto m = py::module_::__main__(); + auto m = py::module::__main__(); struct Point { int x, y; @@ -52,7 +52,7 @@ TEST_F(PYBIND11_TEST, constructor) { } TEST_F(PYBIND11_TEST, args) { - auto m = py::module_::__main__(); + auto m = py::module::__main__(); // test for binding function with args m.def("sum", [](py::args args) { @@ -67,7 +67,7 @@ TEST_F(PYBIND11_TEST, args) { } TEST_F(PYBIND11_TEST, kwargs) { - auto m = py::module_::__main__(); + auto m = py::module::__main__(); // test for binding function with kwargs m.def("cal", [](py::kwargs kwargs) { @@ -79,7 +79,7 @@ TEST_F(PYBIND11_TEST, kwargs) { } TEST_F(PYBIND11_TEST, defaults) { - auto m = py::module_::__main__(); + auto m = py::module::__main__(); // test for binding function with defaults m.def( @@ -99,7 +99,7 @@ TEST_F(PYBIND11_TEST, defaults) { } TEST_F(PYBIND11_TEST, defaults_with_args) { - auto m = py::module_::__main__(); + auto m = py::module::__main__(); // test for binding function with defaults m.def( @@ -123,7 +123,7 @@ TEST_F(PYBIND11_TEST, defaults_with_args) { } 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 m.def( @@ -182,7 +182,7 @@ TEST_F(PYBIND11_TEST, default_with_args_and_kwargs) { } TEST_F(PYBIND11_TEST, overload) { - auto m = py::module_::__main__(); + auto m = py::module::__main__(); // test for binding function with overloads m.def("cal", [](int a, int b) { @@ -231,7 +231,7 @@ TEST_F(PYBIND11_TEST, return_value_policy) { move_constructor_calls = 0; destructor_calls = 0; - auto m = py::module_::__main__(); + auto m = py::module::__main__(); py::class_(m, "Point") .def(py::init()) @@ -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; } }; - auto m = py::module_::__main__(); + auto m = py::module::__main__(); py::class_(m, "Point") .def(py::init()) @@ -333,7 +333,7 @@ TEST_F(PYBIND11_TEST, default_return_value_policy) { } TEST_F(PYBIND11_TEST, lambda) { - auto m = py::module_::__main__(); + auto m = py::module::__main__(); 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; } TEST_F(PYBIND11_TEST, overload_cast) { - auto m = py::module_::__main__(); + auto m = py::module::__main__(); m.def("add", py::overload_cast(add)); m.def("add", py::overload_cast(add)); diff --git a/include/pybind11/tests/module.cpp b/include/pybind11/tests/module.cpp index 9de9538a..30429828 100644 --- a/include/pybind11/tests/module.cpp +++ b/include/pybind11/tests/module.cpp @@ -1,4 +1,5 @@ #include "test.h" +#include PYBIND11_EMBEDDED_MODULE(example, m) { m.def("add", [](int a, int b) { @@ -23,9 +24,32 @@ TEST_F(PYBIND11_TEST, module) { py::exec("from example.math import sub"); 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(), 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(), 1); +} + +} // namespace diff --git a/include/pybind11/tests/object.cpp b/include/pybind11/tests/object.cpp index 083c9c41..a7395737 100644 --- a/include/pybind11/tests/object.cpp +++ b/include/pybind11/tests/object.cpp @@ -58,7 +58,7 @@ class Point: )"; TEST_F(PYBIND11_TEST, object) { - py::module_ m = py::module_::import("__main__"); + py::module m = py::module::import("__main__"); py::exec(source); py::exec("p = Point(3, 4)"); py::object p = py::eval("p"); diff --git a/include/pybind11/tests/operators.cpp b/include/pybind11/tests/operators.cpp index 00a34155..3578e0fa 100644 --- a/include/pybind11/tests/operators.cpp +++ b/include/pybind11/tests/operators.cpp @@ -54,7 +54,7 @@ struct Int { } // namespace TEST_F(PYBIND11_TEST, arithmetic_operators) { - py::module_ m = py::module_::import("__main__"); + py::module m = py::module::import("__main__"); py::class_(m, "Int") .def(py::init()) .def(py::self + py::self) @@ -126,7 +126,7 @@ TEST_F(PYBIND11_TEST, arithmetic_operators) { } TEST_F(PYBIND11_TEST, logic_operators) { - py::module_ m = py::module_::import("__main__"); + py::module m = py::module::import("__main__"); py::class_(m, "Int") .def(py::init()) .def_readwrite("x", &Int::x) @@ -149,7 +149,7 @@ TEST_F(PYBIND11_TEST, logic_operators) { } TEST_F(PYBIND11_TEST, item_operators) { - py::module_ m = py::module_::import("__main__"); + py::module m = py::module::import("__main__"); py::class_>(m, "vector") .def(py::init<>()) diff --git a/include/pybind11/tests/stl.cpp b/include/pybind11/tests/stl.cpp index dfff46cf..ffcd1e37 100644 --- a/include/pybind11/tests/stl.cpp +++ b/include/pybind11/tests/stl.cpp @@ -41,7 +41,7 @@ TEST_F(PYBIND11_TEST, vector_bool) { } TEST_F(PYBIND11_TEST, list_like) { - py::class_(py::module_::__main__(), "Point") + py::class_(py::module::__main__(), "Point") .def(py::init()) .def_readwrite("x", &Point::x) .def_readwrite("y", &Point::y) @@ -89,7 +89,7 @@ TEST_F(PYBIND11_TEST, list_like) { } TEST_F(PYBIND11_TEST, dict_like) { - py::class_(py::module_::__main__(), "Point") + py::class_(py::module::__main__(), "Point") .def(py::init()) .def_readwrite("x", &Point::x) .def_readwrite("y", &Point::y) diff --git a/include/pybind11/tests/types.cpp b/include/pybind11/tests/types.cpp index 4b3818e3..a3ff7ba2 100644 --- a/include/pybind11/tests/types.cpp +++ b/include/pybind11/tests/types.cpp @@ -152,7 +152,7 @@ TEST_F(PYBIND11_TEST, capsule) { delete static_cast(ptr); }); - auto m = py::module_::__main__(); + auto m = py::module::__main__(); m.def("foo", [](int x) { return py::capsule(new int(x));