313 static PyObject *
314 BTreeItems_slice(BTreeItems *self, Py_ssize_t ilow, Py_ssize_t ihigh)
315 {
316 Bucket *lowbucket;
317 Bucket *highbucket;
318 int lowoffset;
319 int highoffset;
320 Py_ssize_t length = -1; /* len(self), but computed only if needed */
321
322 /* Complications:
323 * A Python slice never raises IndexError, but BTreeItems_seek does.
324 * Python did only part of index normalization before calling this:
325 * ilow may be < 0 now, and ihigh may be arbitrarily large. It's
326 * our responsibility to clip them.
327 * A Python slice is exclusive of the high index, but a BTreeItems
328 * struct is inclusive on both ends.
329 */
330
331 /* First adjust ilow and ihigh to be legit endpoints in the Python
332 * sense (ilow inclusive, ihigh exclusive). This block duplicates the
333 * logic from Python's list_slice function (slicing for builtin lists).
334 */
335 if (ilow < 0)
when considering range: 0 <= value <= 0x7fffffffffffffff
taking False path
336 ilow = 0;
337 else {
338 if (length < 0)
taking True path
339 length = BTreeItems_length(self);
340 if (ilow > length)
when taking True path
341 ilow = length;
342 }
343
344 if (ihigh < ilow)
when taking False path
345 ihigh = ilow;
346 else {
347 if (length < 0)
when considering range: -0x8000000000000000 <= value <= -1
taking True path
348 length = BTreeItems_length(self);
349 if (ihigh > length)
when taking False path
350 ihigh = length;
351 }
352 assert(0 <= ilow && ilow <= ihigh);
353 assert(length < 0 || ihigh <= length);
354
355 /* Now adjust for that our struct is inclusive on both ends. This is
356 * easy *except* when the slice is empty: there's no good way to spell
357 * that in an inclusive-on-both-ends scheme. For example, if the
358 * slice is btree.items([:0]), ilow == ihigh == 0 at this point, and if
359 * we were to subtract 1 from ihigh that would get interpreted by
360 * BTreeItems_seek as meaning the *entire* set of items. Setting ilow==1
361 * and ihigh==0 doesn't work either, as BTreeItems_seek raises IndexError
362 * if we attempt to seek to ilow==1 when the underlying sequence is empty.
363 * It seems simplest to deal with empty slices as a special case here.
364 */
365 if (ilow == ihigh) {
when taking False path
366 /* empty slice */
367 lowbucket = highbucket = NULL;
368 lowoffset = 1;
369 highoffset = 0;
370 }
371 else {
372 assert(ilow < ihigh);
373 --ihigh; /* exclusive -> inclusive */
374
375 if (BTreeItems_seek(self, ilow) < 0) return NULL;
when considering range: 0 <= value <= 0x7fffffff
taking False path
376 lowbucket = self->currentbucket;
377 lowoffset = self->currentoffset;
378
379 if (BTreeItems_seek(self, ihigh) < 0) return NULL;
when considering range: -0x80000000 <= value <= -1
taking True path
380
381 highbucket = self->currentbucket;
382 highoffset = self->currentoffset;
383 }
384 return newBTreeItems(self->kind,
385 lowbucket, lowoffset, highbucket, highoffset);
386 }
returning (PyObject*)NULL without setting an exception
found 9 similar trace(s) to this