File: common.c

package info (click to toggle)
libgpiod 2.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,108 kB
  • sloc: ansic: 26,612; sh: 7,554; cpp: 4,944; python: 2,426; makefile: 811; xml: 49
file content (96 lines) | stat: -rw-r--r-- 1,728 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// SPDX-License-Identifier: LGPL-2.1-or-later
// SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>

#include "internal.h"

/* Generic dealloc callback for all gpiod objects. */
void Py_gpiod_dealloc(PyObject *self)
{
	int ret;

	ret = PyObject_CallFinalizerFromDealloc(self);
	if (ret < 0)
		return;

	PyObject_Del(self);
}

PyObject *Py_gpiod_SetErrFromErrno(void)
{
	PyObject *exc;

	if (errno == ENOMEM)
		return PyErr_NoMemory();

	switch (errno) {
	case EINVAL:
		exc = PyExc_ValueError;
		break;
	case EOPNOTSUPP:
		exc = PyExc_NotImplementedError;
		break;
	case EPIPE:
		exc = PyExc_BrokenPipeError;
		break;
	case ECHILD:
		exc = PyExc_ChildProcessError;
		break;
	case EINTR:
		exc = PyExc_InterruptedError;
		break;
	case EEXIST:
		exc = PyExc_FileExistsError;
		break;
	case ENOENT:
		exc = PyExc_FileNotFoundError;
		break;
	case EISDIR:
		exc = PyExc_IsADirectoryError;
		break;
	case ENOTDIR:
		exc = PyExc_NotADirectoryError;
		break;
	case EPERM:
		exc = PyExc_PermissionError;
		break;
	case ETIMEDOUT:
		exc = PyExc_TimeoutError;
		break;
	default:
		exc = PyExc_OSError;
		break;
	}

	return PyErr_SetFromErrno(exc);
}

PyObject *Py_gpiod_GetModuleAttrString(const char *modname,
				       const char *attrname)
{
	PyObject *module, *attribute;

	module = PyImport_ImportModule(modname);
	if (!module)
		return NULL;

	attribute = PyObject_GetAttrString(module, attrname);
	Py_DECREF(module);

	return attribute;
}

unsigned int Py_gpiod_PyLongAsUnsignedInt(PyObject *pylong)
{
	unsigned long tmp;

	tmp = PyLong_AsUnsignedLong(pylong);
	if (PyErr_Occurred())
		return 0;

	if (tmp > UINT_MAX) {
		PyErr_SetString(PyExc_ValueError, "value exceeding UINT_MAX");
		return 0;
	}

	return tmp;
}