Debian Codegraph @

source/module.h

raw symbols SCIP
1 #ifndef AWS_CRT_PYTHON_MODULE_H
2 #define AWS_CRT_PYTHON_MODULE_H
3 /**
4 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: Apache-2.0.
6 */
7
8 /**
9 * This file contains general helpers.
10 */
11
12 #define PY_SSIZE_T_CLEAN 1
13 #include <Python.h>
14
15 #include <aws/common/common.h>
16
17 struct aws_byte_buf;
18 struct aws_byte_cursor;
19 struct aws_string;
20
21 #define AWS_CRT_PYTHON_PACKAGE_ID 10
22
23 /* Error codes, unique to aws-crt-python, for passing back to C layers */
24 enum aws_crt_python_errors {
25 AWS_ERROR_CRT_CALLBACK_EXCEPTION = AWS_ERROR_ENUM_BEGIN_RANGE(AWS_CRT_PYTHON_PACKAGE_ID),
26
27 AWS_ERROR_CRT_END_RANGE = AWS_ERROR_ENUM_END_RANGE(AWS_CRT_PYTHON_PACKAGE_ID)
28 };
29
30 /* AWS Specific Helpers */
31 PyObject *PyUnicode_FromAwsByteCursor(const struct aws_byte_cursor *cursor);
32 PyObject *PyUnicode_FromAwsString(const struct aws_string *aws_str);
33 PyObject *PyBytes_FromAwsByteCursor(const struct aws_byte_cursor *cursor);
34
35 /* Return the named attribute, converted to the specified type.
36 * If conversion cannot occur a python exception is set (check PyExc_Occurred()) */
37 uint32_t PyObject_GetAttrAsUint32(PyObject *o, const char *class_name, const char *attr_name);
38 uint16_t PyObject_GetAttrAsUint16(PyObject *o, const char *class_name, const char *attr_name);
39 uint8_t PyObject_GetAttrAsUint8(PyObject *o, const char *class_name, const char *attr_name);
40 bool PyObject_GetAttrAsBool(PyObject *o, const char *class_name, const char *attr_name);
41 int PyObject_GetAttrAsIntEnum(PyObject *o, const char *class_name, const char *attr_name);
42
43 /* Checks if the named attribute is None, converts it to the specified type, then stores
44 * the value and returns a pointer to the stored value or NULL if it doesn't exist or fails.
45 * If conversion cannot occur a python exception is set (check PyExc_Occurred()) */
46 uint64_t *PyObject_GetAsOptionalUint64(
47 PyObject *o,
48 const char *class_name,
49 const char *attr_name,
50 uint64_t *stored_int);
51 uint32_t *PyObject_GetAsOptionalUint32(
52 PyObject *o,
53 const char *class_name,
54 const char *attr_name,
55 uint32_t *stored_int);
56 uint16_t *PyObject_GetAsOptionalUint16(
57 PyObject *o,
58 const char *class_name,
59 const char *attr_name,
60 uint16_t *stored_int);
61 uint8_t *PyObject_GetAsOptionalUint8(PyObject *o, const char *class_name, const char *attr_name, uint8_t *stored_int);
62 bool *PyObject_GetAsOptionalBool(PyObject *o, const char *class_name, const char *attr_name, bool *stored_bool);
63 int *PyObject_GetAsOptionalIntEnum(PyObject *o, const char *class_name, const char *attr_name, int *stored_enum);
64
65 /* Create cursor from PyUnicode.
66 * If conversion cannot occur, cursor->ptr will be NULL and a python exception is set */
67 struct aws_byte_cursor aws_byte_cursor_from_pyunicode(PyObject *str);
68
69 /* Create cursor from PyBytes.
70 * If conversion cannot occur, cursor->ptr will be NULL and a python exception is set */
71 struct aws_byte_cursor aws_byte_cursor_from_pybytes(PyObject *py_bytes);
72
73 /* Set current thread's error indicator based on aws_last_error() */
74 void PyErr_SetAwsLastError(void);
75
76 /* Set current thread's error indicator based on aws_last_error() and returns NULL */
77 PyObject *PyErr_AwsLastError(void);
78
79 /**
80 * Return an AWS error code corresponding to the current Python error (fallback is AWS_ERROR_UNKNOWN).
81 *
82 * Prints the current Python error to stderr and clears the Python error indicator.
83 *
84 * The Python error indicator MUST be set and the GIL MUST be held when calling this function. */
85 int aws_py_translate_py_error(void);
86
87 /**
88 * Raise an AWS error corresponding to the current Python error.
89 *
90 * Prints the current Python error to stderr and clears the Python error indicator.
91 * Finds an AWS error code corresponding to the current Python error (fallback is AWS_ERROR_UNKNOWN).
92 * Invokes aws_raise_error() with that error code and always returns AWS_OP_ERR.
93 *
94 * The Python error indicator MUST be set and the GIL MUST be held when calling this function. */
95 int aws_py_raise_error(void);
96
97 /**
98 * Return built-in Python exception type corresponding to an AWS_ERROR_ code.
99 * Ex: AWS_ERROR_OOM -> MemoryError.
100 * Returns None if there is no match.
101 */
102 PyObject *aws_py_get_corresponding_builtin_exception(PyObject *self, PyObject *args);
103
104 PyObject *aws_py_get_error_name(PyObject *self, PyObject *args);
105 PyObject *aws_py_get_error_message(PyObject *self, PyObject *args);
106
107 /* Create a write-only memoryview from the remaining free space in an aws_byte_buf */
108 PyObject *aws_py_memory_view_from_byte_buffer(struct aws_byte_buf *buf);
109
110 /* Python 3.13+ changed the function to get a reference from WeakRef. This function is an abstraction over two different
111 * APIs since we support Python versions before 3.13. Returns a strong reference if non-null, which you must release. */
112
113 /**
114 * Given a weak reference, returns a NEW strong reference to the referenced object,
115 * or NULL if the reference is dead (this function NEVER raises a python exception or AWS Error).
116 *
117 * You MUST NOT call this if ref came from a user, or ref is NULL.
118 *
119 * This is a simplified version of PyWeakref_GetRef() / PyWeakref_GetObject().
120 * Simpler because:
121 * - Python 3.13 adds PyWeakref_GetRef() and deprecates PyWeakref_GetObject().
122 * This function calls the appropriate one.
123 *
124 * - This functions has 2 outcomes instead of 3:
125 * The 3rd being a Python exception for calling it incorrectly.
126 * If that happens, this function calls PyErr_WriteUnraisable() to clear the exception,
127 * which is what you would have done anyway.
128 */
129 PyObject *aws_py_weakref_get_ref(PyObject *ref);
130
131 /* Allocator that calls into PyObject_[Malloc|Free|Realloc] */
132 struct aws_allocator *aws_py_get_allocator(void);
133
134 /**
135 * Acquire GIL, unless it is impossible to do so because the application is shutting down.
136 * Returns AWS_OP_ERR and raises AWS error if GIL cannot be acquired.
137 *
138 * Late in application shutdown, attempting to acquire GIL can crash the application.
139 * We encounter this situation if native resources are still running when the application exits.
140 * Python programmers are allowed to exist without shutting everything down, it does not make them criminals.
141 *
142 * If we encounter this situation, we must not crash the application while it finishes terminating.
143 * Do not interact with Python! It's ok not to fulfill promises (ex: it's ok if a completion callback never fires)
144 * because the user clearly no longer cares about the results.
145 */
146 int aws_py_gilstate_ensure(PyGILState_STATE *out_state);
147
148 /* Return the capsule ptr from obj._binding
149 * On error, NULL is returned and a python exception is set. */
150 void *aws_py_get_binding(PyObject *obj, const char *capsule_name, const char *class_name);
151
152 /* Contents of aws_py_get_XYZ() function where obj._binding->native is returned.
153 * NOTE: only works where native is stored by ptr. */
154 #define AWS_PY_RETURN_NATIVE_FROM_BINDING(PYOBJ, CAPSULE_NAME, CLASS_NAME, BINDING_TYPE) \
155 struct BINDING_TYPE *binding = aws_py_get_binding((PYOBJ), (CAPSULE_NAME), (CLASS_NAME)); \
156 if (binding) { \
157 if (binding->native) { \
158 return binding->native; \
159 } \
160 PyErr_Format(PyExc_TypeError, "Expected valid '%s', but '_binding.native' is NULL", (CLASS_NAME)); \
161 } \
162 return NULL
163
164 /* Shorthand for `return &obj._binding->native;` */
165 #define AWS_PY_RETURN_NATIVE_REF_FROM_BINDING(PYOBJ, CAPSULE_NAME, CLASS_NAME, BINDING_TYPE) \
166 struct BINDING_TYPE *binding = aws_py_get_binding((PYOBJ), (CAPSULE_NAME), (CLASS_NAME)); \
167 if (!binding) { \
168 return NULL; \
169 } \
170 return &binding->native
171
172 #endif /* AWS_CRT_PYTHON_MODULE_H */