summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorDong-hee Na <donghee.na@python.org>2022-01-23 15:39:45 (GMT)
committerGitHub <noreply@github.com>2022-01-23 15:39:45 (GMT)
commit76dc047a0e88d10aad0405228d56e94438cdd91c (patch)
tree854e9d3907e40f25d4c5ddaac7a92de44e106cb3 /Objects
parent1f715d5bd3bc9ff444e109b6bbd13011913681b1 (diff)
downloadcpython-76dc047a0e88d10aad0405228d56e94438cdd91c.zip
cpython-76dc047a0e88d10aad0405228d56e94438cdd91c.tar.gz
cpython-76dc047a0e88d10aad0405228d56e94438cdd91c.tar.bz2
bpo-46481: Implement vectorcall for weakref.ref.__call__ method. (GH-30820)
Diffstat (limited to 'Objects')
-rw-r--r--Objects/weakrefobject.c80
1 files changed, 30 insertions, 50 deletions
diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c
index 8922768..b992040 100644
--- a/Objects/weakrefobject.c
+++ b/Objects/weakrefobject.c
@@ -19,6 +19,7 @@ _PyWeakref_GetWeakrefCount(PyWeakReference *head)
return count;
}
+static PyObject *weakref_vectorcall(PyWeakReference *self, PyObject *const *args, size_t nargsf, PyObject *kwnames);
static void
init_weakref(PyWeakReference *self, PyObject *ob, PyObject *callback)
@@ -27,8 +28,8 @@ init_weakref(PyWeakReference *self, PyObject *ob, PyObject *callback)
self->wr_object = ob;
self->wr_prev = NULL;
self->wr_next = NULL;
- Py_XINCREF(callback);
- self->wr_callback = callback;
+ self->wr_callback = Py_XNewRef(callback);
+ self->vectorcall = (vectorcallfunc)weakref_vectorcall;
}
static PyWeakReference *
@@ -128,19 +129,19 @@ gc_clear(PyWeakReference *self)
static PyObject *
-weakref_call(PyWeakReference *self, PyObject *args, PyObject *kw)
+weakref_vectorcall(PyWeakReference *self, PyObject *const *args,
+ size_t nargsf, PyObject *kwnames)
{
- static char *kwlist[] = {NULL};
-
- if (PyArg_ParseTupleAndKeywords(args, kw, ":__call__", kwlist)) {
- PyObject *object = PyWeakref_GET_OBJECT(self);
- Py_INCREF(object);
- return (object);
+ if (!_PyArg_NoKwnames("weakref", kwnames)) {
+ return NULL;
+ }
+ Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
+ if (!_PyArg_CheckPositional("weakref", nargs, 0, 0)) {
+ return NULL;
}
- return NULL;
+ return Py_NewRef(PyWeakref_GET_OBJECT(self));
}
-
static Py_hash_t
weakref_hash(PyWeakReference *self)
{
@@ -371,45 +372,24 @@ static PyMethodDef weakref_methods[] = {
PyTypeObject
_PyWeakref_RefType = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "weakref",
- sizeof(PyWeakReference),
- 0,
- weakref_dealloc, /*tp_dealloc*/
- 0, /*tp_vectorcall_offset*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_as_async*/
- (reprfunc)weakref_repr, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- (hashfunc)weakref_hash, /*tp_hash*/
- (ternaryfunc)weakref_call, /*tp_call*/
- 0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
- | Py_TPFLAGS_BASETYPE, /*tp_flags*/
- 0, /*tp_doc*/
- (traverseproc)gc_traverse, /*tp_traverse*/
- (inquiry)gc_clear, /*tp_clear*/
- (richcmpfunc)weakref_richcompare, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- weakref_methods, /*tp_methods*/
- weakref_members, /*tp_members*/
- 0, /*tp_getset*/
- 0, /*tp_base*/
- 0, /*tp_dict*/
- 0, /*tp_descr_get*/
- 0, /*tp_descr_set*/
- 0, /*tp_dictoffset*/
- weakref___init__, /*tp_init*/
- PyType_GenericAlloc, /*tp_alloc*/
- weakref___new__, /*tp_new*/
- PyObject_GC_Del, /*tp_free*/
+ .tp_name = "weakref",
+ .tp_basicsize = sizeof(PyWeakReference),
+ .tp_dealloc = weakref_dealloc,
+ .tp_vectorcall_offset = offsetof(PyWeakReference, vectorcall),
+ .tp_call = PyVectorcall_Call,
+ .tp_repr = (reprfunc)weakref_repr,
+ .tp_hash = (hashfunc)weakref_hash,
+ .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_HAVE_VECTORCALL | Py_TPFLAGS_BASETYPE,
+ .tp_traverse = (traverseproc)gc_traverse,
+ .tp_clear = (inquiry)gc_clear,
+ .tp_richcompare = (richcmpfunc)weakref_richcompare,
+ .tp_methods = weakref_methods,
+ .tp_members = weakref_members,
+ .tp_init = weakref___init__,
+ .tp_alloc = PyType_GenericAlloc,
+ .tp_new = weakref___new__,
+ .tp_free = PyObject_GC_Del,
};