refactor: module -> module_ with typedef (#2544)
* WIP: module -> module_ without typedef
* refactor: allow py::module to work again
diff --git a/docs/advanced/embedding.rst b/docs/advanced/embedding.rst
index 98a5c52..dfdaad2 100644
--- a/docs/advanced/embedding.rst
+++ b/docs/advanced/embedding.rst
@@ -108,11 +108,11 @@
Importing modules
=================
-Python modules can be imported using `module::import()`:
+Python modules can be imported using `module_::import()`:
.. code-block:: cpp
- py::module sys = py::module::import("sys");
+ py::module_ sys = py::module_::import("sys");
py::print(sys.attr("path"));
For convenience, the current working directory is included in ``sys.path`` when
@@ -128,12 +128,12 @@
.. code-block:: cpp
- py::module calc = py::module::import("calc");
+ py::module_ calc = py::module_::import("calc");
py::object result = calc.attr("add")(1, 2);
int n = result.cast<int>();
assert(n == 3);
-Modules can be reloaded using `module::reload()` if the source is modified e.g.
+Modules can be reloaded using `module_::reload()` if the source is modified e.g.
by an external process. This can be useful in scenarios where the application
imports a user defined data processing script which needs to be updated after
changes by the user. Note that this function does not reload modules recursively.
@@ -153,7 +153,7 @@
namespace py = pybind11;
PYBIND11_EMBEDDED_MODULE(fast_calc, m) {
- // `m` is a `py::module` which is used to bind functions and classes
+ // `m` is a `py::module_` which is used to bind functions and classes
m.def("add", [](int i, int j) {
return i + j;
});
@@ -162,7 +162,7 @@
int main() {
py::scoped_interpreter guard{};
- auto fast_calc = py::module::import("fast_calc");
+ auto fast_calc = py::module_::import("fast_calc");
auto result = fast_calc.attr("add")(1, 2).cast<int>();
assert(result == 3);
}
@@ -196,7 +196,7 @@
int main() {
py::scoped_interpreter guard{};
- auto py_module = py::module::import("py_module");
+ auto py_module = py::module_::import("py_module");
auto locals = py::dict("fmt"_a="{} + {} = {}", **py_module.attr("__dict__"));
assert(locals["a"].cast<int>() == 1);
diff --git a/docs/advanced/exceptions.rst b/docs/advanced/exceptions.rst
index a96f8e8..5eae556 100644
--- a/docs/advanced/exceptions.rst
+++ b/docs/advanced/exceptions.rst
@@ -182,7 +182,7 @@
try {
// open("missing.txt", "r")
- auto file = py::module::import("io").attr("open")("missing.txt", "r");
+ auto file = py::module_::import("io").attr("open")("missing.txt", "r");
auto text = file.attr("read")();
file.attr("close")();
} catch (py::error_already_set &e) {
diff --git a/docs/advanced/functions.rst b/docs/advanced/functions.rst
index c895517..81b4c02 100644
--- a/docs/advanced/functions.rst
+++ b/docs/advanced/functions.rst
@@ -17,7 +17,7 @@
type information, it is not clear whether Python should take charge of the
returned value and eventually free its resources, or if this is handled on the
C++ side. For this reason, pybind11 provides a several *return value policy*
-annotations that can be passed to the :func:`module::def` and
+annotations that can be passed to the :func:`module_::def` and
:func:`class_::def` functions. The default policy is
:enum:`return_value_policy::automatic`.
diff --git a/docs/advanced/misc.rst b/docs/advanced/misc.rst
index a5899c6..5aeb653 100644
--- a/docs/advanced/misc.rst
+++ b/docs/advanced/misc.rst
@@ -132,7 +132,7 @@
.. code-block:: cpp
- py::object pet = (py::object) py::module::import("basic").attr("Pet");
+ py::object pet = (py::object) py::module_::import("basic").attr("Pet");
py::class_<Dog>(m, "Dog", pet)
.def(py::init<const std::string &>())
@@ -146,7 +146,7 @@
.. code-block:: cpp
- py::module::import("basic");
+ py::module_::import("basic");
py::class_<Dog, Pet>(m, "Dog")
.def(py::init<const std::string &>())
@@ -243,7 +243,7 @@
.. code-block:: cpp
- auto atexit = py::module::import("atexit");
+ auto atexit = py::module_::import("atexit");
atexit.attr("register")(py::cpp_function([]() {
// perform cleanup here -- this function is called with the GIL held
}));
@@ -284,7 +284,7 @@
)mydelimiter");
By default, pybind11 automatically generates and prepends a signature to the docstring of a function
-registered with ``module::def()`` and ``class_::def()``. Sometimes this
+registered with ``module_::def()`` and ``class_::def()``. Sometimes this
behavior is not desirable, because you want to provide your own signature or remove
the docstring completely to exclude the function from the Sphinx documentation.
The class ``options`` allows you to selectively suppress auto-generated signatures:
diff --git a/docs/advanced/pycpp/object.rst b/docs/advanced/pycpp/object.rst
index 70e493a..6c7525c 100644
--- a/docs/advanced/pycpp/object.rst
+++ b/docs/advanced/pycpp/object.rst
@@ -56,12 +56,12 @@
.. code-block:: cpp
// Equivalent to "from decimal import Decimal"
- py::object Decimal = py::module::import("decimal").attr("Decimal");
+ py::object Decimal = py::module_::import("decimal").attr("Decimal");
.. code-block:: cpp
// Try to import scipy
- py::object scipy = py::module::import("scipy");
+ py::object scipy = py::module_::import("scipy");
return scipy.attr("__version__");
@@ -81,7 +81,7 @@
.. code-block:: cpp
// Use Python to make our directories
- py::object os = py::module::import("os");
+ py::object os = py::module_::import("os");
py::object makedirs = os.attr("makedirs");
makedirs("/tmp/path/to/somewhere");
@@ -196,9 +196,9 @@
#include <pybind11/numpy.h>
using namespace pybind11::literals;
- py::module os = py::module::import("os");
- py::module path = py::module::import("os.path"); // like 'import os.path as path'
- py::module np = py::module::import("numpy"); // like 'import numpy as np'
+ py::module_ os = py::module_::import("os");
+ py::module_ path = py::module_::import("os.path"); // like 'import os.path as path'
+ py::module_ np = py::module_::import("numpy"); // like 'import numpy as np'
py::str curdir_abs = path.attr("abspath")(path.attr("curdir"));
py::print(py::str("Current directory: ") + curdir_abs);
diff --git a/docs/advanced/pycpp/utilities.rst b/docs/advanced/pycpp/utilities.rst
index 369e7c9..c15051f 100644
--- a/docs/advanced/pycpp/utilities.rst
+++ b/docs/advanced/pycpp/utilities.rst
@@ -42,7 +42,7 @@
m.def("noisy_func", []() {
py::scoped_ostream_redirect stream(
std::cout, // std::ostream&
- py::module::import("sys").attr("stdout") // Python output
+ py::module_::import("sys").attr("stdout") // Python output
);
call_noisy_func();
});
@@ -104,7 +104,7 @@
...
// Evaluate in scope of main module
- py::object scope = py::module::import("__main__").attr("__dict__");
+ py::object scope = py::module_::import("__main__").attr("__dict__");
// Evaluate an isolated expression
int result = py::eval("my_variable + 10", scope).cast<int>();
diff --git a/docs/basics.rst b/docs/basics.rst
index 71440c9..0681d86 100644
--- a/docs/basics.rst
+++ b/docs/basics.rst
@@ -118,8 +118,8 @@
The :func:`PYBIND11_MODULE` macro creates a function that will be called when an
``import`` statement is issued from within Python. The module name (``example``)
is given as the first macro argument (it should not be in quotes). The second
-argument (``m``) defines a variable of type :class:`py::module <module>` which
-is the main interface for creating bindings. The method :func:`module::def`
+argument (``m``) defines a variable of type :class:`py::module_ <module>` which
+is the main interface for creating bindings. The method :func:`module_::def`
generates binding code that exposes the ``add()`` function to Python.
.. note::
@@ -181,7 +181,7 @@
py::arg("i"), py::arg("j"));
:class:`arg` is one of several special tag classes which can be used to pass
-metadata into :func:`module::def`. With this modified binding code, we can now
+metadata into :func:`module_::def`. With this modified binding code, we can now
call the function using keyword arguments, which is a more readable alternative
particularly for functions taking many parameters:
diff --git a/docs/changelog.rst b/docs/changelog.rst
index 1b022b9..568611b 100644
--- a/docs/changelog.rst
+++ b/docs/changelog.rst
@@ -11,9 +11,9 @@
See :ref:`upgrade-guide-2.6` for help upgrading to the new version.
-* Provide an additional spelling of ``py::module`` - ``py::module_`` (with a
- trailing underscore), for C++20 compatibility. Only relevant when used
- unqualified.
+* ``py::module`` was renamed ``py::module_`` to avoid issues with C++20 when
+ used unqualified, but an alias ``py::module`` is provided for backward
+ compatibility.
`#2489 <https://github.com/pybind/pybind11/pull/2489>`_
* ``pybind11_add_module()`` now accepts an optional ``OPT_SIZE`` flag that
@@ -529,7 +529,7 @@
v2.2.1 (September 14, 2017)
-----------------------------------------------------
-* Added ``py::module::reload()`` member function for reloading a module.
+* Added ``py::module_::reload()`` member function for reloading a module.
`#1040 <https://github.com/pybind/pybind11/pull/1040>`_.
* Fixed a reference leak in the number converter.
diff --git a/docs/faq.rst b/docs/faq.rst
index 5f7866f..daf7c08 100644
--- a/docs/faq.rst
+++ b/docs/faq.rst
@@ -100,8 +100,8 @@
.. code-block:: cpp
- void init_ex1(py::module &);
- void init_ex2(py::module &);
+ void init_ex1(py::module_ &);
+ void init_ex2(py::module_ &);
/* ... */
PYBIND11_MODULE(example, m) {
@@ -114,7 +114,7 @@
.. code-block:: cpp
- void init_ex1(py::module &m) {
+ void init_ex1(py::module_ &m) {
m.def("add", [](int a, int b) { return a + b; });
}
@@ -122,7 +122,7 @@
.. code-block:: cpp
- void init_ex2(py::module &m) {
+ void init_ex2(py::module_ &m) {
m.def("sub", [](int a, int b) { return a - b; });
}