2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-19 16:32:16 +00:00

Use qualname for enum repr

This commit is contained in:
Jakob van Santen
2023-04-25 16:29:12 +02:00
committed by Stefan Seefeld
parent a498e2458c
commit c4e3b13dc2
3 changed files with 31 additions and 2 deletions

View File

@@ -49,7 +49,9 @@ extern "C"
if (!self->name)
{
return
#if PY_VERSION_HEX >= 0x03000000
#if PY_VERSION_HEX >= 0x03030000
PyUnicode_FromFormat("%S.%S(%ld)", mod, ((PyHeapTypeObject*)(self_->ob_type))->ht_qualname, PyLong_AsLong(self_));
#elif PY_VERSION_HEX >= 0x03000000
PyUnicode_FromFormat("%S.%s(%ld)", mod, self_->ob_type->tp_name, PyLong_AsLong(self_));
#else
PyString_FromFormat("%s.%s(%ld)", PyString_AsString(mod), self_->ob_type->tp_name, PyInt_AS_LONG(self_));
@@ -62,7 +64,9 @@ extern "C"
return 0;
return
#if PY_VERSION_HEX >= 0x03000000
#if PY_VERSION_HEX >= 0x03030000
PyUnicode_FromFormat("%S.%S.%S", mod, ((PyHeapTypeObject*)(self_->ob_type))->ht_qualname, name);
#elif PY_VERSION_HEX >= 0x03000000
PyUnicode_FromFormat("%S.%s.%S", mod, self_->ob_type->tp_name, name);
#else
PyString_FromFormat("%s.%s.%s",
@@ -145,6 +149,7 @@ static PyTypeObject enum_type_object = {
};
object module_prefix();
object qualname(const char *name);
namespace
{
@@ -175,6 +180,11 @@ namespace
object module_name = module_prefix();
if (module_name)
d["__module__"] = module_name;
#if PY_VERSION_HEX >= 0x03030000
object q = qualname(name);
if (q)
d["__qualname__"] = q;
#endif
if (doc)
d["__doc__"] = doc;

View File

@@ -5,6 +5,7 @@
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/def.hpp>
#include <boost/python/enum.hpp>
#include <boost/python/operators.hpp>
#include <boost/python/scope.hpp>
#include "test_class.hpp"
@@ -17,6 +18,8 @@
typedef test_class<> X;
typedef test_class<1> Y;
enum color { red = 0, blue = 1, green = 2 };
std::ostream& operator<<(std::ostream& s, X const& x)
{
return s << x.value();
@@ -45,6 +48,13 @@ BOOST_PYTHON_MODULE(nested_ext)
class_<Y>("Y", init<int>())
.def(str(self))
;
// so will the enum `color`
enum_<color>("color")
.value("red", red)
.value("green", green)
.value("blue", blue)
;
}
// The generated docstring will use the fully-qualified name of Y

View File

@@ -22,6 +22,15 @@
>>> X.Y.__name__
'Y'
>>> X.color.__qualname__
'X.color'
>>> repr(X.color.red)
'nested_ext.X.color.red'
>>> repr(X.color(1))
'nested_ext.X.color(1)'
>>> test_function.__doc__.strip().split('\\n')[0]
'test_function( (X)arg1, (X.Y)arg2) -> None :'