+++ /dev/null
-\r
-#include "Python.h"\r
-\r
-PyDoc_STRVAR(operator_doc,\r
-"Operator interface.\n\\r
-\n\\r
-This module exports a set of functions implemented in C corresponding\n\\r
-to the intrinsic operators of Python. For example, operator.add(x, y)\n\\r
-is equivalent to the expression x+y. The function names are those\n\\r
-used for special methods; variants without leading and trailing\n\\r
-'__' are also provided for convenience.");\r
-\r
-#define spam1(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a1) { \\r
- return AOP(a1); }\r
-\r
-#define spam2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \\r
- PyObject *a1, *a2; \\r
- if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \\r
- return AOP(a1,a2); }\r
-\r
-#define spamoi(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \\r
- PyObject *a1; int a2; \\r
- if(! PyArg_ParseTuple(a,"Oi:" #OP,&a1,&a2)) return NULL; \\r
- return AOP(a1,a2); }\r
-\r
-#define spam2n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \\r
- PyObject *a1, *a2; \\r
- if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \\r
- if(-1 == AOP(a1,a2)) return NULL; \\r
- Py_INCREF(Py_None); \\r
- return Py_None; }\r
-\r
-#define spam3n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \\r
- PyObject *a1, *a2, *a3; \\r
- if(! PyArg_UnpackTuple(a,#OP,3,3,&a1,&a2,&a3)) return NULL; \\r
- if(-1 == AOP(a1,a2,a3)) return NULL; \\r
- Py_INCREF(Py_None); \\r
- return Py_None; }\r
-\r
-#define spami(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a1) { \\r
- long r; \\r
- if(-1 == (r=AOP(a1))) return NULL; \\r
- return PyBool_FromLong(r); }\r
-\r
-#define spami2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \\r
- PyObject *a1, *a2; long r; \\r
- if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \\r
- if(-1 == (r=AOP(a1,a2))) return NULL; \\r
- return PyInt_FromLong(r); }\r
-\r
-#define spamn2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \\r
- PyObject *a1, *a2; Py_ssize_t r; \\r
- if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \\r
- if(-1 == (r=AOP(a1,a2))) return NULL; \\r
- return PyInt_FromSsize_t(r); }\r
-\r
-#define spami2b(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \\r
- PyObject *a1, *a2; long r; \\r
- if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \\r
- if(-1 == (r=AOP(a1,a2))) return NULL; \\r
- return PyBool_FromLong(r); }\r
-\r
-#define spamrc(OP,A) static PyObject *OP(PyObject *s, PyObject *a) { \\r
- PyObject *a1, *a2; \\r
- if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \\r
- return PyObject_RichCompare(a1,a2,A); }\r
-\r
-/* Deprecated operators that need warnings. */\r
-static int\r
-op_isCallable(PyObject *x)\r
-{\r
- if (PyErr_WarnPy3k("operator.isCallable() is not supported in 3.x. "\r
- "Use hasattr(obj, '__call__').", 1) < 0)\r
- return -1;\r
- return PyCallable_Check(x);\r
-}\r
-\r
-static int\r
-op_sequenceIncludes(PyObject *seq, PyObject* ob)\r
-{\r
- if (PyErr_WarnPy3k("operator.sequenceIncludes() is not supported "\r
- "in 3.x. Use operator.contains().", 1) < 0)\r
- return -1;\r
- return PySequence_Contains(seq, ob);\r
-}\r
-\r
-spami(isCallable , op_isCallable)\r
-spami(isNumberType , PyNumber_Check)\r
-spami(truth , PyObject_IsTrue)\r
-spam2(op_add , PyNumber_Add)\r
-spam2(op_sub , PyNumber_Subtract)\r
-spam2(op_mul , PyNumber_Multiply)\r
-spam2(op_div , PyNumber_Divide)\r
-spam2(op_floordiv , PyNumber_FloorDivide)\r
-spam2(op_truediv , PyNumber_TrueDivide)\r
-spam2(op_mod , PyNumber_Remainder)\r
-spam1(op_neg , PyNumber_Negative)\r
-spam1(op_pos , PyNumber_Positive)\r
-spam1(op_abs , PyNumber_Absolute)\r
-spam1(op_inv , PyNumber_Invert)\r
-spam1(op_invert , PyNumber_Invert)\r
-spam2(op_lshift , PyNumber_Lshift)\r
-spam2(op_rshift , PyNumber_Rshift)\r
-spami(op_not_ , PyObject_Not)\r
-spam2(op_and_ , PyNumber_And)\r
-spam2(op_xor , PyNumber_Xor)\r
-spam2(op_or_ , PyNumber_Or)\r
-spam2(op_iadd , PyNumber_InPlaceAdd)\r
-spam2(op_isub , PyNumber_InPlaceSubtract)\r
-spam2(op_imul , PyNumber_InPlaceMultiply)\r
-spam2(op_idiv , PyNumber_InPlaceDivide)\r
-spam2(op_ifloordiv , PyNumber_InPlaceFloorDivide)\r
-spam2(op_itruediv , PyNumber_InPlaceTrueDivide)\r
-spam2(op_imod , PyNumber_InPlaceRemainder)\r
-spam2(op_ilshift , PyNumber_InPlaceLshift)\r
-spam2(op_irshift , PyNumber_InPlaceRshift)\r
-spam2(op_iand , PyNumber_InPlaceAnd)\r
-spam2(op_ixor , PyNumber_InPlaceXor)\r
-spam2(op_ior , PyNumber_InPlaceOr)\r
-spami(isSequenceType , PySequence_Check)\r
-spam2(op_concat , PySequence_Concat)\r
-spamoi(op_repeat , PySequence_Repeat)\r
-spam2(op_iconcat , PySequence_InPlaceConcat)\r
-spamoi(op_irepeat , PySequence_InPlaceRepeat)\r
-spami2b(op_contains , PySequence_Contains)\r
-spami2b(sequenceIncludes, op_sequenceIncludes)\r
-spamn2(indexOf , PySequence_Index)\r
-spamn2(countOf , PySequence_Count)\r
-spami(isMappingType , PyMapping_Check)\r
-spam2(op_getitem , PyObject_GetItem)\r
-spam2n(op_delitem , PyObject_DelItem)\r
-spam3n(op_setitem , PyObject_SetItem)\r
-spamrc(op_lt , Py_LT)\r
-spamrc(op_le , Py_LE)\r
-spamrc(op_eq , Py_EQ)\r
-spamrc(op_ne , Py_NE)\r
-spamrc(op_gt , Py_GT)\r
-spamrc(op_ge , Py_GE)\r
-\r
-static PyObject*\r
-op_pow(PyObject *s, PyObject *a)\r
-{\r
- PyObject *a1, *a2;\r
- if (PyArg_UnpackTuple(a,"pow", 2, 2, &a1, &a2))\r
- return PyNumber_Power(a1, a2, Py_None);\r
- return NULL;\r
-}\r
-\r
-static PyObject*\r
-op_ipow(PyObject *s, PyObject *a)\r
-{\r
- PyObject *a1, *a2;\r
- if (PyArg_UnpackTuple(a,"ipow", 2, 2, &a1, &a2))\r
- return PyNumber_InPlacePower(a1, a2, Py_None);\r
- return NULL;\r
-}\r
-\r
-static PyObject *\r
-op_index(PyObject *s, PyObject *a)\r
-{\r
- return PyNumber_Index(a);\r
-}\r
-\r
-static PyObject*\r
-is_(PyObject *s, PyObject *a)\r
-{\r
- PyObject *a1, *a2, *result = NULL;\r
- if (PyArg_UnpackTuple(a,"is_", 2, 2, &a1, &a2)) {\r
- result = (a1 == a2) ? Py_True : Py_False;\r
- Py_INCREF(result);\r
- }\r
- return result;\r
-}\r
-\r
-static PyObject*\r
-is_not(PyObject *s, PyObject *a)\r
-{\r
- PyObject *a1, *a2, *result = NULL;\r
- if (PyArg_UnpackTuple(a,"is_not", 2, 2, &a1, &a2)) {\r
- result = (a1 != a2) ? Py_True : Py_False;\r
- Py_INCREF(result);\r
- }\r
- return result;\r
-}\r
-\r
-static PyObject*\r
-op_getslice(PyObject *s, PyObject *a)\r
-{\r
- PyObject *a1;\r
- Py_ssize_t a2, a3;\r
-\r
- if (!PyArg_ParseTuple(a, "Onn:getslice", &a1, &a2, &a3))\r
- return NULL;\r
- return PySequence_GetSlice(a1, a2, a3);\r
-}\r
-\r
-static PyObject*\r
-op_setslice(PyObject *s, PyObject *a)\r
-{\r
- PyObject *a1, *a4;\r
- Py_ssize_t a2, a3;\r
-\r
- if (!PyArg_ParseTuple(a, "OnnO:setslice", &a1, &a2, &a3, &a4))\r
- return NULL;\r
-\r
- if (-1 == PySequence_SetSlice(a1, a2, a3, a4))\r
- return NULL;\r
-\r
- Py_RETURN_NONE;\r
-}\r
-\r
-static PyObject*\r
-op_delslice(PyObject *s, PyObject *a)\r
-{\r
- PyObject *a1;\r
- Py_ssize_t a2, a3;\r
-\r
- if (!PyArg_ParseTuple(a, "Onn:delslice", &a1, &a2, &a3))\r
- return NULL;\r
-\r
- if (-1 == PySequence_DelSlice(a1, a2, a3))\r
- return NULL;\r
-\r
- Py_RETURN_NONE;\r
-}\r
-\r
-#undef spam1\r
-#undef spam2\r
-#undef spam1o\r
-#undef spam1o\r
-#define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)},\r
-#define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, \\r
- {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},\r
-#define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)},\r
-#define spam2o(OP,ALTOP,DOC) {#OP, op_##OP, METH_O, PyDoc_STR(DOC)}, \\r
- {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)},\r
-\r
-\r
-\r
-/* compare_digest **********************************************************/\r
-\r
-/*\r
- * timing safe compare\r
- *\r
- * Returns 1 of the strings are equal.\r
- * In case of len(a) != len(b) the function tries to keep the timing\r
- * dependent on the length of b. CPU cache locally may still alter timing\r
- * a bit.\r
- */\r
-static int\r
-_tscmp(const unsigned char *a, const unsigned char *b,\r
- Py_ssize_t len_a, Py_ssize_t len_b)\r
-{\r
- /* The volatile type declarations make sure that the compiler has no\r
- * chance to optimize and fold the code in any way that may change\r
- * the timing.\r
- */\r
- volatile Py_ssize_t length;\r
- volatile const unsigned char *left;\r
- volatile const unsigned char *right;\r
- Py_ssize_t i;\r
- unsigned char result;\r
-\r
- /* loop count depends on length of b */\r
- length = len_b;\r
- left = NULL;\r
- right = b;\r
-\r
- /* don't use else here to keep the amount of CPU instructions constant,\r
- * volatile forces re-evaluation\r
- * */\r
- if (len_a == length) {\r
- left = *((volatile const unsigned char**)&a);\r
- result = 0;\r
- }\r
- if (len_a != length) {\r
- left = b;\r
- result = 1;\r
- }\r
-\r
- for (i=0; i < length; i++) {\r
- result |= *left++ ^ *right++;\r
- }\r
-\r
- return (result == 0);\r
-}\r
-\r
-PyDoc_STRVAR(compare_digest__doc__,\r
-"compare_digest(a, b) -> bool\n"\r
-"\n"\r
-"Return 'a == b'. This function uses an approach designed to prevent\n"\r
-"timing analysis, making it appropriate for cryptography.\n"\r
-"a and b must both be of the same type: either str (ASCII only),\n"\r
-"or any type that supports the buffer protocol (e.g. bytes).\n"\r
-"\n"\r
-"Note: If a and b are of different lengths, or if an error occurs,\n"\r
-"a timing attack could theoretically reveal information about the\n"\r
-"types and lengths of a and b--but not their values.\n");\r
-\r
-static PyObject*\r
-compare_digest(PyObject *self, PyObject *args)\r
-{\r
- PyObject *a, *b;\r
- int rc;\r
-\r
- if (!PyArg_ParseTuple(args, "OO:compare_digest", &a, &b)) {\r
- return NULL;\r
- }\r
-\r
- /* Unicode string */\r
- if (PyUnicode_Check(a) && PyUnicode_Check(b)) {\r
- rc = _tscmp((const unsigned char *)PyUnicode_AS_DATA(a),\r
- (const unsigned char *)PyUnicode_AS_DATA(b),\r
- PyUnicode_GET_DATA_SIZE(a),\r
- PyUnicode_GET_DATA_SIZE(b));\r
- }\r
- /* fallback to buffer interface for bytes, bytesarray and other */\r
- else {\r
- Py_buffer view_a;\r
- Py_buffer view_b;\r
-\r
- if (PyObject_CheckBuffer(a) == 0 && PyObject_CheckBuffer(b) == 0) {\r
- PyErr_Format(PyExc_TypeError,\r
- "unsupported operand types(s) or combination of types: "\r
- "'%.100s' and '%.100s'",\r
- Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);\r
- return NULL;\r
- }\r
-\r
- if (PyObject_GetBuffer(a, &view_a, PyBUF_SIMPLE) == -1) {\r
- return NULL;\r
- }\r
- if (view_a.ndim > 1) {\r
- PyErr_SetString(PyExc_BufferError,\r
- "Buffer must be single dimension");\r
- PyBuffer_Release(&view_a);\r
- return NULL;\r
- }\r
-\r
- if (PyObject_GetBuffer(b, &view_b, PyBUF_SIMPLE) == -1) {\r
- PyBuffer_Release(&view_a);\r
- return NULL;\r
- }\r
- if (view_b.ndim > 1) {\r
- PyErr_SetString(PyExc_BufferError,\r
- "Buffer must be single dimension");\r
- PyBuffer_Release(&view_a);\r
- PyBuffer_Release(&view_b);\r
- return NULL;\r
- }\r
-\r
- rc = _tscmp((const unsigned char*)view_a.buf,\r
- (const unsigned char*)view_b.buf,\r
- view_a.len,\r
- view_b.len);\r
-\r
- PyBuffer_Release(&view_a);\r
- PyBuffer_Release(&view_b);\r
- }\r
-\r
- return PyBool_FromLong(rc);\r
-}\r
-\r
-static struct PyMethodDef operator_methods[] = {\r
-\r
-spam1o(isCallable,\r
- "isCallable(a) -- Same as callable(a).")\r
-spam1o(isNumberType,\r
- "isNumberType(a) -- Return True if a has a numeric type, False otherwise.")\r
-spam1o(isSequenceType,\r
- "isSequenceType(a) -- Return True if a has a sequence type, False otherwise.")\r
-spam1o(truth,\r
- "truth(a) -- Return True if a is true, False otherwise.")\r
-spam2(contains,__contains__,\r
- "contains(a, b) -- Same as b in a (note reversed operands).")\r
-spam1(sequenceIncludes,\r
- "sequenceIncludes(a, b) -- Same as b in a (note reversed operands; deprecated).")\r
-spam1(indexOf,\r
- "indexOf(a, b) -- Return the first index of b in a.")\r
-spam1(countOf,\r
- "countOf(a, b) -- Return the number of times b occurs in a.")\r
-spam1o(isMappingType,\r
- "isMappingType(a) -- Return True if a has a mapping type, False otherwise.")\r
-\r
-spam1(is_, "is_(a, b) -- Same as a is b.")\r
-spam1(is_not, "is_not(a, b) -- Same as a is not b.")\r
-spam2o(index, __index__, "index(a) -- Same as a.__index__()")\r
-spam2(add,__add__, "add(a, b) -- Same as a + b.")\r
-spam2(sub,__sub__, "sub(a, b) -- Same as a - b.")\r
-spam2(mul,__mul__, "mul(a, b) -- Same as a * b.")\r
-spam2(div,__div__, "div(a, b) -- Same as a / b when __future__.division is not in effect.")\r
-spam2(floordiv,__floordiv__, "floordiv(a, b) -- Same as a // b.")\r
-spam2(truediv,__truediv__, "truediv(a, b) -- Same as a / b when __future__.division is in effect.")\r
-spam2(mod,__mod__, "mod(a, b) -- Same as a % b.")\r
-spam2o(neg,__neg__, "neg(a) -- Same as -a.")\r
-spam2o(pos,__pos__, "pos(a) -- Same as +a.")\r
-spam2o(abs,__abs__, "abs(a) -- Same as abs(a).")\r
-spam2o(inv,__inv__, "inv(a) -- Same as ~a.")\r
-spam2o(invert,__invert__, "invert(a) -- Same as ~a.")\r
-spam2(lshift,__lshift__, "lshift(a, b) -- Same as a << b.")\r
-spam2(rshift,__rshift__, "rshift(a, b) -- Same as a >> b.")\r
-spam2o(not_,__not__, "not_(a) -- Same as not a.")\r
-spam2(and_,__and__, "and_(a, b) -- Same as a & b.")\r
-spam2(xor,__xor__, "xor(a, b) -- Same as a ^ b.")\r
-spam2(or_,__or__, "or_(a, b) -- Same as a | b.")\r
-spam2(iadd,__iadd__, "a = iadd(a, b) -- Same as a += b.")\r
-spam2(isub,__isub__, "a = isub(a, b) -- Same as a -= b.")\r
-spam2(imul,__imul__, "a = imul(a, b) -- Same as a *= b.")\r
-spam2(idiv,__idiv__, "a = idiv(a, b) -- Same as a /= b when __future__.division is not in effect.")\r
-spam2(ifloordiv,__ifloordiv__, "a = ifloordiv(a, b) -- Same as a //= b.")\r
-spam2(itruediv,__itruediv__, "a = itruediv(a, b) -- Same as a /= b when __future__.division is in effect.")\r
-spam2(imod,__imod__, "a = imod(a, b) -- Same as a %= b.")\r
-spam2(ilshift,__ilshift__, "a = ilshift(a, b) -- Same as a <<= b.")\r
-spam2(irshift,__irshift__, "a = irshift(a, b) -- Same as a >>= b.")\r
-spam2(iand,__iand__, "a = iand(a, b) -- Same as a &= b.")\r
-spam2(ixor,__ixor__, "a = ixor(a, b) -- Same as a ^= b.")\r
-spam2(ior,__ior__, "a = ior(a, b) -- Same as a |= b.")\r
-spam2(concat,__concat__,\r
- "concat(a, b) -- Same as a + b, for a and b sequences.")\r
-spam2(repeat,__repeat__,\r
- "repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.")\r
-spam2(iconcat,__iconcat__,\r
- "a = iconcat(a, b) -- Same as a += b, for a and b sequences.")\r
-spam2(irepeat,__irepeat__,\r
- "a = irepeat(a, b) -- Same as a *= b, where a is a sequence, and b is an integer.")\r
-spam2(getitem,__getitem__,\r
- "getitem(a, b) -- Same as a[b].")\r
-spam2(setitem,__setitem__,\r
- "setitem(a, b, c) -- Same as a[b] = c.")\r
-spam2(delitem,__delitem__,\r
- "delitem(a, b) -- Same as del a[b].")\r
-spam2(pow,__pow__, "pow(a, b) -- Same as a ** b.")\r
-spam2(ipow,__ipow__, "a = ipow(a, b) -- Same as a **= b.")\r
-spam2(getslice,__getslice__,\r
- "getslice(a, b, c) -- Same as a[b:c].")\r
-spam2(setslice,__setslice__,\r
-"setslice(a, b, c, d) -- Same as a[b:c] = d.")\r
-spam2(delslice,__delslice__,\r
-"delslice(a, b, c) -- Same as del a[b:c].")\r
-spam2(lt,__lt__, "lt(a, b) -- Same as a<b.")\r
-spam2(le,__le__, "le(a, b) -- Same as a<=b.")\r
-spam2(eq,__eq__, "eq(a, b) -- Same as a==b.")\r
-spam2(ne,__ne__, "ne(a, b) -- Same as a!=b.")\r
-spam2(gt,__gt__, "gt(a, b) -- Same as a>b.")\r
-spam2(ge,__ge__, "ge(a, b) -- Same as a>=b.")\r
-\r
- {"_compare_digest", (PyCFunction)compare_digest, METH_VARARGS,\r
- compare_digest__doc__},\r
- {NULL, NULL} /* sentinel */\r
-\r
-};\r
-\r
-/* itemgetter object **********************************************************/\r
-\r
-typedef struct {\r
- PyObject_HEAD\r
- Py_ssize_t nitems;\r
- PyObject *item;\r
-} itemgetterobject;\r
-\r
-static PyTypeObject itemgetter_type;\r
-\r
-static PyObject *\r
-itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)\r
-{\r
- itemgetterobject *ig;\r
- PyObject *item;\r
- Py_ssize_t nitems;\r
-\r
- if (!_PyArg_NoKeywords("itemgetter()", kwds))\r
- return NULL;\r
-\r
- nitems = PyTuple_GET_SIZE(args);\r
- if (nitems <= 1) {\r
- if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))\r
- return NULL;\r
- } else\r
- item = args;\r
-\r
- /* create itemgetterobject structure */\r
- ig = PyObject_GC_New(itemgetterobject, &itemgetter_type);\r
- if (ig == NULL)\r
- return NULL;\r
-\r
- Py_INCREF(item);\r
- ig->item = item;\r
- ig->nitems = nitems;\r
-\r
- PyObject_GC_Track(ig);\r
- return (PyObject *)ig;\r
-}\r
-\r
-static void\r
-itemgetter_dealloc(itemgetterobject *ig)\r
-{\r
- PyObject_GC_UnTrack(ig);\r
- Py_XDECREF(ig->item);\r
- PyObject_GC_Del(ig);\r
-}\r
-\r
-static int\r
-itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg)\r
-{\r
- Py_VISIT(ig->item);\r
- return 0;\r
-}\r
-\r
-static PyObject *\r
-itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw)\r
-{\r
- PyObject *obj, *result;\r
- Py_ssize_t i, nitems=ig->nitems;\r
-\r
- if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj))\r
- return NULL;\r
- if (nitems == 1)\r
- return PyObject_GetItem(obj, ig->item);\r
-\r
- assert(PyTuple_Check(ig->item));\r
- assert(PyTuple_GET_SIZE(ig->item) == nitems);\r
-\r
- result = PyTuple_New(nitems);\r
- if (result == NULL)\r
- return NULL;\r
-\r
- for (i=0 ; i < nitems ; i++) {\r
- PyObject *item, *val;\r
- item = PyTuple_GET_ITEM(ig->item, i);\r
- val = PyObject_GetItem(obj, item);\r
- if (val == NULL) {\r
- Py_DECREF(result);\r
- return NULL;\r
- }\r
- PyTuple_SET_ITEM(result, i, val);\r
- }\r
- return result;\r
-}\r
-\r
-PyDoc_STRVAR(itemgetter_doc,\r
-"itemgetter(item, ...) --> itemgetter object\n\\r
-\n\\r
-Return a callable object that fetches the given item(s) from its operand.\n\\r
-After f = itemgetter(2), the call f(r) returns r[2].\n\\r
-After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])");\r
-\r
-static PyTypeObject itemgetter_type = {\r
- PyVarObject_HEAD_INIT(NULL, 0)\r
- "operator.itemgetter", /* tp_name */\r
- sizeof(itemgetterobject), /* tp_basicsize */\r
- 0, /* tp_itemsize */\r
- /* methods */\r
- (destructor)itemgetter_dealloc, /* tp_dealloc */\r
- 0, /* tp_print */\r
- 0, /* tp_getattr */\r
- 0, /* tp_setattr */\r
- 0, /* tp_compare */\r
- 0, /* tp_repr */\r
- 0, /* tp_as_number */\r
- 0, /* tp_as_sequence */\r
- 0, /* tp_as_mapping */\r
- 0, /* tp_hash */\r
- (ternaryfunc)itemgetter_call, /* tp_call */\r
- 0, /* tp_str */\r
- PyObject_GenericGetAttr, /* tp_getattro */\r
- 0, /* tp_setattro */\r
- 0, /* tp_as_buffer */\r
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */\r
- itemgetter_doc, /* tp_doc */\r
- (traverseproc)itemgetter_traverse, /* tp_traverse */\r
- 0, /* tp_clear */\r
- 0, /* tp_richcompare */\r
- 0, /* tp_weaklistoffset */\r
- 0, /* tp_iter */\r
- 0, /* tp_iternext */\r
- 0, /* tp_methods */\r
- 0, /* tp_members */\r
- 0, /* tp_getset */\r
- 0, /* tp_base */\r
- 0, /* tp_dict */\r
- 0, /* tp_descr_get */\r
- 0, /* tp_descr_set */\r
- 0, /* tp_dictoffset */\r
- 0, /* tp_init */\r
- 0, /* tp_alloc */\r
- itemgetter_new, /* tp_new */\r
- 0, /* tp_free */\r
-};\r
-\r
-\r
-/* attrgetter object **********************************************************/\r
-\r
-typedef struct {\r
- PyObject_HEAD\r
- Py_ssize_t nattrs;\r
- PyObject *attr;\r
-} attrgetterobject;\r
-\r
-static PyTypeObject attrgetter_type;\r
-\r
-static PyObject *\r
-attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)\r
-{\r
- attrgetterobject *ag;\r
- PyObject *attr;\r
- Py_ssize_t nattrs;\r
-\r
- if (!_PyArg_NoKeywords("attrgetter()", kwds))\r
- return NULL;\r
-\r
- nattrs = PyTuple_GET_SIZE(args);\r
- if (nattrs <= 1) {\r
- if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))\r
- return NULL;\r
- } else\r
- attr = args;\r
-\r
- /* create attrgetterobject structure */\r
- ag = PyObject_GC_New(attrgetterobject, &attrgetter_type);\r
- if (ag == NULL)\r
- return NULL;\r
-\r
- Py_INCREF(attr);\r
- ag->attr = attr;\r
- ag->nattrs = nattrs;\r
-\r
- PyObject_GC_Track(ag);\r
- return (PyObject *)ag;\r
-}\r
-\r
-static void\r
-attrgetter_dealloc(attrgetterobject *ag)\r
-{\r
- PyObject_GC_UnTrack(ag);\r
- Py_XDECREF(ag->attr);\r
- PyObject_GC_Del(ag);\r
-}\r
-\r
-static int\r
-attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg)\r
-{\r
- Py_VISIT(ag->attr);\r
- return 0;\r
-}\r
-\r
-static PyObject *\r
-dotted_getattr(PyObject *obj, PyObject *attr)\r
-{\r
- char *s, *p;\r
-\r
-#ifdef Py_USING_UNICODE\r
- if (PyUnicode_Check(attr)) {\r
- attr = _PyUnicode_AsDefaultEncodedString(attr, NULL);\r
- if (attr == NULL)\r
- return NULL;\r
- }\r
-#endif\r
-\r
- if (!PyString_Check(attr)) {\r
- PyErr_SetString(PyExc_TypeError,\r
- "attribute name must be a string");\r
- return NULL;\r
- }\r
-\r
- s = PyString_AS_STRING(attr);\r
- Py_INCREF(obj);\r
- for (;;) {\r
- PyObject *newobj, *str;\r
- p = strchr(s, '.');\r
- str = p ? PyString_FromStringAndSize(s, (p-s)) :\r
- PyString_FromString(s);\r
- if (str == NULL) {\r
- Py_DECREF(obj);\r
- return NULL;\r
- }\r
- newobj = PyObject_GetAttr(obj, str);\r
- Py_DECREF(str);\r
- Py_DECREF(obj);\r
- if (newobj == NULL)\r
- return NULL;\r
- obj = newobj;\r
- if (p == NULL) break;\r
- s = p+1;\r
- }\r
-\r
- return obj;\r
-}\r
-\r
-static PyObject *\r
-attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)\r
-{\r
- PyObject *obj, *result;\r
- Py_ssize_t i, nattrs=ag->nattrs;\r
-\r
- if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj))\r
- return NULL;\r
- if (ag->nattrs == 1)\r
- return dotted_getattr(obj, ag->attr);\r
-\r
- assert(PyTuple_Check(ag->attr));\r
- assert(PyTuple_GET_SIZE(ag->attr) == nattrs);\r
-\r
- result = PyTuple_New(nattrs);\r
- if (result == NULL)\r
- return NULL;\r
-\r
- for (i=0 ; i < nattrs ; i++) {\r
- PyObject *attr, *val;\r
- attr = PyTuple_GET_ITEM(ag->attr, i);\r
- val = dotted_getattr(obj, attr);\r
- if (val == NULL) {\r
- Py_DECREF(result);\r
- return NULL;\r
- }\r
- PyTuple_SET_ITEM(result, i, val);\r
- }\r
- return result;\r
-}\r
-\r
-PyDoc_STRVAR(attrgetter_doc,\r
-"attrgetter(attr, ...) --> attrgetter object\n\\r
-\n\\r
-Return a callable object that fetches the given attribute(s) from its operand.\n\\r
-After f = attrgetter('name'), the call f(r) returns r.name.\n\\r
-After g = attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\\r
-After h = attrgetter('name.first', 'name.last'), the call h(r) returns\n\\r
-(r.name.first, r.name.last).");\r
-\r
-static PyTypeObject attrgetter_type = {\r
- PyVarObject_HEAD_INIT(NULL, 0)\r
- "operator.attrgetter", /* tp_name */\r
- sizeof(attrgetterobject), /* tp_basicsize */\r
- 0, /* tp_itemsize */\r
- /* methods */\r
- (destructor)attrgetter_dealloc, /* tp_dealloc */\r
- 0, /* tp_print */\r
- 0, /* tp_getattr */\r
- 0, /* tp_setattr */\r
- 0, /* tp_compare */\r
- 0, /* tp_repr */\r
- 0, /* tp_as_number */\r
- 0, /* tp_as_sequence */\r
- 0, /* tp_as_mapping */\r
- 0, /* tp_hash */\r
- (ternaryfunc)attrgetter_call, /* tp_call */\r
- 0, /* tp_str */\r
- PyObject_GenericGetAttr, /* tp_getattro */\r
- 0, /* tp_setattro */\r
- 0, /* tp_as_buffer */\r
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */\r
- attrgetter_doc, /* tp_doc */\r
- (traverseproc)attrgetter_traverse, /* tp_traverse */\r
- 0, /* tp_clear */\r
- 0, /* tp_richcompare */\r
- 0, /* tp_weaklistoffset */\r
- 0, /* tp_iter */\r
- 0, /* tp_iternext */\r
- 0, /* tp_methods */\r
- 0, /* tp_members */\r
- 0, /* tp_getset */\r
- 0, /* tp_base */\r
- 0, /* tp_dict */\r
- 0, /* tp_descr_get */\r
- 0, /* tp_descr_set */\r
- 0, /* tp_dictoffset */\r
- 0, /* tp_init */\r
- 0, /* tp_alloc */\r
- attrgetter_new, /* tp_new */\r
- 0, /* tp_free */\r
-};\r
-\r
-\r
-/* methodcaller object **********************************************************/\r
-\r
-typedef struct {\r
- PyObject_HEAD\r
- PyObject *name;\r
- PyObject *args;\r
- PyObject *kwds;\r
-} methodcallerobject;\r
-\r
-static PyTypeObject methodcaller_type;\r
-\r
-static PyObject *\r
-methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds)\r
-{\r
- methodcallerobject *mc;\r
- PyObject *name, *newargs;\r
-\r
- if (PyTuple_GET_SIZE(args) < 1) {\r
- PyErr_SetString(PyExc_TypeError, "methodcaller needs at least "\r
- "one argument, the method name");\r
- return NULL;\r
- }\r
-\r
- /* create methodcallerobject structure */\r
- mc = PyObject_GC_New(methodcallerobject, &methodcaller_type);\r
- if (mc == NULL)\r
- return NULL;\r
-\r
- newargs = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));\r
- if (newargs == NULL) {\r
- Py_DECREF(mc);\r
- return NULL;\r
- }\r
- mc->args = newargs;\r
-\r
- name = PyTuple_GET_ITEM(args, 0);\r
- Py_INCREF(name);\r
- mc->name = name;\r
-\r
- Py_XINCREF(kwds);\r
- mc->kwds = kwds;\r
-\r
- PyObject_GC_Track(mc);\r
- return (PyObject *)mc;\r
-}\r
-\r
-static void\r
-methodcaller_dealloc(methodcallerobject *mc)\r
-{\r
- PyObject_GC_UnTrack(mc);\r
- Py_XDECREF(mc->name);\r
- Py_XDECREF(mc->args);\r
- Py_XDECREF(mc->kwds);\r
- PyObject_GC_Del(mc);\r
-}\r
-\r
-static int\r
-methodcaller_traverse(methodcallerobject *mc, visitproc visit, void *arg)\r
-{\r
- Py_VISIT(mc->args);\r
- Py_VISIT(mc->kwds);\r
- return 0;\r
-}\r
-\r
-static PyObject *\r
-methodcaller_call(methodcallerobject *mc, PyObject *args, PyObject *kw)\r
-{\r
- PyObject *method, *obj, *result;\r
-\r
- if (!PyArg_UnpackTuple(args, "methodcaller", 1, 1, &obj))\r
- return NULL;\r
- method = PyObject_GetAttr(obj, mc->name);\r
- if (method == NULL)\r
- return NULL;\r
- result = PyObject_Call(method, mc->args, mc->kwds);\r
- Py_DECREF(method);\r
- return result;\r
-}\r
-\r
-PyDoc_STRVAR(methodcaller_doc,\r
-"methodcaller(name, ...) --> methodcaller object\n\\r
-\n\\r
-Return a callable object that calls the given method on its operand.\n\\r
-After f = methodcaller('name'), the call f(r) returns r.name().\n\\r
-After g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\\r
-r.name('date', foo=1).");\r
-\r
-static PyTypeObject methodcaller_type = {\r
- PyVarObject_HEAD_INIT(NULL, 0)\r
- "operator.methodcaller", /* tp_name */\r
- sizeof(methodcallerobject), /* tp_basicsize */\r
- 0, /* tp_itemsize */\r
- /* methods */\r
- (destructor)methodcaller_dealloc, /* tp_dealloc */\r
- 0, /* tp_print */\r
- 0, /* tp_getattr */\r
- 0, /* tp_setattr */\r
- 0, /* tp_compare */\r
- 0, /* tp_repr */\r
- 0, /* tp_as_number */\r
- 0, /* tp_as_sequence */\r
- 0, /* tp_as_mapping */\r
- 0, /* tp_hash */\r
- (ternaryfunc)methodcaller_call, /* tp_call */\r
- 0, /* tp_str */\r
- PyObject_GenericGetAttr, /* tp_getattro */\r
- 0, /* tp_setattro */\r
- 0, /* tp_as_buffer */\r
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */\r
- methodcaller_doc, /* tp_doc */\r
- (traverseproc)methodcaller_traverse, /* tp_traverse */\r
- 0, /* tp_clear */\r
- 0, /* tp_richcompare */\r
- 0, /* tp_weaklistoffset */\r
- 0, /* tp_iter */\r
- 0, /* tp_iternext */\r
- 0, /* tp_methods */\r
- 0, /* tp_members */\r
- 0, /* tp_getset */\r
- 0, /* tp_base */\r
- 0, /* tp_dict */\r
- 0, /* tp_descr_get */\r
- 0, /* tp_descr_set */\r
- 0, /* tp_dictoffset */\r
- 0, /* tp_init */\r
- 0, /* tp_alloc */\r
- methodcaller_new, /* tp_new */\r
- 0, /* tp_free */\r
-};\r
-\r
-\r
-/* Initialization function for the module (*must* be called initoperator) */\r
-\r
-PyMODINIT_FUNC\r
-initoperator(void)\r
-{\r
- PyObject *m;\r
-\r
- /* Create the module and add the functions */\r
- m = Py_InitModule4("operator", operator_methods, operator_doc,\r
- (PyObject*)NULL, PYTHON_API_VERSION);\r
- if (m == NULL)\r
- return;\r
-\r
- if (PyType_Ready(&itemgetter_type) < 0)\r
- return;\r
- Py_INCREF(&itemgetter_type);\r
- PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type);\r
-\r
- if (PyType_Ready(&attrgetter_type) < 0)\r
- return;\r
- Py_INCREF(&attrgetter_type);\r
- PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type);\r
-\r
- if (PyType_Ready(&methodcaller_type) < 0)\r
- return;\r
- Py_INCREF(&methodcaller_type);\r
- PyModule_AddObject(m, "methodcaller", (PyObject *)&methodcaller_type);\r
-}\r