File: common.h

package info (click to toggle)
python-pyalsa 1.2.12-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 496 kB
  • sloc: ansic: 5,616; python: 1,260; makefile: 4
file content (155 lines) | stat: -rw-r--r-- 3,734 bytes parent folder | download
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 *  Python binding for the ALSA library - Common macros
 *  Copyright (c) 2018 by Jaroslav Kysela <perex@perex.cz>
 *
 *
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as
 *   published by the Free Software Foundation; either version 2.1 of
 *   the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifndef __PYALSA_COMMON_H
#define __PYALSA_COMMON_H

#define PY_SSIZE_T_CLEAN

#include "Python.h"
#include "structmember.h"
#include "frameobject.h"
#ifndef PY_LONG_LONG
  #define PY_LONG_LONG LONG_LONG
#endif

#ifndef Py_RETURN_NONE
  #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
#endif
#ifndef Py_RETURN_TRUE
  #define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True
#endif
#ifndef Py_RETURN_FALSE
  #define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False
#endif

#if PY_MAJOR_VERSION >= 3
  #define PyInt_FromLong PyLong_FromLong
#endif

/*
 *
 */

#if PY_MAJOR_VERSION >= 3
  #define MOD_ERROR_VAL NULL
  #define MOD_SUCCESS_VAL(val) val
  #define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
  #define MOD_DEF(ob, name, doc, methods) \
          static struct PyModuleDef moduledef = { \
            PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
          ob = PyModule_Create(&moduledef);
#else
  #define MOD_ERROR_VAL
  #define MOD_SUCCESS_VAL(val)
  #define MOD_INIT(name) void init##name(void)
  #define MOD_DEF(ob, name, doc, methods) \
          ob = Py_InitModule3(name, methods, doc);
#endif

/*
 *
 */
#if PY_MAJOR_VERSION >= 3
  #define CALLBACK_VARIABLES \
	PyGILState_STATE __gstate
  #define CALLBACK_INIT \
	__gstate = PyGILState_Ensure()
  #define CALLBACK_DONE \
	PyGILState_Release(__gstate)
#else
  #define CALLBACK_VARIABLES \
	PyThreadState *__tstate, *__origstate
  #define CALLBACK_INIT \
	do { \
		__tstate = PyThreadState_New(main_interpreter); \
		__origstate = PyThreadState_Swap(__tstate); \
	} while (0)
  #define CALLBACK_DONE \
	do { \
		PyThreadState_Swap(origstate); \
		PyThreadState_Delete(tstate); \
	} while (0)
#endif

/*
 *
 */

static inline PyObject *get_bool(int val)
{
	if (val) {
		Py_INCREF(Py_True);
		return Py_True;
	} else {
		Py_INCREF(Py_False);
		return Py_False;
	}
}

static inline int get_long1(PyObject *o, long *val)
{
#if PY_MAJOR_VERSION < 3
	if (PyInt_Check(o)) {
		*val = PyInt_AsLong(o);
		return 0;
	}
#endif
	if (PyLong_Check(o)) {
		*val = PyLong_AsLong(o);
		return 0;
	}
	return 1;
}

static inline int get_long(PyObject *o, long *val)
{
	if (!get_long1(o, val))
		return 0;
#if PY_MAJOR_VERSION < 3
	PyErr_Format(PyExc_TypeError, "Only None, Int or Long types are expected!");
#else
	PyErr_Format(PyExc_TypeError, "Only None or Long types are expected!");
#endif
	return 1;
}

static inline int get_utf8_string(PyObject *o, char **str)
{
	PyObject *e = PyUnicode_AsEncodedString(o, "utf-8", "strict");
	char *s = e ? PyBytes_AsString(e) : NULL;
	if (s)
		s = strdup(s);
	*str = s;
	Py_XDECREF(e);
	return s == NULL;
}

static inline PyObject *InternFromString(const char *name)
{
#if PY_MAJOR_VERSION < 3
	return PyString_InternFromString(name);
#else
	return PyUnicode_InternFromString(name);
#endif
}

#endif /* __PYALSA_COMMON_H */