|
7 | 7 | #include "pycore_pystate.h" // _PyThreadState_GET() |
8 | 8 | #include "structmember.h" // PyMemberDef |
9 | 9 |
|
| 10 | +#include "clinic/classobject.c.h" |
| 11 | + |
10 | 12 | #define TP_DESCR_GET(t) ((t)->tp_descr_get) |
11 | 13 |
|
| 14 | +/*[clinic input] |
| 15 | +class method "PyMethodObject *" "&PyMethod_Type" |
| 16 | +[clinic start generated code]*/ |
| 17 | +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b16e47edf6107c23]*/ |
| 18 | + |
12 | 19 |
|
13 | 20 | PyObject * |
14 | 21 | PyMethod_Function(PyObject *im) |
@@ -115,23 +122,26 @@ PyMethod_New(PyObject *func, PyObject *self) |
115 | 122 | return (PyObject *)im; |
116 | 123 | } |
117 | 124 |
|
| 125 | +/*[clinic input] |
| 126 | +method.__reduce__ |
| 127 | +[clinic start generated code]*/ |
| 128 | + |
118 | 129 | static PyObject * |
119 | | -method_reduce(PyMethodObject *im, PyObject *Py_UNUSED(ignored)) |
| 130 | +method___reduce___impl(PyMethodObject *self) |
| 131 | +/*[clinic end generated code: output=6c04506d0fa6fdcb input=143a0bf5e96de6e8]*/ |
120 | 132 | { |
121 | | - PyObject *self = PyMethod_GET_SELF(im); |
122 | | - PyObject *func = PyMethod_GET_FUNCTION(im); |
123 | | - PyObject *funcname; |
124 | | - |
125 | | - funcname = PyObject_GetAttr(func, &_Py_ID(__name__)); |
| 133 | + PyObject *funcself = PyMethod_GET_SELF(self); |
| 134 | + PyObject *func = PyMethod_GET_FUNCTION(self); |
| 135 | + PyObject *funcname = PyObject_GetAttr(func, &_Py_ID(__name__)); |
126 | 136 | if (funcname == NULL) { |
127 | 137 | return NULL; |
128 | 138 | } |
129 | 139 | return Py_BuildValue( |
130 | | - "N(ON)", _PyEval_GetBuiltin(&_Py_ID(getattr)), self, funcname); |
| 140 | + "N(ON)", _PyEval_GetBuiltin(&_Py_ID(getattr)), funcself, funcname); |
131 | 141 | } |
132 | 142 |
|
133 | 143 | static PyMethodDef method_methods[] = { |
134 | | - {"__reduce__", (PyCFunction)method_reduce, METH_NOARGS, NULL}, |
| 144 | + METHOD___REDUCE___METHODDEF |
135 | 145 | {NULL, NULL} |
136 | 146 | }; |
137 | 147 |
|
@@ -193,34 +203,32 @@ method_getattro(PyObject *obj, PyObject *name) |
193 | 203 | return PyObject_GetAttr(im->im_func, name); |
194 | 204 | } |
195 | 205 |
|
196 | | -PyDoc_STRVAR(method_doc, |
197 | | -"method(function, instance)\n\ |
198 | | -\n\ |
199 | | -Create a bound instance method object."); |
| 206 | +/*[clinic input] |
| 207 | +@classmethod |
| 208 | +method.__new__ as method_new |
| 209 | + function: object |
| 210 | + instance: object |
| 211 | + / |
| 212 | +
|
| 213 | +Create a bound instance method object. |
| 214 | +[clinic start generated code]*/ |
200 | 215 |
|
201 | 216 | static PyObject * |
202 | | -method_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
| 217 | +method_new_impl(PyTypeObject *type, PyObject *function, PyObject *instance) |
| 218 | +/*[clinic end generated code: output=d33ef4ebf702e1f7 input=4e32facc3c3108ae]*/ |
203 | 219 | { |
204 | | - PyObject *func; |
205 | | - PyObject *self; |
206 | | - |
207 | | - if (!_PyArg_NoKeywords("method", kw)) |
208 | | - return NULL; |
209 | | - if (!PyArg_UnpackTuple(args, "method", 2, 2, |
210 | | - &func, &self)) |
211 | | - return NULL; |
212 | | - if (!PyCallable_Check(func)) { |
| 220 | + if (!PyCallable_Check(function)) { |
213 | 221 | PyErr_SetString(PyExc_TypeError, |
214 | 222 | "first argument must be callable"); |
215 | 223 | return NULL; |
216 | 224 | } |
217 | | - if (self == NULL || self == Py_None) { |
| 225 | + if (instance == NULL || instance == Py_None) { |
218 | 226 | PyErr_SetString(PyExc_TypeError, |
219 | | - "self must not be None"); |
| 227 | + "instance must not be None"); |
220 | 228 | return NULL; |
221 | 229 | } |
222 | 230 |
|
223 | | - return PyMethod_New(func, self); |
| 231 | + return PyMethod_New(function, instance); |
224 | 232 | } |
225 | 233 |
|
226 | 234 | static void |
@@ -322,50 +330,37 @@ method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls) |
322 | 330 |
|
323 | 331 | PyTypeObject PyMethod_Type = { |
324 | 332 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
325 | | - "method", |
326 | | - sizeof(PyMethodObject), |
327 | | - 0, |
328 | | - (destructor)method_dealloc, /* tp_dealloc */ |
329 | | - offsetof(PyMethodObject, vectorcall), /* tp_vectorcall_offset */ |
330 | | - 0, /* tp_getattr */ |
331 | | - 0, /* tp_setattr */ |
332 | | - 0, /* tp_as_async */ |
333 | | - (reprfunc)method_repr, /* tp_repr */ |
334 | | - 0, /* tp_as_number */ |
335 | | - 0, /* tp_as_sequence */ |
336 | | - 0, /* tp_as_mapping */ |
337 | | - (hashfunc)method_hash, /* tp_hash */ |
338 | | - PyVectorcall_Call, /* tp_call */ |
339 | | - 0, /* tp_str */ |
340 | | - method_getattro, /* tp_getattro */ |
341 | | - PyObject_GenericSetAttr, /* tp_setattro */ |
342 | | - 0, /* tp_as_buffer */ |
343 | | - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
344 | | - Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */ |
345 | | - method_doc, /* tp_doc */ |
346 | | - (traverseproc)method_traverse, /* tp_traverse */ |
347 | | - 0, /* tp_clear */ |
348 | | - method_richcompare, /* tp_richcompare */ |
349 | | - offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */ |
350 | | - 0, /* tp_iter */ |
351 | | - 0, /* tp_iternext */ |
352 | | - method_methods, /* tp_methods */ |
353 | | - method_memberlist, /* tp_members */ |
354 | | - method_getset, /* tp_getset */ |
355 | | - 0, /* tp_base */ |
356 | | - 0, /* tp_dict */ |
357 | | - method_descr_get, /* tp_descr_get */ |
358 | | - 0, /* tp_descr_set */ |
359 | | - 0, /* tp_dictoffset */ |
360 | | - 0, /* tp_init */ |
361 | | - 0, /* tp_alloc */ |
362 | | - method_new, /* tp_new */ |
| 333 | + .tp_name = "method", |
| 334 | + .tp_basicsize = sizeof(PyMethodObject), |
| 335 | + .tp_dealloc = (destructor)method_dealloc, |
| 336 | + .tp_vectorcall_offset = offsetof(PyMethodObject, vectorcall), |
| 337 | + .tp_repr = (reprfunc)method_repr, |
| 338 | + .tp_hash = (hashfunc)method_hash, |
| 339 | + .tp_call = PyVectorcall_Call, |
| 340 | + .tp_getattro = method_getattro, |
| 341 | + .tp_setattro = PyObject_GenericSetAttr, |
| 342 | + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 343 | + Py_TPFLAGS_HAVE_VECTORCALL, |
| 344 | + .tp_doc = method_new__doc__, |
| 345 | + .tp_traverse = (traverseproc)method_traverse, |
| 346 | + .tp_richcompare = method_richcompare, |
| 347 | + .tp_weaklistoffset = offsetof(PyMethodObject, im_weakreflist), |
| 348 | + .tp_methods = method_methods, |
| 349 | + .tp_members = method_memberlist, |
| 350 | + .tp_getset = method_getset, |
| 351 | + .tp_descr_get = method_descr_get, |
| 352 | + .tp_new = method_new, |
363 | 353 | }; |
364 | 354 |
|
365 | 355 | /* ------------------------------------------------------------------------ |
366 | 356 | * instance method |
367 | 357 | */ |
368 | 358 |
|
| 359 | +/*[clinic input] |
| 360 | +class instancemethod "PyInstanceMethodObject *" "&PyInstanceMethod_Type" |
| 361 | +[clinic start generated code]*/ |
| 362 | +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=28c9762a9016f4d2]*/ |
| 363 | + |
369 | 364 | PyObject * |
370 | 365 | PyInstanceMethod_New(PyObject *func) { |
371 | 366 | PyInstanceMethodObject *method; |
@@ -516,67 +511,43 @@ instancemethod_repr(PyObject *self) |
516 | 511 | return result; |
517 | 512 | } |
518 | 513 |
|
519 | | -PyDoc_STRVAR(instancemethod_doc, |
520 | | -"instancemethod(function)\n\ |
521 | | -\n\ |
522 | | -Bind a function to a class."); |
| 514 | +/*[clinic input] |
| 515 | +@classmethod |
| 516 | +instancemethod.__new__ as instancemethod_new |
| 517 | + function: object |
| 518 | + / |
| 519 | +
|
| 520 | +Bind a function to a class. |
| 521 | +[clinic start generated code]*/ |
523 | 522 |
|
524 | 523 | static PyObject * |
525 | | -instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
| 524 | +instancemethod_new_impl(PyTypeObject *type, PyObject *function) |
| 525 | +/*[clinic end generated code: output=5e0397b2bdb750be input=cfc54e8b973664a8]*/ |
526 | 526 | { |
527 | | - PyObject *func; |
528 | | - |
529 | | - if (!_PyArg_NoKeywords("instancemethod", kw)) |
530 | | - return NULL; |
531 | | - if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func)) |
532 | | - return NULL; |
533 | | - if (!PyCallable_Check(func)) { |
| 527 | + if (!PyCallable_Check(function)) { |
534 | 528 | PyErr_SetString(PyExc_TypeError, |
535 | 529 | "first argument must be callable"); |
536 | 530 | return NULL; |
537 | 531 | } |
538 | 532 |
|
539 | | - return PyInstanceMethod_New(func); |
| 533 | + return PyInstanceMethod_New(function); |
540 | 534 | } |
541 | 535 |
|
542 | 536 | PyTypeObject PyInstanceMethod_Type = { |
543 | 537 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
544 | | - "instancemethod", /* tp_name */ |
545 | | - sizeof(PyInstanceMethodObject), /* tp_basicsize */ |
546 | | - 0, /* tp_itemsize */ |
547 | | - instancemethod_dealloc, /* tp_dealloc */ |
548 | | - 0, /* tp_vectorcall_offset */ |
549 | | - 0, /* tp_getattr */ |
550 | | - 0, /* tp_setattr */ |
551 | | - 0, /* tp_as_async */ |
552 | | - (reprfunc)instancemethod_repr, /* tp_repr */ |
553 | | - 0, /* tp_as_number */ |
554 | | - 0, /* tp_as_sequence */ |
555 | | - 0, /* tp_as_mapping */ |
556 | | - 0, /* tp_hash */ |
557 | | - instancemethod_call, /* tp_call */ |
558 | | - 0, /* tp_str */ |
559 | | - instancemethod_getattro, /* tp_getattro */ |
560 | | - PyObject_GenericSetAttr, /* tp_setattro */ |
561 | | - 0, /* tp_as_buffer */ |
562 | | - Py_TPFLAGS_DEFAULT |
563 | | - | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
564 | | - instancemethod_doc, /* tp_doc */ |
565 | | - instancemethod_traverse, /* tp_traverse */ |
566 | | - 0, /* tp_clear */ |
567 | | - instancemethod_richcompare, /* tp_richcompare */ |
568 | | - 0, /* tp_weaklistoffset */ |
569 | | - 0, /* tp_iter */ |
570 | | - 0, /* tp_iternext */ |
571 | | - 0, /* tp_methods */ |
572 | | - instancemethod_memberlist, /* tp_members */ |
573 | | - instancemethod_getset, /* tp_getset */ |
574 | | - 0, /* tp_base */ |
575 | | - 0, /* tp_dict */ |
576 | | - instancemethod_descr_get, /* tp_descr_get */ |
577 | | - 0, /* tp_descr_set */ |
578 | | - 0, /* tp_dictoffset */ |
579 | | - 0, /* tp_init */ |
580 | | - 0, /* tp_alloc */ |
581 | | - instancemethod_new, /* tp_new */ |
| 538 | + .tp_name = "instancemethod", |
| 539 | + .tp_basicsize = sizeof(PyInstanceMethodObject), |
| 540 | + .tp_dealloc = instancemethod_dealloc, |
| 541 | + .tp_repr = (reprfunc)instancemethod_repr, |
| 542 | + .tp_call = instancemethod_call, |
| 543 | + .tp_getattro = instancemethod_getattro, |
| 544 | + .tp_setattro = PyObject_GenericSetAttr, |
| 545 | + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 546 | + .tp_doc = instancemethod_new__doc__, |
| 547 | + .tp_traverse = instancemethod_traverse, |
| 548 | + .tp_richcompare = instancemethod_richcompare, |
| 549 | + .tp_members = instancemethod_memberlist, |
| 550 | + .tp_getset = instancemethod_getset, |
| 551 | + .tp_descr_get = instancemethod_descr_get, |
| 552 | + .tp_new = instancemethod_new, |
582 | 553 | }; |
0 commit comments