#include "Python.h" #include "pycore_modsupport.h" // _PyArg_NoKwnames() #include "pycore_moduleobject.h" // _PyModule_GetState() #include "pycore_runtime.h" // _Py_ID() #include "pycore_pystate.h" // _PyInterpreterState_GET() #include "clinic/_operator.c.h" _operator_state; static inline _operator_state* get_operator_state(PyObject *module) { … } /*[clinic input] module _operator [clinic start generated code]*/ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=672ecf48487521e7]*/ PyDoc_STRVAR(operator_doc, "Operator interface.\n\ \n\ This module exports a set of functions implemented in C corresponding\n\ to the intrinsic operators of Python. For example, operator.add(x, y)\n\ is equivalent to the expression x+y. The function names are those\n\ used for special methods; variants without leading and trailing\n\ '__' are also provided for convenience."); /*[clinic input] _operator.truth -> bool a: object / Return True if a is true, False otherwise. [clinic start generated code]*/ static int _operator_truth_impl(PyObject *module, PyObject *a) /*[clinic end generated code: output=eaf87767234fa5d7 input=bc74a4cd90235875]*/ { … } /*[clinic input] _operator.add a: object b: object / Same as a + b. [clinic start generated code]*/ static PyObject * _operator_add_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=8292984204f45164 input=5efe3bff856ac215]*/ { … } /*[clinic input] _operator.sub = _operator.add Same as a - b. [clinic start generated code]*/ static PyObject * _operator_sub_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=4adfc3b888c1ee2e input=6494c6b100b8e795]*/ { … } /*[clinic input] _operator.mul = _operator.add Same as a * b. [clinic start generated code]*/ static PyObject * _operator_mul_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=d24d66f55a01944c input=2368615b4358b70d]*/ { … } /*[clinic input] _operator.matmul = _operator.add Same as a @ b. [clinic start generated code]*/ static PyObject * _operator_matmul_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=a20d917eb35d0101 input=9ab304e37fb42dd4]*/ { … } /*[clinic input] _operator.floordiv = _operator.add Same as a // b. [clinic start generated code]*/ static PyObject * _operator_floordiv_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=df26b71a60589f99 input=bb2e88ba446c612c]*/ { … } /*[clinic input] _operator.truediv = _operator.add Same as a / b. [clinic start generated code]*/ static PyObject * _operator_truediv_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=0e6a959944d77719 input=ecbb947673f4eb1f]*/ { … } /*[clinic input] _operator.mod = _operator.add Same as a % b. [clinic start generated code]*/ static PyObject * _operator_mod_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=9519822f0bbec166 input=102e19b422342ac1]*/ { … } /*[clinic input] _operator.neg a: object / Same as -a. [clinic start generated code]*/ static PyObject * _operator_neg(PyObject *module, PyObject *a) /*[clinic end generated code: output=36e08ecfc6a1c08c input=84f09bdcf27c96ec]*/ { … } /*[clinic input] _operator.pos = _operator.neg Same as +a. [clinic start generated code]*/ static PyObject * _operator_pos(PyObject *module, PyObject *a) /*[clinic end generated code: output=dad7a126221dd091 input=b6445b63fddb8772]*/ { … } /*[clinic input] _operator.abs = _operator.neg Same as abs(a). [clinic start generated code]*/ static PyObject * _operator_abs(PyObject *module, PyObject *a) /*[clinic end generated code: output=1389a93ba053ea3e input=341d07ba86f58039]*/ { … } /*[clinic input] _operator.inv = _operator.neg Same as ~a. [clinic start generated code]*/ static PyObject * _operator_inv(PyObject *module, PyObject *a) /*[clinic end generated code: output=a56875ba075ee06d input=b01a4677739f6eb2]*/ { … } /*[clinic input] _operator.invert = _operator.neg Same as ~a. [clinic start generated code]*/ static PyObject * _operator_invert(PyObject *module, PyObject *a) /*[clinic end generated code: output=406b5aa030545fcc input=7f2d607176672e55]*/ { … } /*[clinic input] _operator.lshift = _operator.add Same as a << b. [clinic start generated code]*/ static PyObject * _operator_lshift_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=37f7e52c41435bd8 input=746e8a160cbbc9eb]*/ { … } /*[clinic input] _operator.rshift = _operator.add Same as a >> b. [clinic start generated code]*/ static PyObject * _operator_rshift_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=4593c7ef30ec2ee3 input=d2c85bb5a64504c2]*/ { … } /*[clinic input] _operator.not_ = _operator.truth Same as not a. [clinic start generated code]*/ static int _operator_not__impl(PyObject *module, PyObject *a) /*[clinic end generated code: output=743f9c24a09759ef input=854156d50804d9b8]*/ { … } /*[clinic input] _operator.and_ = _operator.add Same as a & b. [clinic start generated code]*/ static PyObject * _operator_and__impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=93c4fe88f7b76d9e input=4f3057c90ec4c99f]*/ { … } /*[clinic input] _operator.xor = _operator.add Same as a ^ b. [clinic start generated code]*/ static PyObject * _operator_xor_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=b24cd8b79fde0004 input=3c5cfa7253d808dd]*/ { … } /*[clinic input] _operator.or_ = _operator.add Same as a | b. [clinic start generated code]*/ static PyObject * _operator_or__impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=58024867b8d90461 input=b40c6c44f7c79c09]*/ { … } /*[clinic input] _operator.iadd = _operator.add Same as a += b. [clinic start generated code]*/ static PyObject * _operator_iadd_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=07dc627832526eb5 input=d22a91c07ac69227]*/ { … } /*[clinic input] _operator.isub = _operator.add Same as a -= b. [clinic start generated code]*/ static PyObject * _operator_isub_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=4513467d23b5e0b1 input=4591b00d0a0ccafd]*/ { … } /*[clinic input] _operator.imul = _operator.add Same as a *= b. [clinic start generated code]*/ static PyObject * _operator_imul_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=5e87dacd19a71eab input=0e01fb8631e1b76f]*/ { … } /*[clinic input] _operator.imatmul = _operator.add Same as a @= b. [clinic start generated code]*/ static PyObject * _operator_imatmul_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=d603cbdf716ce519 input=bb614026372cd542]*/ { … } /*[clinic input] _operator.ifloordiv = _operator.add Same as a //= b. [clinic start generated code]*/ static PyObject * _operator_ifloordiv_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=535336048c681794 input=9df3b5021cff4ca1]*/ { … } /*[clinic input] _operator.itruediv = _operator.add Same as a /= b. [clinic start generated code]*/ static PyObject * _operator_itruediv_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=28017fbd3563952f input=9a1ee01608f5f590]*/ { … } /*[clinic input] _operator.imod = _operator.add Same as a %= b. [clinic start generated code]*/ static PyObject * _operator_imod_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=f7c540ae0fc70904 input=d0c384a3ce38e1dd]*/ { … } /*[clinic input] _operator.ilshift = _operator.add Same as a <<= b. [clinic start generated code]*/ static PyObject * _operator_ilshift_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=e73a8fee1ac18749 input=e21b6b310f54572e]*/ { … } /*[clinic input] _operator.irshift = _operator.add Same as a >>= b. [clinic start generated code]*/ static PyObject * _operator_irshift_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=97f2af6b5ff2ed81 input=6778dbd0f6e1ec16]*/ { … } /*[clinic input] _operator.iand = _operator.add Same as a &= b. [clinic start generated code]*/ static PyObject * _operator_iand_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=4599e9d40cbf7d00 input=71dfd8e70c156a7b]*/ { … } /*[clinic input] _operator.ixor = _operator.add Same as a ^= b. [clinic start generated code]*/ static PyObject * _operator_ixor_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=5ff881766872be03 input=695c32bec0604d86]*/ { … } /*[clinic input] _operator.ior = _operator.add Same as a |= b. [clinic start generated code]*/ static PyObject * _operator_ior_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=48aac319445bf759 input=8f01d03eda9920cf]*/ { … } /*[clinic input] _operator.concat = _operator.add Same as a + b, for a and b sequences. [clinic start generated code]*/ static PyObject * _operator_concat_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=80028390942c5f11 input=8544ccd5341a3658]*/ { … } /*[clinic input] _operator.iconcat = _operator.add Same as a += b, for a and b sequences. [clinic start generated code]*/ static PyObject * _operator_iconcat_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=3ea0a162ebb2e26d input=8f5fe5722fcd837e]*/ { … } /*[clinic input] _operator.contains -> bool a: object b: object / Same as b in a (note reversed operands). [clinic start generated code]*/ static int _operator_contains_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=413b4dbe82b6ffc1 input=9122a69b505fde13]*/ { … } /*[clinic input] _operator.indexOf -> Py_ssize_t a: object b: object / Return the first index of b in a. [clinic start generated code]*/ static Py_ssize_t _operator_indexOf_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=c6226d8e0fb60fa6 input=8be2e43b6a6fffe3]*/ { … } /*[clinic input] _operator.countOf = _operator.indexOf Return the number of items in a which are, or which equal, b. [clinic start generated code]*/ static Py_ssize_t _operator_countOf_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=9e1623197daf3382 input=93ea57f170f3f0bb]*/ { … } /*[clinic input] _operator.getitem a: object b: object / Same as a[b]. [clinic start generated code]*/ static PyObject * _operator_getitem_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=6c8d8101a676e594 input=6682797320e48845]*/ { … } /*[clinic input] _operator.setitem a: object b: object c: object / Same as a[b] = c. [clinic start generated code]*/ static PyObject * _operator_setitem_impl(PyObject *module, PyObject *a, PyObject *b, PyObject *c) /*[clinic end generated code: output=1324f9061ae99e25 input=ceaf453c4d3a58df]*/ { … } /*[clinic input] _operator.delitem = _operator.getitem Same as del a[b]. [clinic start generated code]*/ static PyObject * _operator_delitem_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=db18f61506295799 input=991bec56a0d3ec7f]*/ { … } /*[clinic input] _operator.eq a: object b: object / Same as a == b. [clinic start generated code]*/ static PyObject * _operator_eq_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=8d7d46ed4135677c input=586fca687a95a83f]*/ { … } /*[clinic input] _operator.ne = _operator.eq Same as a != b. [clinic start generated code]*/ static PyObject * _operator_ne_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=c99bd0c3a4c01297 input=5d88f23d35e9abac]*/ { … } /*[clinic input] _operator.lt = _operator.eq Same as a < b. [clinic start generated code]*/ static PyObject * _operator_lt_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=082d7c45c440e535 input=34a59ad6d39d3a2b]*/ { … } /*[clinic input] _operator.le = _operator.eq Same as a <= b. [clinic start generated code]*/ static PyObject * _operator_le_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=00970a2923d0ae17 input=b812a7860a0bef44]*/ { … } /*[clinic input] _operator.gt = _operator.eq Same as a > b. [clinic start generated code]*/ static PyObject * _operator_gt_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=8d373349ecf25641 input=9bdb45b995ada35b]*/ { … } /*[clinic input] _operator.ge = _operator.eq Same as a >= b. [clinic start generated code]*/ static PyObject * _operator_ge_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=7ce3882256d4b137 input=cf1dc4a5ca9c35f5]*/ { … } /*[clinic input] _operator.pow = _operator.add Same as a ** b. [clinic start generated code]*/ static PyObject * _operator_pow_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=09e668ad50036120 input=690b40f097ab1637]*/ { … } /*[clinic input] _operator.ipow = _operator.add Same as a **= b. [clinic start generated code]*/ static PyObject * _operator_ipow_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=7189ff4d4367c808 input=f00623899d07499a]*/ { … } /*[clinic input] _operator.index a: object / Same as a.__index__() [clinic start generated code]*/ static PyObject * _operator_index(PyObject *module, PyObject *a) /*[clinic end generated code: output=d972b0764ac305fc input=6f54d50ea64a579c]*/ { … } /*[clinic input] _operator.is_ = _operator.add Same as a is b. [clinic start generated code]*/ static PyObject * _operator_is__impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=bcd47a402e482e1d input=5fa9b97df03c427f]*/ { … } /*[clinic input] _operator.is_not = _operator.add Same as a is not b. [clinic start generated code]*/ static PyObject * _operator_is_not_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=491a1f2f81f6c7f9 input=5a93f7e1a93535f1]*/ { … } /*[clinic input] _operator.is_none = _operator.neg Same as a is None. [clinic start generated code]*/ static PyObject * _operator_is_none(PyObject *module, PyObject *a) /*[clinic end generated code: output=07159cc102261dec input=0448b38af7b8533d]*/ { … } /*[clinic input] _operator.is_not_none = _operator.neg Same as a is not None. [clinic start generated code]*/ static PyObject * _operator_is_not_none(PyObject *module, PyObject *a) /*[clinic end generated code: output=b0168a51451d9140 input=7587f38ebac51688]*/ { … } /* compare_digest **********************************************************/ /* * timing safe compare * * Returns 1 if the strings are equal. * In case of len(a) != len(b) the function tries to keep the timing * dependent on the length of b. CPU cache locality may still alter timing * a bit. */ static int _tscmp(const unsigned char *a, const unsigned char *b, Py_ssize_t len_a, Py_ssize_t len_b) { … } /*[clinic input] _operator.length_hint -> Py_ssize_t obj: object default: Py_ssize_t = 0 / Return an estimate of the number of items in obj. This is useful for presizing containers when building from an iterable. If the object supports len(), the result will be exact. Otherwise, it may over- or under-estimate by an arbitrary amount. The result will be an integer >= 0. [clinic start generated code]*/ static Py_ssize_t _operator_length_hint_impl(PyObject *module, PyObject *obj, Py_ssize_t default_value) /*[clinic end generated code: output=01d469edc1d612ad input=65ed29f04401e96a]*/ { … } /* NOTE: Keep in sync with _hashopenssl.c implementation. */ /*[clinic input] _operator._compare_digest = _operator.eq Return 'a == b'. This function uses an approach designed to prevent timing analysis, making it appropriate for cryptography. a and b must both be of the same type: either str (ASCII only), or any bytes-like object. Note: If a and b are of different lengths, or if an error occurs, a timing attack could theoretically reveal information about the types and lengths of a and b--but not their values. [clinic start generated code]*/ static PyObject * _operator__compare_digest_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=11d452bdd3a23cbc input=9ac7e2c4e30bc356]*/ { … } PyDoc_STRVAR(_operator_call__doc__, "call($module, obj, /, *args, **kwargs)\n" "--\n" "\n" "Same as obj(*args, **kwargs)."); #define _OPERATOR_CALL_METHODDEF … static PyObject * _operator_call(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) { … } /* operator methods **********************************************************/ static struct PyMethodDef operator_methods[] = …; static PyObject * text_signature(PyObject *self, void *Py_UNUSED(ignored)) { … } static PyGetSetDef common_getset[] = …; /* itemgetter object **********************************************************/ itemgetterobject; // Forward declarations static PyObject * itemgetter_vectorcall(PyObject *, PyObject *const *, size_t, PyObject *); static PyObject * itemgetter_call_impl(itemgetterobject *, PyObject *); /* AC 3.5: treats first argument as an iterable, otherwise uses *args */ static PyObject * itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { … } static int itemgetter_clear(itemgetterobject *ig) { … } static void itemgetter_dealloc(itemgetterobject *ig) { … } static int itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg) { … } static PyObject * itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw) { … } static PyObject * itemgetter_vectorcall(PyObject *ig, PyObject *const *args, size_t nargsf, PyObject *kwnames) { … } static PyObject * itemgetter_call_impl(itemgetterobject *ig, PyObject *obj) { … } static PyObject * itemgetter_repr(itemgetterobject *ig) { … } static PyObject * itemgetter_reduce(itemgetterobject *ig, PyObject *Py_UNUSED(ignored)) { … } PyDoc_STRVAR(reduce_doc, "Return state information for pickling"); static PyMethodDef itemgetter_methods[] = …; static PyMemberDef itemgetter_members[] = …; PyDoc_STRVAR(itemgetter_doc, "itemgetter(item, /, *items)\n--\n\n\ Return a callable object that fetches the given item(s) from its operand.\n\ After f = itemgetter(2), the call f(r) returns r[2].\n\ After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])"); static PyType_Slot itemgetter_type_slots[] = …; static PyType_Spec itemgetter_type_spec = …; /* attrgetter object **********************************************************/ attrgetterobject; // Forward declarations static PyObject * attrgetter_vectorcall(PyObject *, PyObject *const *, size_t, PyObject *); static PyObject * attrgetter_call_impl(attrgetterobject *, PyObject *); /* AC 3.5: treats first argument as an iterable, otherwise uses *args */ static PyObject * attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { … } static int attrgetter_clear(attrgetterobject *ag) { … } static void attrgetter_dealloc(attrgetterobject *ag) { … } static int attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg) { … } static PyObject * dotted_getattr(PyObject *obj, PyObject *attr) { … } static PyObject * attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw) { … } static PyObject * attrgetter_vectorcall(PyObject *ag, PyObject *const *args, size_t nargsf, PyObject *kwnames) { … } static PyObject * attrgetter_call_impl(attrgetterobject *ag, PyObject *obj) { … } static PyObject * dotjoinattr(PyObject *attr, PyObject **attrsep) { … } static PyObject * attrgetter_args(attrgetterobject *ag) { … } static PyObject * attrgetter_repr(attrgetterobject *ag) { … } static PyObject * attrgetter_reduce(attrgetterobject *ag, PyObject *Py_UNUSED(ignored)) { … } static PyMethodDef attrgetter_methods[] = …; static PyMemberDef attrgetter_members[] = …; PyDoc_STRVAR(attrgetter_doc, "attrgetter(attr, /, *attrs)\n--\n\n\ Return a callable object that fetches the given attribute(s) from its operand.\n\ After f = attrgetter('name'), the call f(r) returns r.name.\n\ After g = attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\ After h = attrgetter('name.first', 'name.last'), the call h(r) returns\n\ (r.name.first, r.name.last)."); static PyType_Slot attrgetter_type_slots[] = …; static PyType_Spec attrgetter_type_spec = …; /* methodcaller object **********************************************************/ methodcallerobject; #ifndef Py_GIL_DISABLED static int _methodcaller_initialize_vectorcall(methodcallerobject* mc) { … } static PyObject * methodcaller_vectorcall( methodcallerobject *mc, PyObject *const *args, size_t nargsf, PyObject* kwnames) { … } #endif /* AC 3.5: variable number of arguments, not currently support by AC */ static PyObject * methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { … } static void methodcaller_clear(methodcallerobject *mc) { … } static void methodcaller_dealloc(methodcallerobject *mc) { … } static int methodcaller_traverse(methodcallerobject *mc, visitproc visit, void *arg) { … } static PyObject * methodcaller_call(methodcallerobject *mc, PyObject *args, PyObject *kw) { … } static PyObject * methodcaller_repr(methodcallerobject *mc) { … } static PyObject * methodcaller_reduce(methodcallerobject *mc, PyObject *Py_UNUSED(ignored)) { … } static PyMethodDef methodcaller_methods[] = …; static PyMemberDef methodcaller_members[] = …; PyDoc_STRVAR(methodcaller_doc, "methodcaller(name, /, *args, **kwargs)\n--\n\n\ Return a callable object that calls the given method on its operand.\n\ After f = methodcaller('name'), the call f(r) returns r.name().\n\ After g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\ r.name('date', foo=1)."); static PyType_Slot methodcaller_type_slots[] = …; static PyType_Spec methodcaller_type_spec = …; static int operator_exec(PyObject *module) { … } static struct PyModuleDef_Slot operator_slots[] = …; static int operator_traverse(PyObject *module, visitproc visit, void *arg) { … } static int operator_clear(PyObject *module) { … } static void operator_free(void *module) { … } static struct PyModuleDef operatormodule = …; PyMODINIT_FUNC PyInit__operator(void) { … }