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 >= 0x3e0
17 result_slice = (PySliceObject *)Nuitka_PyFreeList_Pop(&_Py_freelists_GET()->slices);
18
19 if (result_slice == NULL) {
20 result_slice = (PySliceObject *)Nuitka_GC_New(&PySlice_Type);
21 } else {
22 Nuitka_Py_NewReference((PyObject *)result_slice);
23 }
24#else
25#if PYTHON_VERSION >= 0x3d0
26 struct _Py_object_freelists *freelists = _Nuitka_object_freelists_GET(tstate);
27 PySliceObject **slice_cache_ptr = &freelists->slices.slice_cache;
28#else
29 PyInterpreterState *interp = tstate->interp;
30 PySliceObject **slice_cache_ptr = &interp->slice_cache;
31#endif
32
33 if (*slice_cache_ptr != NULL) {
34 result_slice = *slice_cache_ptr;
35 *slice_cache_ptr = NULL;
36
37 Nuitka_Py_NewReference((PyObject *)result_slice);
38 } else {
39 result_slice = (PySliceObject *)Nuitka_GC_New(&PySlice_Type);
40 }
41#endif
42
43 if (step == NULL) {
44 step = Py_None;
45 }
46 if (start == NULL) {
47 start = Py_None;
48 }
49 if (stop == NULL) {
50 stop = Py_None;
51 }
52
53 CHECK_OBJECT(result_slice);
54 assert(PySlice_Check(result_slice));
55
56 Py_INCREF(step);
57 result_slice->step = step;
58 Py_INCREF(start);
59 result_slice->start = start;
60 Py_INCREF(stop);
61 result_slice->stop = stop;
62
63 Nuitka_GC_Track(result_slice);
64
65 return (PyObject *)result_slice;
66}
67
68#endif
69// Part of "Nuitka", an optimizing Python compiler that is compatible and
70// integrates with CPython, but also works on its own.
71//
72// Licensed under the Apache License, Version 2.0 (the "License");
73// you may not use this file except in compliance with the License.
74// You may obtain a copy of the License at
75//
76// http://www.apache.org/licenses/LICENSE-2.0
77//
78// Unless required by applicable law or agreed to in writing, software
79// distributed under the License is distributed on an "AS IS" BASIS,
80// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
81// See the License for the specific language governing permissions and
82// limitations under the License.