mirror of
https://github.com/pybind/pybind11.git
synced 2026-05-13 01:36:21 +00:00
refactor: module -> module_ with typedef (#2544)
* WIP: module -> module_ without typedef * refactor: allow py::module to work again
This commit is contained in:
@@ -569,17 +569,17 @@ inline PyObject* make_new_python_type(const type_record &rec) {
|
||||
#endif
|
||||
}
|
||||
|
||||
object module;
|
||||
object module_;
|
||||
if (rec.scope) {
|
||||
if (hasattr(rec.scope, "__module__"))
|
||||
module = rec.scope.attr("__module__");
|
||||
module_ = rec.scope.attr("__module__");
|
||||
else if (hasattr(rec.scope, "__name__"))
|
||||
module = rec.scope.attr("__name__");
|
||||
module_ = rec.scope.attr("__name__");
|
||||
}
|
||||
|
||||
auto full_name = c_str(
|
||||
#if !defined(PYPY_VERSION)
|
||||
module ? str(module).cast<std::string>() + "." + rec.name :
|
||||
module_ ? str(module_).cast<std::string>() + "." + rec.name :
|
||||
#endif
|
||||
rec.name);
|
||||
|
||||
@@ -658,8 +658,8 @@ inline PyObject* make_new_python_type(const type_record &rec) {
|
||||
else
|
||||
Py_INCREF(type); // Keep it alive forever (reference leak)
|
||||
|
||||
if (module) // Needed by pydoc
|
||||
setattr((PyObject *) type, "__module__", module);
|
||||
if (module_) // Needed by pydoc
|
||||
setattr((PyObject *) type, "__module__", module_);
|
||||
|
||||
PYBIND11_SET_OLDPY_QUALNAME(type, qualname);
|
||||
|
||||
|
||||
@@ -263,13 +263,13 @@ extern "C" {
|
||||
***Deprecated in favor of PYBIND11_MODULE***
|
||||
|
||||
This macro creates the entry point that will be invoked when the Python interpreter
|
||||
imports a plugin library. Please create a `module` in the function body and return
|
||||
imports a plugin library. Please create a `module_` in the function body and return
|
||||
the pointer to its underlying Python object at the end.
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
PYBIND11_PLUGIN(example) {
|
||||
pybind11::module m("example", "pybind11 example plugin");
|
||||
pybind11::module_ m("example", "pybind11 example plugin");
|
||||
/// Set up bindings here
|
||||
return m.ptr();
|
||||
}
|
||||
@@ -290,7 +290,7 @@ extern "C" {
|
||||
This macro creates the entry point that will be invoked when the Python interpreter
|
||||
imports an extension module. The module name is given as the fist argument and it
|
||||
should not be in quotes. The second macro argument defines a variable of type
|
||||
`py::module` which can be used to initialize the module.
|
||||
`py::module_` which can be used to initialize the module.
|
||||
|
||||
The entry point is marked as "maybe unused" to aid dead-code detection analysis:
|
||||
since the entry point is typically only looked up at runtime and not referenced
|
||||
@@ -317,12 +317,12 @@ extern "C" {
|
||||
#else
|
||||
#define PYBIND11_DETAIL_MODULE_STATIC_DEF(name)
|
||||
#define PYBIND11_DETAIL_MODULE_CREATE(name) \
|
||||
auto m = pybind11::module(PYBIND11_TOSTRING(name));
|
||||
auto m = pybind11::module_(PYBIND11_TOSTRING(name));
|
||||
#endif
|
||||
#define PYBIND11_MODULE(name, variable) \
|
||||
PYBIND11_DETAIL_MODULE_STATIC_DEF(name) \
|
||||
PYBIND11_MAYBE_UNUSED \
|
||||
static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &); \
|
||||
static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module_ &); \
|
||||
PYBIND11_PLUGIN_IMPL(name) { \
|
||||
PYBIND11_CHECK_PYTHON_VERSION \
|
||||
PYBIND11_ENSURE_INTERNALS_READY \
|
||||
@@ -332,7 +332,7 @@ extern "C" {
|
||||
return m.ptr(); \
|
||||
} PYBIND11_CATCH_INIT_EXCEPTIONS \
|
||||
} \
|
||||
void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &variable)
|
||||
void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module_ &variable)
|
||||
|
||||
|
||||
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
|
||||
|
||||
@@ -549,7 +549,7 @@ struct type_caster<Type, enable_if_t<is_eigen_sparse<Type>::value>> {
|
||||
return false;
|
||||
|
||||
auto obj = reinterpret_borrow<object>(src);
|
||||
object sparse_module = module::import("scipy.sparse");
|
||||
object sparse_module = module_::import("scipy.sparse");
|
||||
object matrix_type = sparse_module.attr(
|
||||
rowMajor ? "csr_matrix" : "csc_matrix");
|
||||
|
||||
@@ -580,7 +580,7 @@ struct type_caster<Type, enable_if_t<is_eigen_sparse<Type>::value>> {
|
||||
static handle cast(const Type &src, return_value_policy /* policy */, handle /* parent */) {
|
||||
const_cast<Type&>(src).makeCompressed();
|
||||
|
||||
object matrix_type = module::import("scipy.sparse").attr(
|
||||
object matrix_type = module_::import("scipy.sparse").attr(
|
||||
rowMajor ? "csr_matrix" : "csc_matrix");
|
||||
|
||||
array data(src.nonZeros(), src.valuePtr());
|
||||
|
||||
@@ -46,9 +46,9 @@
|
||||
}
|
||||
\endrst */
|
||||
#define PYBIND11_EMBEDDED_MODULE(name, variable) \
|
||||
static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &); \
|
||||
static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module_ &); \
|
||||
static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \
|
||||
auto m = pybind11::module(PYBIND11_TOSTRING(name)); \
|
||||
auto m = pybind11::module_(PYBIND11_TOSTRING(name)); \
|
||||
try { \
|
||||
PYBIND11_CONCAT(pybind11_init_, name)(m); \
|
||||
return m.ptr(); \
|
||||
@@ -64,7 +64,7 @@
|
||||
pybind11::detail::embedded_module PYBIND11_CONCAT(pybind11_module_, name) \
|
||||
(PYBIND11_TOSTRING(name), \
|
||||
PYBIND11_CONCAT(pybind11_init_impl_, name)); \
|
||||
void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &variable)
|
||||
void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module_ &variable)
|
||||
|
||||
|
||||
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
|
||||
@@ -109,7 +109,7 @@ inline void initialize_interpreter(bool init_signal_handlers = true) {
|
||||
Py_InitializeEx(init_signal_handlers ? 1 : 0);
|
||||
|
||||
// Make .py files in the working directory available by default
|
||||
module::import("sys").attr("path").cast<list>().append(".");
|
||||
module_::import("sys").attr("path").cast<list>().append(".");
|
||||
}
|
||||
|
||||
/** \rst
|
||||
|
||||
@@ -52,7 +52,7 @@ object eval(str expr, object global = globals(), object local = object()) {
|
||||
template <eval_mode mode = eval_expr, size_t N>
|
||||
object eval(const char (&s)[N], object global = globals(), object local = object()) {
|
||||
/* Support raw string literals by removing common leading whitespace */
|
||||
auto expr = (s[0] == '\n') ? str(module::import("textwrap").attr("dedent")(s))
|
||||
auto expr = (s[0] == '\n') ? str(module_::import("textwrap").attr("dedent")(s))
|
||||
: str(s);
|
||||
return eval<mode>(expr, global, local);
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ PYBIND11_NAMESPACE_END(detail)
|
||||
.. code-block:: cpp
|
||||
|
||||
{
|
||||
py::scoped_ostream_redirect output{std::cerr, py::module::import("sys").attr("stderr")};
|
||||
py::scoped_ostream_redirect output{std::cerr, py::module_::import("sys").attr("stderr")};
|
||||
std::cerr << "Hello, World!";
|
||||
}
|
||||
\endrst */
|
||||
@@ -115,7 +115,7 @@ protected:
|
||||
public:
|
||||
scoped_ostream_redirect(
|
||||
std::ostream &costream = std::cout,
|
||||
object pyostream = module::import("sys").attr("stdout"))
|
||||
object pyostream = module_::import("sys").attr("stdout"))
|
||||
: costream(costream), buffer(pyostream) {
|
||||
old = costream.rdbuf(&buffer);
|
||||
}
|
||||
@@ -146,7 +146,7 @@ class scoped_estream_redirect : public scoped_ostream_redirect {
|
||||
public:
|
||||
scoped_estream_redirect(
|
||||
std::ostream &costream = std::cerr,
|
||||
object pyostream = module::import("sys").attr("stderr"))
|
||||
object pyostream = module_::import("sys").attr("stderr"))
|
||||
: scoped_ostream_redirect(costream,pyostream) {}
|
||||
};
|
||||
|
||||
@@ -206,7 +206,7 @@ PYBIND11_NAMESPACE_END(detail)
|
||||
m.noisy_function_with_error_printing()
|
||||
|
||||
\endrst */
|
||||
inline class_<detail::OstreamRedirect> add_ostream_redirect(module m, std::string name = "ostream_redirect") {
|
||||
inline class_<detail::OstreamRedirect> add_ostream_redirect(module_ m, std::string name = "ostream_redirect") {
|
||||
return class_<detail::OstreamRedirect>(m, name.c_str(), module_local())
|
||||
.def(init<bool,bool>(), arg("stdout")=true, arg("stderr")=true)
|
||||
.def("__enter__", &detail::OstreamRedirect::enter)
|
||||
|
||||
@@ -222,7 +222,7 @@ private:
|
||||
};
|
||||
|
||||
static npy_api lookup() {
|
||||
module_ m = module::import("numpy.core.multiarray");
|
||||
module_ m = module_::import("numpy.core.multiarray");
|
||||
auto c = m.attr("_ARRAY_API");
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
void **api_ptr = (void **) PyCapsule_GetPointer(c.ptr(), NULL);
|
||||
@@ -505,7 +505,7 @@ public:
|
||||
|
||||
private:
|
||||
static object _dtype_from_pep3118() {
|
||||
static PyObject *obj = module::import("numpy.core._internal")
|
||||
static PyObject *obj = module_::import("numpy.core._internal")
|
||||
.attr("_dtype_from_pep3118").cast<object>().release().ptr();
|
||||
return reinterpret_borrow<object>(obj);
|
||||
}
|
||||
|
||||
@@ -904,9 +904,9 @@ public:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
py::module m("example", "pybind11 example plugin");
|
||||
py::module m2 = m.def_submodule("sub", "A submodule of 'example'");
|
||||
py::module m3 = m2.def_submodule("subsub", "A submodule of 'example.sub'");
|
||||
py::module_ m("example", "pybind11 example plugin");
|
||||
py::module_ m2 = m.def_submodule("sub", "A submodule of 'example'");
|
||||
py::module_ m3 = m2.def_submodule("subsub", "A submodule of 'example.sub'");
|
||||
\endrst */
|
||||
module_ def_submodule(const char *name, const char *doc = nullptr) {
|
||||
std::string full_name = std::string(PyModule_GetName(m_ptr))
|
||||
@@ -965,12 +965,15 @@ private:
|
||||
};
|
||||
m_ptr = PyModule_Create(def);
|
||||
if (m_ptr == nullptr)
|
||||
pybind11_fail("Internal error in module::module()");
|
||||
pybind11_fail("Internal error in module_::module_()");
|
||||
inc_ref();
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
// When inside a namespace (or anywhere as long as it's not the first item on a line),
|
||||
// C++20 allows "module" to be used. This is provided for backward compatibility, and for
|
||||
// simplicity, if someone wants to use py::module for example, that is perfectly safe.
|
||||
using module = module_;
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
@@ -986,7 +989,7 @@ PYBIND11_NAMESPACE_END(detail)
|
||||
/// or ``__main__.__dict__`` if there is no frame (usually when the interpreter is embedded).
|
||||
inline dict globals() {
|
||||
PyObject *p = PyEval_GetGlobals();
|
||||
return reinterpret_borrow<dict>(p ? p : module::import("__main__").attr("__dict__").ptr());
|
||||
return reinterpret_borrow<dict>(p ? p : module_::import("__main__").attr("__dict__").ptr());
|
||||
}
|
||||
|
||||
PYBIND11_NAMESPACE_BEGIN(detail)
|
||||
@@ -1973,7 +1976,7 @@ PYBIND11_NOINLINE inline void print(tuple args, dict kwargs) {
|
||||
file = kwargs["file"].cast<object>();
|
||||
} else {
|
||||
try {
|
||||
file = module::import("sys").attr("stdout");
|
||||
file = module_::import("sys").attr("stdout");
|
||||
} catch (const error_already_set &) {
|
||||
/* If print() is called from code that is executed as
|
||||
part of garbage collection during interpreter shutdown,
|
||||
|
||||
Reference in New Issue
Block a user