Skip to content

Commit 3cc2fa9

Browse files
committed
Factor out setting function docstrings into a macro
1 parent a932f7a commit 3cc2fa9

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

include/pybind11/detail/common.h

+14
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,20 @@ PYBIND11_WARNING_POP
387387
#define PYBIND11_CONCAT(first, second) first##second
388388
#define PYBIND11_ENSURE_INTERNALS_READY pybind11::detail::get_internals();
389389

390+
#if !defined(GRAALVM_PYTHON)
391+
# define PYBIND11_PYCFUNCTION_GET_DOC(func) ((func)->m_ml->ml_doc)
392+
# define PYBIND11_PYCFUNCTION_SET_DOC(func, doc) \
393+
do { \
394+
(func)->m_ml->ml_doc = (doc); \
395+
} while (0)
396+
#else
397+
# define PYBIND11_PYCFUNCTION_GET_DOC(func) (GraalPyCFunction_GetDoc((PyObject *) (func)))
398+
# define PYBIND11_PYCFUNCTION_SET_DOC(func, doc) \
399+
do { \
400+
GraalPyCFunction_SetDoc((PyObject *) (func), (doc)); \
401+
} while (0)
402+
#endif
403+
390404
#define PYBIND11_CHECK_PYTHON_VERSION \
391405
{ \
392406
const char *compiled_ver \

include/pybind11/pybind11.h

+3-10
Original file line numberDiff line numberDiff line change
@@ -633,18 +633,11 @@ class cpp_function : public function {
633633
}
634634
}
635635

636-
/* Install docstring */
637636
auto *func = (PyCFunctionObject *) m_ptr;
638-
#if !defined(GRAALVM_PYTHON)
639-
std::free(const_cast<char *>(func->m_ml->ml_doc));
640637
// Install docstring if it's non-empty (when at least one option is enabled)
641-
func->m_ml->ml_doc
642-
= signatures.empty() ? nullptr : PYBIND11_COMPAT_STRDUP(signatures.c_str());
643-
#else
644-
std::free(const_cast<char *>(GraalPyCFunction_GetDoc(m_ptr)));
645-
GraalPyCFunction_SetDoc(
646-
m_ptr, signatures.empty() ? nullptr : PYBIND11_COMPAT_STRDUP(signatures.c_str()));
647-
#endif
638+
auto *doc = signatures.empty() ? nullptr : PYBIND11_COMPAT_STRDUP(signatures.c_str());
639+
std::free(const_cast<char *>(PYBIND11_PYCFUNCTION_GET_DOC(func)));
640+
PYBIND11_PYCFUNCTION_SET_DOC(func, doc);
648641

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

0 commit comments

Comments
 (0)