Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Modules/operator.c

    r2 r391  
    88to the intrinsic operators of Python.  For example, operator.add(x, y)\n\
    99is equivalent to the expression x+y.  The function names are those\n\
    10 used for special class methods; variants without leading and trailing\n\
     10used for special methods; variants without leading and trailing\n\
    1111'__' are also provided for convenience.");
    1212
     
    6666  return PyObject_RichCompare(a1,a2,A); }
    6767
    68 spami(isCallable       , PyCallable_Check)
     68/* Deprecated operators that need warnings. */
     69static int
     70op_isCallable(PyObject *x)
     71{
     72    if (PyErr_WarnPy3k("operator.isCallable() is not supported in 3.x. "
     73                       "Use hasattr(obj, '__call__').", 1) < 0)
     74        return -1;
     75    return PyCallable_Check(x);
     76}
     77
     78static int
     79op_sequenceIncludes(PyObject *seq, PyObject* ob)
     80{
     81    if (PyErr_WarnPy3k("operator.sequenceIncludes() is not supported "
     82                       "in 3.x. Use operator.contains().", 1) < 0)
     83        return -1;
     84    return PySequence_Contains(seq, ob);
     85}
     86
     87spami(isCallable       , op_isCallable)
    6988spami(isNumberType     , PyNumber_Check)
    7089spami(truth            , PyObject_IsTrue)
     
    105124spamoi(op_irepeat      , PySequence_InPlaceRepeat)
    106125spami2b(op_contains     , PySequence_Contains)
    107 spami2b(sequenceIncludes, PySequence_Contains)
     126spami2b(sequenceIncludes, op_sequenceIncludes)
    108127spamn2(indexOf         , PySequence_Index)
    109128spamn2(countOf         , PySequence_Count)
     
    122141op_pow(PyObject *s, PyObject *a)
    123142{
    124         PyObject *a1, *a2;
    125         if (PyArg_UnpackTuple(a,"pow", 2, 2, &a1, &a2))
    126                 return PyNumber_Power(a1, a2, Py_None);
    127         return NULL;
     143    PyObject *a1, *a2;
     144    if (PyArg_UnpackTuple(a,"pow", 2, 2, &a1, &a2))
     145        return PyNumber_Power(a1, a2, Py_None);
     146    return NULL;
    128147}
    129148
     
    131150op_ipow(PyObject *s, PyObject *a)
    132151{
    133         PyObject *a1, *a2;
    134         if (PyArg_UnpackTuple(a,"ipow", 2, 2, &a1, &a2))
    135                 return PyNumber_InPlacePower(a1, a2, Py_None);
    136         return NULL;
     152    PyObject *a1, *a2;
     153    if (PyArg_UnpackTuple(a,"ipow", 2, 2, &a1, &a2))
     154        return PyNumber_InPlacePower(a1, a2, Py_None);
     155    return NULL;
    137156}
    138157
     
    140159op_index(PyObject *s, PyObject *a)
    141160{
    142         return PyNumber_Index(a);
     161    return PyNumber_Index(a);
    143162}
    144163
     
    146165is_(PyObject *s, PyObject *a)
    147166{
    148         PyObject *a1, *a2, *result = NULL;
    149         if (PyArg_UnpackTuple(a,"is_", 2, 2, &a1, &a2)) {
    150                 result = (a1 == a2) ? Py_True : Py_False;
    151                 Py_INCREF(result);
    152         }
    153         return result;
     167    PyObject *a1, *a2, *result = NULL;
     168    if (PyArg_UnpackTuple(a,"is_", 2, 2, &a1, &a2)) {
     169        result = (a1 == a2) ? Py_True : Py_False;
     170        Py_INCREF(result);
     171    }
     172    return result;
    154173}
    155174
     
    157176is_not(PyObject *s, PyObject *a)
    158177{
    159         PyObject *a1, *a2, *result = NULL;
    160         if (PyArg_UnpackTuple(a,"is_not", 2, 2, &a1, &a2)) {
    161                 result = (a1 != a2) ? Py_True : Py_False;
    162                 Py_INCREF(result);
    163         }
    164         return result;
     178    PyObject *a1, *a2, *result = NULL;
     179    if (PyArg_UnpackTuple(a,"is_not", 2, 2, &a1, &a2)) {
     180        result = (a1 != a2) ? Py_True : Py_False;
     181        Py_INCREF(result);
     182    }
     183    return result;
    165184}
    166185
     
    168187op_getslice(PyObject *s, PyObject *a)
    169188{
    170         PyObject *a1;
    171         Py_ssize_t a2, a3;
    172 
    173         if (!PyArg_ParseTuple(a, "Onn:getslice", &a1, &a2, &a3))
    174                 return NULL;
    175         return PySequence_GetSlice(a1, a2, a3);
     189    PyObject *a1;
     190    Py_ssize_t a2, a3;
     191
     192    if (!PyArg_ParseTuple(a, "Onn:getslice", &a1, &a2, &a3))
     193        return NULL;
     194    return PySequence_GetSlice(a1, a2, a3);
    176195}
    177196
     
    179198op_setslice(PyObject *s, PyObject *a)
    180199{
    181         PyObject *a1, *a4;
    182         Py_ssize_t a2, a3;
    183 
    184         if (!PyArg_ParseTuple(a, "OnnO:setslice", &a1, &a2, &a3, &a4))
    185                 return NULL;
    186 
    187         if (-1 == PySequence_SetSlice(a1, a2, a3, a4))
    188                 return NULL;
    189 
    190         Py_RETURN_NONE;
     200    PyObject *a1, *a4;
     201    Py_ssize_t a2, a3;
     202
     203    if (!PyArg_ParseTuple(a, "OnnO:setslice", &a1, &a2, &a3, &a4))
     204        return NULL;
     205
     206    if (-1 == PySequence_SetSlice(a1, a2, a3, a4))
     207        return NULL;
     208
     209    Py_RETURN_NONE;
    191210}
    192211
     
    194213op_delslice(PyObject *s, PyObject *a)
    195214{
    196         PyObject *a1;
    197         Py_ssize_t a2, a3;
    198 
    199         if (!PyArg_ParseTuple(a, "Onn:delslice", &a1, &a2, &a3))
    200                 return NULL;
    201 
    202         if (-1 == PySequence_DelSlice(a1, a2, a3))
    203                 return NULL;
    204 
    205         Py_RETURN_NONE;
     215    PyObject *a1;
     216    Py_ssize_t a2, a3;
     217
     218    if (!PyArg_ParseTuple(a, "Onn:delslice", &a1, &a2, &a3))
     219        return NULL;
     220
     221    if (-1 == PySequence_DelSlice(a1, a2, a3))
     222        return NULL;
     223
     224    Py_RETURN_NONE;
    206225}
    207226
     
    212231#define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)},
    213232#define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, \
    214                            {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},
     233                           {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},
    215234#define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)},
    216235#define spam2o(OP,ALTOP,DOC) {#OP, op_##OP, METH_O, PyDoc_STR(DOC)}, \
    217                            {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)},
     236                           {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)},
    218237
    219238static struct PyMethodDef operator_methods[] = {
     
    259278spam2(xor,__xor__, "xor(a, b) -- Same as a ^ b.")
    260279spam2(or_,__or__, "or_(a, b) -- Same as a | b.")
    261 spam2(iadd,__iadd__, "iadd(a, b) -- Same as a += b.")
    262 spam2(isub,__isub__, "isub(a, b) -- Same as a -= b.")
    263 spam2(imul,__imul__, "imul(a, b) -- Same as a *= b.")
    264 spam2(idiv,__idiv__, "idiv(a, b) -- Same as a /= b when __future__.division is not in effect.")
    265 spam2(ifloordiv,__ifloordiv__, "ifloordiv(a, b) -- Same as a //= b.")
    266 spam2(itruediv,__itruediv__, "itruediv(a, b) -- Same as a /= b when __future__.division is in effect.")
    267 spam2(imod,__imod__, "imod(a, b) -- Same as a %= b.")
    268 spam2(ilshift,__ilshift__, "ilshift(a, b) -- Same as a <<= b.")
    269 spam2(irshift,__irshift__, "irshift(a, b) -- Same as a >>= b.")
    270 spam2(iand,__iand__, "iand(a, b) -- Same as a &= b.")
    271 spam2(ixor,__ixor__, "ixor(a, b) -- Same as a ^= b.")
    272 spam2(ior,__ior__, "ior(a, b) -- Same as a |= b.")
     280spam2(iadd,__iadd__, "a = iadd(a, b) -- Same as a += b.")
     281spam2(isub,__isub__, "a = isub(a, b) -- Same as a -= b.")
     282spam2(imul,__imul__, "a = imul(a, b) -- Same as a *= b.")
     283spam2(idiv,__idiv__, "a = idiv(a, b) -- Same as a /= b when __future__.division is not in effect.")
     284spam2(ifloordiv,__ifloordiv__, "a = ifloordiv(a, b) -- Same as a //= b.")
     285spam2(itruediv,__itruediv__, "a = itruediv(a, b) -- Same as a /= b when __future__.division is in effect.")
     286spam2(imod,__imod__, "a = imod(a, b) -- Same as a %= b.")
     287spam2(ilshift,__ilshift__, "a = ilshift(a, b) -- Same as a <<= b.")
     288spam2(irshift,__irshift__, "a = irshift(a, b) -- Same as a >>= b.")
     289spam2(iand,__iand__, "a = iand(a, b) -- Same as a &= b.")
     290spam2(ixor,__ixor__, "a = ixor(a, b) -- Same as a ^= b.")
     291spam2(ior,__ior__, "a = ior(a, b) -- Same as a |= b.")
    273292spam2(concat,__concat__,
    274293 "concat(a, b) -- Same as a + b, for a and b sequences.")
     
    276295 "repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.")
    277296spam2(iconcat,__iconcat__,
    278  "iconcat(a, b) -- Same as a += b, for a and b sequences.")
     297 "a = iconcat(a, b) -- Same as a += b, for a and b sequences.")
    279298spam2(irepeat,__irepeat__,
    280  "irepeat(a, b) -- Same as a *= b, where a is a sequence, and b is an integer.")
     299 "a = irepeat(a, b) -- Same as a *= b, where a is a sequence, and b is an integer.")
    281300spam2(getitem,__getitem__,
    282301 "getitem(a, b) -- Same as a[b].")
     
    286305 "delitem(a, b) -- Same as del a[b].")
    287306spam2(pow,__pow__, "pow(a, b) -- Same as a ** b.")
    288 spam2(ipow,__ipow__, "ipow(a, b) -- Same as a **= b.")
     307spam2(ipow,__ipow__, "a = ipow(a, b) -- Same as a **= b.")
    289308spam2(getslice,__getslice__,
    290309 "getslice(a, b, c) -- Same as a[b:c].")
     
    300319spam2(ge,__ge__, "ge(a, b) -- Same as a>=b.")
    301320
    302         {NULL,          NULL}           /* sentinel */
     321    {NULL,              NULL}           /* sentinel */
    303322
    304323};
     
    307326
    308327typedef struct {
    309         PyObject_HEAD
    310         Py_ssize_t nitems;
    311         PyObject *item;
     328    PyObject_HEAD
     329    Py_ssize_t nitems;
     330    PyObject *item;
    312331} itemgetterobject;
    313332
     
    317336itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
    318337{
    319         itemgetterobject *ig;
    320         PyObject *item;
    321         Py_ssize_t nitems;
    322 
    323         if (!_PyArg_NoKeywords("itemgetter()", kwds))
    324                 return NULL;
    325 
    326         nitems = PyTuple_GET_SIZE(args);
    327         if (nitems <= 1) {
    328                 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))
    329                         return NULL;
    330         } else
    331                 item = args;
    332 
    333         /* create itemgetterobject structure */
    334         ig = PyObject_GC_New(itemgetterobject, &itemgetter_type);
    335         if (ig == NULL)
    336                 return NULL;   
    337        
    338         Py_INCREF(item);
    339         ig->item = item;
    340         ig->nitems = nitems;
    341 
    342         PyObject_GC_Track(ig);
    343         return (PyObject *)ig;
     338    itemgetterobject *ig;
     339    PyObject *item;
     340    Py_ssize_t nitems;
     341
     342    if (!_PyArg_NoKeywords("itemgetter()", kwds))
     343        return NULL;
     344
     345    nitems = PyTuple_GET_SIZE(args);
     346    if (nitems <= 1) {
     347        if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))
     348            return NULL;
     349    } else
     350        item = args;
     351
     352    /* create itemgetterobject structure */
     353    ig = PyObject_GC_New(itemgetterobject, &itemgetter_type);
     354    if (ig == NULL)
     355        return NULL;
     356
     357    Py_INCREF(item);
     358    ig->item = item;
     359    ig->nitems = nitems;
     360
     361    PyObject_GC_Track(ig);
     362    return (PyObject *)ig;
    344363}
    345364
     
    347366itemgetter_dealloc(itemgetterobject *ig)
    348367{
    349         PyObject_GC_UnTrack(ig);
    350         Py_XDECREF(ig->item);
    351         PyObject_GC_Del(ig);
     368    PyObject_GC_UnTrack(ig);
     369    Py_XDECREF(ig->item);
     370    PyObject_GC_Del(ig);
    352371}
    353372
     
    355374itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg)
    356375{
    357         Py_VISIT(ig->item);
    358         return 0;
     376    Py_VISIT(ig->item);
     377    return 0;
    359378}
    360379
     
    362381itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw)
    363382{
    364         PyObject *obj, *result;
    365         Py_ssize_t i, nitems=ig->nitems;
    366 
    367         if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj))
    368                 return NULL;
    369         if (nitems == 1)
    370                 return PyObject_GetItem(obj, ig->item);
    371 
    372         assert(PyTuple_Check(ig->item));
    373         assert(PyTuple_GET_SIZE(ig->item) == nitems);
    374 
    375         result = PyTuple_New(nitems);
    376         if (result == NULL)
    377                 return NULL;
    378 
    379         for (i=0 ; i < nitems ; i++) {
    380                 PyObject *item, *val;
    381                 item = PyTuple_GET_ITEM(ig->item, i);
    382                 val = PyObject_GetItem(obj, item);
    383                 if (val == NULL) {
    384                         Py_DECREF(result);
    385                         return NULL;
    386                 }
    387                 PyTuple_SET_ITEM(result, i, val);
    388         }
    389         return result;
     383    PyObject *obj, *result;
     384    Py_ssize_t i, nitems=ig->nitems;
     385
     386    if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj))
     387        return NULL;
     388    if (nitems == 1)
     389        return PyObject_GetItem(obj, ig->item);
     390
     391    assert(PyTuple_Check(ig->item));
     392    assert(PyTuple_GET_SIZE(ig->item) == nitems);
     393
     394    result = PyTuple_New(nitems);
     395    if (result == NULL)
     396        return NULL;
     397
     398    for (i=0 ; i < nitems ; i++) {
     399        PyObject *item, *val;
     400        item = PyTuple_GET_ITEM(ig->item, i);
     401        val = PyObject_GetItem(obj, item);
     402        if (val == NULL) {
     403            Py_DECREF(result);
     404            return NULL;
     405        }
     406        PyTuple_SET_ITEM(result, i, val);
     407    }
     408    return result;
    390409}
    391410
     
    394413\n\
    395414Return a callable object that fetches the given item(s) from its operand.\n\
    396 After, f=itemgetter(2), the call f(r) returns r[2].\n\
    397 After, g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3])");
     415After f = itemgetter(2), the call f(r) returns r[2].\n\
     416After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])");
    398417
    399418static PyTypeObject itemgetter_type = {
    400         PyVarObject_HEAD_INIT(NULL, 0)
    401         "operator.itemgetter",          /* tp_name */
    402         sizeof(itemgetterobject),       /* tp_basicsize */
    403         0,                              /* tp_itemsize */
    404         /* methods */
    405         (destructor)itemgetter_dealloc, /* tp_dealloc */
    406         0,                              /* tp_print */
    407         0,                              /* tp_getattr */
    408         0,                              /* tp_setattr */
    409         0,                              /* tp_compare */
    410         0,                              /* tp_repr */
    411         0,                              /* tp_as_number */
    412         0,                              /* tp_as_sequence */
    413         0,                              /* tp_as_mapping */
    414         0,                              /* tp_hash */
    415         (ternaryfunc)itemgetter_call,   /* tp_call */
    416         0,                              /* tp_str */
    417         PyObject_GenericGetAttr,        /* tp_getattro */
    418         0,                              /* tp_setattro */
    419         0,                              /* tp_as_buffer */
    420         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,        /* tp_flags */
    421         itemgetter_doc,                 /* tp_doc */
    422         (traverseproc)itemgetter_traverse,      /* tp_traverse */
    423         0,                              /* tp_clear */
    424         0,                              /* tp_richcompare */
    425         0,                              /* tp_weaklistoffset */
    426         0,                              /* tp_iter */
    427         0,                              /* tp_iternext */
    428         0,                              /* tp_methods */
    429         0,                              /* tp_members */
    430         0,                              /* tp_getset */
    431         0,                              /* tp_base */
    432         0,                              /* tp_dict */
    433         0,                              /* tp_descr_get */
    434         0,                              /* tp_descr_set */
    435         0,                              /* tp_dictoffset */
    436         0,                              /* tp_init */
    437         0,                              /* tp_alloc */
    438         itemgetter_new,                 /* tp_new */
    439         0,                              /* tp_free */
     419    PyVarObject_HEAD_INIT(NULL, 0)
     420    "operator.itemgetter",              /* tp_name */
     421    sizeof(itemgetterobject),           /* tp_basicsize */
     422    0,                                  /* tp_itemsize */
     423    /* methods */
     424    (destructor)itemgetter_dealloc,     /* tp_dealloc */
     425    0,                                  /* tp_print */
     426    0,                                  /* tp_getattr */
     427    0,                                  /* tp_setattr */
     428    0,                                  /* tp_compare */
     429    0,                                  /* tp_repr */
     430    0,                                  /* tp_as_number */
     431    0,                                  /* tp_as_sequence */
     432    0,                                  /* tp_as_mapping */
     433    0,                                  /* tp_hash */
     434    (ternaryfunc)itemgetter_call,       /* tp_call */
     435    0,                                  /* tp_str */
     436    PyObject_GenericGetAttr,            /* tp_getattro */
     437    0,                                  /* tp_setattro */
     438    0,                                  /* tp_as_buffer */
     439    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,            /* tp_flags */
     440    itemgetter_doc,                     /* tp_doc */
     441    (traverseproc)itemgetter_traverse,          /* tp_traverse */
     442    0,                                  /* tp_clear */
     443    0,                                  /* tp_richcompare */
     444    0,                                  /* tp_weaklistoffset */
     445    0,                                  /* tp_iter */
     446    0,                                  /* tp_iternext */
     447    0,                                  /* tp_methods */
     448    0,                                  /* tp_members */
     449    0,                                  /* tp_getset */
     450    0,                                  /* tp_base */
     451    0,                                  /* tp_dict */
     452    0,                                  /* tp_descr_get */
     453    0,                                  /* tp_descr_set */
     454    0,                                  /* tp_dictoffset */
     455    0,                                  /* tp_init */
     456    0,                                  /* tp_alloc */
     457    itemgetter_new,                     /* tp_new */
     458    0,                                  /* tp_free */
    440459};
    441460
     
    444463
    445464typedef struct {
    446         PyObject_HEAD
    447         Py_ssize_t nattrs;
    448         PyObject *attr;
     465    PyObject_HEAD
     466    Py_ssize_t nattrs;
     467    PyObject *attr;
    449468} attrgetterobject;
    450469
     
    454473attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
    455474{
    456         attrgetterobject *ag;
    457         PyObject *attr;
    458         Py_ssize_t nattrs;
    459 
    460         if (!_PyArg_NoKeywords("attrgetter()", kwds))
    461                 return NULL;
    462 
    463         nattrs = PyTuple_GET_SIZE(args);
    464         if (nattrs <= 1) {
    465                 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))
    466                         return NULL;
    467         } else
    468                 attr = args;
    469 
    470         /* create attrgetterobject structure */
    471         ag = PyObject_GC_New(attrgetterobject, &attrgetter_type);
    472         if (ag == NULL)
    473                 return NULL;   
    474        
    475         Py_INCREF(attr);
    476         ag->attr = attr;
    477         ag->nattrs = nattrs;
    478 
    479         PyObject_GC_Track(ag);
    480         return (PyObject *)ag;
     475    attrgetterobject *ag;
     476    PyObject *attr;
     477    Py_ssize_t nattrs;
     478
     479    if (!_PyArg_NoKeywords("attrgetter()", kwds))
     480        return NULL;
     481
     482    nattrs = PyTuple_GET_SIZE(args);
     483    if (nattrs <= 1) {
     484        if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))
     485            return NULL;
     486    } else
     487        attr = args;
     488
     489    /* create attrgetterobject structure */
     490    ag = PyObject_GC_New(attrgetterobject, &attrgetter_type);
     491    if (ag == NULL)
     492        return NULL;
     493
     494    Py_INCREF(attr);
     495    ag->attr = attr;
     496    ag->nattrs = nattrs;
     497
     498    PyObject_GC_Track(ag);
     499    return (PyObject *)ag;
    481500}
    482501
     
    484503attrgetter_dealloc(attrgetterobject *ag)
    485504{
    486         PyObject_GC_UnTrack(ag);
    487         Py_XDECREF(ag->attr);
    488         PyObject_GC_Del(ag);
     505    PyObject_GC_UnTrack(ag);
     506    Py_XDECREF(ag->attr);
     507    PyObject_GC_Del(ag);
    489508}
    490509
     
    492511attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg)
    493512{
    494         Py_VISIT(ag->attr);
    495         return 0;
     513    Py_VISIT(ag->attr);
     514    return 0;
    496515}
    497516
     
    499518dotted_getattr(PyObject *obj, PyObject *attr)
    500519{
    501         char *s, *p;
     520    char *s, *p;
    502521
    503522#ifdef Py_USING_UNICODE
    504         if (PyUnicode_Check(attr)) {
    505                 attr = _PyUnicode_AsDefaultEncodedString(attr, NULL);
    506                 if (attr == NULL)
    507                         return NULL;
    508         }
     523    if (PyUnicode_Check(attr)) {
     524        attr = _PyUnicode_AsDefaultEncodedString(attr, NULL);
     525        if (attr == NULL)
     526            return NULL;
     527    }
    509528#endif
    510        
    511         if (!PyString_Check(attr)) {
    512                 PyErr_SetString(PyExc_TypeError,
    513                                 "attribute name must be a string");
    514                 return NULL;
    515         }
    516 
    517         s = PyString_AS_STRING(attr);
    518         Py_INCREF(obj);
    519         for (;;) {
    520                 PyObject *newobj, *str;
    521                 p = strchr(s, '.');
    522                 str = p ? PyString_FromStringAndSize(s, (p-s)) :
    523                           PyString_FromString(s);
    524                 if (str == NULL) {
    525                         Py_DECREF(obj);
    526                         return NULL;
    527                 }
    528                 newobj = PyObject_GetAttr(obj, str);
    529                 Py_DECREF(str);
    530                 Py_DECREF(obj);
    531                 if (newobj == NULL)
    532                         return NULL;
    533                 obj = newobj;
    534                 if (p == NULL) break;
    535                 s = p+1;
    536         }
    537 
    538         return obj;
     529
     530    if (!PyString_Check(attr)) {
     531        PyErr_SetString(PyExc_TypeError,
     532                        "attribute name must be a string");
     533        return NULL;
     534    }
     535
     536    s = PyString_AS_STRING(attr);
     537    Py_INCREF(obj);
     538    for (;;) {
     539        PyObject *newobj, *str;
     540        p = strchr(s, '.');
     541        str = p ? PyString_FromStringAndSize(s, (p-s)) :
     542              PyString_FromString(s);
     543        if (str == NULL) {
     544            Py_DECREF(obj);
     545            return NULL;
     546        }
     547        newobj = PyObject_GetAttr(obj, str);
     548        Py_DECREF(str);
     549        Py_DECREF(obj);
     550        if (newobj == NULL)
     551            return NULL;
     552        obj = newobj;
     553        if (p == NULL) break;
     554        s = p+1;
     555    }
     556
     557    return obj;
    539558}
    540559
     
    542561attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)
    543562{
    544         PyObject *obj, *result;
    545         Py_ssize_t i, nattrs=ag->nattrs;
    546 
    547         if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj))
    548                 return NULL;
    549         if (ag->nattrs == 1)
    550                 return dotted_getattr(obj, ag->attr);
    551 
    552         assert(PyTuple_Check(ag->attr));
    553         assert(PyTuple_GET_SIZE(ag->attr) == nattrs);
    554 
    555         result = PyTuple_New(nattrs);
    556         if (result == NULL)
    557                 return NULL;
    558 
    559         for (i=0 ; i < nattrs ; i++) {
    560                 PyObject *attr, *val;
    561                 attr = PyTuple_GET_ITEM(ag->attr, i);
    562                 val = dotted_getattr(obj, attr);
    563                 if (val == NULL) {
    564                         Py_DECREF(result);
    565                         return NULL;
    566                 }
    567                 PyTuple_SET_ITEM(result, i, val);
    568         }
    569         return result;
     563    PyObject *obj, *result;
     564    Py_ssize_t i, nattrs=ag->nattrs;
     565
     566    if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj))
     567        return NULL;
     568    if (ag->nattrs == 1)
     569        return dotted_getattr(obj, ag->attr);
     570
     571    assert(PyTuple_Check(ag->attr));
     572    assert(PyTuple_GET_SIZE(ag->attr) == nattrs);
     573
     574    result = PyTuple_New(nattrs);
     575    if (result == NULL)
     576        return NULL;
     577
     578    for (i=0 ; i < nattrs ; i++) {
     579        PyObject *attr, *val;
     580        attr = PyTuple_GET_ITEM(ag->attr, i);
     581        val = dotted_getattr(obj, attr);
     582        if (val == NULL) {
     583            Py_DECREF(result);
     584            return NULL;
     585        }
     586        PyTuple_SET_ITEM(result, i, val);
     587    }
     588    return result;
    570589}
    571590
     
    574593\n\
    575594Return a callable object that fetches the given attribute(s) from its operand.\n\
    576 After, f=attrgetter('name'), the call f(r) returns r.name.\n\
    577 After, g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\
    578 After, h=attrgetter('name.first', 'name.last'), the call h(r) returns\n\
     595After f = attrgetter('name'), the call f(r) returns r.name.\n\
     596After g = attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\
     597After h = attrgetter('name.first', 'name.last'), the call h(r) returns\n\
    579598(r.name.first, r.name.last).");
    580599
    581600static PyTypeObject attrgetter_type = {
    582         PyVarObject_HEAD_INIT(NULL, 0)
    583         "operator.attrgetter",          /* tp_name */
    584         sizeof(attrgetterobject),       /* tp_basicsize */
    585         0,                              /* tp_itemsize */
    586         /* methods */
    587         (destructor)attrgetter_dealloc, /* tp_dealloc */
    588         0,                              /* tp_print */
    589         0,                              /* tp_getattr */
    590         0,                              /* tp_setattr */
    591         0,                              /* tp_compare */
    592         0,                              /* tp_repr */
    593         0,                              /* tp_as_number */
    594         0,                              /* tp_as_sequence */
    595         0,                              /* tp_as_mapping */
    596         0,                              /* tp_hash */
    597         (ternaryfunc)attrgetter_call,   /* tp_call */
    598         0,                              /* tp_str */
    599         PyObject_GenericGetAttr,        /* tp_getattro */
    600         0,                              /* tp_setattro */
    601         0,                              /* tp_as_buffer */
    602         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,        /* tp_flags */
    603         attrgetter_doc,                 /* tp_doc */
    604         (traverseproc)attrgetter_traverse,      /* tp_traverse */
    605         0,                              /* tp_clear */
    606         0,                              /* tp_richcompare */
    607         0,                              /* tp_weaklistoffset */
    608         0,                              /* tp_iter */
    609         0,                              /* tp_iternext */
    610         0,                              /* tp_methods */
    611         0,                              /* tp_members */
    612         0,                              /* tp_getset */
    613         0,                              /* tp_base */
    614         0,                              /* tp_dict */
    615         0,                              /* tp_descr_get */
    616         0,                              /* tp_descr_set */
    617         0,                              /* tp_dictoffset */
    618         0,                              /* tp_init */
    619         0,                              /* tp_alloc */
    620         attrgetter_new,                 /* tp_new */
    621         0,                              /* tp_free */
     601    PyVarObject_HEAD_INIT(NULL, 0)
     602    "operator.attrgetter",              /* tp_name */
     603    sizeof(attrgetterobject),           /* tp_basicsize */
     604    0,                                  /* tp_itemsize */
     605    /* methods */
     606    (destructor)attrgetter_dealloc,     /* tp_dealloc */
     607    0,                                  /* tp_print */
     608    0,                                  /* tp_getattr */
     609    0,                                  /* tp_setattr */
     610    0,                                  /* tp_compare */
     611    0,                                  /* tp_repr */
     612    0,                                  /* tp_as_number */
     613    0,                                  /* tp_as_sequence */
     614    0,                                  /* tp_as_mapping */
     615    0,                                  /* tp_hash */
     616    (ternaryfunc)attrgetter_call,       /* tp_call */
     617    0,                                  /* tp_str */
     618    PyObject_GenericGetAttr,            /* tp_getattro */
     619    0,                                  /* tp_setattro */
     620    0,                                  /* tp_as_buffer */
     621    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,            /* tp_flags */
     622    attrgetter_doc,                     /* tp_doc */
     623    (traverseproc)attrgetter_traverse,          /* tp_traverse */
     624    0,                                  /* tp_clear */
     625    0,                                  /* tp_richcompare */
     626    0,                                  /* tp_weaklistoffset */
     627    0,                                  /* tp_iter */
     628    0,                                  /* tp_iternext */
     629    0,                                  /* tp_methods */
     630    0,                                  /* tp_members */
     631    0,                                  /* tp_getset */
     632    0,                                  /* tp_base */
     633    0,                                  /* tp_dict */
     634    0,                                  /* tp_descr_get */
     635    0,                                  /* tp_descr_set */
     636    0,                                  /* tp_dictoffset */
     637    0,                                  /* tp_init */
     638    0,                                  /* tp_alloc */
     639    attrgetter_new,                     /* tp_new */
     640    0,                                  /* tp_free */
    622641};
    623642
     
    626645
    627646typedef struct {
    628         PyObject_HEAD
    629         PyObject *name;
    630         PyObject *args;
    631         PyObject *kwds;
     647    PyObject_HEAD
     648    PyObject *name;
     649    PyObject *args;
     650    PyObject *kwds;
    632651} methodcallerobject;
    633652
     
    637656methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
    638657{
    639         methodcallerobject *mc;
    640         PyObject *name, *newargs;
    641 
    642         if (PyTuple_GET_SIZE(args) < 1) {
    643                 PyErr_SetString(PyExc_TypeError, "methodcaller needs at least "
    644                                 "one argument, the method name");
    645                 return NULL;
    646         }
    647 
    648         /* create methodcallerobject structure */
    649         mc = PyObject_GC_New(methodcallerobject, &methodcaller_type);
    650         if (mc == NULL)
    651                 return NULL;   
    652 
    653         newargs = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
    654         if (newargs == NULL) {
    655                 Py_DECREF(mc);
    656                 return NULL;
    657         }
    658         mc->args = newargs;
    659 
    660         name = PyTuple_GET_ITEM(args, 0);
    661         Py_INCREF(name);
    662         mc->name = name;
    663 
    664         Py_XINCREF(kwds);
    665         mc->kwds = kwds;
    666 
    667         PyObject_GC_Track(mc);
    668         return (PyObject *)mc;
     658    methodcallerobject *mc;
     659    PyObject *name, *newargs;
     660
     661    if (PyTuple_GET_SIZE(args) < 1) {
     662        PyErr_SetString(PyExc_TypeError, "methodcaller needs at least "
     663                        "one argument, the method name");
     664        return NULL;
     665    }
     666
     667    /* create methodcallerobject structure */
     668    mc = PyObject_GC_New(methodcallerobject, &methodcaller_type);
     669    if (mc == NULL)
     670        return NULL;
     671
     672    newargs = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
     673    if (newargs == NULL) {
     674        Py_DECREF(mc);
     675        return NULL;
     676    }
     677    mc->args = newargs;
     678
     679    name = PyTuple_GET_ITEM(args, 0);
     680    Py_INCREF(name);
     681    mc->name = name;
     682
     683    Py_XINCREF(kwds);
     684    mc->kwds = kwds;
     685
     686    PyObject_GC_Track(mc);
     687    return (PyObject *)mc;
    669688}
    670689
     
    672691methodcaller_dealloc(methodcallerobject *mc)
    673692{
    674         PyObject_GC_UnTrack(mc);
    675         Py_XDECREF(mc->name);
    676         Py_XDECREF(mc->args);
    677         Py_XDECREF(mc->kwds);
    678         PyObject_GC_Del(mc);
     693    PyObject_GC_UnTrack(mc);
     694    Py_XDECREF(mc->name);
     695    Py_XDECREF(mc->args);
     696    Py_XDECREF(mc->kwds);
     697    PyObject_GC_Del(mc);
    679698}
    680699
     
    682701methodcaller_traverse(methodcallerobject *mc, visitproc visit, void *arg)
    683702{
    684         Py_VISIT(mc->args);
    685         Py_VISIT(mc->kwds);
    686         return 0;
     703    Py_VISIT(mc->args);
     704    Py_VISIT(mc->kwds);
     705    return 0;
    687706}
    688707
     
    690709methodcaller_call(methodcallerobject *mc, PyObject *args, PyObject *kw)
    691710{
    692         PyObject *method, *obj, *result;
    693 
    694         if (!PyArg_UnpackTuple(args, "methodcaller", 1, 1, &obj))
    695                 return NULL;
    696         method = PyObject_GetAttr(obj, mc->name);
    697         if (method == NULL)
    698                 return NULL;
    699         result = PyObject_Call(method, mc->args, mc->kwds);
    700         Py_DECREF(method);
    701         return result;
     711    PyObject *method, *obj, *result;
     712
     713    if (!PyArg_UnpackTuple(args, "methodcaller", 1, 1, &obj))
     714        return NULL;
     715    method = PyObject_GetAttr(obj, mc->name);
     716    if (method == NULL)
     717        return NULL;
     718    result = PyObject_Call(method, mc->args, mc->kwds);
     719    Py_DECREF(method);
     720    return result;
    702721}
    703722
     
    706725\n\
    707726Return a callable object that calls the given method on its operand.\n\
    708 After, f = methodcaller('name'), the call f(r) returns r.name().\n\
    709 After, g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\
     727After f = methodcaller('name'), the call f(r) returns r.name().\n\
     728After g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\
    710729r.name('date', foo=1).");
    711730
    712731static PyTypeObject methodcaller_type = {
    713         PyVarObject_HEAD_INIT(NULL, 0)
    714         "operator.methodcaller",        /* tp_name */
    715         sizeof(methodcallerobject),     /* tp_basicsize */
    716         0,                              /* tp_itemsize */
    717         /* methods */
    718         (destructor)methodcaller_dealloc, /* tp_dealloc */
    719         0,                              /* tp_print */
    720         0,                              /* tp_getattr */
    721         0,                              /* tp_setattr */
    722         0,                              /* tp_compare */
    723         0,                              /* tp_repr */
    724         0,                              /* tp_as_number */
    725         0,                              /* tp_as_sequence */
    726         0,                              /* tp_as_mapping */
    727         0,                              /* tp_hash */
    728         (ternaryfunc)methodcaller_call, /* tp_call */
    729         0,                              /* tp_str */
    730         PyObject_GenericGetAttr,        /* tp_getattro */
    731         0,                              /* tp_setattro */
    732         0,                              /* tp_as_buffer */
    733         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
    734         methodcaller_doc,                       /* tp_doc */
    735         (traverseproc)methodcaller_traverse,    /* tp_traverse */
    736         0,                              /* tp_clear */
    737         0,                              /* tp_richcompare */
    738         0,                              /* tp_weaklistoffset */
    739         0,                              /* tp_iter */
    740         0,                              /* tp_iternext */
    741         0,                              /* tp_methods */
    742         0,                              /* tp_members */
    743         0,                              /* tp_getset */
    744         0,                              /* tp_base */
    745         0,                              /* tp_dict */
    746         0,                              /* tp_descr_get */
    747         0,                              /* tp_descr_set */
    748         0,                              /* tp_dictoffset */
    749         0,                              /* tp_init */
    750         0,                              /* tp_alloc */
    751         methodcaller_new,               /* tp_new */
    752         0,                              /* tp_free */
     732    PyVarObject_HEAD_INIT(NULL, 0)
     733    "operator.methodcaller",            /* tp_name */
     734    sizeof(methodcallerobject),         /* tp_basicsize */
     735    0,                                  /* tp_itemsize */
     736    /* methods */
     737    (destructor)methodcaller_dealloc, /* tp_dealloc */
     738    0,                                  /* tp_print */
     739    0,                                  /* tp_getattr */
     740    0,                                  /* tp_setattr */
     741    0,                                  /* tp_compare */
     742    0,                                  /* tp_repr */
     743    0,                                  /* tp_as_number */
     744    0,                                  /* tp_as_sequence */
     745    0,                                  /* tp_as_mapping */
     746    0,                                  /* tp_hash */
     747    (ternaryfunc)methodcaller_call,     /* tp_call */
     748    0,                                  /* tp_str */
     749    PyObject_GenericGetAttr,            /* tp_getattro */
     750    0,                                  /* tp_setattro */
     751    0,                                  /* tp_as_buffer */
     752    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
     753    methodcaller_doc,                           /* tp_doc */
     754    (traverseproc)methodcaller_traverse,        /* tp_traverse */
     755    0,                                  /* tp_clear */
     756    0,                                  /* tp_richcompare */
     757    0,                                  /* tp_weaklistoffset */
     758    0,                                  /* tp_iter */
     759    0,                                  /* tp_iternext */
     760    0,                                  /* tp_methods */
     761    0,                                  /* tp_members */
     762    0,                                  /* tp_getset */
     763    0,                                  /* tp_base */
     764    0,                                  /* tp_dict */
     765    0,                                  /* tp_descr_get */
     766    0,                                  /* tp_descr_set */
     767    0,                                  /* tp_dictoffset */
     768    0,                                  /* tp_init */
     769    0,                                  /* tp_alloc */
     770    methodcaller_new,                   /* tp_new */
     771    0,                                  /* tp_free */
    753772};
    754773
     
    759778initoperator(void)
    760779{
    761         PyObject *m;
    762        
    763         /* Create the module and add the functions */
    764         m = Py_InitModule4("operator", operator_methods, operator_doc,
    765                        (PyObject*)NULL, PYTHON_API_VERSION);
    766         if (m == NULL)
    767                 return;
    768 
    769         if (PyType_Ready(&itemgetter_type) < 0)
    770                 return;
    771         Py_INCREF(&itemgetter_type);
    772         PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type);
    773 
    774         if (PyType_Ready(&attrgetter_type) < 0)
    775                 return;
    776         Py_INCREF(&attrgetter_type);
    777         PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type);
    778 
    779         if (PyType_Ready(&methodcaller_type) < 0)
    780                 return;
    781         Py_INCREF(&methodcaller_type);
    782         PyModule_AddObject(m, "methodcaller", (PyObject *)&methodcaller_type);
    783 }
     780    PyObject *m;
     781
     782    /* Create the module and add the functions */
     783    m = Py_InitModule4("operator", operator_methods, operator_doc,
     784                   (PyObject*)NULL, PYTHON_API_VERSION);
     785    if (m == NULL)
     786        return;
     787
     788    if (PyType_Ready(&itemgetter_type) < 0)
     789        return;
     790    Py_INCREF(&itemgetter_type);
     791    PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type);
     792
     793    if (PyType_Ready(&attrgetter_type) < 0)
     794        return;
     795    Py_INCREF(&attrgetter_type);
     796    PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type);
     797
     798    if (PyType_Ready(&methodcaller_type) < 0)
     799        return;
     800    Py_INCREF(&methodcaller_type);
     801    PyModule_AddObject(m, "methodcaller", (PyObject *)&methodcaller_type);
     802}
Note: See TracChangeset for help on using the changeset viewer.