Skip to content

Commit c4e3b13

Browse files
jvansantenstefanseefeld
authored andcommitted
Use qualname for enum repr
1 parent a498e24 commit c4e3b13

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

src/object/enum.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ extern "C"
4949
if (!self->name)
5050
{
5151
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
5355
PyUnicode_FromFormat("%S.%s(%ld)", mod, self_->ob_type->tp_name, PyLong_AsLong(self_));
5456
#else
5557
PyString_FromFormat("%s.%s(%ld)", PyString_AsString(mod), self_->ob_type->tp_name, PyInt_AS_LONG(self_));
@@ -62,7 +64,9 @@ extern "C"
6264
return 0;
6365

6466
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
6670
PyUnicode_FromFormat("%S.%s.%S", mod, self_->ob_type->tp_name, name);
6771
#else
6872
PyString_FromFormat("%s.%s.%s",
@@ -145,6 +149,7 @@ static PyTypeObject enum_type_object = {
145149
};
146150

147151
object module_prefix();
152+
object qualname(const char *name);
148153

149154
namespace
150155
{
@@ -175,6 +180,11 @@ namespace
175180
object module_name = module_prefix();
176181
if (module_name)
177182
d["__module__"] = module_name;
183+
#if PY_VERSION_HEX >= 0x03030000
184+
object q = qualname(name);
185+
if (q)
186+
d["__qualname__"] = q;
187+
#endif
178188
if (doc)
179189
d["__doc__"] = doc;
180190

test/nested.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <boost/python/module.hpp>
66
#include <boost/python/class.hpp>
77
#include <boost/python/def.hpp>
8+
#include <boost/python/enum.hpp>
89
#include <boost/python/operators.hpp>
910
#include <boost/python/scope.hpp>
1011
#include "test_class.hpp"
@@ -17,6 +18,8 @@
1718
typedef test_class<> X;
1819
typedef test_class<1> Y;
1920

21+
enum color { red = 0, blue = 1, green = 2 };
22+
2023
std::ostream& operator<<(std::ostream& s, X const& x)
2124
{
2225
return s << x.value();
@@ -45,6 +48,13 @@ BOOST_PYTHON_MODULE(nested_ext)
4548
class_<Y>("Y", init<int>())
4649
.def(str(self))
4750
;
51+
52+
// so will the enum `color`
53+
enum_<color>("color")
54+
.value("red", red)
55+
.value("green", green)
56+
.value("blue", blue)
57+
;
4858
}
4959

5060
// The generated docstring will use the fully-qualified name of Y

test/nested.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@
2222
>>> X.Y.__name__
2323
'Y'
2424
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+
2534
>>> test_function.__doc__.strip().split('\\n')[0]
2635
'test_function( (X)arg1, (X.Y)arg2) -> None :'
2736

0 commit comments

Comments
 (0)