Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Victor Stinner | cdad272 | 2021-04-22 00:52:52 +0200 | [diff] [blame] | 2 | #include "pycore_moduleobject.h" // _PyModule_GetState() |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 3 | #include "clinic/_operator.c.h" |
| 4 | |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 5 | typedef struct { |
| 6 | PyObject *itemgetter_type; |
| 7 | PyObject *attrgetter_type; |
| 8 | PyObject *methodcaller_type; |
| 9 | } _operator_state; |
| 10 | |
| 11 | static inline _operator_state* |
| 12 | get_operator_state(PyObject *module) |
| 13 | { |
Victor Stinner | cdad272 | 2021-04-22 00:52:52 +0200 | [diff] [blame] | 14 | void *state = _PyModule_GetState(module); |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 15 | assert(state != NULL); |
| 16 | return (_operator_state *)state; |
| 17 | } |
| 18 | |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 19 | /*[clinic input] |
| 20 | module _operator |
| 21 | [clinic start generated code]*/ |
| 22 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=672ecf48487521e7]*/ |
| 23 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 24 | PyDoc_STRVAR(operator_doc, |
| 25 | "Operator interface.\n\ |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 26 | \n\ |
| 27 | This module exports a set of functions implemented in C corresponding\n\ |
| 28 | to the intrinsic operators of Python. For example, operator.add(x, y)\n\ |
| 29 | is equivalent to the expression x+y. The function names are those\n\ |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 30 | used for special methods; variants without leading and trailing\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 31 | '__' are also provided for convenience."); |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 33 | |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 34 | /*[clinic input] |
| 35 | _operator.truth -> bool |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 36 | |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 37 | a: object |
| 38 | / |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 39 | |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 40 | Return True if a is true, False otherwise. |
| 41 | [clinic start generated code]*/ |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 42 | |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 43 | static int |
| 44 | _operator_truth_impl(PyObject *module, PyObject *a) |
| 45 | /*[clinic end generated code: output=eaf87767234fa5d7 input=bc74a4cd90235875]*/ |
Raymond Hettinger | 5959c55 | 2002-08-19 03:19:09 +0000 | [diff] [blame] | 46 | { |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 47 | return PyObject_IsTrue(a); |
Raymond Hettinger | 5959c55 | 2002-08-19 03:19:09 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 50 | /*[clinic input] |
| 51 | _operator.add |
| 52 | |
| 53 | a: object |
| 54 | b: object |
| 55 | / |
| 56 | |
| 57 | Same as a + b. |
| 58 | [clinic start generated code]*/ |
Armin Rigo | f5bd3b4 | 2005-12-29 16:50:42 +0000 | [diff] [blame] | 59 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 60 | static PyObject * |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 61 | _operator_add_impl(PyObject *module, PyObject *a, PyObject *b) |
| 62 | /*[clinic end generated code: output=8292984204f45164 input=5efe3bff856ac215]*/ |
| 63 | { |
| 64 | return PyNumber_Add(a, b); |
| 65 | } |
| 66 | |
| 67 | /*[clinic input] |
| 68 | _operator.sub = _operator.add |
| 69 | |
| 70 | Same as a - b. |
| 71 | [clinic start generated code]*/ |
| 72 | |
| 73 | static PyObject * |
| 74 | _operator_sub_impl(PyObject *module, PyObject *a, PyObject *b) |
| 75 | /*[clinic end generated code: output=4adfc3b888c1ee2e input=6494c6b100b8e795]*/ |
| 76 | { |
| 77 | return PyNumber_Subtract(a, b); |
| 78 | } |
| 79 | |
| 80 | /*[clinic input] |
| 81 | _operator.mul = _operator.add |
| 82 | |
| 83 | Same as a * b. |
| 84 | [clinic start generated code]*/ |
| 85 | |
| 86 | static PyObject * |
| 87 | _operator_mul_impl(PyObject *module, PyObject *a, PyObject *b) |
| 88 | /*[clinic end generated code: output=d24d66f55a01944c input=2368615b4358b70d]*/ |
| 89 | { |
| 90 | return PyNumber_Multiply(a, b); |
| 91 | } |
| 92 | |
| 93 | /*[clinic input] |
| 94 | _operator.matmul = _operator.add |
| 95 | |
| 96 | Same as a @ b. |
| 97 | [clinic start generated code]*/ |
| 98 | |
| 99 | static PyObject * |
| 100 | _operator_matmul_impl(PyObject *module, PyObject *a, PyObject *b) |
| 101 | /*[clinic end generated code: output=a20d917eb35d0101 input=9ab304e37fb42dd4]*/ |
| 102 | { |
| 103 | return PyNumber_MatrixMultiply(a, b); |
| 104 | } |
| 105 | |
| 106 | /*[clinic input] |
| 107 | _operator.floordiv = _operator.add |
| 108 | |
| 109 | Same as a // b. |
| 110 | [clinic start generated code]*/ |
| 111 | |
| 112 | static PyObject * |
| 113 | _operator_floordiv_impl(PyObject *module, PyObject *a, PyObject *b) |
| 114 | /*[clinic end generated code: output=df26b71a60589f99 input=bb2e88ba446c612c]*/ |
| 115 | { |
| 116 | return PyNumber_FloorDivide(a, b); |
| 117 | } |
| 118 | |
| 119 | /*[clinic input] |
| 120 | _operator.truediv = _operator.add |
| 121 | |
| 122 | Same as a / b. |
| 123 | [clinic start generated code]*/ |
| 124 | |
| 125 | static PyObject * |
| 126 | _operator_truediv_impl(PyObject *module, PyObject *a, PyObject *b) |
| 127 | /*[clinic end generated code: output=0e6a959944d77719 input=ecbb947673f4eb1f]*/ |
| 128 | { |
| 129 | return PyNumber_TrueDivide(a, b); |
| 130 | } |
| 131 | |
| 132 | /*[clinic input] |
| 133 | _operator.mod = _operator.add |
| 134 | |
| 135 | Same as a % b. |
| 136 | [clinic start generated code]*/ |
| 137 | |
| 138 | static PyObject * |
| 139 | _operator_mod_impl(PyObject *module, PyObject *a, PyObject *b) |
| 140 | /*[clinic end generated code: output=9519822f0bbec166 input=102e19b422342ac1]*/ |
| 141 | { |
| 142 | return PyNumber_Remainder(a, b); |
| 143 | } |
| 144 | |
| 145 | /*[clinic input] |
| 146 | _operator.neg |
| 147 | |
| 148 | a: object |
| 149 | / |
| 150 | |
| 151 | Same as -a. |
| 152 | [clinic start generated code]*/ |
| 153 | |
| 154 | static PyObject * |
| 155 | _operator_neg(PyObject *module, PyObject *a) |
| 156 | /*[clinic end generated code: output=36e08ecfc6a1c08c input=84f09bdcf27c96ec]*/ |
| 157 | { |
| 158 | return PyNumber_Negative(a); |
| 159 | } |
| 160 | |
| 161 | /*[clinic input] |
| 162 | _operator.pos = _operator.neg |
| 163 | |
| 164 | Same as +a. |
| 165 | [clinic start generated code]*/ |
| 166 | |
| 167 | static PyObject * |
| 168 | _operator_pos(PyObject *module, PyObject *a) |
| 169 | /*[clinic end generated code: output=dad7a126221dd091 input=b6445b63fddb8772]*/ |
| 170 | { |
| 171 | return PyNumber_Positive(a); |
| 172 | } |
| 173 | |
| 174 | /*[clinic input] |
| 175 | _operator.abs = _operator.neg |
| 176 | |
| 177 | Same as abs(a). |
| 178 | [clinic start generated code]*/ |
| 179 | |
| 180 | static PyObject * |
| 181 | _operator_abs(PyObject *module, PyObject *a) |
| 182 | /*[clinic end generated code: output=1389a93ba053ea3e input=341d07ba86f58039]*/ |
| 183 | { |
| 184 | return PyNumber_Absolute(a); |
| 185 | } |
| 186 | |
| 187 | /*[clinic input] |
| 188 | _operator.inv = _operator.neg |
| 189 | |
| 190 | Same as ~a. |
| 191 | [clinic start generated code]*/ |
| 192 | |
| 193 | static PyObject * |
| 194 | _operator_inv(PyObject *module, PyObject *a) |
| 195 | /*[clinic end generated code: output=a56875ba075ee06d input=b01a4677739f6eb2]*/ |
| 196 | { |
| 197 | return PyNumber_Invert(a); |
| 198 | } |
| 199 | |
| 200 | /*[clinic input] |
| 201 | _operator.invert = _operator.neg |
| 202 | |
| 203 | Same as ~a. |
| 204 | [clinic start generated code]*/ |
| 205 | |
| 206 | static PyObject * |
| 207 | _operator_invert(PyObject *module, PyObject *a) |
| 208 | /*[clinic end generated code: output=406b5aa030545fcc input=7f2d607176672e55]*/ |
| 209 | { |
| 210 | return PyNumber_Invert(a); |
| 211 | } |
| 212 | |
| 213 | /*[clinic input] |
| 214 | _operator.lshift = _operator.add |
| 215 | |
| 216 | Same as a << b. |
| 217 | [clinic start generated code]*/ |
| 218 | |
| 219 | static PyObject * |
| 220 | _operator_lshift_impl(PyObject *module, PyObject *a, PyObject *b) |
| 221 | /*[clinic end generated code: output=37f7e52c41435bd8 input=746e8a160cbbc9eb]*/ |
| 222 | { |
| 223 | return PyNumber_Lshift(a, b); |
| 224 | } |
| 225 | |
| 226 | /*[clinic input] |
| 227 | _operator.rshift = _operator.add |
| 228 | |
| 229 | Same as a >> b. |
| 230 | [clinic start generated code]*/ |
| 231 | |
| 232 | static PyObject * |
| 233 | _operator_rshift_impl(PyObject *module, PyObject *a, PyObject *b) |
| 234 | /*[clinic end generated code: output=4593c7ef30ec2ee3 input=d2c85bb5a64504c2]*/ |
| 235 | { |
| 236 | return PyNumber_Rshift(a, b); |
| 237 | } |
| 238 | |
| 239 | /*[clinic input] |
| 240 | _operator.not_ = _operator.truth |
| 241 | |
| 242 | Same as not a. |
| 243 | [clinic start generated code]*/ |
| 244 | |
| 245 | static int |
| 246 | _operator_not__impl(PyObject *module, PyObject *a) |
| 247 | /*[clinic end generated code: output=743f9c24a09759ef input=854156d50804d9b8]*/ |
| 248 | { |
| 249 | return PyObject_Not(a); |
| 250 | } |
| 251 | |
| 252 | /*[clinic input] |
| 253 | _operator.and_ = _operator.add |
| 254 | |
| 255 | Same as a & b. |
| 256 | [clinic start generated code]*/ |
| 257 | |
| 258 | static PyObject * |
| 259 | _operator_and__impl(PyObject *module, PyObject *a, PyObject *b) |
| 260 | /*[clinic end generated code: output=93c4fe88f7b76d9e input=4f3057c90ec4c99f]*/ |
| 261 | { |
| 262 | return PyNumber_And(a, b); |
| 263 | } |
| 264 | |
| 265 | /*[clinic input] |
| 266 | _operator.xor = _operator.add |
| 267 | |
| 268 | Same as a ^ b. |
| 269 | [clinic start generated code]*/ |
| 270 | |
| 271 | static PyObject * |
| 272 | _operator_xor_impl(PyObject *module, PyObject *a, PyObject *b) |
| 273 | /*[clinic end generated code: output=b24cd8b79fde0004 input=3c5cfa7253d808dd]*/ |
| 274 | { |
| 275 | return PyNumber_Xor(a, b); |
| 276 | } |
| 277 | |
| 278 | /*[clinic input] |
| 279 | _operator.or_ = _operator.add |
| 280 | |
| 281 | Same as a | b. |
| 282 | [clinic start generated code]*/ |
| 283 | |
| 284 | static PyObject * |
| 285 | _operator_or__impl(PyObject *module, PyObject *a, PyObject *b) |
| 286 | /*[clinic end generated code: output=58024867b8d90461 input=b40c6c44f7c79c09]*/ |
| 287 | { |
| 288 | return PyNumber_Or(a, b); |
| 289 | } |
| 290 | |
| 291 | /*[clinic input] |
| 292 | _operator.iadd = _operator.add |
| 293 | |
| 294 | Same as a += b. |
| 295 | [clinic start generated code]*/ |
| 296 | |
| 297 | static PyObject * |
| 298 | _operator_iadd_impl(PyObject *module, PyObject *a, PyObject *b) |
| 299 | /*[clinic end generated code: output=07dc627832526eb5 input=d22a91c07ac69227]*/ |
| 300 | { |
| 301 | return PyNumber_InPlaceAdd(a, b); |
| 302 | } |
| 303 | |
| 304 | /*[clinic input] |
| 305 | _operator.isub = _operator.add |
| 306 | |
| 307 | Same as a -= b. |
| 308 | [clinic start generated code]*/ |
| 309 | |
| 310 | static PyObject * |
| 311 | _operator_isub_impl(PyObject *module, PyObject *a, PyObject *b) |
| 312 | /*[clinic end generated code: output=4513467d23b5e0b1 input=4591b00d0a0ccafd]*/ |
| 313 | { |
| 314 | return PyNumber_InPlaceSubtract(a, b); |
| 315 | } |
| 316 | |
| 317 | /*[clinic input] |
| 318 | _operator.imul = _operator.add |
| 319 | |
| 320 | Same as a *= b. |
| 321 | [clinic start generated code]*/ |
| 322 | |
| 323 | static PyObject * |
| 324 | _operator_imul_impl(PyObject *module, PyObject *a, PyObject *b) |
| 325 | /*[clinic end generated code: output=5e87dacd19a71eab input=0e01fb8631e1b76f]*/ |
| 326 | { |
| 327 | return PyNumber_InPlaceMultiply(a, b); |
| 328 | } |
| 329 | |
| 330 | /*[clinic input] |
| 331 | _operator.imatmul = _operator.add |
| 332 | |
| 333 | Same as a @= b. |
| 334 | [clinic start generated code]*/ |
| 335 | |
| 336 | static PyObject * |
| 337 | _operator_imatmul_impl(PyObject *module, PyObject *a, PyObject *b) |
| 338 | /*[clinic end generated code: output=d603cbdf716ce519 input=bb614026372cd542]*/ |
| 339 | { |
| 340 | return PyNumber_InPlaceMatrixMultiply(a, b); |
| 341 | } |
| 342 | |
| 343 | /*[clinic input] |
| 344 | _operator.ifloordiv = _operator.add |
| 345 | |
| 346 | Same as a //= b. |
| 347 | [clinic start generated code]*/ |
| 348 | |
| 349 | static PyObject * |
| 350 | _operator_ifloordiv_impl(PyObject *module, PyObject *a, PyObject *b) |
| 351 | /*[clinic end generated code: output=535336048c681794 input=9df3b5021cff4ca1]*/ |
| 352 | { |
| 353 | return PyNumber_InPlaceFloorDivide(a, b); |
| 354 | } |
| 355 | |
| 356 | /*[clinic input] |
| 357 | _operator.itruediv = _operator.add |
| 358 | |
| 359 | Same as a /= b. |
| 360 | [clinic start generated code]*/ |
| 361 | |
| 362 | static PyObject * |
| 363 | _operator_itruediv_impl(PyObject *module, PyObject *a, PyObject *b) |
| 364 | /*[clinic end generated code: output=28017fbd3563952f input=9a1ee01608f5f590]*/ |
| 365 | { |
| 366 | return PyNumber_InPlaceTrueDivide(a, b); |
| 367 | } |
| 368 | |
| 369 | /*[clinic input] |
| 370 | _operator.imod = _operator.add |
| 371 | |
| 372 | Same as a %= b. |
| 373 | [clinic start generated code]*/ |
| 374 | |
| 375 | static PyObject * |
| 376 | _operator_imod_impl(PyObject *module, PyObject *a, PyObject *b) |
| 377 | /*[clinic end generated code: output=f7c540ae0fc70904 input=d0c384a3ce38e1dd]*/ |
| 378 | { |
| 379 | return PyNumber_InPlaceRemainder(a, b); |
| 380 | } |
| 381 | |
| 382 | /*[clinic input] |
| 383 | _operator.ilshift = _operator.add |
| 384 | |
| 385 | Same as a <<= b. |
| 386 | [clinic start generated code]*/ |
| 387 | |
| 388 | static PyObject * |
| 389 | _operator_ilshift_impl(PyObject *module, PyObject *a, PyObject *b) |
| 390 | /*[clinic end generated code: output=e73a8fee1ac18749 input=e21b6b310f54572e]*/ |
| 391 | { |
| 392 | return PyNumber_InPlaceLshift(a, b); |
| 393 | } |
| 394 | |
| 395 | /*[clinic input] |
| 396 | _operator.irshift = _operator.add |
| 397 | |
| 398 | Same as a >>= b. |
| 399 | [clinic start generated code]*/ |
| 400 | |
| 401 | static PyObject * |
| 402 | _operator_irshift_impl(PyObject *module, PyObject *a, PyObject *b) |
| 403 | /*[clinic end generated code: output=97f2af6b5ff2ed81 input=6778dbd0f6e1ec16]*/ |
| 404 | { |
| 405 | return PyNumber_InPlaceRshift(a, b); |
| 406 | } |
| 407 | |
| 408 | /*[clinic input] |
| 409 | _operator.iand = _operator.add |
| 410 | |
| 411 | Same as a &= b. |
| 412 | [clinic start generated code]*/ |
| 413 | |
| 414 | static PyObject * |
| 415 | _operator_iand_impl(PyObject *module, PyObject *a, PyObject *b) |
| 416 | /*[clinic end generated code: output=4599e9d40cbf7d00 input=71dfd8e70c156a7b]*/ |
| 417 | { |
| 418 | return PyNumber_InPlaceAnd(a, b); |
| 419 | } |
| 420 | |
| 421 | /*[clinic input] |
| 422 | _operator.ixor = _operator.add |
| 423 | |
| 424 | Same as a ^= b. |
| 425 | [clinic start generated code]*/ |
| 426 | |
| 427 | static PyObject * |
| 428 | _operator_ixor_impl(PyObject *module, PyObject *a, PyObject *b) |
| 429 | /*[clinic end generated code: output=5ff881766872be03 input=695c32bec0604d86]*/ |
| 430 | { |
| 431 | return PyNumber_InPlaceXor(a, b); |
| 432 | } |
| 433 | |
| 434 | /*[clinic input] |
| 435 | _operator.ior = _operator.add |
| 436 | |
| 437 | Same as a |= b. |
| 438 | [clinic start generated code]*/ |
| 439 | |
| 440 | static PyObject * |
| 441 | _operator_ior_impl(PyObject *module, PyObject *a, PyObject *b) |
| 442 | /*[clinic end generated code: output=48aac319445bf759 input=8f01d03eda9920cf]*/ |
| 443 | { |
| 444 | return PyNumber_InPlaceOr(a, b); |
| 445 | } |
| 446 | |
| 447 | /*[clinic input] |
| 448 | _operator.concat = _operator.add |
| 449 | |
| 450 | Same as a + b, for a and b sequences. |
| 451 | [clinic start generated code]*/ |
| 452 | |
| 453 | static PyObject * |
| 454 | _operator_concat_impl(PyObject *module, PyObject *a, PyObject *b) |
| 455 | /*[clinic end generated code: output=80028390942c5f11 input=8544ccd5341a3658]*/ |
| 456 | { |
| 457 | return PySequence_Concat(a, b); |
| 458 | } |
| 459 | |
| 460 | /*[clinic input] |
| 461 | _operator.iconcat = _operator.add |
| 462 | |
| 463 | Same as a += b, for a and b sequences. |
| 464 | [clinic start generated code]*/ |
| 465 | |
| 466 | static PyObject * |
| 467 | _operator_iconcat_impl(PyObject *module, PyObject *a, PyObject *b) |
| 468 | /*[clinic end generated code: output=3ea0a162ebb2e26d input=8f5fe5722fcd837e]*/ |
| 469 | { |
| 470 | return PySequence_InPlaceConcat(a, b); |
| 471 | } |
| 472 | |
| 473 | /*[clinic input] |
| 474 | _operator.contains -> bool |
| 475 | |
| 476 | a: object |
| 477 | b: object |
| 478 | / |
| 479 | |
| 480 | Same as b in a (note reversed operands). |
| 481 | [clinic start generated code]*/ |
| 482 | |
| 483 | static int |
| 484 | _operator_contains_impl(PyObject *module, PyObject *a, PyObject *b) |
| 485 | /*[clinic end generated code: output=413b4dbe82b6ffc1 input=9122a69b505fde13]*/ |
| 486 | { |
| 487 | return PySequence_Contains(a, b); |
| 488 | } |
| 489 | |
| 490 | /*[clinic input] |
| 491 | _operator.indexOf -> Py_ssize_t |
| 492 | |
| 493 | a: object |
| 494 | b: object |
| 495 | / |
| 496 | |
| 497 | Return the first index of b in a. |
| 498 | [clinic start generated code]*/ |
| 499 | |
| 500 | static Py_ssize_t |
| 501 | _operator_indexOf_impl(PyObject *module, PyObject *a, PyObject *b) |
| 502 | /*[clinic end generated code: output=c6226d8e0fb60fa6 input=8be2e43b6a6fffe3]*/ |
| 503 | { |
| 504 | return PySequence_Index(a, b); |
| 505 | } |
| 506 | |
| 507 | /*[clinic input] |
| 508 | _operator.countOf = _operator.indexOf |
| 509 | |
| 510 | Return the number of times b occurs in a. |
| 511 | [clinic start generated code]*/ |
| 512 | |
| 513 | static Py_ssize_t |
| 514 | _operator_countOf_impl(PyObject *module, PyObject *a, PyObject *b) |
| 515 | /*[clinic end generated code: output=9e1623197daf3382 input=0c3a2656add252db]*/ |
| 516 | { |
| 517 | return PySequence_Count(a, b); |
| 518 | } |
| 519 | |
| 520 | /*[clinic input] |
| 521 | _operator.getitem |
| 522 | |
| 523 | a: object |
| 524 | b: object |
| 525 | / |
| 526 | |
| 527 | Same as a[b]. |
| 528 | [clinic start generated code]*/ |
| 529 | |
| 530 | static PyObject * |
| 531 | _operator_getitem_impl(PyObject *module, PyObject *a, PyObject *b) |
| 532 | /*[clinic end generated code: output=6c8d8101a676e594 input=6682797320e48845]*/ |
| 533 | { |
| 534 | return PyObject_GetItem(a, b); |
| 535 | } |
| 536 | |
| 537 | /*[clinic input] |
| 538 | _operator.setitem |
| 539 | |
| 540 | a: object |
| 541 | b: object |
| 542 | c: object |
| 543 | / |
| 544 | |
| 545 | Same as a[b] = c. |
| 546 | [clinic start generated code]*/ |
| 547 | |
| 548 | static PyObject * |
| 549 | _operator_setitem_impl(PyObject *module, PyObject *a, PyObject *b, |
| 550 | PyObject *c) |
| 551 | /*[clinic end generated code: output=1324f9061ae99e25 input=ceaf453c4d3a58df]*/ |
| 552 | { |
| 553 | if (-1 == PyObject_SetItem(a, b, c)) |
| 554 | return NULL; |
| 555 | Py_RETURN_NONE; |
| 556 | } |
| 557 | |
| 558 | /*[clinic input] |
| 559 | _operator.delitem = _operator.getitem |
| 560 | |
| 561 | Same as del a[b]. |
| 562 | [clinic start generated code]*/ |
| 563 | |
| 564 | static PyObject * |
| 565 | _operator_delitem_impl(PyObject *module, PyObject *a, PyObject *b) |
| 566 | /*[clinic end generated code: output=db18f61506295799 input=991bec56a0d3ec7f]*/ |
| 567 | { |
| 568 | if (-1 == PyObject_DelItem(a, b)) |
| 569 | return NULL; |
| 570 | Py_RETURN_NONE; |
| 571 | } |
| 572 | |
| 573 | /*[clinic input] |
| 574 | _operator.eq |
| 575 | |
| 576 | a: object |
| 577 | b: object |
| 578 | / |
| 579 | |
| 580 | Same as a == b. |
| 581 | [clinic start generated code]*/ |
| 582 | |
| 583 | static PyObject * |
| 584 | _operator_eq_impl(PyObject *module, PyObject *a, PyObject *b) |
| 585 | /*[clinic end generated code: output=8d7d46ed4135677c input=586fca687a95a83f]*/ |
| 586 | { |
| 587 | return PyObject_RichCompare(a, b, Py_EQ); |
| 588 | } |
| 589 | |
| 590 | /*[clinic input] |
| 591 | _operator.ne = _operator.eq |
| 592 | |
| 593 | Same as a != b. |
| 594 | [clinic start generated code]*/ |
| 595 | |
| 596 | static PyObject * |
| 597 | _operator_ne_impl(PyObject *module, PyObject *a, PyObject *b) |
| 598 | /*[clinic end generated code: output=c99bd0c3a4c01297 input=5d88f23d35e9abac]*/ |
| 599 | { |
| 600 | return PyObject_RichCompare(a, b, Py_NE); |
| 601 | } |
| 602 | |
| 603 | /*[clinic input] |
| 604 | _operator.lt = _operator.eq |
| 605 | |
| 606 | Same as a < b. |
| 607 | [clinic start generated code]*/ |
| 608 | |
| 609 | static PyObject * |
| 610 | _operator_lt_impl(PyObject *module, PyObject *a, PyObject *b) |
| 611 | /*[clinic end generated code: output=082d7c45c440e535 input=34a59ad6d39d3a2b]*/ |
| 612 | { |
| 613 | return PyObject_RichCompare(a, b, Py_LT); |
| 614 | } |
| 615 | |
| 616 | /*[clinic input] |
| 617 | _operator.le = _operator.eq |
| 618 | |
| 619 | Same as a <= b. |
| 620 | [clinic start generated code]*/ |
| 621 | |
| 622 | static PyObject * |
| 623 | _operator_le_impl(PyObject *module, PyObject *a, PyObject *b) |
| 624 | /*[clinic end generated code: output=00970a2923d0ae17 input=b812a7860a0bef44]*/ |
| 625 | { |
| 626 | return PyObject_RichCompare(a, b, Py_LE); |
| 627 | } |
| 628 | |
| 629 | /*[clinic input] |
| 630 | _operator.gt = _operator.eq |
| 631 | |
| 632 | Same as a > b. |
| 633 | [clinic start generated code]*/ |
| 634 | |
| 635 | static PyObject * |
| 636 | _operator_gt_impl(PyObject *module, PyObject *a, PyObject *b) |
| 637 | /*[clinic end generated code: output=8d373349ecf25641 input=9bdb45b995ada35b]*/ |
| 638 | { |
| 639 | return PyObject_RichCompare(a, b, Py_GT); |
| 640 | } |
| 641 | |
| 642 | /*[clinic input] |
| 643 | _operator.ge = _operator.eq |
| 644 | |
| 645 | Same as a >= b. |
| 646 | [clinic start generated code]*/ |
| 647 | |
| 648 | static PyObject * |
| 649 | _operator_ge_impl(PyObject *module, PyObject *a, PyObject *b) |
| 650 | /*[clinic end generated code: output=7ce3882256d4b137 input=cf1dc4a5ca9c35f5]*/ |
| 651 | { |
| 652 | return PyObject_RichCompare(a, b, Py_GE); |
| 653 | } |
| 654 | |
| 655 | /*[clinic input] |
| 656 | _operator.pow = _operator.add |
| 657 | |
| 658 | Same as a ** b. |
| 659 | [clinic start generated code]*/ |
| 660 | |
| 661 | static PyObject * |
| 662 | _operator_pow_impl(PyObject *module, PyObject *a, PyObject *b) |
| 663 | /*[clinic end generated code: output=09e668ad50036120 input=690b40f097ab1637]*/ |
| 664 | { |
| 665 | return PyNumber_Power(a, b, Py_None); |
| 666 | } |
| 667 | |
| 668 | /*[clinic input] |
| 669 | _operator.ipow = _operator.add |
| 670 | |
| 671 | Same as a **= b. |
| 672 | [clinic start generated code]*/ |
| 673 | |
| 674 | static PyObject * |
| 675 | _operator_ipow_impl(PyObject *module, PyObject *a, PyObject *b) |
| 676 | /*[clinic end generated code: output=7189ff4d4367c808 input=f00623899d07499a]*/ |
| 677 | { |
| 678 | return PyNumber_InPlacePower(a, b, Py_None); |
| 679 | } |
| 680 | |
| 681 | /*[clinic input] |
| 682 | _operator.index |
| 683 | |
| 684 | a: object |
| 685 | / |
| 686 | |
| 687 | Same as a.__index__() |
| 688 | [clinic start generated code]*/ |
| 689 | |
| 690 | static PyObject * |
| 691 | _operator_index(PyObject *module, PyObject *a) |
| 692 | /*[clinic end generated code: output=d972b0764ac305fc input=6f54d50ea64a579c]*/ |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 693 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 694 | return PyNumber_Index(a); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 697 | /*[clinic input] |
| 698 | _operator.is_ = _operator.add |
| 699 | |
| 700 | Same as a is b. |
| 701 | [clinic start generated code]*/ |
| 702 | |
| 703 | static PyObject * |
| 704 | _operator_is__impl(PyObject *module, PyObject *a, PyObject *b) |
| 705 | /*[clinic end generated code: output=bcd47a402e482e1d input=5fa9b97df03c427f]*/ |
Raymond Hettinger | 9543b34 | 2003-01-18 23:22:20 +0000 | [diff] [blame] | 706 | { |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 707 | PyObject *result; |
| 708 | result = (a == b) ? Py_True : Py_False; |
| 709 | Py_INCREF(result); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 710 | return result; |
Raymond Hettinger | 9543b34 | 2003-01-18 23:22:20 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 713 | /*[clinic input] |
| 714 | _operator.is_not = _operator.add |
| 715 | |
| 716 | Same as a is not b. |
| 717 | [clinic start generated code]*/ |
| 718 | |
| 719 | static PyObject * |
| 720 | _operator_is_not_impl(PyObject *module, PyObject *a, PyObject *b) |
| 721 | /*[clinic end generated code: output=491a1f2f81f6c7f9 input=5a93f7e1a93535f1]*/ |
Raymond Hettinger | 9543b34 | 2003-01-18 23:22:20 +0000 | [diff] [blame] | 722 | { |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 723 | PyObject *result; |
| 724 | result = (a != b) ? Py_True : Py_False; |
| 725 | Py_INCREF(result); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 726 | return result; |
Raymond Hettinger | 9543b34 | 2003-01-18 23:22:20 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Christian Heimes | 6cea655 | 2012-06-24 13:48:32 +0200 | [diff] [blame] | 729 | /* compare_digest **********************************************************/ |
| 730 | |
| 731 | /* |
| 732 | * timing safe compare |
| 733 | * |
| 734 | * Returns 1 of the strings are equal. |
| 735 | * In case of len(a) != len(b) the function tries to keep the timing |
| 736 | * dependent on the length of b. CPU cache locally may still alter timing |
| 737 | * a bit. |
| 738 | */ |
| 739 | static int |
| 740 | _tscmp(const unsigned char *a, const unsigned char *b, |
| 741 | Py_ssize_t len_a, Py_ssize_t len_b) |
| 742 | { |
| 743 | /* The volatile type declarations make sure that the compiler has no |
| 744 | * chance to optimize and fold the code in any way that may change |
| 745 | * the timing. |
| 746 | */ |
| 747 | volatile Py_ssize_t length; |
| 748 | volatile const unsigned char *left; |
| 749 | volatile const unsigned char *right; |
| 750 | Py_ssize_t i; |
Devin Jeanpierre | 3172936 | 2020-11-21 01:55:23 -0700 | [diff] [blame] | 751 | volatile unsigned char result; |
Christian Heimes | 6cea655 | 2012-06-24 13:48:32 +0200 | [diff] [blame] | 752 | |
| 753 | /* loop count depends on length of b */ |
| 754 | length = len_b; |
| 755 | left = NULL; |
| 756 | right = b; |
| 757 | |
| 758 | /* don't use else here to keep the amount of CPU instructions constant, |
| 759 | * volatile forces re-evaluation |
| 760 | * */ |
| 761 | if (len_a == length) { |
| 762 | left = *((volatile const unsigned char**)&a); |
| 763 | result = 0; |
| 764 | } |
| 765 | if (len_a != length) { |
| 766 | left = b; |
| 767 | result = 1; |
| 768 | } |
| 769 | |
| 770 | for (i=0; i < length; i++) { |
| 771 | result |= *left++ ^ *right++; |
| 772 | } |
| 773 | |
| 774 | return (result == 0); |
| 775 | } |
| 776 | |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 777 | /*[clinic input] |
| 778 | _operator.length_hint -> Py_ssize_t |
Armin Ronacher | aa9a79d | 2012-10-06 14:03:24 +0200 | [diff] [blame] | 779 | |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 780 | obj: object |
| 781 | default: Py_ssize_t = 0 |
| 782 | / |
| 783 | |
| 784 | Return an estimate of the number of items in obj. |
| 785 | |
| 786 | This is useful for presizing containers when building from an iterable. |
| 787 | |
| 788 | If the object supports len(), the result will be exact. |
| 789 | Otherwise, it may over- or under-estimate by an arbitrary amount. |
| 790 | The result will be an integer >= 0. |
| 791 | [clinic start generated code]*/ |
| 792 | |
| 793 | static Py_ssize_t |
| 794 | _operator_length_hint_impl(PyObject *module, PyObject *obj, |
| 795 | Py_ssize_t default_value) |
| 796 | /*[clinic end generated code: output=01d469edc1d612ad input=65ed29f04401e96a]*/ |
Armin Ronacher | aa9a79d | 2012-10-06 14:03:24 +0200 | [diff] [blame] | 797 | { |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 798 | return PyObject_LengthHint(obj, default_value); |
Armin Ronacher | aa9a79d | 2012-10-06 14:03:24 +0200 | [diff] [blame] | 799 | } |
| 800 | |
Christian Heimes | db5aed9 | 2020-05-27 21:50:06 +0200 | [diff] [blame] | 801 | /* NOTE: Keep in sync with _hashopenssl.c implementation. */ |
| 802 | |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 803 | /*[clinic input] |
| 804 | _operator._compare_digest = _operator.eq |
Armin Ronacher | aa9a79d | 2012-10-06 14:03:24 +0200 | [diff] [blame] | 805 | |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 806 | Return 'a == b'. |
Christian Heimes | 6cea655 | 2012-06-24 13:48:32 +0200 | [diff] [blame] | 807 | |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 808 | This function uses an approach designed to prevent |
| 809 | timing analysis, making it appropriate for cryptography. |
| 810 | |
| 811 | a and b must both be of the same type: either str (ASCII only), |
| 812 | or any bytes-like object. |
| 813 | |
| 814 | Note: If a and b are of different lengths, or if an error occurs, |
| 815 | a timing attack could theoretically reveal information about the |
| 816 | types and lengths of a and b--but not their values. |
| 817 | [clinic start generated code]*/ |
| 818 | |
| 819 | static PyObject * |
| 820 | _operator__compare_digest_impl(PyObject *module, PyObject *a, PyObject *b) |
| 821 | /*[clinic end generated code: output=11d452bdd3a23cbc input=9ac7e2c4e30bc356]*/ |
Christian Heimes | 6cea655 | 2012-06-24 13:48:32 +0200 | [diff] [blame] | 822 | { |
Christian Heimes | 6cea655 | 2012-06-24 13:48:32 +0200 | [diff] [blame] | 823 | int rc; |
Christian Heimes | 6cea655 | 2012-06-24 13:48:32 +0200 | [diff] [blame] | 824 | |
Christian Heimes | 6cea655 | 2012-06-24 13:48:32 +0200 | [diff] [blame] | 825 | /* ASCII unicode string */ |
| 826 | if(PyUnicode_Check(a) && PyUnicode_Check(b)) { |
| 827 | if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) { |
| 828 | return NULL; |
| 829 | } |
| 830 | if (!PyUnicode_IS_ASCII(a) || !PyUnicode_IS_ASCII(b)) { |
| 831 | PyErr_SetString(PyExc_TypeError, |
| 832 | "comparing strings with non-ASCII characters is " |
| 833 | "not supported"); |
| 834 | return NULL; |
| 835 | } |
| 836 | |
| 837 | rc = _tscmp(PyUnicode_DATA(a), |
| 838 | PyUnicode_DATA(b), |
| 839 | PyUnicode_GET_LENGTH(a), |
| 840 | PyUnicode_GET_LENGTH(b)); |
| 841 | } |
| 842 | /* fallback to buffer interface for bytes, bytesarray and other */ |
| 843 | else { |
| 844 | Py_buffer view_a; |
| 845 | Py_buffer view_b; |
| 846 | |
Benjamin Peterson | 23a192d | 2014-05-11 16:17:02 -0700 | [diff] [blame] | 847 | if (PyObject_CheckBuffer(a) == 0 && PyObject_CheckBuffer(b) == 0) { |
Christian Heimes | 6cea655 | 2012-06-24 13:48:32 +0200 | [diff] [blame] | 848 | PyErr_Format(PyExc_TypeError, |
| 849 | "unsupported operand types(s) or combination of types: " |
| 850 | "'%.100s' and '%.100s'", |
| 851 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
| 852 | return NULL; |
| 853 | } |
| 854 | |
| 855 | if (PyObject_GetBuffer(a, &view_a, PyBUF_SIMPLE) == -1) { |
| 856 | return NULL; |
| 857 | } |
| 858 | if (view_a.ndim > 1) { |
| 859 | PyErr_SetString(PyExc_BufferError, |
| 860 | "Buffer must be single dimension"); |
| 861 | PyBuffer_Release(&view_a); |
| 862 | return NULL; |
| 863 | } |
| 864 | |
| 865 | if (PyObject_GetBuffer(b, &view_b, PyBUF_SIMPLE) == -1) { |
| 866 | PyBuffer_Release(&view_a); |
| 867 | return NULL; |
| 868 | } |
| 869 | if (view_b.ndim > 1) { |
| 870 | PyErr_SetString(PyExc_BufferError, |
| 871 | "Buffer must be single dimension"); |
| 872 | PyBuffer_Release(&view_a); |
| 873 | PyBuffer_Release(&view_b); |
| 874 | return NULL; |
| 875 | } |
| 876 | |
| 877 | rc = _tscmp((const unsigned char*)view_a.buf, |
| 878 | (const unsigned char*)view_b.buf, |
| 879 | view_a.len, |
| 880 | view_b.len); |
| 881 | |
| 882 | PyBuffer_Release(&view_a); |
| 883 | PyBuffer_Release(&view_b); |
| 884 | } |
| 885 | |
Georg Brandl | 93b7d7e | 2012-06-24 13:54:51 +0200 | [diff] [blame] | 886 | return PyBool_FromLong(rc); |
Christian Heimes | 6cea655 | 2012-06-24 13:48:32 +0200 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | /* operator methods **********************************************************/ |
| 890 | |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 891 | static struct PyMethodDef operator_methods[] = { |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 892 | |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 893 | _OPERATOR_TRUTH_METHODDEF |
| 894 | _OPERATOR_CONTAINS_METHODDEF |
| 895 | _OPERATOR_INDEXOF_METHODDEF |
| 896 | _OPERATOR_COUNTOF_METHODDEF |
| 897 | _OPERATOR_IS__METHODDEF |
| 898 | _OPERATOR_IS_NOT_METHODDEF |
| 899 | _OPERATOR_INDEX_METHODDEF |
| 900 | _OPERATOR_ADD_METHODDEF |
| 901 | _OPERATOR_SUB_METHODDEF |
| 902 | _OPERATOR_MUL_METHODDEF |
| 903 | _OPERATOR_MATMUL_METHODDEF |
| 904 | _OPERATOR_FLOORDIV_METHODDEF |
| 905 | _OPERATOR_TRUEDIV_METHODDEF |
| 906 | _OPERATOR_MOD_METHODDEF |
| 907 | _OPERATOR_NEG_METHODDEF |
| 908 | _OPERATOR_POS_METHODDEF |
| 909 | _OPERATOR_ABS_METHODDEF |
| 910 | _OPERATOR_INV_METHODDEF |
| 911 | _OPERATOR_INVERT_METHODDEF |
| 912 | _OPERATOR_LSHIFT_METHODDEF |
| 913 | _OPERATOR_RSHIFT_METHODDEF |
| 914 | _OPERATOR_NOT__METHODDEF |
| 915 | _OPERATOR_AND__METHODDEF |
| 916 | _OPERATOR_XOR_METHODDEF |
| 917 | _OPERATOR_OR__METHODDEF |
| 918 | _OPERATOR_IADD_METHODDEF |
| 919 | _OPERATOR_ISUB_METHODDEF |
| 920 | _OPERATOR_IMUL_METHODDEF |
| 921 | _OPERATOR_IMATMUL_METHODDEF |
| 922 | _OPERATOR_IFLOORDIV_METHODDEF |
| 923 | _OPERATOR_ITRUEDIV_METHODDEF |
| 924 | _OPERATOR_IMOD_METHODDEF |
| 925 | _OPERATOR_ILSHIFT_METHODDEF |
| 926 | _OPERATOR_IRSHIFT_METHODDEF |
| 927 | _OPERATOR_IAND_METHODDEF |
| 928 | _OPERATOR_IXOR_METHODDEF |
| 929 | _OPERATOR_IOR_METHODDEF |
| 930 | _OPERATOR_CONCAT_METHODDEF |
| 931 | _OPERATOR_ICONCAT_METHODDEF |
| 932 | _OPERATOR_GETITEM_METHODDEF |
| 933 | _OPERATOR_SETITEM_METHODDEF |
| 934 | _OPERATOR_DELITEM_METHODDEF |
| 935 | _OPERATOR_POW_METHODDEF |
| 936 | _OPERATOR_IPOW_METHODDEF |
| 937 | _OPERATOR_EQ_METHODDEF |
| 938 | _OPERATOR_NE_METHODDEF |
| 939 | _OPERATOR_LT_METHODDEF |
| 940 | _OPERATOR_LE_METHODDEF |
| 941 | _OPERATOR_GT_METHODDEF |
| 942 | _OPERATOR_GE_METHODDEF |
| 943 | _OPERATOR__COMPARE_DIGEST_METHODDEF |
| 944 | _OPERATOR_LENGTH_HINT_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 945 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 946 | |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 947 | }; |
| 948 | |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 949 | /* itemgetter object **********************************************************/ |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 950 | |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 951 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | PyObject_HEAD |
| 953 | Py_ssize_t nitems; |
| 954 | PyObject *item; |
Raymond Hettinger | 2d53bed | 2019-01-07 09:38:41 -0700 | [diff] [blame] | 955 | Py_ssize_t index; // -1 unless *item* is a single non-negative integer index |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 956 | } itemgetterobject; |
| 957 | |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 958 | /* AC 3.5: treats first argument as an iterable, otherwise uses *args */ |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 959 | static PyObject * |
| 960 | itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 961 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 962 | itemgetterobject *ig; |
| 963 | PyObject *item; |
| 964 | Py_ssize_t nitems; |
Raymond Hettinger | 2d53bed | 2019-01-07 09:38:41 -0700 | [diff] [blame] | 965 | Py_ssize_t index; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 966 | |
Serhiy Storchaka | 6cca5c8 | 2017-06-08 14:41:19 +0300 | [diff] [blame] | 967 | if (!_PyArg_NoKeywords("itemgetter", kwds)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 968 | return NULL; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 969 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 970 | nitems = PyTuple_GET_SIZE(args); |
| 971 | if (nitems <= 1) { |
| 972 | if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item)) |
| 973 | return NULL; |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 974 | } else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 975 | item = args; |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 976 | } |
| 977 | _operator_state *state = PyType_GetModuleState(type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 978 | /* create itemgetterobject structure */ |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 979 | ig = PyObject_GC_New(itemgetterobject, (PyTypeObject *) state->itemgetter_type); |
| 980 | if (ig == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 981 | return NULL; |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 982 | } |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 983 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 984 | Py_INCREF(item); |
| 985 | ig->item = item; |
| 986 | ig->nitems = nitems; |
Raymond Hettinger | 2d53bed | 2019-01-07 09:38:41 -0700 | [diff] [blame] | 987 | ig->index = -1; |
| 988 | if (PyLong_CheckExact(item)) { |
| 989 | index = PyLong_AsSsize_t(item); |
| 990 | if (index < 0) { |
| 991 | /* If we get here, then either the index conversion failed |
| 992 | * due to being out of range, or the index was a negative |
| 993 | * integer. Either way, we clear any possible exception |
| 994 | * and fall back to the slow path, where ig->index is -1. |
| 995 | */ |
| 996 | PyErr_Clear(); |
| 997 | } |
| 998 | else { |
| 999 | ig->index = index; |
| 1000 | } |
| 1001 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | |
| 1003 | PyObject_GC_Track(ig); |
| 1004 | return (PyObject *)ig; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | static void |
| 1008 | itemgetter_dealloc(itemgetterobject *ig) |
| 1009 | { |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 1010 | PyTypeObject *tp = Py_TYPE(ig); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | PyObject_GC_UnTrack(ig); |
| 1012 | Py_XDECREF(ig->item); |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 1013 | tp->tp_free(ig); |
| 1014 | Py_DECREF(tp); |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | static int |
| 1018 | itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg) |
| 1019 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | Py_VISIT(ig->item); |
| 1021 | return 0; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | static PyObject * |
| 1025 | itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw) |
| 1026 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | PyObject *obj, *result; |
| 1028 | Py_ssize_t i, nitems=ig->nitems; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1029 | |
Raymond Hettinger | 2d53bed | 2019-01-07 09:38:41 -0700 | [diff] [blame] | 1030 | assert(PyTuple_CheckExact(args)); |
Serhiy Storchaka | 7934266 | 2019-01-12 08:25:41 +0200 | [diff] [blame] | 1031 | if (!_PyArg_NoKeywords("itemgetter", kw)) |
| 1032 | return NULL; |
| 1033 | if (!_PyArg_CheckPositional("itemgetter", PyTuple_GET_SIZE(args), 1, 1)) |
| 1034 | return NULL; |
| 1035 | |
| 1036 | obj = PyTuple_GET_ITEM(args, 0); |
Raymond Hettinger | 2d53bed | 2019-01-07 09:38:41 -0700 | [diff] [blame] | 1037 | if (nitems == 1) { |
| 1038 | if (ig->index >= 0 |
| 1039 | && PyTuple_CheckExact(obj) |
| 1040 | && ig->index < PyTuple_GET_SIZE(obj)) |
| 1041 | { |
| 1042 | result = PyTuple_GET_ITEM(obj, ig->index); |
| 1043 | Py_INCREF(result); |
| 1044 | return result; |
| 1045 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1046 | return PyObject_GetItem(obj, ig->item); |
Raymond Hettinger | 2d53bed | 2019-01-07 09:38:41 -0700 | [diff] [blame] | 1047 | } |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 1048 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | assert(PyTuple_Check(ig->item)); |
| 1050 | assert(PyTuple_GET_SIZE(ig->item) == nitems); |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 1051 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1052 | result = PyTuple_New(nitems); |
| 1053 | if (result == NULL) |
| 1054 | return NULL; |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 1055 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1056 | for (i=0 ; i < nitems ; i++) { |
| 1057 | PyObject *item, *val; |
| 1058 | item = PyTuple_GET_ITEM(ig->item, i); |
| 1059 | val = PyObject_GetItem(obj, item); |
| 1060 | if (val == NULL) { |
| 1061 | Py_DECREF(result); |
| 1062 | return NULL; |
| 1063 | } |
| 1064 | PyTuple_SET_ITEM(result, i, val); |
| 1065 | } |
| 1066 | return result; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
Serhiy Storchaka | 35ac5f8 | 2015-05-20 18:29:18 +0300 | [diff] [blame] | 1069 | static PyObject * |
| 1070 | itemgetter_repr(itemgetterobject *ig) |
| 1071 | { |
| 1072 | PyObject *repr; |
| 1073 | const char *reprfmt; |
| 1074 | |
| 1075 | int status = Py_ReprEnter((PyObject *)ig); |
| 1076 | if (status != 0) { |
| 1077 | if (status < 0) |
| 1078 | return NULL; |
| 1079 | return PyUnicode_FromFormat("%s(...)", Py_TYPE(ig)->tp_name); |
| 1080 | } |
| 1081 | |
| 1082 | reprfmt = ig->nitems == 1 ? "%s(%R)" : "%s%R"; |
| 1083 | repr = PyUnicode_FromFormat(reprfmt, Py_TYPE(ig)->tp_name, ig->item); |
| 1084 | Py_ReprLeave((PyObject *)ig); |
| 1085 | return repr; |
| 1086 | } |
| 1087 | |
| 1088 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1089 | itemgetter_reduce(itemgetterobject *ig, PyObject *Py_UNUSED(ignored)) |
Serhiy Storchaka | 35ac5f8 | 2015-05-20 18:29:18 +0300 | [diff] [blame] | 1090 | { |
| 1091 | if (ig->nitems == 1) |
| 1092 | return Py_BuildValue("O(O)", Py_TYPE(ig), ig->item); |
| 1093 | return PyTuple_Pack(2, Py_TYPE(ig), ig->item); |
| 1094 | } |
| 1095 | |
| 1096 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling"); |
| 1097 | |
| 1098 | static PyMethodDef itemgetter_methods[] = { |
| 1099 | {"__reduce__", (PyCFunction)itemgetter_reduce, METH_NOARGS, |
| 1100 | reduce_doc}, |
| 1101 | {NULL} |
| 1102 | }; |
| 1103 | |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1104 | PyDoc_STRVAR(itemgetter_doc, |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 1105 | "itemgetter(item, ...) --> itemgetter object\n\ |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1106 | \n\ |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 1107 | Return a callable object that fetches the given item(s) from its operand.\n\ |
Ezio Melotti | babc822 | 2013-05-08 10:53:11 +0300 | [diff] [blame] | 1108 | After f = itemgetter(2), the call f(r) returns r[2].\n\ |
| 1109 | After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])"); |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1110 | |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 1111 | static PyType_Slot itemgetter_type_slots[] = { |
| 1112 | {Py_tp_doc, (void *)itemgetter_doc}, |
| 1113 | {Py_tp_dealloc, itemgetter_dealloc}, |
| 1114 | {Py_tp_call, itemgetter_call}, |
| 1115 | {Py_tp_traverse, itemgetter_traverse}, |
| 1116 | {Py_tp_methods, itemgetter_methods}, |
| 1117 | {Py_tp_new, itemgetter_new}, |
| 1118 | {Py_tp_getattro, PyObject_GenericGetAttr}, |
| 1119 | {Py_tp_repr, itemgetter_repr}, |
| 1120 | {0, 0} |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1121 | }; |
| 1122 | |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 1123 | static PyType_Spec itemgetter_type_spec = { |
| 1124 | .name = "operator.itemgetter", |
| 1125 | .basicsize = sizeof(itemgetterobject), |
| 1126 | .itemsize = 0, |
| 1127 | .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 1128 | .slots = itemgetter_type_slots, |
| 1129 | }; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1130 | |
| 1131 | /* attrgetter object **********************************************************/ |
| 1132 | |
| 1133 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1134 | PyObject_HEAD |
| 1135 | Py_ssize_t nattrs; |
| 1136 | PyObject *attr; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1137 | } attrgetterobject; |
| 1138 | |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 1139 | /* AC 3.5: treats first argument as an iterable, otherwise uses *args */ |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1140 | static PyObject * |
| 1141 | attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1142 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1143 | attrgetterobject *ag; |
| 1144 | PyObject *attr; |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 1145 | Py_ssize_t nattrs, idx, char_idx; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1146 | |
Serhiy Storchaka | 6cca5c8 | 2017-06-08 14:41:19 +0300 | [diff] [blame] | 1147 | if (!_PyArg_NoKeywords("attrgetter", kwds)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1148 | return NULL; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1149 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1150 | nattrs = PyTuple_GET_SIZE(args); |
| 1151 | if (nattrs <= 1) { |
| 1152 | if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr)) |
| 1153 | return NULL; |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | attr = PyTuple_New(nattrs); |
| 1157 | if (attr == NULL) |
| 1158 | return NULL; |
| 1159 | |
| 1160 | /* prepare attr while checking args */ |
| 1161 | for (idx = 0; idx < nattrs; ++idx) { |
| 1162 | PyObject *item = PyTuple_GET_ITEM(args, idx); |
| 1163 | Py_ssize_t item_len; |
Serhiy Storchaka | cd8295f | 2020-04-11 10:48:40 +0300 | [diff] [blame] | 1164 | const void *data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1165 | unsigned int kind; |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 1166 | int dot_count; |
| 1167 | |
| 1168 | if (!PyUnicode_Check(item)) { |
| 1169 | PyErr_SetString(PyExc_TypeError, |
| 1170 | "attribute name must be a string"); |
| 1171 | Py_DECREF(attr); |
| 1172 | return NULL; |
| 1173 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1174 | if (PyUnicode_READY(item)) { |
| 1175 | Py_DECREF(attr); |
| 1176 | return NULL; |
| 1177 | } |
| 1178 | item_len = PyUnicode_GET_LENGTH(item); |
| 1179 | kind = PyUnicode_KIND(item); |
| 1180 | data = PyUnicode_DATA(item); |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 1181 | |
| 1182 | /* check whethere the string is dotted */ |
| 1183 | dot_count = 0; |
| 1184 | for (char_idx = 0; char_idx < item_len; ++char_idx) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1185 | if (PyUnicode_READ(kind, data, char_idx) == '.') |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 1186 | ++dot_count; |
| 1187 | } |
| 1188 | |
| 1189 | if (dot_count == 0) { |
| 1190 | Py_INCREF(item); |
| 1191 | PyUnicode_InternInPlace(&item); |
| 1192 | PyTuple_SET_ITEM(attr, idx, item); |
| 1193 | } else { /* make it a tuple of non-dotted attrnames */ |
| 1194 | PyObject *attr_chain = PyTuple_New(dot_count + 1); |
| 1195 | PyObject *attr_chain_item; |
Antoine Pitrou | 87298c4 | 2010-10-31 21:03:01 +0000 | [diff] [blame] | 1196 | Py_ssize_t unibuff_from = 0; |
| 1197 | Py_ssize_t unibuff_till = 0; |
| 1198 | Py_ssize_t attr_chain_idx = 0; |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 1199 | |
| 1200 | if (attr_chain == NULL) { |
| 1201 | Py_DECREF(attr); |
| 1202 | return NULL; |
| 1203 | } |
| 1204 | |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 1205 | for (; dot_count > 0; --dot_count) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1206 | while (PyUnicode_READ(kind, data, unibuff_till) != '.') { |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 1207 | ++unibuff_till; |
| 1208 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1209 | attr_chain_item = PyUnicode_Substring(item, |
| 1210 | unibuff_from, |
| 1211 | unibuff_till); |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 1212 | if (attr_chain_item == NULL) { |
| 1213 | Py_DECREF(attr_chain); |
| 1214 | Py_DECREF(attr); |
| 1215 | return NULL; |
| 1216 | } |
| 1217 | PyUnicode_InternInPlace(&attr_chain_item); |
| 1218 | PyTuple_SET_ITEM(attr_chain, attr_chain_idx, attr_chain_item); |
| 1219 | ++attr_chain_idx; |
| 1220 | unibuff_till = unibuff_from = unibuff_till + 1; |
| 1221 | } |
| 1222 | |
| 1223 | /* now add the last dotless name */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1224 | attr_chain_item = PyUnicode_Substring(item, |
| 1225 | unibuff_from, item_len); |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 1226 | if (attr_chain_item == NULL) { |
| 1227 | Py_DECREF(attr_chain); |
| 1228 | Py_DECREF(attr); |
| 1229 | return NULL; |
| 1230 | } |
| 1231 | PyUnicode_InternInPlace(&attr_chain_item); |
| 1232 | PyTuple_SET_ITEM(attr_chain, attr_chain_idx, attr_chain_item); |
| 1233 | |
| 1234 | PyTuple_SET_ITEM(attr, idx, attr_chain); |
| 1235 | } |
| 1236 | } |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1237 | |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 1238 | _operator_state *state = PyType_GetModuleState(type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1239 | /* create attrgetterobject structure */ |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 1240 | ag = PyObject_GC_New(attrgetterobject, (PyTypeObject *)state->attrgetter_type); |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 1241 | if (ag == NULL) { |
| 1242 | Py_DECREF(attr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1243 | return NULL; |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 1244 | } |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1245 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1246 | ag->attr = attr; |
| 1247 | ag->nattrs = nattrs; |
| 1248 | |
| 1249 | PyObject_GC_Track(ag); |
| 1250 | return (PyObject *)ag; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | static void |
| 1254 | attrgetter_dealloc(attrgetterobject *ag) |
| 1255 | { |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 1256 | PyTypeObject *tp = Py_TYPE(ag); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1257 | PyObject_GC_UnTrack(ag); |
| 1258 | Py_XDECREF(ag->attr); |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 1259 | tp->tp_free(ag); |
| 1260 | Py_DECREF(tp); |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | static int |
| 1264 | attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg) |
| 1265 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1266 | Py_VISIT(ag->attr); |
| 1267 | return 0; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | static PyObject * |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1271 | dotted_getattr(PyObject *obj, PyObject *attr) |
| 1272 | { |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 1273 | PyObject *newobj; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1274 | |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 1275 | /* attr is either a tuple or instance of str. |
| 1276 | Ensured by the setup code of attrgetter_new */ |
| 1277 | if (PyTuple_CheckExact(attr)) { /* chained getattr */ |
| 1278 | Py_ssize_t name_idx = 0, name_count; |
| 1279 | PyObject *attr_name; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1280 | |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 1281 | name_count = PyTuple_GET_SIZE(attr); |
| 1282 | Py_INCREF(obj); |
| 1283 | for (name_idx = 0; name_idx < name_count; ++name_idx) { |
| 1284 | attr_name = PyTuple_GET_ITEM(attr, name_idx); |
| 1285 | newobj = PyObject_GetAttr(obj, attr_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1286 | Py_DECREF(obj); |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 1287 | if (newobj == NULL) { |
| 1288 | return NULL; |
| 1289 | } |
| 1290 | /* here */ |
| 1291 | obj = newobj; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1292 | } |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 1293 | } else { /* single getattr */ |
| 1294 | newobj = PyObject_GetAttr(obj, attr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1295 | if (newobj == NULL) |
| 1296 | return NULL; |
| 1297 | obj = newobj; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1298 | } |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1299 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1300 | return obj; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | static PyObject * |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1304 | attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw) |
| 1305 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1306 | PyObject *obj, *result; |
| 1307 | Py_ssize_t i, nattrs=ag->nattrs; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1308 | |
Serhiy Storchaka | 68a001d | 2017-02-06 10:41:46 +0200 | [diff] [blame] | 1309 | if (!_PyArg_NoKeywords("attrgetter", kw)) |
Serhiy Storchaka | c2a2a75 | 2016-04-23 10:51:39 +0300 | [diff] [blame] | 1310 | return NULL; |
Serhiy Storchaka | 7934266 | 2019-01-12 08:25:41 +0200 | [diff] [blame] | 1311 | if (!_PyArg_CheckPositional("attrgetter", PyTuple_GET_SIZE(args), 1, 1)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1312 | return NULL; |
Serhiy Storchaka | 7934266 | 2019-01-12 08:25:41 +0200 | [diff] [blame] | 1313 | obj = PyTuple_GET_ITEM(args, 0); |
Antoine Pitrou | e974571 | 2010-10-31 15:26:04 +0000 | [diff] [blame] | 1314 | if (ag->nattrs == 1) /* ag->attr is always a tuple */ |
| 1315 | return dotted_getattr(obj, PyTuple_GET_ITEM(ag->attr, 0)); |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 1316 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1317 | assert(PyTuple_Check(ag->attr)); |
| 1318 | assert(PyTuple_GET_SIZE(ag->attr) == nattrs); |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 1319 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1320 | result = PyTuple_New(nattrs); |
| 1321 | if (result == NULL) |
| 1322 | return NULL; |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 1323 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1324 | for (i=0 ; i < nattrs ; i++) { |
| 1325 | PyObject *attr, *val; |
| 1326 | attr = PyTuple_GET_ITEM(ag->attr, i); |
| 1327 | val = dotted_getattr(obj, attr); |
| 1328 | if (val == NULL) { |
| 1329 | Py_DECREF(result); |
| 1330 | return NULL; |
| 1331 | } |
| 1332 | PyTuple_SET_ITEM(result, i, val); |
| 1333 | } |
| 1334 | return result; |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
Serhiy Storchaka | 35ac5f8 | 2015-05-20 18:29:18 +0300 | [diff] [blame] | 1337 | static PyObject * |
| 1338 | dotjoinattr(PyObject *attr, PyObject **attrsep) |
| 1339 | { |
| 1340 | if (PyTuple_CheckExact(attr)) { |
| 1341 | if (*attrsep == NULL) { |
| 1342 | *attrsep = PyUnicode_FromString("."); |
| 1343 | if (*attrsep == NULL) |
| 1344 | return NULL; |
| 1345 | } |
| 1346 | return PyUnicode_Join(*attrsep, attr); |
| 1347 | } else { |
| 1348 | Py_INCREF(attr); |
| 1349 | return attr; |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | static PyObject * |
| 1354 | attrgetter_args(attrgetterobject *ag) |
| 1355 | { |
| 1356 | Py_ssize_t i; |
| 1357 | PyObject *attrsep = NULL; |
| 1358 | PyObject *attrstrings = PyTuple_New(ag->nattrs); |
| 1359 | if (attrstrings == NULL) |
| 1360 | return NULL; |
| 1361 | |
| 1362 | for (i = 0; i < ag->nattrs; ++i) { |
| 1363 | PyObject *attr = PyTuple_GET_ITEM(ag->attr, i); |
| 1364 | PyObject *attrstr = dotjoinattr(attr, &attrsep); |
| 1365 | if (attrstr == NULL) { |
| 1366 | Py_XDECREF(attrsep); |
| 1367 | Py_DECREF(attrstrings); |
| 1368 | return NULL; |
| 1369 | } |
| 1370 | PyTuple_SET_ITEM(attrstrings, i, attrstr); |
| 1371 | } |
| 1372 | Py_XDECREF(attrsep); |
| 1373 | return attrstrings; |
| 1374 | } |
| 1375 | |
| 1376 | static PyObject * |
| 1377 | attrgetter_repr(attrgetterobject *ag) |
| 1378 | { |
| 1379 | PyObject *repr = NULL; |
| 1380 | int status = Py_ReprEnter((PyObject *)ag); |
| 1381 | if (status != 0) { |
| 1382 | if (status < 0) |
| 1383 | return NULL; |
| 1384 | return PyUnicode_FromFormat("%s(...)", Py_TYPE(ag)->tp_name); |
| 1385 | } |
| 1386 | |
| 1387 | if (ag->nattrs == 1) { |
| 1388 | PyObject *attrsep = NULL; |
| 1389 | PyObject *attr = dotjoinattr(PyTuple_GET_ITEM(ag->attr, 0), &attrsep); |
Serhiy Storchaka | 548de2b | 2015-05-21 14:19:20 +0300 | [diff] [blame] | 1390 | if (attr != NULL) { |
Serhiy Storchaka | 35ac5f8 | 2015-05-20 18:29:18 +0300 | [diff] [blame] | 1391 | repr = PyUnicode_FromFormat("%s(%R)", Py_TYPE(ag)->tp_name, attr); |
Serhiy Storchaka | 548de2b | 2015-05-21 14:19:20 +0300 | [diff] [blame] | 1392 | Py_DECREF(attr); |
| 1393 | } |
Serhiy Storchaka | 35ac5f8 | 2015-05-20 18:29:18 +0300 | [diff] [blame] | 1394 | Py_XDECREF(attrsep); |
| 1395 | } |
| 1396 | else { |
| 1397 | PyObject *attrstrings = attrgetter_args(ag); |
| 1398 | if (attrstrings != NULL) { |
| 1399 | repr = PyUnicode_FromFormat("%s%R", |
| 1400 | Py_TYPE(ag)->tp_name, attrstrings); |
| 1401 | Py_DECREF(attrstrings); |
| 1402 | } |
| 1403 | } |
| 1404 | Py_ReprLeave((PyObject *)ag); |
| 1405 | return repr; |
| 1406 | } |
| 1407 | |
| 1408 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1409 | attrgetter_reduce(attrgetterobject *ag, PyObject *Py_UNUSED(ignored)) |
Serhiy Storchaka | 35ac5f8 | 2015-05-20 18:29:18 +0300 | [diff] [blame] | 1410 | { |
| 1411 | PyObject *attrstrings = attrgetter_args(ag); |
| 1412 | if (attrstrings == NULL) |
| 1413 | return NULL; |
| 1414 | |
| 1415 | return Py_BuildValue("ON", Py_TYPE(ag), attrstrings); |
| 1416 | } |
| 1417 | |
| 1418 | static PyMethodDef attrgetter_methods[] = { |
| 1419 | {"__reduce__", (PyCFunction)attrgetter_reduce, METH_NOARGS, |
| 1420 | reduce_doc}, |
| 1421 | {NULL} |
| 1422 | }; |
| 1423 | |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1424 | PyDoc_STRVAR(attrgetter_doc, |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 1425 | "attrgetter(attr, ...) --> attrgetter object\n\ |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1426 | \n\ |
Raymond Hettinger | 984f9bb | 2005-03-09 16:38:48 +0000 | [diff] [blame] | 1427 | Return a callable object that fetches the given attribute(s) from its operand.\n\ |
Ezio Melotti | babc822 | 2013-05-08 10:53:11 +0300 | [diff] [blame] | 1428 | After f = attrgetter('name'), the call f(r) returns r.name.\n\ |
| 1429 | After g = attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\ |
| 1430 | After h = attrgetter('name.first', 'name.last'), the call h(r) returns\n\ |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1431 | (r.name.first, r.name.last)."); |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1432 | |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 1433 | static PyType_Slot attrgetter_type_slots[] = { |
| 1434 | {Py_tp_doc, (void *)attrgetter_doc}, |
| 1435 | {Py_tp_dealloc, attrgetter_dealloc}, |
| 1436 | {Py_tp_call, attrgetter_call}, |
| 1437 | {Py_tp_traverse, attrgetter_traverse}, |
| 1438 | {Py_tp_methods, attrgetter_methods}, |
| 1439 | {Py_tp_new, attrgetter_new}, |
| 1440 | {Py_tp_getattro, PyObject_GenericGetAttr}, |
| 1441 | {Py_tp_repr, attrgetter_repr}, |
| 1442 | {0, 0} |
| 1443 | }; |
| 1444 | |
| 1445 | static PyType_Spec attrgetter_type_spec = { |
| 1446 | .name = "operator.attrgetter", |
| 1447 | .basicsize = sizeof(attrgetterobject), |
| 1448 | .itemsize = 0, |
| 1449 | .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 1450 | .slots = attrgetter_type_slots, |
Raymond Hettinger | 166958b | 2003-12-01 13:18:39 +0000 | [diff] [blame] | 1451 | }; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1452 | |
| 1453 | |
| 1454 | /* methodcaller object **********************************************************/ |
| 1455 | |
| 1456 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1457 | PyObject_HEAD |
| 1458 | PyObject *name; |
| 1459 | PyObject *args; |
| 1460 | PyObject *kwds; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1461 | } methodcallerobject; |
| 1462 | |
Serhiy Storchaka | b813a0e | 2017-01-19 17:44:13 +0200 | [diff] [blame] | 1463 | /* AC 3.5: variable number of arguments, not currently support by AC */ |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1464 | static PyObject * |
| 1465 | methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1466 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1467 | methodcallerobject *mc; |
Benjamin Peterson | 1f0e7c9 | 2016-08-16 23:35:35 -0700 | [diff] [blame] | 1468 | PyObject *name; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1469 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1470 | if (PyTuple_GET_SIZE(args) < 1) { |
| 1471 | PyErr_SetString(PyExc_TypeError, "methodcaller needs at least " |
| 1472 | "one argument, the method name"); |
| 1473 | return NULL; |
| 1474 | } |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1475 | |
Serhiy Storchaka | 35ac5f8 | 2015-05-20 18:29:18 +0300 | [diff] [blame] | 1476 | name = PyTuple_GET_ITEM(args, 0); |
| 1477 | if (!PyUnicode_Check(name)) { |
| 1478 | PyErr_SetString(PyExc_TypeError, |
| 1479 | "method name must be a string"); |
| 1480 | return NULL; |
| 1481 | } |
| 1482 | |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 1483 | _operator_state *state = PyType_GetModuleState(type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1484 | /* create methodcallerobject structure */ |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 1485 | mc = PyObject_GC_New(methodcallerobject, (PyTypeObject *)state->methodcaller_type); |
| 1486 | if (mc == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1487 | return NULL; |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 1488 | } |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1489 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1490 | name = PyTuple_GET_ITEM(args, 0); |
| 1491 | Py_INCREF(name); |
Serhiy Storchaka | 35ac5f8 | 2015-05-20 18:29:18 +0300 | [diff] [blame] | 1492 | PyUnicode_InternInPlace(&name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1493 | mc->name = name; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1494 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1495 | Py_XINCREF(kwds); |
| 1496 | mc->kwds = kwds; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1497 | |
Benjamin Peterson | 1f0e7c9 | 2016-08-16 23:35:35 -0700 | [diff] [blame] | 1498 | mc->args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 1499 | if (mc->args == NULL) { |
| 1500 | Py_DECREF(mc); |
| 1501 | return NULL; |
| 1502 | } |
| 1503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1504 | PyObject_GC_Track(mc); |
| 1505 | return (PyObject *)mc; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | static void |
| 1509 | methodcaller_dealloc(methodcallerobject *mc) |
| 1510 | { |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 1511 | PyTypeObject *tp = Py_TYPE(mc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1512 | PyObject_GC_UnTrack(mc); |
| 1513 | Py_XDECREF(mc->name); |
| 1514 | Py_XDECREF(mc->args); |
| 1515 | Py_XDECREF(mc->kwds); |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 1516 | tp->tp_free(mc); |
| 1517 | Py_DECREF(tp); |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | static int |
| 1521 | methodcaller_traverse(methodcallerobject *mc, visitproc visit, void *arg) |
| 1522 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1523 | Py_VISIT(mc->args); |
| 1524 | Py_VISIT(mc->kwds); |
| 1525 | return 0; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | static PyObject * |
| 1529 | methodcaller_call(methodcallerobject *mc, PyObject *args, PyObject *kw) |
| 1530 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1531 | PyObject *method, *obj, *result; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1532 | |
Serhiy Storchaka | 68a001d | 2017-02-06 10:41:46 +0200 | [diff] [blame] | 1533 | if (!_PyArg_NoKeywords("methodcaller", kw)) |
Serhiy Storchaka | c2a2a75 | 2016-04-23 10:51:39 +0300 | [diff] [blame] | 1534 | return NULL; |
Serhiy Storchaka | 7934266 | 2019-01-12 08:25:41 +0200 | [diff] [blame] | 1535 | if (!_PyArg_CheckPositional("methodcaller", PyTuple_GET_SIZE(args), 1, 1)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1536 | return NULL; |
Serhiy Storchaka | 7934266 | 2019-01-12 08:25:41 +0200 | [diff] [blame] | 1537 | obj = PyTuple_GET_ITEM(args, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1538 | method = PyObject_GetAttr(obj, mc->name); |
| 1539 | if (method == NULL) |
| 1540 | return NULL; |
| 1541 | result = PyObject_Call(method, mc->args, mc->kwds); |
| 1542 | Py_DECREF(method); |
| 1543 | return result; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Serhiy Storchaka | 35ac5f8 | 2015-05-20 18:29:18 +0300 | [diff] [blame] | 1546 | static PyObject * |
| 1547 | methodcaller_repr(methodcallerobject *mc) |
| 1548 | { |
| 1549 | PyObject *argreprs, *repr = NULL, *sep, *joinedargreprs; |
| 1550 | Py_ssize_t numtotalargs, numposargs, numkwdargs, i; |
| 1551 | int status = Py_ReprEnter((PyObject *)mc); |
| 1552 | if (status != 0) { |
| 1553 | if (status < 0) |
| 1554 | return NULL; |
| 1555 | return PyUnicode_FromFormat("%s(...)", Py_TYPE(mc)->tp_name); |
| 1556 | } |
| 1557 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 1558 | numkwdargs = mc->kwds != NULL ? PyDict_GET_SIZE(mc->kwds) : 0; |
Serhiy Storchaka | 35ac5f8 | 2015-05-20 18:29:18 +0300 | [diff] [blame] | 1559 | numposargs = PyTuple_GET_SIZE(mc->args); |
| 1560 | numtotalargs = numposargs + numkwdargs; |
| 1561 | |
| 1562 | if (numtotalargs == 0) { |
| 1563 | repr = PyUnicode_FromFormat("%s(%R)", Py_TYPE(mc)->tp_name, mc->name); |
| 1564 | Py_ReprLeave((PyObject *)mc); |
| 1565 | return repr; |
| 1566 | } |
| 1567 | |
| 1568 | argreprs = PyTuple_New(numtotalargs); |
| 1569 | if (argreprs == NULL) { |
| 1570 | Py_ReprLeave((PyObject *)mc); |
| 1571 | return NULL; |
| 1572 | } |
| 1573 | |
| 1574 | for (i = 0; i < numposargs; ++i) { |
| 1575 | PyObject *onerepr = PyObject_Repr(PyTuple_GET_ITEM(mc->args, i)); |
| 1576 | if (onerepr == NULL) |
| 1577 | goto done; |
| 1578 | PyTuple_SET_ITEM(argreprs, i, onerepr); |
| 1579 | } |
| 1580 | |
| 1581 | if (numkwdargs != 0) { |
| 1582 | PyObject *key, *value; |
| 1583 | Py_ssize_t pos = 0; |
| 1584 | while (PyDict_Next(mc->kwds, &pos, &key, &value)) { |
| 1585 | PyObject *onerepr = PyUnicode_FromFormat("%U=%R", key, value); |
| 1586 | if (onerepr == NULL) |
| 1587 | goto done; |
| 1588 | if (i >= numtotalargs) { |
| 1589 | i = -1; |
Zackery Spytz | 5b83ef7 | 2018-11-23 12:26:46 -0700 | [diff] [blame] | 1590 | Py_DECREF(onerepr); |
Serhiy Storchaka | 35ac5f8 | 2015-05-20 18:29:18 +0300 | [diff] [blame] | 1591 | break; |
| 1592 | } |
| 1593 | PyTuple_SET_ITEM(argreprs, i, onerepr); |
| 1594 | ++i; |
| 1595 | } |
| 1596 | if (i != numtotalargs) { |
| 1597 | PyErr_SetString(PyExc_RuntimeError, |
| 1598 | "keywords dict changed size during iteration"); |
| 1599 | goto done; |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | sep = PyUnicode_FromString(", "); |
| 1604 | if (sep == NULL) |
| 1605 | goto done; |
| 1606 | |
| 1607 | joinedargreprs = PyUnicode_Join(sep, argreprs); |
| 1608 | Py_DECREF(sep); |
| 1609 | if (joinedargreprs == NULL) |
| 1610 | goto done; |
| 1611 | |
| 1612 | repr = PyUnicode_FromFormat("%s(%R, %U)", Py_TYPE(mc)->tp_name, |
| 1613 | mc->name, joinedargreprs); |
| 1614 | Py_DECREF(joinedargreprs); |
| 1615 | |
| 1616 | done: |
| 1617 | Py_DECREF(argreprs); |
| 1618 | Py_ReprLeave((PyObject *)mc); |
| 1619 | return repr; |
| 1620 | } |
| 1621 | |
| 1622 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1623 | methodcaller_reduce(methodcallerobject *mc, PyObject *Py_UNUSED(ignored)) |
Serhiy Storchaka | 35ac5f8 | 2015-05-20 18:29:18 +0300 | [diff] [blame] | 1624 | { |
| 1625 | PyObject *newargs; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 1626 | if (!mc->kwds || PyDict_GET_SIZE(mc->kwds) == 0) { |
Serhiy Storchaka | 35ac5f8 | 2015-05-20 18:29:18 +0300 | [diff] [blame] | 1627 | Py_ssize_t i; |
| 1628 | Py_ssize_t callargcount = PyTuple_GET_SIZE(mc->args); |
| 1629 | newargs = PyTuple_New(1 + callargcount); |
| 1630 | if (newargs == NULL) |
| 1631 | return NULL; |
| 1632 | Py_INCREF(mc->name); |
| 1633 | PyTuple_SET_ITEM(newargs, 0, mc->name); |
| 1634 | for (i = 0; i < callargcount; ++i) { |
| 1635 | PyObject *arg = PyTuple_GET_ITEM(mc->args, i); |
| 1636 | Py_INCREF(arg); |
| 1637 | PyTuple_SET_ITEM(newargs, i + 1, arg); |
| 1638 | } |
| 1639 | return Py_BuildValue("ON", Py_TYPE(mc), newargs); |
| 1640 | } |
| 1641 | else { |
| 1642 | PyObject *functools; |
| 1643 | PyObject *partial; |
| 1644 | PyObject *constructor; |
Victor Stinner | 7e7823a | 2016-08-23 00:23:23 +0200 | [diff] [blame] | 1645 | PyObject *newargs[2]; |
| 1646 | |
Serhiy Storchaka | 35ac5f8 | 2015-05-20 18:29:18 +0300 | [diff] [blame] | 1647 | _Py_IDENTIFIER(partial); |
| 1648 | functools = PyImport_ImportModule("functools"); |
| 1649 | if (!functools) |
| 1650 | return NULL; |
| 1651 | partial = _PyObject_GetAttrId(functools, &PyId_partial); |
| 1652 | Py_DECREF(functools); |
| 1653 | if (!partial) |
| 1654 | return NULL; |
Victor Stinner | 7e7823a | 2016-08-23 00:23:23 +0200 | [diff] [blame] | 1655 | |
| 1656 | newargs[0] = (PyObject *)Py_TYPE(mc); |
| 1657 | newargs[1] = mc->name; |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 1658 | constructor = PyObject_VectorcallDict(partial, newargs, 2, mc->kwds); |
Victor Stinner | 7e7823a | 2016-08-23 00:23:23 +0200 | [diff] [blame] | 1659 | |
Serhiy Storchaka | 35ac5f8 | 2015-05-20 18:29:18 +0300 | [diff] [blame] | 1660 | Py_DECREF(partial); |
| 1661 | return Py_BuildValue("NO", constructor, mc->args); |
| 1662 | } |
| 1663 | } |
| 1664 | |
| 1665 | static PyMethodDef methodcaller_methods[] = { |
| 1666 | {"__reduce__", (PyCFunction)methodcaller_reduce, METH_NOARGS, |
| 1667 | reduce_doc}, |
| 1668 | {NULL} |
| 1669 | }; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1670 | PyDoc_STRVAR(methodcaller_doc, |
| 1671 | "methodcaller(name, ...) --> methodcaller object\n\ |
| 1672 | \n\ |
| 1673 | Return a callable object that calls the given method on its operand.\n\ |
Antoine Pitrou | a85017f | 2013-04-20 19:21:44 +0200 | [diff] [blame] | 1674 | After f = methodcaller('name'), the call f(r) returns r.name().\n\ |
| 1675 | After g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\ |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1676 | r.name('date', foo=1)."); |
| 1677 | |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 1678 | static PyType_Slot methodcaller_type_slots[] = { |
| 1679 | {Py_tp_doc, (void *)methodcaller_doc}, |
| 1680 | {Py_tp_dealloc, methodcaller_dealloc}, |
| 1681 | {Py_tp_call, methodcaller_call}, |
| 1682 | {Py_tp_traverse, methodcaller_traverse}, |
| 1683 | {Py_tp_methods, methodcaller_methods}, |
| 1684 | {Py_tp_new, methodcaller_new}, |
| 1685 | {Py_tp_getattro, PyObject_GenericGetAttr}, |
| 1686 | {Py_tp_repr, methodcaller_repr}, |
| 1687 | {0, 0} |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1688 | }; |
| 1689 | |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 1690 | static PyType_Spec methodcaller_type_spec = { |
| 1691 | .name = "operator.methodcaller", |
| 1692 | .basicsize = sizeof(methodcallerobject), |
| 1693 | .itemsize = 0, |
| 1694 | .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 1695 | .slots = methodcaller_type_slots, |
| 1696 | }; |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1697 | |
Paulo Henrique Silva | f3d5ac4 | 2020-03-24 23:18:47 -0300 | [diff] [blame] | 1698 | static int |
| 1699 | operator_exec(PyObject *module) |
| 1700 | { |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 1701 | _operator_state *state = get_operator_state(module); |
| 1702 | state->attrgetter_type = PyType_FromModuleAndSpec(module, &attrgetter_type_spec, NULL); |
| 1703 | if (state->attrgetter_type == NULL) { |
| 1704 | return -1; |
| 1705 | } |
| 1706 | if (PyModule_AddType(module, (PyTypeObject *)state->attrgetter_type) < 0) { |
| 1707 | return -1; |
| 1708 | } |
Paulo Henrique Silva | f3d5ac4 | 2020-03-24 23:18:47 -0300 | [diff] [blame] | 1709 | |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 1710 | state->itemgetter_type = PyType_FromModuleAndSpec(module, &itemgetter_type_spec, NULL); |
| 1711 | if (state->itemgetter_type == NULL) { |
| 1712 | return -1; |
| 1713 | } |
| 1714 | if (PyModule_AddType(module, (PyTypeObject *)state->itemgetter_type) < 0) { |
| 1715 | return -1; |
| 1716 | } |
| 1717 | |
| 1718 | state->methodcaller_type = PyType_FromModuleAndSpec(module, &methodcaller_type_spec, NULL); |
| 1719 | if (state->methodcaller_type == NULL) { |
| 1720 | return -1; |
| 1721 | } |
| 1722 | if (PyModule_AddType(module, (PyTypeObject *)state->methodcaller_type) < 0) { |
| 1723 | return -1; |
Paulo Henrique Silva | f3d5ac4 | 2020-03-24 23:18:47 -0300 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | return 0; |
| 1727 | } |
| 1728 | |
| 1729 | |
| 1730 | static struct PyModuleDef_Slot operator_slots[] = { |
| 1731 | {Py_mod_exec, operator_exec}, |
| 1732 | {0, NULL} |
| 1733 | }; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1734 | |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 1735 | static int |
| 1736 | operator_traverse(PyObject *module, visitproc visit, void *arg) |
| 1737 | { |
| 1738 | _operator_state *state = get_operator_state(module); |
| 1739 | Py_VISIT(state->attrgetter_type); |
| 1740 | Py_VISIT(state->itemgetter_type); |
| 1741 | Py_VISIT(state->methodcaller_type); |
| 1742 | return 0; |
| 1743 | } |
| 1744 | |
| 1745 | static int |
| 1746 | operator_clear(PyObject *module) |
| 1747 | { |
| 1748 | _operator_state *state = get_operator_state(module); |
| 1749 | Py_CLEAR(state->attrgetter_type); |
| 1750 | Py_CLEAR(state->itemgetter_type); |
| 1751 | Py_CLEAR(state->methodcaller_type); |
| 1752 | return 0; |
| 1753 | } |
| 1754 | |
| 1755 | static void |
| 1756 | operator_free(void *module) |
| 1757 | { |
| 1758 | operator_clear((PyObject *)module); |
| 1759 | } |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1760 | |
| 1761 | static struct PyModuleDef operatormodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1762 | PyModuleDef_HEAD_INIT, |
Dong-hee Na | 31967fd | 2020-08-26 17:22:27 +0000 | [diff] [blame] | 1763 | .m_name = "_operator", |
| 1764 | .m_doc = operator_doc, |
| 1765 | .m_size = sizeof(_operator_state), |
| 1766 | .m_methods = operator_methods, |
| 1767 | .m_slots = operator_slots, |
| 1768 | .m_traverse = operator_traverse, |
| 1769 | .m_clear = operator_clear, |
| 1770 | .m_free = operator_free, |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1771 | }; |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 1772 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 1773 | PyMODINIT_FUNC |
Antoine Pitrou | a85017f | 2013-04-20 19:21:44 +0200 | [diff] [blame] | 1774 | PyInit__operator(void) |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 1775 | { |
Paulo Henrique Silva | f3d5ac4 | 2020-03-24 23:18:47 -0300 | [diff] [blame] | 1776 | return PyModuleDef_Init(&operatormodule); |
Guido van Rossum | 037b940 | 1996-07-30 16:55:54 +0000 | [diff] [blame] | 1777 | } |