Debian Codegraph @

bindings/python/gpiod/ext/common.c

raw symbols SCIP
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 // SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]>
3
4 #include "internal.h"
5
6 /* Generic dealloc callback for all gpiod objects. */
7 void Py_gpiod_dealloc(PyObject *self)
8 {
9 int ret;
10
11 ret = PyObject_CallFinalizerFromDealloc(self);
12 if (ret < 0)
13 return;
14
15 PyObject_Del(self);
16 }
17
18 PyObject *Py_gpiod_SetErrFromErrno(void)
19 {
20 PyObject *exc;
21
22 if (errno == ENOMEM)
23 return PyErr_NoMemory();
24
25 switch (errno) {
26 case EINVAL:
27 exc = PyExc_ValueError;
28 break;
29 case EOPNOTSUPP:
30 exc = PyExc_NotImplementedError;
31 break;
32 case EPIPE:
33 exc = PyExc_BrokenPipeError;
34 break;
35 case ECHILD:
36 exc = PyExc_ChildProcessError;
37 break;
38 case EINTR:
39 exc = PyExc_InterruptedError;
40 break;
41 case EEXIST:
42 exc = PyExc_FileExistsError;
43 break;
44 case ENOENT:
45 exc = PyExc_FileNotFoundError;
46 break;
47 case EISDIR:
48 exc = PyExc_IsADirectoryError;
49 break;
50 case ENOTDIR:
51 exc = PyExc_NotADirectoryError;
52 break;
53 case EPERM:
54 exc = PyExc_PermissionError;
55 break;
56 case ETIMEDOUT:
57 exc = PyExc_TimeoutError;
58 break;
59 default:
60 exc = PyExc_OSError;
61 break;
62 }
63
64 return PyErr_SetFromErrno(exc);
65 }
66
67 PyObject *Py_gpiod_GetModuleAttrString(const char *modname,
68 const char *attrname)
69 {
70 PyObject *module, *attribute;
71
72 module = PyImport_ImportModule(modname);
73 if (!module)
74 return NULL;
75
76 attribute = PyObject_GetAttrString(module, attrname);
77 Py_DECREF(module);
78
79 return attribute;
80 }
81
82 unsigned int Py_gpiod_PyLongAsUnsignedInt(PyObject *pylong)
83 {
84 unsigned long tmp;
85
86 tmp = PyLong_AsUnsignedLong(pylong);
87 if (PyErr_Occurred())
88 return 0;
89
90 if (tmp > UINT_MAX) {
91 PyErr_SetString(PyExc_ValueError, "value exceeding UINT_MAX");
92 return 0;
93 }
94
95 return tmp;
96 }