Lines Matching refs:py

15 namespace py = pybind11;
16 using namespace py::literals;
37 py::class_<Widget, PyWidget>(m, "Widget")
38 .def(py::init<std::string>())
49 auto d = py::dict();
50 d["missing"].cast<py::object>();
54 auto module = py::module::import("test_interpreter");
55 REQUIRE(py::hasattr(module, "DerivedWidget"));
57 auto locals = py::dict("hello"_a="Hello, World!", "x"_a=5, **module.attr("__dict__"));
58 py::exec(R"(
61 )", py::globals(), locals);
73 REQUIRE_NOTHROW(py::module::import("widget_module"));
74 REQUIRE_THROWS_WITH(py::module::import("throw_exception"),
76 REQUIRE_THROWS_WITH(py::module::import("throw_error_already_set"),
81 static_assert(std::is_move_constructible<py::scoped_interpreter>::value, "");
82 static_assert(!std::is_move_assignable<py::scoped_interpreter>::value, "");
83 static_assert(!std::is_copy_constructible<py::scoped_interpreter>::value, "");
84 static_assert(!std::is_copy_assignable<py::scoped_interpreter>::value, "");
86 REQUIRE_THROWS_WITH(py::initialize_interpreter(), "The interpreter is already running");
87 REQUIRE_THROWS_WITH(py::scoped_interpreter(), "The interpreter is already running");
89 py::finalize_interpreter();
90 REQUIRE_NOTHROW(py::scoped_interpreter());
92 auto pyi1 = py::scoped_interpreter();
95 py::initialize_interpreter();
99 auto builtins = py::handle(PyEval_GetBuiltins());
104 auto **&ipp = py::detail::get_internals_pp();
110 REQUIRE(py::module::import("widget_module").attr("add")(1, 2).cast<int>() == 3);
113 REQUIRE(py::module::import("external_module").attr("A")(123).attr("value").cast<int>() == 123);
116 REQUIRE(reinterpret_cast<uintptr_t>(*py::detail::get_internals_pp()) ==
117 py::module::import("external_module").attr("internals_at")().cast<uintptr_t>());
120 py::finalize_interpreter();
123 py::initialize_interpreter();
132 REQUIRE(reinterpret_cast<uintptr_t>(*py::detail::get_internals_pp()) ==
133 py::module::import("external_module").attr("internals_at")().cast<uintptr_t>());
137 py::finalize_interpreter();
138 py::initialize_interpreter();
140 py::module::import("__main__").attr("internals_destroy_test") =
141 py::capsule(&ran, [](void *ran) { py::detail::get_internals(); *static_cast<bool *>(ran) = true; });
145 py::finalize_interpreter();
147 py::initialize_interpreter();
152 auto cpp_module = py::module::import("widget_module");
156 auto py_module = py::module::import("test_interpreter");
163 py::module::import("__main__").attr("main_tag") = "main interpreter";
165 auto m = py::module::import("widget_module");
184 REQUIRE_FALSE(py::hasattr(py::module::import("__main__"), "tag"));
186 auto m = py::module::import("widget_module");
187 REQUIRE_FALSE(py::hasattr(m, "extension_module_tag"));
197 REQUIRE(py::hasattr(py::module::import("__main__"), "main_tag"));
198 REQUIRE(py::hasattr(py::module::import("widget_module"), "extension_module_tag"));
202 // When the interpreter is embedded, there is no execution frame, but `py::exec`
204 py::exec("var = dict(number=42)");
205 REQUIRE(py::globals()["var"]["number"].cast<int>() == 42);
210 py::finalize_interpreter();
211 py::initialize_interpreter();
215 auto locals = py::dict("count"_a=0);
218 py::gil_scoped_release gil_release{};
224 py::gil_scoped_acquire gil{};
247 // of the .py files generated below
248 auto sys = py::module::import("sys");
257 std::string module_file = module_name + ".py";
259 // Create the module .py file
270 auto module = py::module::import(module_name.c_str());
274 // Update the module .py file with a small change