Nuitka
The Python compiler
Loading...
Searching...
No Matches
HelpersMappings.c
1// Copyright 2025, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file
2
3/* This helpers is used to work with mapping interfaces.
4
5*/
6
7// This file is included from another C file, help IDEs to still parse it on
8// its own.
9#ifdef __IDE_ONLY__
10#include "nuitka/prelude.h"
11#endif
12
13Py_ssize_t Nuitka_PyMapping_Size(PyObject *mapping) {
14 CHECK_OBJECT(mapping);
15
16 PyMappingMethods *tp_as_mapping = Py_TYPE(mapping)->tp_as_mapping;
17
18 if (tp_as_mapping != NULL && tp_as_mapping->mp_length) {
19 Py_ssize_t result = tp_as_mapping->mp_length(mapping);
20 assert(result >= 0);
21 return result;
22 }
23
24 if (Py_TYPE(mapping)->tp_as_sequence && Py_TYPE(mapping)->tp_as_sequence->sq_length) {
25 SET_CURRENT_EXCEPTION_TYPE_COMPLAINT("%s is not a mapping", mapping);
26 return -1;
27 }
28
29 SET_CURRENT_EXCEPTION_TYPE_COMPLAINT("object of type '%s' has no len()", mapping);
30 return -1;
31}
32
33// Part of "Nuitka", an optimizing Python compiler that is compatible and
34// integrates with CPython, but also works on its own.
35//
36// Licensed under the Apache License, Version 2.0 (the "License");
37// you may not use this file except in compliance with the License.
38// You may obtain a copy of the License at
39//
40// http://www.apache.org/licenses/LICENSE-2.0
41//
42// Unless required by applicable law or agreed to in writing, software
43// distributed under the License is distributed on an "AS IS" BASIS,
44// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
45// See the License for the specific language governing permissions and
46// limitations under the License.