Nuitka
The Python compiler
Loading...
Searching...
No Matches
HelpersSlices.c
1// Copyright 2025, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file
2
3/* Small helpers to work with slices their contents */
4
5// This file is included from another C file, help IDEs to still parse it on
6// its own.
7#ifdef __IDE_ONLY__
8#include "nuitka/prelude.h"
9#endif
10
11#if PYTHON_VERSION >= 0x3a0
12
13PyObject *Nuitka_Slice_New(PyThreadState *tstate, PyObject *start, PyObject *stop, PyObject *step) {
14 PySliceObject *result_slice;
15
16#if PYTHON_VERSION >= 0x3d0
17 struct _Py_object_freelists *freelists = _Nuitka_object_freelists_GET(tstate);
18 PySliceObject **slice_cache_ptr = &freelists->slices.slice_cache;
19#else
20 PyInterpreterState *interp = tstate->interp;
21 PySliceObject **slice_cache_ptr = &interp->slice_cache;
22#endif
23
24 if (*slice_cache_ptr != NULL) {
25 result_slice = *slice_cache_ptr;
26 *slice_cache_ptr = NULL;
27
28 Nuitka_Py_NewReference((PyObject *)result_slice);
29 } else {
30 result_slice = (PySliceObject *)Nuitka_GC_New(&PySlice_Type);
31
32 if (result_slice == NULL) {
33 return NULL;
34 }
35 }
36
37 if (step == NULL) {
38 step = Py_None;
39 }
40 if (start == NULL) {
41 start = Py_None;
42 }
43 if (stop == NULL) {
44 stop = Py_None;
45 }
46
47 Py_INCREF(step);
48 result_slice->step = step;
49 Py_INCREF(start);
50 result_slice->start = start;
51 Py_INCREF(stop);
52 result_slice->stop = stop;
53
54 Nuitka_GC_Track(result_slice);
55
56 return (PyObject *)result_slice;
57}
58
59#endif
60// Part of "Nuitka", an optimizing Python compiler that is compatible and
61// integrates with CPython, but also works on its own.
62//
63// Licensed under the Apache License, Version 2.0 (the "License");
64// you may not use this file except in compliance with the License.
65// You may obtain a copy of the License at
66//
67// http://www.apache.org/licenses/LICENSE-2.0
68//
69// Unless required by applicable law or agreed to in writing, software
70// distributed under the License is distributed on an "AS IS" BASIS,
71// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
72// See the License for the specific language governing permissions and
73// limitations under the License.