Issue #7205: Fix a possible deadlock when using a BZ2File object from several threads...
[python.git] / Modules / cgensupport.c
blob7e7d0ff9f4c6356ffacafaae27901903de37745a
2 /* Functions used by cgen output */
4 #include "Python.h"
5 #include "cgensupport.h"
8 /* Functions to extract arguments.
9 These needs to know the total number of arguments supplied,
10 since the argument list is a tuple only of there is more than
11 one argument. */
13 int
14 PyArg_GetObject(register PyObject *args, int nargs, int i, PyObject **p_arg)
16 if (nargs != 1) {
17 if (args == NULL || !PyTuple_Check(args) ||
18 nargs != PyTuple_Size(args) ||
19 i < 0 || i >= nargs) {
20 return PyErr_BadArgument();
22 else {
23 args = PyTuple_GetItem(args, i);
26 if (args == NULL) {
27 return PyErr_BadArgument();
29 *p_arg = args;
30 return 1;
33 int
34 PyArg_GetLong(register PyObject *args, int nargs, int i, long *p_arg)
36 if (nargs != 1) {
37 if (args == NULL || !PyTuple_Check(args) ||
38 nargs != PyTuple_Size(args) ||
39 i < 0 || i >= nargs) {
40 return PyErr_BadArgument();
42 args = PyTuple_GetItem(args, i);
44 if (args == NULL || !PyInt_Check(args)) {
45 return PyErr_BadArgument();
47 *p_arg = PyInt_AsLong(args);
48 return 1;
51 int
52 PyArg_GetShort(register PyObject *args, int nargs, int i, short *p_arg)
54 long x;
55 if (!PyArg_GetLong(args, nargs, i, &x))
56 return 0;
57 *p_arg = (short) x;
58 return 1;
61 static int
62 extractdouble(register PyObject *v, double *p_arg)
64 if (v == NULL) {
65 /* Fall through to error return at end of function */
67 else if (PyFloat_Check(v)) {
68 *p_arg = PyFloat_AS_DOUBLE((PyFloatObject *)v);
69 return 1;
71 else if (PyInt_Check(v)) {
72 *p_arg = PyInt_AS_LONG((PyIntObject *)v);
73 return 1;
75 else if (PyLong_Check(v)) {
76 *p_arg = PyLong_AsDouble(v);
77 return 1;
79 return PyErr_BadArgument();
82 static int
83 extractfloat(register PyObject *v, float *p_arg)
85 if (v == NULL) {
86 /* Fall through to error return at end of function */
88 else if (PyFloat_Check(v)) {
89 *p_arg = (float) PyFloat_AS_DOUBLE((PyFloatObject *)v);
90 return 1;
92 else if (PyInt_Check(v)) {
93 *p_arg = (float) PyInt_AS_LONG((PyIntObject *)v);
94 return 1;
96 else if (PyLong_Check(v)) {
97 *p_arg = (float) PyLong_AsDouble(v);
98 return 1;
100 return PyErr_BadArgument();
104 PyArg_GetFloat(register PyObject *args, int nargs, int i, float *p_arg)
106 PyObject *v;
107 float x;
108 if (!PyArg_GetObject(args, nargs, i, &v))
109 return 0;
110 if (!extractfloat(v, &x))
111 return 0;
112 *p_arg = x;
113 return 1;
117 PyArg_GetString(PyObject *args, int nargs, int i, string *p_arg)
119 PyObject *v;
120 if (!PyArg_GetObject(args, nargs, i, &v))
121 return 0;
122 if (!PyString_Check(v)) {
123 return PyErr_BadArgument();
125 *p_arg = PyString_AsString(v);
126 return 1;
130 PyArg_GetChar(PyObject *args, int nargs, int i, char *p_arg)
132 string x;
133 if (!PyArg_GetString(args, nargs, i, &x))
134 return 0;
135 if (x[0] == '\0' || x[1] != '\0') {
136 /* Not exactly one char */
137 return PyErr_BadArgument();
139 *p_arg = x[0];
140 return 1;
144 PyArg_GetLongArraySize(PyObject *args, int nargs, int i, long *p_arg)
146 PyObject *v;
147 if (!PyArg_GetObject(args, nargs, i, &v))
148 return 0;
149 if (PyTuple_Check(v)) {
150 *p_arg = PyTuple_Size(v);
151 return 1;
153 if (PyList_Check(v)) {
154 *p_arg = PyList_Size(v);
155 return 1;
157 return PyErr_BadArgument();
161 PyArg_GetShortArraySize(PyObject *args, int nargs, int i, short *p_arg)
163 long x;
164 if (!PyArg_GetLongArraySize(args, nargs, i, &x))
165 return 0;
166 *p_arg = (short) x;
167 return 1;
170 /* XXX The following four are too similar. Should share more code. */
173 PyArg_GetLongArray(PyObject *args, int nargs, int i, int n, long *p_arg)
175 PyObject *v, *w;
176 if (!PyArg_GetObject(args, nargs, i, &v))
177 return 0;
178 if (PyTuple_Check(v)) {
179 if (PyTuple_Size(v) != n) {
180 return PyErr_BadArgument();
182 for (i = 0; i < n; i++) {
183 w = PyTuple_GetItem(v, i);
184 if (!PyInt_Check(w)) {
185 return PyErr_BadArgument();
187 p_arg[i] = PyInt_AsLong(w);
189 return 1;
191 else if (PyList_Check(v)) {
192 if (PyList_Size(v) != n) {
193 return PyErr_BadArgument();
195 for (i = 0; i < n; i++) {
196 w = PyList_GetItem(v, i);
197 if (!PyInt_Check(w)) {
198 return PyErr_BadArgument();
200 p_arg[i] = PyInt_AsLong(w);
202 return 1;
204 else {
205 return PyErr_BadArgument();
210 PyArg_GetShortArray(PyObject *args, int nargs, int i, int n, short *p_arg)
212 PyObject *v, *w;
213 if (!PyArg_GetObject(args, nargs, i, &v))
214 return 0;
215 if (PyTuple_Check(v)) {
216 if (PyTuple_Size(v) != n) {
217 return PyErr_BadArgument();
219 for (i = 0; i < n; i++) {
220 w = PyTuple_GetItem(v, i);
221 if (!PyInt_Check(w)) {
222 return PyErr_BadArgument();
224 p_arg[i] = (short) PyInt_AsLong(w);
226 return 1;
228 else if (PyList_Check(v)) {
229 if (PyList_Size(v) != n) {
230 return PyErr_BadArgument();
232 for (i = 0; i < n; i++) {
233 w = PyList_GetItem(v, i);
234 if (!PyInt_Check(w)) {
235 return PyErr_BadArgument();
237 p_arg[i] = (short) PyInt_AsLong(w);
239 return 1;
241 else {
242 return PyErr_BadArgument();
247 PyArg_GetDoubleArray(PyObject *args, int nargs, int i, int n, double *p_arg)
249 PyObject *v, *w;
250 if (!PyArg_GetObject(args, nargs, i, &v))
251 return 0;
252 if (PyTuple_Check(v)) {
253 if (PyTuple_Size(v) != n) {
254 return PyErr_BadArgument();
256 for (i = 0; i < n; i++) {
257 w = PyTuple_GetItem(v, i);
258 if (!extractdouble(w, &p_arg[i]))
259 return 0;
261 return 1;
263 else if (PyList_Check(v)) {
264 if (PyList_Size(v) != n) {
265 return PyErr_BadArgument();
267 for (i = 0; i < n; i++) {
268 w = PyList_GetItem(v, i);
269 if (!extractdouble(w, &p_arg[i]))
270 return 0;
272 return 1;
274 else {
275 return PyErr_BadArgument();
280 PyArg_GetFloatArray(PyObject *args, int nargs, int i, int n, float *p_arg)
282 PyObject *v, *w;
283 if (!PyArg_GetObject(args, nargs, i, &v))
284 return 0;
285 if (PyTuple_Check(v)) {
286 if (PyTuple_Size(v) != n) {
287 return PyErr_BadArgument();
289 for (i = 0; i < n; i++) {
290 w = PyTuple_GetItem(v, i);
291 if (!extractfloat(w, &p_arg[i]))
292 return 0;
294 return 1;
296 else if (PyList_Check(v)) {
297 if (PyList_Size(v) != n) {
298 return PyErr_BadArgument();
300 for (i = 0; i < n; i++) {
301 w = PyList_GetItem(v, i);
302 if (!extractfloat(w, &p_arg[i]))
303 return 0;
305 return 1;
307 else {
308 return PyErr_BadArgument();