mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
parent
389adc64d2
commit
2d4cf81356
@ -11,11 +11,17 @@ class module : public object {
|
||||
|
||||
static module import(const char* 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) {
|
||||
// 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 m = py_newmodule(fname.c_str());
|
||||
setattr(*this, name, m);
|
||||
|
@ -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_<Point>(m, "Point")
|
||||
.def(py::init<>())
|
||||
.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++; }
|
||||
};
|
||||
|
||||
py::module_ m = py::module_::import("__main__");
|
||||
py::module m = py::module::import("__main__");
|
||||
|
||||
py::class_<Point>(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> color(m, "Color");
|
||||
|
||||
|
@ -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, []() { \
|
||||
|
@ -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_<Point>(m, "Point")
|
||||
.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; }
|
||||
};
|
||||
|
||||
auto m = py::module_::__main__();
|
||||
auto m = py::module::__main__();
|
||||
|
||||
py::class_<Point>(m, "Point")
|
||||
.def(py::init<int, int>())
|
||||
@ -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<int, int>(add));
|
||||
m.def("add", py::overload_cast<int, int, int>(add));
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "test.h"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
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<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
|
||||
|
@ -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");
|
||||
|
@ -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_<Int>(m, "Int")
|
||||
.def(py::init<int>())
|
||||
.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_<Int>(m, "Int")
|
||||
.def(py::init<int>())
|
||||
.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_<std::vector<int>>(m, "vector")
|
||||
.def(py::init<>())
|
||||
|
@ -41,7 +41,7 @@ TEST_F(PYBIND11_TEST, vector_bool) {
|
||||
}
|
||||
|
||||
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_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_<Point>(py::module_::__main__(), "Point")
|
||||
py::class_<Point>(py::module::__main__(), "Point")
|
||||
.def(py::init<int, int>())
|
||||
.def_readwrite("x", &Point::x)
|
||||
.def_readwrite("y", &Point::y)
|
||||
|
@ -152,7 +152,7 @@ TEST_F(PYBIND11_TEST, capsule) {
|
||||
delete static_cast<NotTrivial*>(ptr);
|
||||
});
|
||||
|
||||
auto m = py::module_::__main__();
|
||||
auto m = py::module::__main__();
|
||||
|
||||
m.def("foo", [](int x) {
|
||||
return py::capsule(new int(x));
|
||||
|
Loading…
x
Reference in New Issue
Block a user