6.2.4.2.7. irange

Comienzo python section to interscript/core/mxTools.py[8 /26 ] Siguiente Previo Primero Último
    40: #line 753 "mxTools.pak"
    41: def irange(object,indices = NotGiven):
    42:   if indices is NotGiven: indices = trange(len(object))
    43:   x = []
    44:   for index in indices:
    45:     x.append((index, object[index]))
    46:   return tuple(x)
    47: 
End python section to interscript/core/mxTools.py[8]
Comienzo C section to interscript/core/mxTools.c[9 /30 ] Siguiente Previo Primero Último
   631: #line 761 "mxTools.pak"
   632: 
   633: Py_C_Function( mxTools_irange,
   634:                "irange(object[,indices])\n\n"
   635:                "Returns a tuple of tuples (index,object[index]), one\n"
   636:                "for each item in the sequence indices or, if this is not\n"
   637:                "given, in trange(len(object)).")
   638: {
   639:     register int n;
   640:     register int index;
   641:     PyObject *t = 0;
   642:     PyObject *w;
   643:     PyObject *indices = 0;
   644: 
   645:     Py_Get2Args("O|O",w,indices);
   646: 
   647:     if (!indices) {
   648:         n = PyObject_Length(w);
   649:         if (n < 0)
   650:             Py_Error(PyExc_TypeError,
   651:                      "second argument must have a __len__ method");
   652:     }
   653:     else {
   654:         n = PyObject_Length(indices);
   655:         if (n < 0)
   656:             Py_Error(PyExc_TypeError,
   657:                      "third argument must be a sequence");
   658:     }
   659: 
   660:     t = PyTuple_New(n);
   661:     if (!t)
   662:         goto onError;
   663: 
   664:     if (!indices)
   665:         for (index = 0; index < n; index++) {
   666:             register PyObject *u;
   667:             PyObject *v;
   668:             PyObject *x;
   669:             v = PyInt_FromLong((long)index);
   670:             if (!v)
   671:                 goto onError;
   672:             u = PyTuple_New(2);
   673:             if (!u) {
   674:                 Py_DECREF(v);
   675:                 goto onError;
   676:             }
   677:             x = PyObject_GetItem(w,v);
   678:             if (!x) {
   679:                 Py_DECREF(v);
   680:                 Py_DECREF(u);
   681:                 goto onError;
   682:             }
   683:             PyTuple_SET_ITEM(u,0,v);
   684:             PyTuple_SET_ITEM(u,1,x);
   685:             PyTuple_SET_ITEM(t,index,u);
   686:         }
   687:     else
   688:         for (index = 0; index < n; index++) {
   689:             register PyObject *u;
   690:             PyObject *x;
   691:             PyObject *v;
   692:             v = PySequence_GetItem(indices,index);
   693:             if (!v)
   694:                 goto onError;
   695:             u = PyTuple_New(2);
   696:             if (!u) {
   697:                 Py_DECREF(v);
   698:                 goto onError;
   699:             }
   700:             x = PyObject_GetItem(w,v);
   701:             if (!x) {
   702:                 Py_DECREF(v);
   703:                 Py_DECREF(u);
   704:                 goto onError;
   705:             }
   706:             PyTuple_SET_ITEM(u,0,v);
   707:             PyTuple_SET_ITEM(u,1,x);
   708:             PyTuple_SET_ITEM(t,index,u);
   709:         }
   710: 
   711:     return t;
   712: 
   713:  onError:
   714:     Py_XDECREF(t);
   715:     return NULL;
   716: }
   717: 
End C section to interscript/core/mxTools.c[9]