Changeset 391 for python/trunk/Modules/operator.c
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Modules/operator.c
r2 r391 8 8 to the intrinsic operators of Python. For example, operator.add(x, y)\n\ 9 9 is equivalent to the expression x+y. The function names are those\n\ 10 used for special classmethods; variants without leading and trailing\n\10 used for special methods; variants without leading and trailing\n\ 11 11 '__' are also provided for convenience."); 12 12 … … 66 66 return PyObject_RichCompare(a1,a2,A); } 67 67 68 spami(isCallable , PyCallable_Check) 68 /* Deprecated operators that need warnings. */ 69 static int 70 op_isCallable(PyObject *x) 71 { 72 if (PyErr_WarnPy3k("operator.isCallable() is not supported in 3.x. " 73 "Use hasattr(obj, '__call__').", 1) < 0) 74 return -1; 75 return PyCallable_Check(x); 76 } 77 78 static int 79 op_sequenceIncludes(PyObject *seq, PyObject* ob) 80 { 81 if (PyErr_WarnPy3k("operator.sequenceIncludes() is not supported " 82 "in 3.x. Use operator.contains().", 1) < 0) 83 return -1; 84 return PySequence_Contains(seq, ob); 85 } 86 87 spami(isCallable , op_isCallable) 69 88 spami(isNumberType , PyNumber_Check) 70 89 spami(truth , PyObject_IsTrue) … … 105 124 spamoi(op_irepeat , PySequence_InPlaceRepeat) 106 125 spami2b(op_contains , PySequence_Contains) 107 spami2b(sequenceIncludes, PySequence_Contains)126 spami2b(sequenceIncludes, op_sequenceIncludes) 108 127 spamn2(indexOf , PySequence_Index) 109 128 spamn2(countOf , PySequence_Count) … … 122 141 op_pow(PyObject *s, PyObject *a) 123 142 { 124 125 126 127 143 PyObject *a1, *a2; 144 if (PyArg_UnpackTuple(a,"pow", 2, 2, &a1, &a2)) 145 return PyNumber_Power(a1, a2, Py_None); 146 return NULL; 128 147 } 129 148 … … 131 150 op_ipow(PyObject *s, PyObject *a) 132 151 { 133 134 135 136 152 PyObject *a1, *a2; 153 if (PyArg_UnpackTuple(a,"ipow", 2, 2, &a1, &a2)) 154 return PyNumber_InPlacePower(a1, a2, Py_None); 155 return NULL; 137 156 } 138 157 … … 140 159 op_index(PyObject *s, PyObject *a) 141 160 { 142 161 return PyNumber_Index(a); 143 162 } 144 163 … … 146 165 is_(PyObject *s, PyObject *a) 147 166 { 148 149 150 151 152 153 167 PyObject *a1, *a2, *result = NULL; 168 if (PyArg_UnpackTuple(a,"is_", 2, 2, &a1, &a2)) { 169 result = (a1 == a2) ? Py_True : Py_False; 170 Py_INCREF(result); 171 } 172 return result; 154 173 } 155 174 … … 157 176 is_not(PyObject *s, PyObject *a) 158 177 { 159 160 161 162 163 164 178 PyObject *a1, *a2, *result = NULL; 179 if (PyArg_UnpackTuple(a,"is_not", 2, 2, &a1, &a2)) { 180 result = (a1 != a2) ? Py_True : Py_False; 181 Py_INCREF(result); 182 } 183 return result; 165 184 } 166 185 … … 168 187 op_getslice(PyObject *s, PyObject *a) 169 188 { 170 171 172 173 174 175 189 PyObject *a1; 190 Py_ssize_t a2, a3; 191 192 if (!PyArg_ParseTuple(a, "Onn:getslice", &a1, &a2, &a3)) 193 return NULL; 194 return PySequence_GetSlice(a1, a2, a3); 176 195 } 177 196 … … 179 198 op_setslice(PyObject *s, PyObject *a) 180 199 { 181 182 183 184 185 186 187 188 189 190 200 PyObject *a1, *a4; 201 Py_ssize_t a2, a3; 202 203 if (!PyArg_ParseTuple(a, "OnnO:setslice", &a1, &a2, &a3, &a4)) 204 return NULL; 205 206 if (-1 == PySequence_SetSlice(a1, a2, a3, a4)) 207 return NULL; 208 209 Py_RETURN_NONE; 191 210 } 192 211 … … 194 213 op_delslice(PyObject *s, PyObject *a) 195 214 { 196 197 198 199 200 201 202 203 204 205 215 PyObject *a1; 216 Py_ssize_t a2, a3; 217 218 if (!PyArg_ParseTuple(a, "Onn:delslice", &a1, &a2, &a3)) 219 return NULL; 220 221 if (-1 == PySequence_DelSlice(a1, a2, a3)) 222 return NULL; 223 224 Py_RETURN_NONE; 206 225 } 207 226 … … 212 231 #define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)}, 213 232 #define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, \ 214 {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, 233 {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, 215 234 #define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)}, 216 235 #define spam2o(OP,ALTOP,DOC) {#OP, op_##OP, METH_O, PyDoc_STR(DOC)}, \ 217 {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)}, 236 {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)}, 218 237 219 238 static struct PyMethodDef operator_methods[] = { … … 259 278 spam2(xor,__xor__, "xor(a, b) -- Same as a ^ b.") 260 279 spam2(or_,__or__, "or_(a, b) -- Same as a | b.") 261 spam2(iadd,__iadd__, " iadd(a, b) -- Same as a += b.")262 spam2(isub,__isub__, " isub(a, b) -- Same as a -= b.")263 spam2(imul,__imul__, " imul(a, b) -- Same as a *= b.")264 spam2(idiv,__idiv__, " idiv(a, b) -- Same as a /= b when __future__.division is not in effect.")265 spam2(ifloordiv,__ifloordiv__, " ifloordiv(a, b) -- Same as a //= b.")266 spam2(itruediv,__itruediv__, " itruediv(a, b) -- Same as a /= b when __future__.division is in effect.")267 spam2(imod,__imod__, " imod(a, b) -- Same as a %= b.")268 spam2(ilshift,__ilshift__, " ilshift(a, b) -- Same as a <<= b.")269 spam2(irshift,__irshift__, " irshift(a, b) -- Same as a >>= b.")270 spam2(iand,__iand__, " iand(a, b) -- Same as a &= b.")271 spam2(ixor,__ixor__, " ixor(a, b) -- Same as a ^= b.")272 spam2(ior,__ior__, " ior(a, b) -- Same as a |= b.")280 spam2(iadd,__iadd__, "a = iadd(a, b) -- Same as a += b.") 281 spam2(isub,__isub__, "a = isub(a, b) -- Same as a -= b.") 282 spam2(imul,__imul__, "a = imul(a, b) -- Same as a *= b.") 283 spam2(idiv,__idiv__, "a = idiv(a, b) -- Same as a /= b when __future__.division is not in effect.") 284 spam2(ifloordiv,__ifloordiv__, "a = ifloordiv(a, b) -- Same as a //= b.") 285 spam2(itruediv,__itruediv__, "a = itruediv(a, b) -- Same as a /= b when __future__.division is in effect.") 286 spam2(imod,__imod__, "a = imod(a, b) -- Same as a %= b.") 287 spam2(ilshift,__ilshift__, "a = ilshift(a, b) -- Same as a <<= b.") 288 spam2(irshift,__irshift__, "a = irshift(a, b) -- Same as a >>= b.") 289 spam2(iand,__iand__, "a = iand(a, b) -- Same as a &= b.") 290 spam2(ixor,__ixor__, "a = ixor(a, b) -- Same as a ^= b.") 291 spam2(ior,__ior__, "a = ior(a, b) -- Same as a |= b.") 273 292 spam2(concat,__concat__, 274 293 "concat(a, b) -- Same as a + b, for a and b sequences.") … … 276 295 "repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.") 277 296 spam2(iconcat,__iconcat__, 278 " iconcat(a, b) -- Same as a += b, for a and b sequences.")297 "a = iconcat(a, b) -- Same as a += b, for a and b sequences.") 279 298 spam2(irepeat,__irepeat__, 280 " irepeat(a, b) -- Same as a *= b, where a is a sequence, and b is an integer.")299 "a = irepeat(a, b) -- Same as a *= b, where a is a sequence, and b is an integer.") 281 300 spam2(getitem,__getitem__, 282 301 "getitem(a, b) -- Same as a[b].") … … 286 305 "delitem(a, b) -- Same as del a[b].") 287 306 spam2(pow,__pow__, "pow(a, b) -- Same as a ** b.") 288 spam2(ipow,__ipow__, " ipow(a, b) -- Same as a **= b.")307 spam2(ipow,__ipow__, "a = ipow(a, b) -- Same as a **= b.") 289 308 spam2(getslice,__getslice__, 290 309 "getslice(a, b, c) -- Same as a[b:c].") … … 300 319 spam2(ge,__ge__, "ge(a, b) -- Same as a>=b.") 301 320 302 {NULL, NULL}/* sentinel */321 {NULL, NULL} /* sentinel */ 303 322 304 323 }; … … 307 326 308 327 typedef struct { 309 310 311 328 PyObject_HEAD 329 Py_ssize_t nitems; 330 PyObject *item; 312 331 } itemgetterobject; 313 332 … … 317 336 itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 318 337 { 319 320 321 322 323 324 325 326 327 328 329 330 } else 331 332 333 334 335 if (ig == NULL) 336 return NULL; 337 338 339 340 341 342 343 338 itemgetterobject *ig; 339 PyObject *item; 340 Py_ssize_t nitems; 341 342 if (!_PyArg_NoKeywords("itemgetter()", kwds)) 343 return NULL; 344 345 nitems = PyTuple_GET_SIZE(args); 346 if (nitems <= 1) { 347 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item)) 348 return NULL; 349 } else 350 item = args; 351 352 /* create itemgetterobject structure */ 353 ig = PyObject_GC_New(itemgetterobject, &itemgetter_type); 354 if (ig == NULL) 355 return NULL; 356 357 Py_INCREF(item); 358 ig->item = item; 359 ig->nitems = nitems; 360 361 PyObject_GC_Track(ig); 362 return (PyObject *)ig; 344 363 } 345 364 … … 347 366 itemgetter_dealloc(itemgetterobject *ig) 348 367 { 349 350 351 368 PyObject_GC_UnTrack(ig); 369 Py_XDECREF(ig->item); 370 PyObject_GC_Del(ig); 352 371 } 353 372 … … 355 374 itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg) 356 375 { 357 358 376 Py_VISIT(ig->item); 377 return 0; 359 378 } 360 379 … … 362 381 itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw) 363 382 { 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 383 PyObject *obj, *result; 384 Py_ssize_t i, nitems=ig->nitems; 385 386 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj)) 387 return NULL; 388 if (nitems == 1) 389 return PyObject_GetItem(obj, ig->item); 390 391 assert(PyTuple_Check(ig->item)); 392 assert(PyTuple_GET_SIZE(ig->item) == nitems); 393 394 result = PyTuple_New(nitems); 395 if (result == NULL) 396 return NULL; 397 398 for (i=0 ; i < nitems ; i++) { 399 PyObject *item, *val; 400 item = PyTuple_GET_ITEM(ig->item, i); 401 val = PyObject_GetItem(obj, item); 402 if (val == NULL) { 403 Py_DECREF(result); 404 return NULL; 405 } 406 PyTuple_SET_ITEM(result, i, val); 407 } 408 return result; 390 409 } 391 410 … … 394 413 \n\ 395 414 Return a callable object that fetches the given item(s) from its operand.\n\ 396 After , f=itemgetter(2), the call f(r) returns r[2].\n\397 After , g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3])");415 After f = itemgetter(2), the call f(r) returns r[2].\n\ 416 After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])"); 398 417 399 418 static PyTypeObject itemgetter_type = { 400 401 "operator.itemgetter",/* tp_name */402 sizeof(itemgetterobject),/* tp_basicsize */403 0,/* tp_itemsize */404 405 (destructor)itemgetter_dealloc,/* tp_dealloc */406 0,/* tp_print */407 0,/* tp_getattr */408 0,/* tp_setattr */409 0,/* tp_compare */410 0,/* tp_repr */411 0,/* tp_as_number */412 0,/* tp_as_sequence */413 0,/* tp_as_mapping */414 0,/* tp_hash */415 (ternaryfunc)itemgetter_call,/* tp_call */416 0,/* tp_str */417 PyObject_GenericGetAttr,/* tp_getattro */418 0,/* tp_setattro */419 0,/* tp_as_buffer */420 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */421 itemgetter_doc,/* tp_doc */422 (traverseproc)itemgetter_traverse,/* tp_traverse */423 0,/* tp_clear */424 0,/* tp_richcompare */425 0,/* tp_weaklistoffset */426 0,/* tp_iter */427 0,/* tp_iternext */428 0,/* tp_methods */429 0,/* tp_members */430 0,/* tp_getset */431 0,/* tp_base */432 0,/* tp_dict */433 0,/* tp_descr_get */434 0,/* tp_descr_set */435 0,/* tp_dictoffset */436 0,/* tp_init */437 0,/* tp_alloc */438 itemgetter_new,/* tp_new */439 0,/* tp_free */419 PyVarObject_HEAD_INIT(NULL, 0) 420 "operator.itemgetter", /* tp_name */ 421 sizeof(itemgetterobject), /* tp_basicsize */ 422 0, /* tp_itemsize */ 423 /* methods */ 424 (destructor)itemgetter_dealloc, /* tp_dealloc */ 425 0, /* tp_print */ 426 0, /* tp_getattr */ 427 0, /* tp_setattr */ 428 0, /* tp_compare */ 429 0, /* tp_repr */ 430 0, /* tp_as_number */ 431 0, /* tp_as_sequence */ 432 0, /* tp_as_mapping */ 433 0, /* tp_hash */ 434 (ternaryfunc)itemgetter_call, /* tp_call */ 435 0, /* tp_str */ 436 PyObject_GenericGetAttr, /* tp_getattro */ 437 0, /* tp_setattro */ 438 0, /* tp_as_buffer */ 439 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ 440 itemgetter_doc, /* tp_doc */ 441 (traverseproc)itemgetter_traverse, /* tp_traverse */ 442 0, /* tp_clear */ 443 0, /* tp_richcompare */ 444 0, /* tp_weaklistoffset */ 445 0, /* tp_iter */ 446 0, /* tp_iternext */ 447 0, /* tp_methods */ 448 0, /* tp_members */ 449 0, /* tp_getset */ 450 0, /* tp_base */ 451 0, /* tp_dict */ 452 0, /* tp_descr_get */ 453 0, /* tp_descr_set */ 454 0, /* tp_dictoffset */ 455 0, /* tp_init */ 456 0, /* tp_alloc */ 457 itemgetter_new, /* tp_new */ 458 0, /* tp_free */ 440 459 }; 441 460 … … 444 463 445 464 typedef struct { 446 447 448 465 PyObject_HEAD 466 Py_ssize_t nattrs; 467 PyObject *attr; 449 468 } attrgetterobject; 450 469 … … 454 473 attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 455 474 { 456 457 458 459 460 461 462 463 464 465 466 467 } else 468 469 470 471 472 if (ag == NULL) 473 return NULL; 474 475 476 477 478 479 480 475 attrgetterobject *ag; 476 PyObject *attr; 477 Py_ssize_t nattrs; 478 479 if (!_PyArg_NoKeywords("attrgetter()", kwds)) 480 return NULL; 481 482 nattrs = PyTuple_GET_SIZE(args); 483 if (nattrs <= 1) { 484 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr)) 485 return NULL; 486 } else 487 attr = args; 488 489 /* create attrgetterobject structure */ 490 ag = PyObject_GC_New(attrgetterobject, &attrgetter_type); 491 if (ag == NULL) 492 return NULL; 493 494 Py_INCREF(attr); 495 ag->attr = attr; 496 ag->nattrs = nattrs; 497 498 PyObject_GC_Track(ag); 499 return (PyObject *)ag; 481 500 } 482 501 … … 484 503 attrgetter_dealloc(attrgetterobject *ag) 485 504 { 486 487 488 505 PyObject_GC_UnTrack(ag); 506 Py_XDECREF(ag->attr); 507 PyObject_GC_Del(ag); 489 508 } 490 509 … … 492 511 attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg) 493 512 { 494 495 513 Py_VISIT(ag->attr); 514 return 0; 496 515 } 497 516 … … 499 518 dotted_getattr(PyObject *obj, PyObject *attr) 500 519 { 501 520 char *s, *p; 502 521 503 522 #ifdef Py_USING_UNICODE 504 505 506 507 508 523 if (PyUnicode_Check(attr)) { 524 attr = _PyUnicode_AsDefaultEncodedString(attr, NULL); 525 if (attr == NULL) 526 return NULL; 527 } 509 528 #endif 510 511 512 513 514 515 516 517 518 519 520 521 522 str = p ? PyString_FromStringAndSize(s, (p-s)) : 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 529 530 if (!PyString_Check(attr)) { 531 PyErr_SetString(PyExc_TypeError, 532 "attribute name must be a string"); 533 return NULL; 534 } 535 536 s = PyString_AS_STRING(attr); 537 Py_INCREF(obj); 538 for (;;) { 539 PyObject *newobj, *str; 540 p = strchr(s, '.'); 541 str = p ? PyString_FromStringAndSize(s, (p-s)) : 542 PyString_FromString(s); 543 if (str == NULL) { 544 Py_DECREF(obj); 545 return NULL; 546 } 547 newobj = PyObject_GetAttr(obj, str); 548 Py_DECREF(str); 549 Py_DECREF(obj); 550 if (newobj == NULL) 551 return NULL; 552 obj = newobj; 553 if (p == NULL) break; 554 s = p+1; 555 } 556 557 return obj; 539 558 } 540 559 … … 542 561 attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw) 543 562 { 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 563 PyObject *obj, *result; 564 Py_ssize_t i, nattrs=ag->nattrs; 565 566 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj)) 567 return NULL; 568 if (ag->nattrs == 1) 569 return dotted_getattr(obj, ag->attr); 570 571 assert(PyTuple_Check(ag->attr)); 572 assert(PyTuple_GET_SIZE(ag->attr) == nattrs); 573 574 result = PyTuple_New(nattrs); 575 if (result == NULL) 576 return NULL; 577 578 for (i=0 ; i < nattrs ; i++) { 579 PyObject *attr, *val; 580 attr = PyTuple_GET_ITEM(ag->attr, i); 581 val = dotted_getattr(obj, attr); 582 if (val == NULL) { 583 Py_DECREF(result); 584 return NULL; 585 } 586 PyTuple_SET_ITEM(result, i, val); 587 } 588 return result; 570 589 } 571 590 … … 574 593 \n\ 575 594 Return a callable object that fetches the given attribute(s) from its operand.\n\ 576 After , f=attrgetter('name'), the call f(r) returns r.name.\n\577 After , g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\578 After , h=attrgetter('name.first', 'name.last'), the call h(r) returns\n\595 After f = attrgetter('name'), the call f(r) returns r.name.\n\ 596 After g = attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\ 597 After h = attrgetter('name.first', 'name.last'), the call h(r) returns\n\ 579 598 (r.name.first, r.name.last)."); 580 599 581 600 static PyTypeObject attrgetter_type = { 582 583 "operator.attrgetter",/* tp_name */584 sizeof(attrgetterobject),/* tp_basicsize */585 0,/* tp_itemsize */586 587 (destructor)attrgetter_dealloc,/* tp_dealloc */588 0,/* tp_print */589 0,/* tp_getattr */590 0,/* tp_setattr */591 0,/* tp_compare */592 0,/* tp_repr */593 0,/* tp_as_number */594 0,/* tp_as_sequence */595 0,/* tp_as_mapping */596 0,/* tp_hash */597 (ternaryfunc)attrgetter_call,/* tp_call */598 0,/* tp_str */599 PyObject_GenericGetAttr,/* tp_getattro */600 0,/* tp_setattro */601 0,/* tp_as_buffer */602 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */603 attrgetter_doc,/* tp_doc */604 (traverseproc)attrgetter_traverse,/* tp_traverse */605 0,/* tp_clear */606 0,/* tp_richcompare */607 0,/* tp_weaklistoffset */608 0,/* tp_iter */609 0,/* tp_iternext */610 0,/* tp_methods */611 0,/* tp_members */612 0,/* tp_getset */613 0,/* tp_base */614 0,/* tp_dict */615 0,/* tp_descr_get */616 0,/* tp_descr_set */617 0,/* tp_dictoffset */618 0,/* tp_init */619 0,/* tp_alloc */620 attrgetter_new,/* tp_new */621 0,/* tp_free */601 PyVarObject_HEAD_INIT(NULL, 0) 602 "operator.attrgetter", /* tp_name */ 603 sizeof(attrgetterobject), /* tp_basicsize */ 604 0, /* tp_itemsize */ 605 /* methods */ 606 (destructor)attrgetter_dealloc, /* tp_dealloc */ 607 0, /* tp_print */ 608 0, /* tp_getattr */ 609 0, /* tp_setattr */ 610 0, /* tp_compare */ 611 0, /* tp_repr */ 612 0, /* tp_as_number */ 613 0, /* tp_as_sequence */ 614 0, /* tp_as_mapping */ 615 0, /* tp_hash */ 616 (ternaryfunc)attrgetter_call, /* tp_call */ 617 0, /* tp_str */ 618 PyObject_GenericGetAttr, /* tp_getattro */ 619 0, /* tp_setattro */ 620 0, /* tp_as_buffer */ 621 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ 622 attrgetter_doc, /* tp_doc */ 623 (traverseproc)attrgetter_traverse, /* tp_traverse */ 624 0, /* tp_clear */ 625 0, /* tp_richcompare */ 626 0, /* tp_weaklistoffset */ 627 0, /* tp_iter */ 628 0, /* tp_iternext */ 629 0, /* tp_methods */ 630 0, /* tp_members */ 631 0, /* tp_getset */ 632 0, /* tp_base */ 633 0, /* tp_dict */ 634 0, /* tp_descr_get */ 635 0, /* tp_descr_set */ 636 0, /* tp_dictoffset */ 637 0, /* tp_init */ 638 0, /* tp_alloc */ 639 attrgetter_new, /* tp_new */ 640 0, /* tp_free */ 622 641 }; 623 642 … … 626 645 627 646 typedef struct { 628 629 630 631 647 PyObject_HEAD 648 PyObject *name; 649 PyObject *args; 650 PyObject *kwds; 632 651 } methodcallerobject; 633 652 … … 637 656 methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 638 657 { 639 640 641 642 643 644 645 646 647 648 649 650 if (mc == NULL) 651 return NULL; 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 658 methodcallerobject *mc; 659 PyObject *name, *newargs; 660 661 if (PyTuple_GET_SIZE(args) < 1) { 662 PyErr_SetString(PyExc_TypeError, "methodcaller needs at least " 663 "one argument, the method name"); 664 return NULL; 665 } 666 667 /* create methodcallerobject structure */ 668 mc = PyObject_GC_New(methodcallerobject, &methodcaller_type); 669 if (mc == NULL) 670 return NULL; 671 672 newargs = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); 673 if (newargs == NULL) { 674 Py_DECREF(mc); 675 return NULL; 676 } 677 mc->args = newargs; 678 679 name = PyTuple_GET_ITEM(args, 0); 680 Py_INCREF(name); 681 mc->name = name; 682 683 Py_XINCREF(kwds); 684 mc->kwds = kwds; 685 686 PyObject_GC_Track(mc); 687 return (PyObject *)mc; 669 688 } 670 689 … … 672 691 methodcaller_dealloc(methodcallerobject *mc) 673 692 { 674 675 676 677 678 693 PyObject_GC_UnTrack(mc); 694 Py_XDECREF(mc->name); 695 Py_XDECREF(mc->args); 696 Py_XDECREF(mc->kwds); 697 PyObject_GC_Del(mc); 679 698 } 680 699 … … 682 701 methodcaller_traverse(methodcallerobject *mc, visitproc visit, void *arg) 683 702 { 684 685 686 703 Py_VISIT(mc->args); 704 Py_VISIT(mc->kwds); 705 return 0; 687 706 } 688 707 … … 690 709 methodcaller_call(methodcallerobject *mc, PyObject *args, PyObject *kw) 691 710 { 692 693 694 695 696 697 698 699 700 701 711 PyObject *method, *obj, *result; 712 713 if (!PyArg_UnpackTuple(args, "methodcaller", 1, 1, &obj)) 714 return NULL; 715 method = PyObject_GetAttr(obj, mc->name); 716 if (method == NULL) 717 return NULL; 718 result = PyObject_Call(method, mc->args, mc->kwds); 719 Py_DECREF(method); 720 return result; 702 721 } 703 722 … … 706 725 \n\ 707 726 Return a callable object that calls the given method on its operand.\n\ 708 After ,f = methodcaller('name'), the call f(r) returns r.name().\n\709 After ,g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\727 After f = methodcaller('name'), the call f(r) returns r.name().\n\ 728 After g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\ 710 729 r.name('date', foo=1)."); 711 730 712 731 static PyTypeObject methodcaller_type = { 713 714 "operator.methodcaller",/* tp_name */715 sizeof(methodcallerobject),/* tp_basicsize */716 0,/* tp_itemsize */717 718 719 0,/* tp_print */720 0,/* tp_getattr */721 0,/* tp_setattr */722 0,/* tp_compare */723 0,/* tp_repr */724 0,/* tp_as_number */725 0,/* tp_as_sequence */726 0,/* tp_as_mapping */727 0,/* tp_hash */728 (ternaryfunc)methodcaller_call,/* tp_call */729 0,/* tp_str */730 PyObject_GenericGetAttr,/* tp_getattro */731 0,/* tp_setattro */732 0,/* tp_as_buffer */733 734 methodcaller_doc,/* tp_doc */735 (traverseproc)methodcaller_traverse,/* tp_traverse */736 0,/* tp_clear */737 0,/* tp_richcompare */738 0,/* tp_weaklistoffset */739 0,/* tp_iter */740 0,/* tp_iternext */741 0,/* tp_methods */742 0,/* tp_members */743 0,/* tp_getset */744 0,/* tp_base */745 0,/* tp_dict */746 0,/* tp_descr_get */747 0,/* tp_descr_set */748 0,/* tp_dictoffset */749 0,/* tp_init */750 0,/* tp_alloc */751 methodcaller_new,/* tp_new */752 0,/* tp_free */732 PyVarObject_HEAD_INIT(NULL, 0) 733 "operator.methodcaller", /* tp_name */ 734 sizeof(methodcallerobject), /* tp_basicsize */ 735 0, /* tp_itemsize */ 736 /* methods */ 737 (destructor)methodcaller_dealloc, /* tp_dealloc */ 738 0, /* tp_print */ 739 0, /* tp_getattr */ 740 0, /* tp_setattr */ 741 0, /* tp_compare */ 742 0, /* tp_repr */ 743 0, /* tp_as_number */ 744 0, /* tp_as_sequence */ 745 0, /* tp_as_mapping */ 746 0, /* tp_hash */ 747 (ternaryfunc)methodcaller_call, /* tp_call */ 748 0, /* tp_str */ 749 PyObject_GenericGetAttr, /* tp_getattro */ 750 0, /* tp_setattro */ 751 0, /* tp_as_buffer */ 752 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ 753 methodcaller_doc, /* tp_doc */ 754 (traverseproc)methodcaller_traverse, /* tp_traverse */ 755 0, /* tp_clear */ 756 0, /* tp_richcompare */ 757 0, /* tp_weaklistoffset */ 758 0, /* tp_iter */ 759 0, /* tp_iternext */ 760 0, /* tp_methods */ 761 0, /* tp_members */ 762 0, /* tp_getset */ 763 0, /* tp_base */ 764 0, /* tp_dict */ 765 0, /* tp_descr_get */ 766 0, /* tp_descr_set */ 767 0, /* tp_dictoffset */ 768 0, /* tp_init */ 769 0, /* tp_alloc */ 770 methodcaller_new, /* tp_new */ 771 0, /* tp_free */ 753 772 }; 754 773 … … 759 778 initoperator(void) 760 779 { 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 } 780 PyObject *m; 781 782 /* Create the module and add the functions */ 783 m = Py_InitModule4("operator", operator_methods, operator_doc, 784 (PyObject*)NULL, PYTHON_API_VERSION); 785 if (m == NULL) 786 return; 787 788 if (PyType_Ready(&itemgetter_type) < 0) 789 return; 790 Py_INCREF(&itemgetter_type); 791 PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type); 792 793 if (PyType_Ready(&attrgetter_type) < 0) 794 return; 795 Py_INCREF(&attrgetter_type); 796 PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type); 797 798 if (PyType_Ready(&methodcaller_type) < 0) 799 return; 800 Py_INCREF(&methodcaller_type); 801 PyModule_AddObject(m, "methodcaller", (PyObject *)&methodcaller_type); 802 }
Note:
See TracChangeset
for help on using the changeset viewer.