Nuitka
The Python compiler
Loading...
Searching...
No Matches
attributes.h
1// Copyright 2025, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file
2
3#ifndef __NUITKA_HELPER_ATTRIBUTES_H__
4#define __NUITKA_HELPER_ATTRIBUTES_H__
5
6// Attribute lookup except special slots below.
7extern PyObject *LOOKUP_ATTRIBUTE(PyThreadState *tstate, PyObject *source, PyObject *attr_name);
8
9// Attribute lookup of attribute slot "__dict__".
10extern PyObject *LOOKUP_ATTRIBUTE_DICT_SLOT(PyThreadState *tstate, PyObject *source);
11
12// Attribute lookup of attribute slot "__class__".
13extern PyObject *LOOKUP_ATTRIBUTE_CLASS_SLOT(PyThreadState *tstate, PyObject *source);
14
15// For built-in "hasattr" functionality.
16extern int BUILTIN_HASATTR_BOOL(PyThreadState *tstate, PyObject *source, PyObject *attr_name);
17
18// Check for an attribute, cannot raise an exception.
19extern bool HAS_ATTR_BOOL(PyThreadState *tstate, PyObject *source, PyObject *attr_name);
20
21// Check for an attribute, can raise an exception.
22extern int HAS_ATTR_BOOL2(PyThreadState *tstate, PyObject *source, PyObject *attr_name);
23
24// Set an attribute except for attribute slots below.
25extern bool SET_ATTRIBUTE(PyThreadState *tstate, PyObject *target, PyObject *attr_name, PyObject *value);
26
27// Set the "__dict__" special attribute slot.
28extern bool SET_ATTRIBUTE_DICT_SLOT(PyThreadState *tstate, PyObject *target, PyObject *value);
29
30// Set the "__class__" special attribute slot.
31extern bool SET_ATTRIBUTE_CLASS_SLOT(PyThreadState *tstate, PyObject *target, PyObject *value);
32
33// Special attribute lookups, e.g. "__enter__".
34extern PyObject *LOOKUP_SPECIAL(PyThreadState *tstate, PyObject *source, PyObject *attr_name);
35
36// Find an attribute in a class, Python2 only.
37#if PYTHON_VERSION < 0x300
38extern PyObject *FIND_ATTRIBUTE_IN_CLASS(PyClassObject *class_object, PyObject *attr_name);
39#endif
40
41extern PyObject *LOOKUP_MODULE_VALUE(PyDictObject *module_dict, PyObject *var_name);
42
43// In case of DLL usage, this avoids looking up the symbol from it.
44extern getattrofunc PyObject_GenericGetAttr_resolved;
45
46// Avoid repeated code, this checks if a type has the standard implementation, then
47// we can just try and do the same in slightly faster ways.
48static inline bool hasTypeGenericGetAttr(PyTypeObject *type) {
49#if PYTHON_VERSION >= 0x3b0
50 // TODO: Big performance loss here
51 return false;
52#else
53 return type->tp_getattro == PyObject_GenericGetAttr_resolved;
54#endif
55}
56
57// In case of DLL usage, this avoids looking up the symbol from it.
58extern setattrofunc PyObject_GenericSetAttr_resolved;
59
60static inline bool hasTypeGenericSetAttr(PyTypeObject *type) {
61#if PYTHON_VERSION >= 0x3b0
62 // TODO: Big performance loss here
63 return false;
64#else
65 return type->tp_setattro == PyObject_GenericSetAttr_resolved;
66#endif
67}
68
69#if PYTHON_VERSION >= 0x3a0
70static inline bool Nuitka_Descr_IsData(PyObject *object) { return Py_TYPE(object)->tp_descr_set != NULL; }
71#else
72#define Nuitka_Descr_IsData(object) PyDescr_IsData(object)
73#endif
74
75#endif
76
77// Part of "Nuitka", an optimizing Python compiler that is compatible and
78// integrates with CPython, but also works on its own.
79//
80// Licensed under the Apache License, Version 2.0 (the "License");
81// you may not use this file except in compliance with the License.
82// You may obtain a copy of the License at
83//
84// http://www.apache.org/licenses/LICENSE-2.0
85//
86// Unless required by applicable law or agreed to in writing, software
87// distributed under the License is distributed on an "AS IS" BASIS,
88// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
89// See the License for the specific language governing permissions and
90// limitations under the License.