Skip to content

Commit d3c999c

Browse files
authored
fix: rename pybind11::module to pybind11::module_ (#2489)
Support C++20. For backwards compatibility, we provide an alias for the old name. This change is necessary to easily avoid errors when a compiler thinks `module` is used as a keyword.
1 parent e37921d commit d3c999c

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

docs/changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ v2.6.0 (IN PROGRESS)
1111

1212
See :ref:`upgrade-guide-2.6` for help upgrading to the new version.
1313

14+
* Provide an additional spelling of ``py::module`` - ``py::module_`` (with a
15+
trailing underscore), for C++20 compatibility. Only relevant when used
16+
unqualified.
17+
`#2489 <https://github.com/pybind/pybind11/pull/2489>`_
18+
1419
* ``pybind11_add_module()`` now accepts an optional ``OPT_SIZE`` flag that
1520
switches the binding target to size-based optimization regardless global
1621
CMake build type (except in debug mode, where optimizations remain disabled).

docs/reference.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ With reference counting
4646
Convenience classes for specific Python types
4747
=============================================
4848

49-
.. doxygenclass:: module
49+
.. doxygenclass:: module_
5050
:members:
5151

5252
.. doxygengroup:: pytypes

include/pybind11/numpy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ struct npy_api {
222222
};
223223

224224
static npy_api lookup() {
225-
module m = module::import("numpy.core.multiarray");
225+
module_ m = module::import("numpy.core.multiarray");
226226
auto c = m.attr("_ARRAY_API");
227227
#if PY_MAJOR_VERSION >= 3
228228
void **api_ptr = (void **) PyCapsule_GetPointer(c.ptr(), NULL);

include/pybind11/pybind11.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -852,12 +852,12 @@ class cpp_function : public function {
852852
};
853853

854854
/// Wrapper for Python extension modules
855-
class module : public object {
855+
class module_ : public object {
856856
public:
857-
PYBIND11_OBJECT_DEFAULT(module, object, PyModule_Check)
857+
PYBIND11_OBJECT_DEFAULT(module_, object, PyModule_Check)
858858

859859
/// Create a new top-level Python module with the given name and docstring
860-
explicit module(const char *name, const char *doc = nullptr) {
860+
explicit module_(const char *name, const char *doc = nullptr) {
861861
if (!options::show_user_defined_docstrings()) doc = nullptr;
862862
#if PY_MAJOR_VERSION >= 3
863863
auto *def = new PyModuleDef();
@@ -871,7 +871,7 @@ class module : public object {
871871
m_ptr = Py_InitModule3(name, nullptr, doc);
872872
#endif
873873
if (m_ptr == nullptr)
874-
pybind11_fail("Internal error in module::module()");
874+
pybind11_fail("Internal error in module_::module_()");
875875
inc_ref();
876876
}
877877

@@ -881,7 +881,7 @@ class module : public object {
881881
details on the ``Extra&& ... extra`` argument, see section :ref:`extras`.
882882
\endrst */
883883
template <typename Func, typename... Extra>
884-
module &def(const char *name_, Func &&f, const Extra& ... extra) {
884+
module_ &def(const char *name_, Func &&f, const Extra& ... extra) {
885885
cpp_function func(std::forward<Func>(f), name(name_), scope(*this),
886886
sibling(getattr(*this, name_, none())), extra...);
887887
// NB: allow overwriting here because cpp_function sets up a chain with the intention of
@@ -900,30 +900,30 @@ class module : public object {
900900
py::module m2 = m.def_submodule("sub", "A submodule of 'example'");
901901
py::module m3 = m2.def_submodule("subsub", "A submodule of 'example.sub'");
902902
\endrst */
903-
module def_submodule(const char *name, const char *doc = nullptr) {
903+
module_ def_submodule(const char *name, const char *doc = nullptr) {
904904
std::string full_name = std::string(PyModule_GetName(m_ptr))
905905
+ std::string(".") + std::string(name);
906-
auto result = reinterpret_borrow<module>(PyImport_AddModule(full_name.c_str()));
906+
auto result = reinterpret_borrow<module_>(PyImport_AddModule(full_name.c_str()));
907907
if (doc && options::show_user_defined_docstrings())
908908
result.attr("__doc__") = pybind11::str(doc);
909909
attr(name) = result;
910910
return result;
911911
}
912912

913913
/// Import and return a module or throws `error_already_set`.
914-
static module import(const char *name) {
914+
static module_ import(const char *name) {
915915
PyObject *obj = PyImport_ImportModule(name);
916916
if (!obj)
917917
throw error_already_set();
918-
return reinterpret_steal<module>(obj);
918+
return reinterpret_steal<module_>(obj);
919919
}
920920

921921
/// Reload the module or throws `error_already_set`.
922922
void reload() {
923923
PyObject *obj = PyImport_ReloadModule(ptr());
924924
if (!obj)
925925
throw error_already_set();
926-
*this = reinterpret_steal<module>(obj);
926+
*this = reinterpret_steal<module_>(obj);
927927
}
928928

929929
// Adds an object to the module using the given name. Throws if an object with the given name
@@ -940,6 +940,8 @@ class module : public object {
940940
}
941941
};
942942

943+
using module = module_;
944+
943945
/// \ingroup python_builtins
944946
/// Return a dictionary representing the global variables in the current execution frame,
945947
/// or ``__main__.__dict__`` if there is no frame (usually when the interpreter is embedded).

0 commit comments

Comments
 (0)