forked from pybind/pybind11
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide more control over automatic generation of docstrings (pybind#486
) Added the docstring_options class, which gives global control over the generation of docstrings and function signatures.
- Loading branch information
Showing
7 changed files
with
190 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
pybind11/options.h: global settings that are configurable at runtime. | ||
Copyright (c) 2016 Wenzel Jakob <[email protected]> | ||
All rights reserved. Use of this source code is governed by a | ||
BSD-style license that can be found in the LICENSE file. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "common.h" | ||
|
||
NAMESPACE_BEGIN(pybind11) | ||
|
||
class options { | ||
public: | ||
|
||
// Default RAII constructor, which leaves settings as they currently are. | ||
options() : previous_state(global_state()) {} | ||
|
||
// Class is non-copyable. | ||
options(const options&) = delete; | ||
options& operator=(const options&) = delete; | ||
|
||
// Destructor, which restores settings that were in effect before. | ||
~options() { | ||
global_state() = previous_state; | ||
} | ||
|
||
// Setter methods (affect the global state): | ||
|
||
options& disable_user_defined_docstrings() & { global_state().show_user_defined_docstrings = false; return *this; } | ||
|
||
options& enable_user_defined_docstrings() & { global_state().show_user_defined_docstrings = true; return *this; } | ||
|
||
options& disable_function_signatures() & { global_state().show_function_signatures = false; return *this; } | ||
|
||
options& enable_function_signatures() & { global_state().show_function_signatures = 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; } | ||
|
||
// This type is not meant to be allocated on the heap. | ||
void* operator new(size_t) = delete; | ||
|
||
private: | ||
|
||
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. | ||
}; | ||
|
||
static state &global_state() { | ||
static state instance; | ||
return instance; | ||
} | ||
|
||
state previous_state; | ||
}; | ||
|
||
NAMESPACE_END(pybind11) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
tests/test_docstring_options.cpp -- generation of docstrings and signatures | ||
Copyright (c) 2016 Wenzel Jakob <[email protected]> | ||
All rights reserved. Use of this source code is governed by a | ||
BSD-style license that can be found in the LICENSE file. | ||
*/ | ||
|
||
#include "pybind11_tests.h" | ||
|
||
struct DocstringTestFoo { | ||
int value; | ||
void setValue(int v) { value = v; } | ||
int getValue() const { return value; } | ||
}; | ||
|
||
test_initializer docstring_generation([](py::module &m) { | ||
|
||
{ | ||
py::options options; | ||
options.disable_function_signatures(); | ||
|
||
m.def("test_function1", [](int, int) {}, py::arg("a"), py::arg("b")); | ||
m.def("test_function2", [](int, int) {}, py::arg("a"), py::arg("b"), "A custom docstring"); | ||
|
||
options.enable_function_signatures(); | ||
|
||
m.def("test_function3", [](int, int) {}, py::arg("a"), py::arg("b")); | ||
m.def("test_function4", [](int, int) {}, py::arg("a"), py::arg("b"), "A custom docstring"); | ||
|
||
options.disable_function_signatures().disable_user_defined_docstrings(); | ||
|
||
m.def("test_function5", [](int, int) {}, py::arg("a"), py::arg("b"), "A custom docstring"); | ||
|
||
{ | ||
py::options nested_options; | ||
nested_options.enable_user_defined_docstrings(); | ||
m.def("test_function6", [](int, int) {}, py::arg("a"), py::arg("b"), "A custom docstring"); | ||
} | ||
} | ||
|
||
m.def("test_function7", [](int, int) {}, py::arg("a"), py::arg("b"), "A custom docstring"); | ||
|
||
{ | ||
py::options options; | ||
options.disable_user_defined_docstrings(); | ||
|
||
py::class_<DocstringTestFoo>(m, "DocstringTestFoo", "This is a class docstring") | ||
.def_property("value_prop", &DocstringTestFoo::getValue, &DocstringTestFoo::setValue, "This is a property docstring") | ||
; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from pybind11_tests import ConstructorStats | ||
|
||
def test_docstring_options(capture): | ||
from pybind11_tests import (test_function1, test_function2, test_function3, | ||
test_function4, test_function5, test_function6, | ||
test_function7, DocstringTestFoo) | ||
|
||
# options.disable_function_signatures() | ||
assert not test_function1.__doc__ | ||
|
||
assert test_function2.__doc__ == "A custom docstring" | ||
|
||
# options.enable_function_signatures() | ||
assert test_function3.__doc__ .startswith("test_function3(a: int, b: int) -> None") | ||
|
||
assert test_function4.__doc__ .startswith("test_function4(a: int, b: int) -> None") | ||
assert test_function4.__doc__ .endswith("A custom docstring\n") | ||
|
||
# options.disable_function_signatures() | ||
# options.disable_user_defined_docstrings() | ||
assert not test_function5.__doc__ | ||
|
||
# nested options.enable_user_defined_docstrings() | ||
assert test_function6.__doc__ == "A custom docstring" | ||
|
||
# RAII destructor | ||
assert test_function7.__doc__ .startswith("test_function7(a: int, b: int) -> None") | ||
assert test_function7.__doc__ .endswith("A custom docstring\n") | ||
|
||
# Suppression of user-defined docstrings for non-function objects | ||
assert not DocstringTestFoo.__doc__ | ||
assert not DocstringTestFoo.value_prop.__doc__ |