Skip to content

Commit

Permalink
Add option for enable/disable enum members in docstring.
Browse files Browse the repository at this point in the history
  • Loading branch information
knarfS committed Jan 5, 2021
1 parent 98f1bbb commit a992757
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
7 changes: 7 additions & 0 deletions include/pybind11/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,18 @@ class options {

options& enable_function_signatures() & { global_state().show_function_signatures = true; return *this; }

options& disable_enum_members_docstring() & { global_state().show_enum_members_docstring = false; return *this; }

options& enable_enum_members_docstring() & { global_state().show_enum_members_docstring = true; return *this; }

// Getter methods (return the global state):

static bool show_user_defined_docstrings() { return global_state().show_user_defined_docstrings; }

static bool show_function_signatures() { return global_state().show_function_signatures; }

static bool show_enum_members_docstring() { return global_state().show_enum_members_docstring; }

// This type is not meant to be allocated on the heap.
void* operator new(size_t) = delete;

Expand All @@ -52,6 +58,7 @@ class options {
struct state {
bool show_user_defined_docstrings = true; //< Include user-supplied texts in docstrings.
bool show_function_signatures = true; //< Include auto-generated function signatures in docstrings.
bool show_enum_members_docstring = true; //< Include auto-generated member list in enum docstring.
};

static state &global_state() {
Expand Down
23 changes: 13 additions & 10 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1594,19 +1594,22 @@ struct enum_base {
}, name("name"), is_method(m_base)
);

bool show_enum_members = options::show_enum_members_docstring();
m_base.attr("__doc__") = static_property(cpp_function(
[](handle arg) -> std::string {
[show_enum_members](handle arg) -> std::string {
std::string docstring;
dict entries = arg.attr("__entries");
if (((PyTypeObject *) arg.ptr())->tp_doc)
docstring += std::string(((PyTypeObject *) arg.ptr())->tp_doc) + "\n\n";
docstring += "Members:";
for (auto kv : entries) {
auto key = std::string(pybind11::str(kv.first));
auto comment = kv.second[int_(1)];
docstring += "\n\n " + key;
if (!comment.is_none())
docstring += " : " + (std::string) pybind11::str(comment);
docstring += std::string(((PyTypeObject *) arg.ptr())->tp_doc);
if (show_enum_members) {
docstring += "\n\nMembers:";
dict entries = arg.attr("__entries");
for (auto kv : entries) {
auto key = std::string(pybind11::str(kv.first));
auto comment = kv.second[int_(1)];
docstring += "\n\n " + key;
if (!comment.is_none())
docstring += " : " + (std::string) pybind11::str(comment);
}
}
return docstring;
}, name("__doc__")
Expand Down

0 comments on commit a992757

Please sign in to comment.