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