Skip to content

Do not set docstring for function when it's empty #2745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,14 @@ class cpp_function : public function {

/* Install docstring */
auto *func = (PyCFunctionObject *) m_ptr;
if (func->m_ml->ml_doc)
// Free and reset previous docstring if it exists
if (func->m_ml->ml_doc) {
std::free(const_cast<char *>(func->m_ml->ml_doc));
func->m_ml->ml_doc = strdup(signatures.c_str());
func->m_ml->ml_doc = nullptr;
}
// Then install new one if it's non-empty (when at least one option is enabled)
if (!signatures.empty())
func->m_ml->ml_doc = strdup(signatures.c_str());

if (rec->is_method) {
m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->scope.ptr());
Expand Down Expand Up @@ -472,7 +477,9 @@ class cpp_function : public function {
arg.value.dec_ref();
}
if (rec->def) {
std::free(const_cast<char *>(rec->def->ml_doc));
if (rec->def->ml_doc) {
std::free(const_cast<char *>(rec->def->ml_doc));
}
// Python 3.9.0 decref's these in the wrong order; rec->def
// If loaded on 3.9.0, let these leak (use Python 3.9.1 at runtime to fix)
// See https://github.com/python/cpython/pull/22670
Expand Down
8 changes: 8 additions & 0 deletions tests/test_docstring_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ TEST_SUBMODULE(docstring_options, m) {

m.def("test_function7", [](int, int) {}, py::arg("a"), py::arg("b"), "A custom docstring");

{
py::options options;
options.disable_user_defined_docstrings();
options.disable_function_signatures();

m.def("test_function8", []() {});
}

{
py::options options;
options.disable_user_defined_docstrings();
Expand Down
3 changes: 3 additions & 0 deletions tests/test_docstring_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def test_docstring_options():
assert m.test_function7.__doc__.startswith("test_function7(a: int, b: int) -> None")
assert m.test_function7.__doc__.endswith("A custom docstring\n")

# when all options are disabled, no docstring (instead of an empty one) should be generated
assert m.test_function8.__doc__ is None

# Suppression of user-defined docstrings for non-function objects
assert not m.DocstringTestFoo.__doc__
assert not m.DocstringTestFoo.value_prop.__doc__