GeezerVison is showing github.com — Use qualname for enum repr · boostorg/python@c4e3b13
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/object/enum.cpp +12-2Lines changed: 12 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -49,7 +49,9 @@ extern "C" | 49 | 49 | if (!self->name) | 50 | 50 | { | 51 | 51 | return | 52 | | -#if PY_VERSION_HEX >= 0x03000000 | | 52 | +#if PY_VERSION_HEX >= 0x03030000 | | 53 | + PyUnicode_FromFormat("%S.%S(%ld)", mod, ((PyHeapTypeObject*)(self_->ob_type))->ht_qualname, PyLong_AsLong(self_)); | | 54 | +#elif PY_VERSION_HEX >= 0x03000000 | 53 | 55 | PyUnicode_FromFormat("%S.%s(%ld)", mod, self_->ob_type->tp_name, PyLong_AsLong(self_)); | 54 | 56 | #else | 55 | 57 | PyString_FromFormat("%s.%s(%ld)", PyString_AsString(mod), self_->ob_type->tp_name, PyInt_AS_LONG(self_)); | @@ -62,7 +64,9 @@ extern "C" | 62 | 64 | return 0; | 63 | 65 |
| 64 | 66 | return | 65 | | -#if PY_VERSION_HEX >= 0x03000000 | | 67 | +#if PY_VERSION_HEX >= 0x03030000 | | 68 | + PyUnicode_FromFormat("%S.%S.%S", mod, ((PyHeapTypeObject*)(self_->ob_type))->ht_qualname, name); | | 69 | +#elif PY_VERSION_HEX >= 0x03000000 | 66 | 70 | PyUnicode_FromFormat("%S.%s.%S", mod, self_->ob_type->tp_name, name); | 67 | 71 | #else | 68 | 72 | PyString_FromFormat("%s.%s.%s", | @@ -145,6 +149,7 @@ static PyTypeObject enum_type_object = { | 145 | 149 | }; | 146 | 150 |
| 147 | 151 | object module_prefix(); | | 152 | +object qualname(const char *name); | 148 | 153 |
| 149 | 154 | namespace | 150 | 155 | { | @@ -175,6 +180,11 @@ namespace | 175 | 180 | object module_name = module_prefix(); | 176 | 181 | if (module_name) | 177 | 182 | d["__module__"] = module_name; | | 183 | +#if PY_VERSION_HEX >= 0x03030000 | | 184 | + object q = qualname(name); | | 185 | + if (q) | | 186 | + d["__qualname__"] = q; | | 187 | +#endif | 178 | 188 | if (doc) | 179 | 189 | d["__doc__"] = doc; | 180 | 190 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/nested.cpp +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | #include <boost/python/module.hpp> | 6 | 6 | #include <boost/python/class.hpp> | 7 | 7 | #include <boost/python/def.hpp> | | 8 | +#include <boost/python/enum.hpp> | 8 | 9 | #include <boost/python/operators.hpp> | 9 | 10 | #include <boost/python/scope.hpp> | 10 | 11 | #include "test_class.hpp" | | 17 | 18 | typedef test_class<> X; | 18 | 19 | typedef test_class<1> Y; | 19 | 20 |
| | 21 | +enum color { red = 0, blue = 1, green = 2 }; | | 22 | + | 20 | 23 | std::ostream& operator<<(std::ostream& s, X const& x) | 21 | 24 | { | 22 | 25 | return s << x.value(); | @@ -45,6 +48,13 @@ BOOST_PYTHON_MODULE(nested_ext) | 45 | 48 | class_<Y>("Y", init<int>()) | 46 | 49 | .def(str(self)) | 47 | 50 | ; | | 51 | + | | 52 | + // so will the enum `color` | | 53 | + enum_<color>("color") | | 54 | + .value("red", red) | | 55 | + .value("green", green) | | 56 | + .value("blue", blue) | | 57 | + ; | 48 | 58 | } | 49 | 59 |
| 50 | 60 | // The generated docstring will use the fully-qualified name of Y | |
Collapse file Copy file name to clipboardExpand all lines: test/nested.py +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | >>> X.Y.__name__ | 23 | 23 | 'Y' | 24 | 24 |
| | 25 | + >>> X.color.__qualname__ | | 26 | + 'X.color' | | 27 | +
| | 28 | + >>> repr(X.color.red) | | 29 | + 'nested_ext.X.color.red' | | 30 | +
| | 31 | + >>> repr(X.color(1)) | | 32 | + 'nested_ext.X.color(1)' | | 33 | +
| 25 | 34 | >>> test_function.__doc__.strip().split('\\n')[0] | 26 | 35 | 'test_function( (X)arg1, (X.Y)arg2) -> None :' | 27 | 36 | | |
|