Skip to content

Commit 58b1a01

Browse files
jvansantenstefanseefeld
authored andcommitted
Set __qualname__ for Python >= 3.3
1 parent 8ca8724 commit 58b1a01

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/object/class.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,16 @@ namespace objects
502502
);
503503
}
504504

505+
str qualname(const char *name)
506+
{
507+
#if PY_VERSION_HEX >= 0x03030000
508+
if (PyObject_HasAttrString(scope().ptr(), "__qualname__")) {
509+
return str("%s.%s" % make_tuple(scope().attr("__qualname__"), name));
510+
}
511+
#endif
512+
return str(name);
513+
}
514+
505515
namespace
506516
{
507517
// Find a registered class object corresponding to id. Return a
@@ -564,6 +574,9 @@ namespace objects
564574

565575
object m = module_prefix();
566576
if (m) d["__module__"] = m;
577+
#if PY_VERSION_HEX >= 0x03030000
578+
d["__qualname__"] = qualname(name);
579+
#endif
567580

568581
if (doc != 0)
569582
d["__doc__"] = doc;

test/nested.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
'X'
1515
1616
>>> X.Y
17-
<class 'nested_ext.Y'>
17+
<class 'nested_ext.X.Y'>
1818
1919
>>> X.Y.__module__
2020
'nested_ext'

0 commit comments

Comments
 (0)