Nuitka
The Python compiler
Loading...
Searching...
No Matches
lists.h
1// Copyright 2025, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file
2
3#ifndef __NUITKA_HELPER_LISTS_H__
4#define __NUITKA_HELPER_LISTS_H__
5
6// Like PyList_SET_ITEM but takes a reference to the item.
7#define PyList_SET_ITEM0(tuple, index, value) \
8 { \
9 PyObject *tmp = value; \
10 Py_INCREF(tmp); \
11 PyList_SET_ITEM(tuple, index, tmp); \
12 }
13
14#ifndef _PyList_ITEMS
15#define _PyList_ITEMS(op) (((PyListObject *)(op))->ob_item)
16#endif
17
18#if PYTHON_VERSION >= 0x3a0
19#define NUITKA_LIST_HAS_FREELIST 1
20extern PyObject *MAKE_LIST_EMPTY(PyThreadState *tstate, Py_ssize_t size);
21#else
22#define NUITKA_LIST_HAS_FREELIST 0
23
24#define MAKE_LIST_EMPTY(tstate, size) PyList_New(size)
25#endif
26
27extern bool LIST_EXTEND_FROM_ITERABLE(PyThreadState *tstate, PyObject *list, PyObject *other);
28extern bool LIST_EXTEND_FOR_UNPACK(PyThreadState *tstate, PyObject *list, PyObject *other);
29
30// Like "PyList_Append", but we get to specify the transfer of refcount ownership.
31extern bool LIST_APPEND1(PyObject *target, PyObject *item);
32extern bool LIST_APPEND0(PyObject *target, PyObject *item);
33
34// Like "list.remove"
35bool LIST_REMOVE(PyObject *target, PyObject *item);
36
37// Like list.clear
38extern void LIST_CLEAR(PyObject *target);
39
40// Like list.reverse
41extern void LIST_REVERSE(PyObject *list);
42
43// Like list.copy
44extern PyObject *LIST_COPY(PyThreadState *tstate, PyObject *list);
45
46// Like list.count
47extern PyObject *LIST_COUNT(PyObject *list, PyObject *item);
48
49// Like list.index
50extern PyObject *LIST_INDEX2(PyThreadState *tstate, PyObject *list, PyObject *item);
51extern PyObject *LIST_INDEX3(PyThreadState *tstate, PyObject *list, PyObject *item, PyObject *start);
52extern PyObject *LIST_INDEX4(PyThreadState *tstate, PyObject *list, PyObject *item, PyObject *start, PyObject *stop);
53
54// Like list.index
55extern bool LIST_INSERT(PyThreadState *tstate, PyObject *list, PyObject *index, PyObject *item);
56// Like PyList_Insert
57extern void LIST_INSERT_CONST(PyObject *list, Py_ssize_t index, PyObject *item);
58
59extern PyObject *MAKE_LIST(PyThreadState *tstate, PyObject *iterable);
60
61extern bool LIST_EXTEND_FROM_LIST(PyObject *list, PyObject *other);
62
63NUITKA_MAY_BE_UNUSED static PyObject *MAKE_LIST_REPEATED(PyThreadState *tstate, Py_ssize_t size, PyObject *element) {
64 PyObject *result = MAKE_LIST_EMPTY(tstate, size);
65
66 if (unlikely(result == NULL)) {
67 return NULL;
68 }
69
70 for (Py_ssize_t i = 0; i < size; i++) {
71 Py_INCREF(element);
72 PyList_SET_ITEM(result, i, element);
73 }
74
75 return result;
76}
77
78#include "lists_generated.h"
79
80#endif
81// Part of "Nuitka", an optimizing Python compiler that is compatible and
82// integrates with CPython, but also works on its own.
83//
84// Licensed under the Apache License, Version 2.0 (the "License");
85// you may not use this file except in compliance with the License.
86// You may obtain a copy of the License at
87//
88// http://www.apache.org/licenses/LICENSE-2.0
89//
90// Unless required by applicable law or agreed to in writing, software
91// distributed under the License is distributed on an "AS IS" BASIS,
92// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
93// See the License for the specific language governing permissions and
94// limitations under the License.