Skip to content

Commit e2b64e5

Browse files
Rename module to module_
Avoids potential conflict with C++20 keyword
1 parent 085a294 commit e2b64e5

27 files changed

+104
-97
lines changed

include/pybind11/detail/class.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -526,17 +526,17 @@ inline PyObject* make_new_python_type(const type_record &rec) {
526526
#endif
527527
}
528528

529-
object module;
529+
object module_;
530530
if (rec.scope) {
531531
if (hasattr(rec.scope, "__module__"))
532-
module = rec.scope.attr("__module__");
532+
module_ = rec.scope.attr("__module__");
533533
else if (hasattr(rec.scope, "__name__"))
534-
module = rec.scope.attr("__name__");
534+
module_ = rec.scope.attr("__name__");
535535
}
536536

537537
auto full_name = c_str(
538538
#if !defined(PYPY_VERSION)
539-
module ? str(module).cast<std::string>() + "." + rec.name :
539+
module_ ? str(module_).cast<std::string>() + "." + rec.name :
540540
#endif
541541
rec.name);
542542

@@ -610,8 +610,8 @@ inline PyObject* make_new_python_type(const type_record &rec) {
610610
else
611611
Py_INCREF(type); // Keep it alive forever (reference leak)
612612

613-
if (module) // Needed by pydoc
614-
setattr((PyObject *) type, "__module__", module);
613+
if (module_) // Needed by pydoc
614+
setattr((PyObject *) type, "__module__", module_);
615615

616616
PYBIND11_SET_OLDPY_QUALNAME(type, qualname);
617617

include/pybind11/detail/common.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,13 @@ extern "C" {
241241
***Deprecated in favor of PYBIND11_MODULE***
242242
243243
This macro creates the entry point that will be invoked when the Python interpreter
244-
imports a plugin library. Please create a `module` in the function body and return
244+
imports a plugin library. Please create a `module_` in the function body and return
245245
the pointer to its underlying Python object at the end.
246246
247247
.. code-block:: cpp
248248
249249
PYBIND11_PLUGIN(example) {
250-
pybind11::module m("example", "pybind11 example plugin");
250+
pybind11::module_ m("example", "pybind11 example plugin");
251251
/// Set up bindings here
252252
return m.ptr();
253253
}
@@ -267,7 +267,7 @@ extern "C" {
267267
This macro creates the entry point that will be invoked when the Python interpreter
268268
imports an extension module. The module name is given as the fist argument and it
269269
should not be in quotes. The second macro argument defines a variable of type
270-
`py::module` which can be used to initialize the module.
270+
`py::module_` which can be used to initialize the module.
271271
272272
.. code-block:: cpp
273273
@@ -281,16 +281,16 @@ extern "C" {
281281
}
282282
\endrst */
283283
#define PYBIND11_MODULE(name, variable) \
284-
static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &); \
284+
static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module_ &); \
285285
PYBIND11_PLUGIN_IMPL(name) { \
286286
PYBIND11_CHECK_PYTHON_VERSION \
287-
auto m = pybind11::module(PYBIND11_TOSTRING(name)); \
287+
auto m = pybind11::module_(PYBIND11_TOSTRING(name)); \
288288
try { \
289289
PYBIND11_CONCAT(pybind11_init_, name)(m); \
290290
return m.ptr(); \
291291
} PYBIND11_CATCH_INIT_EXCEPTIONS \
292292
} \
293-
void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &variable)
293+
void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module_ &variable)
294294

295295

296296
NAMESPACE_BEGIN(PYBIND11_NAMESPACE)

include/pybind11/eigen.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ struct type_caster<Type, enable_if_t<is_eigen_sparse<Type>::value>> {
549549
return false;
550550

551551
auto obj = reinterpret_borrow<object>(src);
552-
object sparse_module = module::import("scipy.sparse");
552+
object sparse_module = module_::import("scipy.sparse");
553553
object matrix_type = sparse_module.attr(
554554
rowMajor ? "csr_matrix" : "csc_matrix");
555555

@@ -580,7 +580,7 @@ struct type_caster<Type, enable_if_t<is_eigen_sparse<Type>::value>> {
580580
static handle cast(const Type &src, return_value_policy /* policy */, handle /* parent */) {
581581
const_cast<Type&>(src).makeCompressed();
582582

583-
object matrix_type = module::import("scipy.sparse").attr(
583+
object matrix_type = module_::import("scipy.sparse").attr(
584584
rowMajor ? "csr_matrix" : "csc_matrix");
585585

586586
array data(src.nonZeros(), src.valuePtr());

include/pybind11/embed.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ inline void initialize_interpreter(bool init_signal_handlers = true) {
106106
Py_InitializeEx(init_signal_handlers ? 1 : 0);
107107

108108
// Make .py files in the working directory available by default
109-
module::import("sys").attr("path").cast<list>().append(".");
109+
module_::import("sys").attr("path").cast<list>().append(".");
110110
}
111111

112112
/** \rst

include/pybind11/eval.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ object eval(str expr, object global = globals(), object local = object()) {
5252
template <eval_mode mode = eval_expr, size_t N>
5353
object eval(const char (&s)[N], object global = globals(), object local = object()) {
5454
/* Support raw string literals by removing common leading whitespace */
55-
auto expr = (s[0] == '\n') ? str(module::import("textwrap").attr("dedent")(s))
55+
auto expr = (s[0] == '\n') ? str(module_::import("textwrap").attr("dedent")(s))
5656
: str(s);
5757
return eval<mode>(expr, global, local);
5858
}

include/pybind11/iostream.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ NAMESPACE_END(detail)
8686
.. code-block:: cpp
8787
8888
{
89-
py::scoped_ostream_redirect output{std::cerr, py::module::import("sys").attr("stderr")};
89+
py::scoped_ostream_redirect output{std::cerr, py::module_::import("sys").attr("stderr")};
9090
std::cerr << "Hello, World!";
9191
}
9292
\endrst */
@@ -99,7 +99,7 @@ class scoped_ostream_redirect {
9999
public:
100100
scoped_ostream_redirect(
101101
std::ostream &costream = std::cout,
102-
object pyostream = module::import("sys").attr("stdout"))
102+
object pyostream = module_::import("sys").attr("stdout"))
103103
: costream(costream), buffer(pyostream) {
104104
old = costream.rdbuf(&buffer);
105105
}
@@ -130,7 +130,7 @@ class scoped_estream_redirect : public scoped_ostream_redirect {
130130
public:
131131
scoped_estream_redirect(
132132
std::ostream &costream = std::cerr,
133-
object pyostream = module::import("sys").attr("stderr"))
133+
object pyostream = module_::import("sys").attr("stderr"))
134134
: scoped_ostream_redirect(costream,pyostream) {}
135135
};
136136

@@ -190,7 +190,7 @@ NAMESPACE_END(detail)
190190
m.noisy_function_with_error_printing()
191191
192192
\endrst */
193-
inline class_<detail::OstreamRedirect> add_ostream_redirect(module m, std::string name = "ostream_redirect") {
193+
inline class_<detail::OstreamRedirect> add_ostream_redirect(module_ m, std::string name = "ostream_redirect") {
194194
return class_<detail::OstreamRedirect>(m, name.c_str(), module_local())
195195
.def(init<bool,bool>(), arg("stdout")=true, arg("stderr")=true)
196196
.def("__enter__", &detail::OstreamRedirect::enter)

include/pybind11/numpy.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ struct npy_api {
188188
};
189189

190190
static npy_api lookup() {
191-
module m = module::import("numpy.core.multiarray");
191+
module_ m = module_::import("numpy.core.multiarray");
192192
auto c = m.attr("_ARRAY_API");
193193
#if PY_MAJOR_VERSION >= 3
194194
void **api_ptr = (void **) PyCapsule_GetPointer(c.ptr(), NULL);
@@ -467,7 +467,7 @@ class dtype : public object {
467467

468468
private:
469469
static object _dtype_from_pep3118() {
470-
static PyObject *obj = module::import("numpy.core._internal")
470+
static PyObject *obj = module_::import("numpy.core._internal")
471471
.attr("_dtype_from_pep3118").cast<object>().release().ptr();
472472
return reinterpret_borrow<object>(obj);
473473
}

include/pybind11/pybind11.h

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -778,12 +778,12 @@ class cpp_function : public function {
778778
};
779779

780780
/// Wrapper for Python extension modules
781-
class module : public object {
781+
class module_ : public object {
782782
public:
783-
PYBIND11_OBJECT_DEFAULT(module, object, PyModule_Check)
783+
PYBIND11_OBJECT_DEFAULT(module_, object, PyModule_Check)
784784

785785
/// Create a new top-level Python module with the given name and docstring
786-
explicit module(const char *name, const char *doc = nullptr) {
786+
explicit module_(const char *name, const char *doc = nullptr) {
787787
if (!options::show_user_defined_docstrings()) doc = nullptr;
788788
#if PY_MAJOR_VERSION >= 3
789789
PyModuleDef *def = new PyModuleDef();
@@ -797,7 +797,7 @@ class module : public object {
797797
m_ptr = Py_InitModule3(name, nullptr, doc);
798798
#endif
799799
if (m_ptr == nullptr)
800-
pybind11_fail("Internal error in module::module()");
800+
pybind11_fail("Internal error in module_::module_()");
801801
inc_ref();
802802
}
803803

@@ -807,7 +807,7 @@ class module : public object {
807807
details on the ``Extra&& ... extra`` argument, see section :ref:`extras`.
808808
\endrst */
809809
template <typename Func, typename... Extra>
810-
module &def(const char *name_, Func &&f, const Extra& ... extra) {
810+
module_ &def(const char *name_, Func &&f, const Extra& ... extra) {
811811
cpp_function func(std::forward<Func>(f), name(name_), scope(*this),
812812
sibling(getattr(*this, name_, none())), extra...);
813813
// NB: allow overwriting here because cpp_function sets up a chain with the intention of
@@ -822,34 +822,34 @@ class module : public object {
822822
823823
.. code-block:: cpp
824824
825-
py::module m("example", "pybind11 example plugin");
826-
py::module m2 = m.def_submodule("sub", "A submodule of 'example'");
827-
py::module m3 = m2.def_submodule("subsub", "A submodule of 'example.sub'");
825+
py::module_ m("example", "pybind11 example plugin");
826+
py::module_ m2 = m.def_submodule("sub", "A submodule of 'example'");
827+
py::module_ m3 = m2.def_submodule("subsub", "A submodule of 'example.sub'");
828828
\endrst */
829-
module def_submodule(const char *name, const char *doc = nullptr) {
829+
module_ def_submodule(const char *name, const char *doc = nullptr) {
830830
std::string full_name = std::string(PyModule_GetName(m_ptr))
831831
+ std::string(".") + std::string(name);
832-
auto result = reinterpret_borrow<module>(PyImport_AddModule(full_name.c_str()));
832+
auto result = reinterpret_borrow<module_>(PyImport_AddModule(full_name.c_str()));
833833
if (doc && options::show_user_defined_docstrings())
834834
result.attr("__doc__") = pybind11::str(doc);
835835
attr(name) = result;
836836
return result;
837837
}
838838

839839
/// Import and return a module or throws `error_already_set`.
840-
static module import(const char *name) {
840+
static module_ import(const char *name) {
841841
PyObject *obj = PyImport_ImportModule(name);
842842
if (!obj)
843843
throw error_already_set();
844-
return reinterpret_steal<module>(obj);
844+
return reinterpret_steal<module_>(obj);
845845
}
846846

847847
/// Reload the module or throws `error_already_set`.
848848
void reload() {
849849
PyObject *obj = PyImport_ReloadModule(ptr());
850850
if (!obj)
851851
throw error_already_set();
852-
*this = reinterpret_steal<module>(obj);
852+
*this = reinterpret_steal<module_>(obj);
853853
}
854854

855855
// Adds an object to the module using the given name. Throws if an object with the given name
@@ -866,12 +866,16 @@ class module : public object {
866866
}
867867
};
868868

869+
#if !defined(PYBIND11_CPP20)
870+
using module = module_;
871+
#endif
872+
869873
/// \ingroup python_builtins
870874
/// Return a dictionary representing the global variables in the current execution frame,
871875
/// or ``__main__.__dict__`` if there is no frame (usually when the interpreter is embedded).
872876
inline dict globals() {
873877
PyObject *p = PyEval_GetGlobals();
874-
return reinterpret_borrow<dict>(p ? p : module::import("__main__").attr("__dict__").ptr());
878+
return reinterpret_borrow<dict>(p ? p : module_::import("__main__").attr("__dict__").ptr());
875879
}
876880

877881
NAMESPACE_BEGIN(detail)
@@ -1816,7 +1820,7 @@ PYBIND11_NOINLINE inline void print(tuple args, dict kwargs) {
18161820
file = kwargs["file"].cast<object>();
18171821
} else {
18181822
try {
1819-
file = module::import("sys").attr("stdout");
1823+
file = module_::import("sys").attr("stdout");
18201824
} catch (const error_already_set &) {
18211825
/* If print() is called from code that is executed as
18221826
part of garbage collection during interpreter shutdown,

tests/constructor_stats.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class ConstructorStats {
120120
throw py::error_already_set();
121121
Py_DECREF(result);
122122
#else
123-
py::module::import("gc").attr("collect")();
123+
py::module_::import("gc").attr("collect")();
124124
#endif
125125
}
126126

tests/pybind11_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ productively.
2626
Instead, see the "How can I reduce the build time?" question in the "Frequently asked questions"
2727
section of the documentation for good practice on splitting binding code over multiple files.
2828
*/
29-
std::list<std::function<void(py::module &)>> &initializers() {
30-
static std::list<std::function<void(py::module &)>> inits;
29+
std::list<std::function<void(py::module_ &)>> &initializers() {
30+
static std::list<std::function<void(py::module_ &)>> inits;
3131
return inits;
3232
}
3333

@@ -36,13 +36,13 @@ test_initializer::test_initializer(Initializer init) {
3636
}
3737

3838
test_initializer::test_initializer(const char *submodule_name, Initializer init) {
39-
initializers().push_back([=](py::module &parent) {
39+
initializers().push_back([=](py::module_ &parent) {
4040
auto m = parent.def_submodule(submodule_name);
4141
init(m);
4242
});
4343
}
4444

45-
void bind_ConstructorStats(py::module &m) {
45+
void bind_ConstructorStats(py::module_ &m) {
4646
py::class_<ConstructorStats>(m, "ConstructorStats")
4747
.def("alive", &ConstructorStats::alive)
4848
.def("values", &ConstructorStats::values)

0 commit comments

Comments
 (0)