← 返回首页
[WIP] Add support for instance descriptors by larryhastings · Pull Request #3534 · python/cpython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (5) .h  (1) .py  (1) All 3 file types selected Viewed files
Conversations
Failed to load comments. Retry
Loading
Jump to
Jump to file
Failed to load files. Retry
Loading
Diff view
Unified
Split
Hide whitespace
Apply and reload
Show whitespace
Diff view
Unified
Split
Hide whitespace
Apply and reload
8 changes: 8 additions & 0 deletions Include/object.h
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,8 @@ PyAPI_FUNC(void) PyType_Modified(PyTypeObject *);
#ifndef Py_LIMITED_API
PyAPI_FUNC(PyObject *) _PyType_GetDocFromInternalDoc(const char *, const char *);
PyAPI_FUNC(PyObject *) _PyType_GetTextSignatureFromInternalDoc(const char *, const char *);

PyAPI_FUNC(int) _PyType_SetAttrObject(PyTypeObject *type, PyObject *name, PyObject *value);
#endif

/* Generic operations on objects */
Expand Down Expand Up @@ -566,6 +568,8 @@ PyAPI_FUNC(int) PyObject_CallFinalizerFromDealloc(PyObject *);
/* Same as PyObject_Generic{Get,Set}Attr, but passing the attributes
dict as the last parameter. */
PyAPI_FUNC(PyObject *)
_PyObject_GenericGetAttrWithDictNoError(PyObject *obj, PyObject *name, PyObject *dict);
PyAPI_FUNC(PyObject *)
_PyObject_GenericGetAttrWithDict(PyObject *, PyObject *, PyObject *);
PyAPI_FUNC(int)
_PyObject_GenericSetAttrWithDict(PyObject *, PyObject *,
Expand Down Expand Up @@ -646,6 +650,7 @@ given type object has a specified feature.
#define Py_TPFLAGS_IS_ABSTRACT (1UL << 20)

/* These flags are used to determine if a type is a subclass. */
#define Py_TPFLAGS_INSTANCE_DESCRIPTOR_SUBCLASS (1UL << 23)
#define Py_TPFLAGS_LONG_SUBCLASS (1UL << 24)
#define Py_TPFLAGS_LIST_SUBCLASS (1UL << 25)
#define Py_TPFLAGS_TUPLE_SUBCLASS (1UL << 26)
Expand Down Expand Up @@ -1071,6 +1076,9 @@ PyAPI_FUNC(void)
_PyObject_DebugTypeStats(FILE *out);
#endif /* ifndef Py_LIMITED_API */

/* Instance descriptor base type */
PyAPI_DATA(PyTypeObject) PyInstanceDescriptor_Type;

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 2 additions & 0 deletions Lib/_collections_abc.py
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from abc import ABCMeta, abstractmethod
import sys
from _collections import _InstanceDescriptor as InstanceDescriptor

__all__ = ["Awaitable", "Coroutine",
"AsyncIterable", "AsyncIterator", "AsyncGenerator",
Expand All @@ -18,6 +19,7 @@
"MappingView", "KeysView", "ItemsView", "ValuesView",
"Sequence", "MutableSequence",
"ByteString",
"InstanceDescriptor",
]

# This module has been renamed from collections.abc to _collections_abc to
Expand Down
5 changes: 5 additions & 0 deletions Modules/_collectionsmodule.c
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2414,5 +2414,10 @@ PyInit__collections(void)
Py_INCREF(&dequereviter_type);
PyModule_AddObject(m, "_deque_reverse_iterator", (PyObject *)&dequereviter_type);

if (PyType_Ready(&PyInstanceDescriptor_Type) < 0)
return NULL;
Py_INCREF(&PyInstanceDescriptor_Type);
PyModule_AddObject(m, "_InstanceDescriptor", (PyObject *)&PyInstanceDescriptor_Type);

return m;
}
4 changes: 2 additions & 2 deletions Objects/descrobject.c
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1674,8 +1674,8 @@ PyTypeObject PyProperty_Type = {
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_BASETYPE, /* tp_flags */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | /* tp_flags */
Py_TPFLAGS_BASETYPE | Py_TPFLAGS_INSTANCE_DESCRIPTOR_SUBCLASS,
property_init__doc__, /* tp_doc */
property_traverse, /* tp_traverse */
(inquiry)property_clear, /* tp_clear */
Expand Down
90 changes: 79 additions & 11 deletions Objects/moduleobject.c
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -669,31 +669,99 @@ module_repr(PyModuleObject *m)
return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
}


static PyObject*
module_getattro(PyModuleObject *m, PyObject *name)
module_getattro_raise(PyModuleObject *m, PyObject *name,
const char *fmt_with_module, const char *fmt_without_module)
{
PyObject *attr, *mod_name;
attr = PyObject_GenericGetAttr((PyObject *)m, name);
if (attr || !PyErr_ExceptionMatches(PyExc_AttributeError))
return attr;
PyErr_Clear();
PyObject *mod_name;

if (m->md_dict) {
_Py_IDENTIFIER(__name__);
mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__);
if (mod_name) {
PyErr_Format(PyExc_AttributeError,
"module '%U' has no attribute '%U'", mod_name, name);
fmt_with_module, mod_name, name);
return NULL;
}
else if (PyErr_Occurred()) {

if (PyErr_Occurred()) {
PyErr_Clear();
}
}
PyErr_Format(PyExc_AttributeError,
"module has no attribute '%U'", name);

PyErr_Format(PyExc_AttributeError, fmt_without_module, name);
return NULL;
}

static PyObject*
module_getattro(PyModuleObject *m, PyObject *name)
{
PyObject *attr = PyObject_GenericGetAttr((PyObject *)m, name);
if (attr) {
if (!PyType_FastSubclass(Py_TYPE(attr),
Py_TPFLAGS_INSTANCE_DESCRIPTOR_SUBCLASS)) {
return attr;
}

descrgetfunc f = attr->ob_type->tp_descr_get;
if (f) {
PyObject *res = f(attr, (PyObject *)m,
(PyObject *)m->ob_base.ob_type);

if (res) {
return res;
}

/* don't stomp on an existing error */
if (PyErr_Occurred()) {
return NULL;
}

return module_getattro_raise(m, name,
"module '%U' property '%U' is invalid",
"module property '%U' is invalid");
}

return module_getattro_raise(m, name,
"module '%U' property '%U' is not readable",
"module property '%U' is not readable");
}

if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
return NULL;
}

PyErr_Clear();
return module_getattro_raise(m, name,
"module '%U' has no attribute '%U'",
"module has no attribute '%U'");
}

static int
module_setattro(PyModuleObject *m, PyObject *name, PyObject *value)
{
PyObject *attr = _PyObject_GenericGetAttrWithDictNoError((PyObject *)m, name, NULL);
if (!attr) {
PyErr_Clear();
}
else if (PyType_FastSubclass(Py_TYPE(attr), Py_TPFLAGS_INSTANCE_DESCRIPTOR_SUBCLASS)) {
descrsetfunc f = attr->ob_type->tp_descr_set;
if (f) {
int res = f(attr, (PyObject *)m, value);
if (res) {
module_getattro_raise(m, name,
"module '%U' property '%U' is invalid",
"module property '%U' is invalid");
return -1;
}
return res;
}
}

return _PyObject_GenericSetAttrWithDict((PyObject *)m, name, value, NULL);
}

static int
module_traverse(PyModuleObject *m, visitproc visit, void *arg)
{
Expand Down Expand Up @@ -765,7 +833,7 @@ PyTypeObject PyModule_Type = {
0, /* tp_call */
0, /* tp_str */
(getattrofunc)module_getattro, /* tp_getattro */
PyObject_GenericSetAttr, /* tp_setattro */
(setattrofunc)module_setattro, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_BASETYPE, /* tp_flags */
Expand Down
20 changes: 16 additions & 4 deletions Objects/object.c
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
/* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */

PyObject *
_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, PyObject *dict)
_PyObject_GenericGetAttrWithDictNoError(PyObject *obj, PyObject *name, PyObject *dict)
{
/* Make sure the logic of _PyObject_GetMethod is in sync with
this method.
Expand Down Expand Up @@ -1194,15 +1194,27 @@ _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, PyObject *dict)
goto done;
}

PyErr_Format(PyExc_AttributeError,
"'%.50s' object has no attribute '%U'",
tp->tp_name, name);
done:
Py_XDECREF(descr);
Py_DECREF(name);
return res;
}

PyObject *
_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, PyObject *dict)
{
Py_INCREF(name);
PyObject *res = _PyObject_GenericGetAttrWithDictNoError(obj, name, dict);
if (!(res || PyErr_Occurred())) {
PyTypeObject *tp = Py_TYPE(obj);
PyErr_Format(PyExc_AttributeError,
"'%.50s' object has no attribute '%U'",
tp->tp_name, name);
}
Py_DECREF(name);
return res;
}

PyObject *
PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
{
Expand Down
56 changes: 56 additions & 0 deletions Objects/typeobject.c
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3154,6 +3154,13 @@ type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
return res;
}

int
_PyType_SetAttrObject(PyTypeObject *type, PyObject *name, PyObject *value)
{
return type_setattro(type, name, value);
}


extern void
_PyDictKeys_DecRef(PyDictKeysObject *keys);

Expand Down Expand Up @@ -4645,6 +4652,53 @@ PyTypeObject PyBaseObject_Type = {
};


PyDoc_STRVAR(PyInstanceDescriptor_Type__doc__,
"InstanceDescriptor base class");

PyTypeObject PyInstanceDescriptor_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"InstanceDescriptor", /* tp_name */
sizeof(PyObject), /* tp_basicsize */
0, /* tp_itemsize */
object_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
object_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
(hashfunc)_Py_HashPointer, /* tp_hash */
0, /* tp_call */
object_str, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
PyObject_GenericSetAttr, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
Py_TPFLAGS_INSTANCE_DESCRIPTOR_SUBCLASS, /* tp_flags */
PyInstanceDescriptor_Type__doc__, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
object_richcompare, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
object_methods, /* tp_methods */
0, /* tp_members */
object_getsets, /* tp_getset */
&PyBaseObject_Type, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
object_init, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
object_new, /* tp_new */
PyObject_Del, /* tp_free */
};


/* Add the methods from tp_methods to the __dict__ in a type object */

static int
Expand Down Expand Up @@ -4797,6 +4851,8 @@ inherit_special(PyTypeObject *type, PyTypeObject *base)
type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS;
else if (PyType_IsSubtype(base, &PyDict_Type))
type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS;
else if (PyType_IsSubtype(base, &PyInstanceDescriptor_Type))
type->tp_flags |= Py_TPFLAGS_INSTANCE_DESCRIPTOR_SUBCLASS;
}

static int
Expand Down
Toggle all file notes Toggle all file annotations

Footer

© 2026 GitHub, Inc.