Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 86a36b5

Browse files
committed
PEP 3155 / issue #13448: Qualified name for classes and functions.
1 parent 0e86a58 commit 86a36b5

21 files changed

Lines changed: 322 additions & 43 deletions

Doc/c-api/function.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ There are a few functions specific to Python functions.
3838
object, the argument defaults and closure are set to *NULL*.
3939
4040
41+
.. c:function:: PyObject* PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
42+
43+
As :c:func:`PyFunction_New`, but also allows to set the function object's
44+
``__qualname__`` attribute. *qualname* should be a unicode object or NULL;
45+
if NULL, the ``__qualname__`` attribute is set to the same value as its
46+
``__name__`` attribute.
47+
48+
.. versionadded:: 3.3
49+
50+
4151
.. c:function:: PyObject* PyFunction_GetCode(PyObject *op)
4252
4353
Return the code object associated with the function object *op*.

Doc/data/refcounts.dat

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,11 @@ PyFunction_New:PyObject*::+1:
465465
PyFunction_New:PyObject*:code:+1:
466466
PyFunction_New:PyObject*:globals:+1:
467467

468+
PyFunction_NewWithQualName:PyObject*::+1:
469+
PyFunction_NewWithQualName:PyObject*:code:+1:
470+
PyFunction_NewWithQualName:PyObject*:globals:+1:
471+
PyFunction_NewWithQualName:PyObject*:qualname:+1:
472+
468473
PyFunction_SetClosure:int:::
469474
PyFunction_SetClosure:PyObject*:op:0:
470475
PyFunction_SetClosure:PyObject*:closure:+1:

Doc/glossary.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,24 @@ Glossary
544544
for piece in food:
545545
print(piece)
546546

547+
qualified name
548+
A dotted name showing the "path" from a module's global scope to a
549+
class, function or method defined in that module, as defined in
550+
:pep:`3155`. For top-level functions and classes, the qualified name
551+
is the same as the object's name::
552+
553+
>>> class C:
554+
... class D:
555+
... def meth(self):
556+
... pass
557+
...
558+
>>> C.__qualname__
559+
'C'
560+
>>> C.D.__qualname__
561+
'C.D'
562+
>>> C.D.meth.__qualname__
563+
'C.D.meth'
564+
547565
reference count
548566
The number of references to an object. When the reference count of an
549567
object drops to zero, it is deallocated. Reference counting is

Doc/library/stdtypes.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2824,6 +2824,13 @@ types, where they are relevant. Some of these are not reported by the
28242824
The name of the class or type.
28252825

28262826

2827+
.. attribute:: class.__qualname__
2828+
2829+
The :term:`qualified name` of the class or type.
2830+
2831+
.. versionadded:: 3.3
2832+
2833+
28272834
.. attribute:: class.__mro__
28282835

28292836
This attribute is a tuple of classes that are considered when looking for

Doc/reference/datamodel.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,11 @@ Callable types
448448
+-------------------------+-------------------------------+-----------+
449449
| :attr:`__name__` | The function's name | Writable |
450450
+-------------------------+-------------------------------+-----------+
451+
| :attr:`__qualname__` | The function's | Writable |
452+
| | :term:`qualified name` | |
453+
| | | |
454+
| | .. versionadded:: 3.3 | |
455+
+-------------------------+-------------------------------+-----------+
451456
| :attr:`__module__` | The name of the module the | Writable |
452457
| | function was defined in, or | |
453458
| | ``None`` if unavailable. | |

Include/funcobject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ typedef struct {
3131
PyObject *func_weakreflist; /* List of weak references */
3232
PyObject *func_module; /* The __module__ attribute, can be anything */
3333
PyObject *func_annotations; /* Annotations, a dict or NULL */
34+
PyObject *func_qualname; /* The qualified name */
3435

3536
/* Invariant:
3637
* func_closure contains the bindings for func_code->co_freevars, so
@@ -44,6 +45,7 @@ PyAPI_DATA(PyTypeObject) PyFunction_Type;
4445
#define PyFunction_Check(op) (Py_TYPE(op) == &PyFunction_Type)
4546

4647
PyAPI_FUNC(PyObject *) PyFunction_New(PyObject *, PyObject *);
48+
PyAPI_FUNC(PyObject *) PyFunction_NewWithQualName(PyObject *, PyObject *, PyObject *);
4749
PyAPI_FUNC(PyObject *) PyFunction_GetCode(PyObject *);
4850
PyAPI_FUNC(PyObject *) PyFunction_GetGlobals(PyObject *);
4951
PyAPI_FUNC(PyObject *) PyFunction_GetModule(PyObject *);

Include/object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ typedef struct _heaptypeobject {
418418
a given operator (e.g. __getitem__).
419419
see add_operators() in typeobject.c . */
420420
PyBufferProcs as_buffer;
421-
PyObject *ht_name, *ht_slots;
421+
PyObject *ht_name, *ht_slots, *ht_qualname;
422422
/* here are optional user slots, followed by the members. */
423423
} PyHeapTypeObject;
424424

Lib/pydoc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def visiblename(name, all=None, obj=None):
166166
if name in {'__builtins__', '__doc__', '__file__', '__path__',
167167
'__module__', '__name__', '__slots__', '__package__',
168168
'__cached__', '__author__', '__credits__', '__date__',
169-
'__version__'}:
169+
'__version__', '__qualname__'}:
170170
return 0
171171
# Private names are hidden, but special names are displayed.
172172
if name.startswith('__') and name.endswith('__'): return 1

Lib/test/test_code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
freevars: ()
1717
nlocals: 2
1818
flags: 3
19-
consts: ('None', '<code object g>')
19+
consts: ('None', '<code object g>', "'f.<locals>.g'")
2020
2121
>>> dump(f(4).__code__)
2222
name: g

Lib/test/test_descr.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4492,9 +4492,14 @@ class C(metaclass=M):
44924492
self.assertEqual(type(C.__dict__), type(B.__dict__))
44934493

44944494
def test_repr(self):
4495-
# Testing dict_proxy.__repr__
4496-
dict_ = {k: v for k, v in self.C.__dict__.items()}
4497-
self.assertEqual(repr(self.C.__dict__), 'dict_proxy({!r})'.format(dict_))
4495+
# Testing dict_proxy.__repr__.
4496+
# We can't blindly compare with the repr of another dict as ordering
4497+
# of keys and values is arbitrary and may differ.
4498+
r = repr(self.C.__dict__)
4499+
self.assertTrue(r.startswith('dict_proxy('), r)
4500+
self.assertTrue(r.endswith(')'), r)
4501+
for k, v in self.C.__dict__.items():
4502+
self.assertIn('{!r}: {!r}'.format(k, v), r)
44984503

44994504

45004505
class PTypesLongInitTest(unittest.TestCase):

0 commit comments

Comments
 (0)