Skip to content

Commit

Permalink
Factor out setting function docstrings into a macro
Browse files Browse the repository at this point in the history
  • Loading branch information
msimacek committed Oct 3, 2024
1 parent a932f7a commit 3cc2fa9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
14 changes: 14 additions & 0 deletions include/pybind11/detail/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,20 @@ PYBIND11_WARNING_POP
#define PYBIND11_CONCAT(first, second) first##second
#define PYBIND11_ENSURE_INTERNALS_READY pybind11::detail::get_internals();

#if !defined(GRAALVM_PYTHON)
# define PYBIND11_PYCFUNCTION_GET_DOC(func) ((func)->m_ml->ml_doc)
# define PYBIND11_PYCFUNCTION_SET_DOC(func, doc) \
do { \
(func)->m_ml->ml_doc = (doc); \
} while (0)
#else
# define PYBIND11_PYCFUNCTION_GET_DOC(func) (GraalPyCFunction_GetDoc((PyObject *) (func)))
# define PYBIND11_PYCFUNCTION_SET_DOC(func, doc) \
do { \
GraalPyCFunction_SetDoc((PyObject *) (func), (doc)); \
} while (0)
#endif

#define PYBIND11_CHECK_PYTHON_VERSION \
{ \
const char *compiled_ver \
Expand Down
13 changes: 3 additions & 10 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -633,18 +633,11 @@ class cpp_function : public function {
}
}

/* Install docstring */
auto *func = (PyCFunctionObject *) m_ptr;
#if !defined(GRAALVM_PYTHON)
std::free(const_cast<char *>(func->m_ml->ml_doc));
// Install docstring if it's non-empty (when at least one option is enabled)
func->m_ml->ml_doc
= signatures.empty() ? nullptr : PYBIND11_COMPAT_STRDUP(signatures.c_str());
#else
std::free(const_cast<char *>(GraalPyCFunction_GetDoc(m_ptr)));
GraalPyCFunction_SetDoc(
m_ptr, signatures.empty() ? nullptr : PYBIND11_COMPAT_STRDUP(signatures.c_str()));
#endif
auto *doc = signatures.empty() ? nullptr : PYBIND11_COMPAT_STRDUP(signatures.c_str());
std::free(const_cast<char *>(PYBIND11_PYCFUNCTION_GET_DOC(func)));
PYBIND11_PYCFUNCTION_SET_DOC(func, doc);

if (rec->is_method) {
m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->scope.ptr());
Expand Down

0 comments on commit 3cc2fa9

Please sign in to comment.