25: #line 594 "mxTools.pak" 26: def trange(start=0, stop=NotGiven, step=1): 27: if stop is NotGiven: 28: stop = start 29: start = 0 30: return tuple(range(start, stop, step)) 31:
490: #line 601 "mxTools.pak" 491: 492: Py_C_Function( mxTools_trange, 493: "trange([start=0,]stop[,step=1])\n\n" 494: "Returns tuple(range(start,stop,step))") 495: { 496: int start,stop=INT_MAX,step=INT_MAX; 497: int n; 498: register int i; 499: register int index; 500: PyObject *t = 0; 501: 502: Py_Get3Args("i|ii",start,stop,step); 503: 504: /* Get the boundaries right... */ 505: if (stop == INT_MAX) { 506: stop = start; 507: if (stop < 0) 508: stop = 0; 509: start = 0; 510: step = 1; 511: n = stop; 512: } 513: else if (step == INT_MAX) { 514: if (start > stop) 515: start = stop; 516: step = 1; 517: n = stop - start; 518: } 519: else { 520: Py_Assert(step != 0, 521: PyExc_ValueError, 522: "step must not be zero"); 523: if (step > 0) { 524: if (start > stop) 525: start = stop; 526: n = (stop - start + step - 1) / step; 527: } 528: else { 529: if (start < stop) 530: start = stop; 531: n = (start - stop - step - 1) / (-step); 532: } 533: } 534: 535: t = PyTuple_New(n); 536: if (!t) 537: goto onError; 538: 539: if (step == 1) 540: for (index = 0, i = start; index < n; index++, i++) { 541: register PyObject *v = PyInt_FromLong((long)i); 542: if (!v) 543: goto onError; 544: PyTuple_SET_ITEM(t,index,v); 545: } 546: else 547: for (index = 0, i = start; index < n; index++, i += step) { 548: register PyObject *v = PyInt_FromLong((long)i); 549: if (!v) 550: goto onError; 551: PyTuple_SET_ITEM(t,index,v); 552: } 553: 554: return t; 555: 556: onError: 557: Py_XDECREF(t); 558: return NULL; 559: } 560: