Skip to content

Commit

Permalink
fix issue on PyPy code path
Browse files Browse the repository at this point in the history
wjakob committed Feb 18, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 8e28ee8 commit 21eaffc
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion include/nanobind/nb_types.h
Original file line number Diff line number Diff line change
@@ -325,7 +325,7 @@ class module_ : public object {
/// Import and return a module or throws `python_error`.
NB_INLINE module_ def_submodule(const char *name,
const char *doc = nullptr) {
return borrow<module_>(detail::module_new_submodule(m_ptr, name, doc));
return steal<module_>(detail::module_new_submodule(m_ptr, name, doc));
}
};

20 changes: 14 additions & 6 deletions src/common.cpp
Original file line number Diff line number Diff line change
@@ -164,6 +164,7 @@ PyObject *module_new_submodule(PyObject *base, const char *name,
goto fail;

name_py = PyUnicode_FromFormat("%U.%s", base_name, name);
Py_DECREF(base_name);
#else
const char *base_name = PyModule_GetName(base);
if (!base_name)
@@ -179,25 +180,32 @@ PyObject *module_new_submodule(PyObject *base, const char *name,
#else
res = PyImport_AddModule(PyUnicode_AsUTF8(name_py));
#endif
Py_DECREF(name_py);
if (!res)
goto fail;

if (doc) {
PyObject *doc_py = PyUnicode_FromString(doc);
if (!doc_py || PyObject_SetAttrString(res, "__doc__", doc_py))
if (!doc_py)
goto fail;
int rv = PyObject_SetAttrString(res, "__doc__", doc_py);
Py_DECREF(doc_py);
if (rv)
goto fail;
}

Py_DECREF(name_py);
Py_DECREF(base_name);
Py_INCREF(res); // extra reference for PyModule_AddObject

Py_INCREF(res);
if (PyModule_AddObject(base, name, res))
if (PyModule_AddObject(base, name, res)) { // steals on success
Py_DECREF(res);
goto fail;
}

Py_INCREF(res); // turned borrowed into new reference
return res;

fail:
check(false, "nanobind::detail::module_new_submodule(): failed.");
raise_python_error();
}

// ========================================================================

0 comments on commit 21eaffc

Please sign in to comment.