Skip to content

Commit 519c505

Browse files
committed
Initializing PyModuleDef object with PyModuleDef_HEAD_INIT.
Python 3.8 documentation: m_base - Always initialize this member to PyModuleDef_HEAD_INIT. Long-standing (since first github commit in 2015), inconsequential bug. Also removing inconsequential Py_INCREF(def): PyModule_Create() resets the reference count to 1.
1 parent 0d0b37c commit 519c505

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

include/pybind11/pybind11.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -830,11 +830,17 @@ class module : public object {
830830
#if PY_MAJOR_VERSION >= 3
831831
explicit module(const char *name, const char *doc = nullptr, PyModuleDef *def = nullptr) {
832832
if (!def) def = new PyModuleDef();
833-
std::memset(def, 0, sizeof(PyModuleDef));
834-
def->m_name = name;
835-
def->m_doc = options::show_user_defined_docstrings() ? doc : nullptr;
836-
def->m_size = -1;
837-
Py_INCREF(def);
833+
new (def) PyModuleDef { // Placement new (not an allocation).
834+
/* m_base */ PyModuleDef_HEAD_INIT,
835+
/* m_name */ name,
836+
/* m_doc */ options::show_user_defined_docstrings() ? doc : nullptr,
837+
/* m_size */ -1,
838+
/* m_methods */ NULL,
839+
/* m_slots */ NULL,
840+
/* m_traverse */ NULL,
841+
/* m_clear */ NULL,
842+
/* m_free */ NULL
843+
};
838844
m_ptr = PyModule_Create(def);
839845
#else
840846
explicit module(const char *name, const char *doc = nullptr) {

0 commit comments

Comments
 (0)