1 | n/a | |
---|
2 | n/a | /* New getargs implementation */ |
---|
3 | n/a | |
---|
4 | n/a | #include "Python.h" |
---|
5 | n/a | |
---|
6 | n/a | #include <ctype.h> |
---|
7 | n/a | |
---|
8 | n/a | |
---|
9 | n/a | #ifdef __cplusplus |
---|
10 | n/a | extern "C" { |
---|
11 | n/a | #endif |
---|
12 | n/a | int PyArg_Parse(PyObject *, const char *, ...); |
---|
13 | n/a | int PyArg_ParseTuple(PyObject *, const char *, ...); |
---|
14 | n/a | int PyArg_VaParse(PyObject *, const char *, va_list); |
---|
15 | n/a | |
---|
16 | n/a | int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *, |
---|
17 | n/a | const char *, char **, ...); |
---|
18 | n/a | int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *, |
---|
19 | n/a | const char *, char **, va_list); |
---|
20 | n/a | |
---|
21 | n/a | int _PyArg_ParseTupleAndKeywordsFast(PyObject *, PyObject *, |
---|
22 | n/a | struct _PyArg_Parser *, ...); |
---|
23 | n/a | int _PyArg_VaParseTupleAndKeywordsFast(PyObject *, PyObject *, |
---|
24 | n/a | struct _PyArg_Parser *, va_list); |
---|
25 | n/a | |
---|
26 | n/a | #ifdef HAVE_DECLSPEC_DLL |
---|
27 | n/a | /* Export functions */ |
---|
28 | n/a | PyAPI_FUNC(int) _PyArg_Parse_SizeT(PyObject *, const char *, ...); |
---|
29 | n/a | PyAPI_FUNC(int) _PyArg_ParseStack_SizeT(PyObject **args, Py_ssize_t nargs, |
---|
30 | n/a | const char *format, ...); |
---|
31 | n/a | PyAPI_FUNC(int) _PyArg_ParseStackAndKeywords_SizeT(PyObject **args, Py_ssize_t nargs, PyObject *kwnames, |
---|
32 | n/a | struct _PyArg_Parser *parser, ...); |
---|
33 | n/a | PyAPI_FUNC(int) _PyArg_ParseTuple_SizeT(PyObject *, const char *, ...); |
---|
34 | n/a | PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywords_SizeT(PyObject *, PyObject *, |
---|
35 | n/a | const char *, char **, ...); |
---|
36 | n/a | PyAPI_FUNC(PyObject *) _Py_BuildValue_SizeT(const char *, ...); |
---|
37 | n/a | PyAPI_FUNC(int) _PyArg_VaParse_SizeT(PyObject *, const char *, va_list); |
---|
38 | n/a | PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *, PyObject *, |
---|
39 | n/a | const char *, char **, va_list); |
---|
40 | n/a | |
---|
41 | n/a | PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *, PyObject *, |
---|
42 | n/a | struct _PyArg_Parser *, ...); |
---|
43 | n/a | PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *, PyObject *, |
---|
44 | n/a | struct _PyArg_Parser *, va_list); |
---|
45 | n/a | #endif |
---|
46 | n/a | |
---|
47 | n/a | #define FLAG_COMPAT 1 |
---|
48 | n/a | #define FLAG_SIZE_T 2 |
---|
49 | n/a | |
---|
50 | n/a | typedef int (*destr_t)(PyObject *, void *); |
---|
51 | n/a | |
---|
52 | n/a | |
---|
53 | n/a | /* Keep track of "objects" that have been allocated or initialized and |
---|
54 | n/a | which will need to be deallocated or cleaned up somehow if overall |
---|
55 | n/a | parsing fails. |
---|
56 | n/a | */ |
---|
57 | n/a | typedef struct { |
---|
58 | n/a | void *item; |
---|
59 | n/a | destr_t destructor; |
---|
60 | n/a | } freelistentry_t; |
---|
61 | n/a | |
---|
62 | n/a | typedef struct { |
---|
63 | n/a | freelistentry_t *entries; |
---|
64 | n/a | int first_available; |
---|
65 | n/a | int entries_malloced; |
---|
66 | n/a | } freelist_t; |
---|
67 | n/a | |
---|
68 | n/a | #define STATIC_FREELIST_ENTRIES 8 |
---|
69 | n/a | |
---|
70 | n/a | /* Forward */ |
---|
71 | n/a | static int vgetargs1_impl(PyObject *args, PyObject **stack, Py_ssize_t nargs, |
---|
72 | n/a | const char *format, va_list *p_va, int flags); |
---|
73 | n/a | static int vgetargs1(PyObject *, const char *, va_list *, int); |
---|
74 | n/a | static void seterror(Py_ssize_t, const char *, int *, const char *, const char *); |
---|
75 | n/a | static const char *convertitem(PyObject *, const char **, va_list *, int, int *, |
---|
76 | n/a | char *, size_t, freelist_t *); |
---|
77 | n/a | static const char *converttuple(PyObject *, const char **, va_list *, int, |
---|
78 | n/a | int *, char *, size_t, int, freelist_t *); |
---|
79 | n/a | static const char *convertsimple(PyObject *, const char **, va_list *, int, |
---|
80 | n/a | char *, size_t, freelist_t *); |
---|
81 | n/a | static Py_ssize_t convertbuffer(PyObject *, const void **p, const char **); |
---|
82 | n/a | static int getbuffer(PyObject *, Py_buffer *, const char**); |
---|
83 | n/a | |
---|
84 | n/a | static int vgetargskeywords(PyObject *, PyObject *, |
---|
85 | n/a | const char *, char **, va_list *, int); |
---|
86 | n/a | static int vgetargskeywordsfast(PyObject *, PyObject *, |
---|
87 | n/a | struct _PyArg_Parser *, va_list *, int); |
---|
88 | n/a | static int vgetargskeywordsfast_impl(PyObject **args, Py_ssize_t nargs, |
---|
89 | n/a | PyObject *keywords, PyObject *kwnames, |
---|
90 | n/a | struct _PyArg_Parser *parser, |
---|
91 | n/a | va_list *p_va, int flags); |
---|
92 | n/a | static const char *skipitem(const char **, va_list *, int); |
---|
93 | n/a | |
---|
94 | n/a | int |
---|
95 | n/a | PyArg_Parse(PyObject *args, const char *format, ...) |
---|
96 | n/a | { |
---|
97 | n/a | int retval; |
---|
98 | n/a | va_list va; |
---|
99 | n/a | |
---|
100 | n/a | va_start(va, format); |
---|
101 | n/a | retval = vgetargs1(args, format, &va, FLAG_COMPAT); |
---|
102 | n/a | va_end(va); |
---|
103 | n/a | return retval; |
---|
104 | n/a | } |
---|
105 | n/a | |
---|
106 | n/a | int |
---|
107 | n/a | _PyArg_Parse_SizeT(PyObject *args, const char *format, ...) |
---|
108 | n/a | { |
---|
109 | n/a | int retval; |
---|
110 | n/a | va_list va; |
---|
111 | n/a | |
---|
112 | n/a | va_start(va, format); |
---|
113 | n/a | retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T); |
---|
114 | n/a | va_end(va); |
---|
115 | n/a | return retval; |
---|
116 | n/a | } |
---|
117 | n/a | |
---|
118 | n/a | |
---|
119 | n/a | int |
---|
120 | n/a | PyArg_ParseTuple(PyObject *args, const char *format, ...) |
---|
121 | n/a | { |
---|
122 | n/a | int retval; |
---|
123 | n/a | va_list va; |
---|
124 | n/a | |
---|
125 | n/a | va_start(va, format); |
---|
126 | n/a | retval = vgetargs1(args, format, &va, 0); |
---|
127 | n/a | va_end(va); |
---|
128 | n/a | return retval; |
---|
129 | n/a | } |
---|
130 | n/a | |
---|
131 | n/a | int |
---|
132 | n/a | _PyArg_ParseTuple_SizeT(PyObject *args, const char *format, ...) |
---|
133 | n/a | { |
---|
134 | n/a | int retval; |
---|
135 | n/a | va_list va; |
---|
136 | n/a | |
---|
137 | n/a | va_start(va, format); |
---|
138 | n/a | retval = vgetargs1(args, format, &va, FLAG_SIZE_T); |
---|
139 | n/a | va_end(va); |
---|
140 | n/a | return retval; |
---|
141 | n/a | } |
---|
142 | n/a | |
---|
143 | n/a | |
---|
144 | n/a | int |
---|
145 | n/a | _PyArg_ParseStack(PyObject **args, Py_ssize_t nargs, const char *format, ...) |
---|
146 | n/a | { |
---|
147 | n/a | int retval; |
---|
148 | n/a | va_list va; |
---|
149 | n/a | |
---|
150 | n/a | va_start(va, format); |
---|
151 | n/a | retval = vgetargs1_impl(NULL, args, nargs, format, &va, 0); |
---|
152 | n/a | va_end(va); |
---|
153 | n/a | return retval; |
---|
154 | n/a | } |
---|
155 | n/a | |
---|
156 | n/a | int |
---|
157 | n/a | _PyArg_ParseStack_SizeT(PyObject **args, Py_ssize_t nargs, const char *format, ...) |
---|
158 | n/a | { |
---|
159 | n/a | int retval; |
---|
160 | n/a | va_list va; |
---|
161 | n/a | |
---|
162 | n/a | va_start(va, format); |
---|
163 | n/a | retval = vgetargs1_impl(NULL, args, nargs, format, &va, FLAG_SIZE_T); |
---|
164 | n/a | va_end(va); |
---|
165 | n/a | return retval; |
---|
166 | n/a | } |
---|
167 | n/a | |
---|
168 | n/a | |
---|
169 | n/a | int |
---|
170 | n/a | PyArg_VaParse(PyObject *args, const char *format, va_list va) |
---|
171 | n/a | { |
---|
172 | n/a | va_list lva; |
---|
173 | n/a | int retval; |
---|
174 | n/a | |
---|
175 | n/a | va_copy(lva, va); |
---|
176 | n/a | |
---|
177 | n/a | retval = vgetargs1(args, format, &lva, 0); |
---|
178 | n/a | va_end(lva); |
---|
179 | n/a | return retval; |
---|
180 | n/a | } |
---|
181 | n/a | |
---|
182 | n/a | int |
---|
183 | n/a | _PyArg_VaParse_SizeT(PyObject *args, const char *format, va_list va) |
---|
184 | n/a | { |
---|
185 | n/a | va_list lva; |
---|
186 | n/a | int retval; |
---|
187 | n/a | |
---|
188 | n/a | va_copy(lva, va); |
---|
189 | n/a | |
---|
190 | n/a | retval = vgetargs1(args, format, &lva, FLAG_SIZE_T); |
---|
191 | n/a | va_end(lva); |
---|
192 | n/a | return retval; |
---|
193 | n/a | } |
---|
194 | n/a | |
---|
195 | n/a | |
---|
196 | n/a | /* Handle cleanup of allocated memory in case of exception */ |
---|
197 | n/a | |
---|
198 | n/a | static int |
---|
199 | n/a | cleanup_ptr(PyObject *self, void *ptr) |
---|
200 | n/a | { |
---|
201 | n/a | if (ptr) { |
---|
202 | n/a | PyMem_FREE(ptr); |
---|
203 | n/a | } |
---|
204 | n/a | return 0; |
---|
205 | n/a | } |
---|
206 | n/a | |
---|
207 | n/a | static int |
---|
208 | n/a | cleanup_buffer(PyObject *self, void *ptr) |
---|
209 | n/a | { |
---|
210 | n/a | Py_buffer *buf = (Py_buffer *)ptr; |
---|
211 | n/a | if (buf) { |
---|
212 | n/a | PyBuffer_Release(buf); |
---|
213 | n/a | } |
---|
214 | n/a | return 0; |
---|
215 | n/a | } |
---|
216 | n/a | |
---|
217 | n/a | static int |
---|
218 | n/a | addcleanup(void *ptr, freelist_t *freelist, destr_t destructor) |
---|
219 | n/a | { |
---|
220 | n/a | int index; |
---|
221 | n/a | |
---|
222 | n/a | index = freelist->first_available; |
---|
223 | n/a | freelist->first_available += 1; |
---|
224 | n/a | |
---|
225 | n/a | freelist->entries[index].item = ptr; |
---|
226 | n/a | freelist->entries[index].destructor = destructor; |
---|
227 | n/a | |
---|
228 | n/a | return 0; |
---|
229 | n/a | } |
---|
230 | n/a | |
---|
231 | n/a | static int |
---|
232 | n/a | cleanreturn(int retval, freelist_t *freelist) |
---|
233 | n/a | { |
---|
234 | n/a | int index; |
---|
235 | n/a | |
---|
236 | n/a | if (retval == 0) { |
---|
237 | n/a | /* A failure occurred, therefore execute all of the cleanup |
---|
238 | n/a | functions. |
---|
239 | n/a | */ |
---|
240 | n/a | for (index = 0; index < freelist->first_available; ++index) { |
---|
241 | n/a | freelist->entries[index].destructor(NULL, |
---|
242 | n/a | freelist->entries[index].item); |
---|
243 | n/a | } |
---|
244 | n/a | } |
---|
245 | n/a | if (freelist->entries_malloced) |
---|
246 | n/a | PyMem_FREE(freelist->entries); |
---|
247 | n/a | return retval; |
---|
248 | n/a | } |
---|
249 | n/a | |
---|
250 | n/a | |
---|
251 | n/a | static int |
---|
252 | n/a | vgetargs1_impl(PyObject *compat_args, PyObject **stack, Py_ssize_t nargs, const char *format, |
---|
253 | n/a | va_list *p_va, int flags) |
---|
254 | n/a | { |
---|
255 | n/a | char msgbuf[256]; |
---|
256 | n/a | int levels[32]; |
---|
257 | n/a | const char *fname = NULL; |
---|
258 | n/a | const char *message = NULL; |
---|
259 | n/a | int min = -1; |
---|
260 | n/a | int max = 0; |
---|
261 | n/a | int level = 0; |
---|
262 | n/a | int endfmt = 0; |
---|
263 | n/a | const char *formatsave = format; |
---|
264 | n/a | Py_ssize_t i; |
---|
265 | n/a | const char *msg; |
---|
266 | n/a | int compat = flags & FLAG_COMPAT; |
---|
267 | n/a | freelistentry_t static_entries[STATIC_FREELIST_ENTRIES]; |
---|
268 | n/a | freelist_t freelist; |
---|
269 | n/a | |
---|
270 | n/a | assert(nargs == 0 || stack != NULL); |
---|
271 | n/a | |
---|
272 | n/a | freelist.entries = static_entries; |
---|
273 | n/a | freelist.first_available = 0; |
---|
274 | n/a | freelist.entries_malloced = 0; |
---|
275 | n/a | |
---|
276 | n/a | flags = flags & ~FLAG_COMPAT; |
---|
277 | n/a | |
---|
278 | n/a | while (endfmt == 0) { |
---|
279 | n/a | int c = *format++; |
---|
280 | n/a | switch (c) { |
---|
281 | n/a | case '(': |
---|
282 | n/a | if (level == 0) |
---|
283 | n/a | max++; |
---|
284 | n/a | level++; |
---|
285 | n/a | if (level >= 30) |
---|
286 | n/a | Py_FatalError("too many tuple nesting levels " |
---|
287 | n/a | "in argument format string"); |
---|
288 | n/a | break; |
---|
289 | n/a | case ')': |
---|
290 | n/a | if (level == 0) |
---|
291 | n/a | Py_FatalError("excess ')' in getargs format"); |
---|
292 | n/a | else |
---|
293 | n/a | level--; |
---|
294 | n/a | break; |
---|
295 | n/a | case '\0': |
---|
296 | n/a | endfmt = 1; |
---|
297 | n/a | break; |
---|
298 | n/a | case ':': |
---|
299 | n/a | fname = format; |
---|
300 | n/a | endfmt = 1; |
---|
301 | n/a | break; |
---|
302 | n/a | case ';': |
---|
303 | n/a | message = format; |
---|
304 | n/a | endfmt = 1; |
---|
305 | n/a | break; |
---|
306 | n/a | case '|': |
---|
307 | n/a | if (level == 0) |
---|
308 | n/a | min = max; |
---|
309 | n/a | break; |
---|
310 | n/a | default: |
---|
311 | n/a | if (level == 0) { |
---|
312 | n/a | if (Py_ISALPHA(Py_CHARMASK(c))) |
---|
313 | n/a | if (c != 'e') /* skip encoded */ |
---|
314 | n/a | max++; |
---|
315 | n/a | } |
---|
316 | n/a | break; |
---|
317 | n/a | } |
---|
318 | n/a | } |
---|
319 | n/a | |
---|
320 | n/a | if (level != 0) |
---|
321 | n/a | Py_FatalError(/* '(' */ "missing ')' in getargs format"); |
---|
322 | n/a | |
---|
323 | n/a | if (min < 0) |
---|
324 | n/a | min = max; |
---|
325 | n/a | |
---|
326 | n/a | format = formatsave; |
---|
327 | n/a | |
---|
328 | n/a | if (max > STATIC_FREELIST_ENTRIES) { |
---|
329 | n/a | freelist.entries = PyMem_NEW(freelistentry_t, max); |
---|
330 | n/a | if (freelist.entries == NULL) { |
---|
331 | n/a | PyErr_NoMemory(); |
---|
332 | n/a | return 0; |
---|
333 | n/a | } |
---|
334 | n/a | freelist.entries_malloced = 1; |
---|
335 | n/a | } |
---|
336 | n/a | |
---|
337 | n/a | if (compat) { |
---|
338 | n/a | if (max == 0) { |
---|
339 | n/a | if (compat_args == NULL) |
---|
340 | n/a | return 1; |
---|
341 | n/a | PyErr_Format(PyExc_TypeError, |
---|
342 | n/a | "%.200s%s takes no arguments", |
---|
343 | n/a | fname==NULL ? "function" : fname, |
---|
344 | n/a | fname==NULL ? "" : "()"); |
---|
345 | n/a | return cleanreturn(0, &freelist); |
---|
346 | n/a | } |
---|
347 | n/a | else if (min == 1 && max == 1) { |
---|
348 | n/a | if (compat_args == NULL) { |
---|
349 | n/a | PyErr_Format(PyExc_TypeError, |
---|
350 | n/a | "%.200s%s takes at least one argument", |
---|
351 | n/a | fname==NULL ? "function" : fname, |
---|
352 | n/a | fname==NULL ? "" : "()"); |
---|
353 | n/a | return cleanreturn(0, &freelist); |
---|
354 | n/a | } |
---|
355 | n/a | msg = convertitem(compat_args, &format, p_va, flags, levels, |
---|
356 | n/a | msgbuf, sizeof(msgbuf), &freelist); |
---|
357 | n/a | if (msg == NULL) |
---|
358 | n/a | return cleanreturn(1, &freelist); |
---|
359 | n/a | seterror(levels[0], msg, levels+1, fname, message); |
---|
360 | n/a | return cleanreturn(0, &freelist); |
---|
361 | n/a | } |
---|
362 | n/a | else { |
---|
363 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
364 | n/a | "old style getargs format uses new features"); |
---|
365 | n/a | return cleanreturn(0, &freelist); |
---|
366 | n/a | } |
---|
367 | n/a | } |
---|
368 | n/a | |
---|
369 | n/a | if (nargs < min || max < nargs) { |
---|
370 | n/a | if (message == NULL) |
---|
371 | n/a | PyErr_Format(PyExc_TypeError, |
---|
372 | n/a | "%.150s%s takes %s %d argument%s (%ld given)", |
---|
373 | n/a | fname==NULL ? "function" : fname, |
---|
374 | n/a | fname==NULL ? "" : "()", |
---|
375 | n/a | min==max ? "exactly" |
---|
376 | n/a | : nargs < min ? "at least" : "at most", |
---|
377 | n/a | nargs < min ? min : max, |
---|
378 | n/a | (nargs < min ? min : max) == 1 ? "" : "s", |
---|
379 | n/a | Py_SAFE_DOWNCAST(nargs, Py_ssize_t, long)); |
---|
380 | n/a | else |
---|
381 | n/a | PyErr_SetString(PyExc_TypeError, message); |
---|
382 | n/a | return cleanreturn(0, &freelist); |
---|
383 | n/a | } |
---|
384 | n/a | |
---|
385 | n/a | for (i = 0; i < nargs; i++) { |
---|
386 | n/a | if (*format == '|') |
---|
387 | n/a | format++; |
---|
388 | n/a | msg = convertitem(stack[i], &format, p_va, |
---|
389 | n/a | flags, levels, msgbuf, |
---|
390 | n/a | sizeof(msgbuf), &freelist); |
---|
391 | n/a | if (msg) { |
---|
392 | n/a | seterror(i+1, msg, levels, fname, message); |
---|
393 | n/a | return cleanreturn(0, &freelist); |
---|
394 | n/a | } |
---|
395 | n/a | } |
---|
396 | n/a | |
---|
397 | n/a | if (*format != '\0' && !Py_ISALPHA(Py_CHARMASK(*format)) && |
---|
398 | n/a | *format != '(' && |
---|
399 | n/a | *format != '|' && *format != ':' && *format != ';') { |
---|
400 | n/a | PyErr_Format(PyExc_SystemError, |
---|
401 | n/a | "bad format string: %.200s", formatsave); |
---|
402 | n/a | return cleanreturn(0, &freelist); |
---|
403 | n/a | } |
---|
404 | n/a | |
---|
405 | n/a | return cleanreturn(1, &freelist); |
---|
406 | n/a | } |
---|
407 | n/a | |
---|
408 | n/a | static int |
---|
409 | n/a | vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags) |
---|
410 | n/a | { |
---|
411 | n/a | PyObject **stack; |
---|
412 | n/a | Py_ssize_t nargs; |
---|
413 | n/a | |
---|
414 | n/a | if (!(flags & FLAG_COMPAT)) { |
---|
415 | n/a | assert(args != NULL); |
---|
416 | n/a | |
---|
417 | n/a | if (!PyTuple_Check(args)) { |
---|
418 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
419 | n/a | "new style getargs format but argument is not a tuple"); |
---|
420 | n/a | return 0; |
---|
421 | n/a | } |
---|
422 | n/a | |
---|
423 | n/a | stack = &PyTuple_GET_ITEM(args, 0); |
---|
424 | n/a | nargs = PyTuple_GET_SIZE(args); |
---|
425 | n/a | } |
---|
426 | n/a | else { |
---|
427 | n/a | stack = NULL; |
---|
428 | n/a | nargs = 0; |
---|
429 | n/a | } |
---|
430 | n/a | |
---|
431 | n/a | return vgetargs1_impl(args, stack, nargs, format, p_va, flags); |
---|
432 | n/a | } |
---|
433 | n/a | |
---|
434 | n/a | |
---|
435 | n/a | static void |
---|
436 | n/a | seterror(Py_ssize_t iarg, const char *msg, int *levels, const char *fname, |
---|
437 | n/a | const char *message) |
---|
438 | n/a | { |
---|
439 | n/a | char buf[512]; |
---|
440 | n/a | int i; |
---|
441 | n/a | char *p = buf; |
---|
442 | n/a | |
---|
443 | n/a | if (PyErr_Occurred()) |
---|
444 | n/a | return; |
---|
445 | n/a | else if (message == NULL) { |
---|
446 | n/a | if (fname != NULL) { |
---|
447 | n/a | PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname); |
---|
448 | n/a | p += strlen(p); |
---|
449 | n/a | } |
---|
450 | n/a | if (iarg != 0) { |
---|
451 | n/a | PyOS_snprintf(p, sizeof(buf) - (p - buf), |
---|
452 | n/a | "argument %" PY_FORMAT_SIZE_T "d", iarg); |
---|
453 | n/a | i = 0; |
---|
454 | n/a | p += strlen(p); |
---|
455 | n/a | while (i < 32 && levels[i] > 0 && (int)(p-buf) < 220) { |
---|
456 | n/a | PyOS_snprintf(p, sizeof(buf) - (p - buf), |
---|
457 | n/a | ", item %d", levels[i]-1); |
---|
458 | n/a | p += strlen(p); |
---|
459 | n/a | i++; |
---|
460 | n/a | } |
---|
461 | n/a | } |
---|
462 | n/a | else { |
---|
463 | n/a | PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument"); |
---|
464 | n/a | p += strlen(p); |
---|
465 | n/a | } |
---|
466 | n/a | PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg); |
---|
467 | n/a | message = buf; |
---|
468 | n/a | } |
---|
469 | n/a | if (msg[0] == '(') { |
---|
470 | n/a | PyErr_SetString(PyExc_SystemError, message); |
---|
471 | n/a | } |
---|
472 | n/a | else { |
---|
473 | n/a | PyErr_SetString(PyExc_TypeError, message); |
---|
474 | n/a | } |
---|
475 | n/a | } |
---|
476 | n/a | |
---|
477 | n/a | |
---|
478 | n/a | /* Convert a tuple argument. |
---|
479 | n/a | On entry, *p_format points to the character _after_ the opening '('. |
---|
480 | n/a | On successful exit, *p_format points to the closing ')'. |
---|
481 | n/a | If successful: |
---|
482 | n/a | *p_format and *p_va are updated, |
---|
483 | n/a | *levels and *msgbuf are untouched, |
---|
484 | n/a | and NULL is returned. |
---|
485 | n/a | If the argument is invalid: |
---|
486 | n/a | *p_format is unchanged, |
---|
487 | n/a | *p_va is undefined, |
---|
488 | n/a | *levels is a 0-terminated list of item numbers, |
---|
489 | n/a | *msgbuf contains an error message, whose format is: |
---|
490 | n/a | "must be <typename1>, not <typename2>", where: |
---|
491 | n/a | <typename1> is the name of the expected type, and |
---|
492 | n/a | <typename2> is the name of the actual type, |
---|
493 | n/a | and msgbuf is returned. |
---|
494 | n/a | */ |
---|
495 | n/a | |
---|
496 | n/a | static const char * |
---|
497 | n/a | converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags, |
---|
498 | n/a | int *levels, char *msgbuf, size_t bufsize, int toplevel, |
---|
499 | n/a | freelist_t *freelist) |
---|
500 | n/a | { |
---|
501 | n/a | int level = 0; |
---|
502 | n/a | int n = 0; |
---|
503 | n/a | const char *format = *p_format; |
---|
504 | n/a | int i; |
---|
505 | n/a | Py_ssize_t len; |
---|
506 | n/a | |
---|
507 | n/a | for (;;) { |
---|
508 | n/a | int c = *format++; |
---|
509 | n/a | if (c == '(') { |
---|
510 | n/a | if (level == 0) |
---|
511 | n/a | n++; |
---|
512 | n/a | level++; |
---|
513 | n/a | } |
---|
514 | n/a | else if (c == ')') { |
---|
515 | n/a | if (level == 0) |
---|
516 | n/a | break; |
---|
517 | n/a | level--; |
---|
518 | n/a | } |
---|
519 | n/a | else if (c == ':' || c == ';' || c == '\0') |
---|
520 | n/a | break; |
---|
521 | n/a | else if (level == 0 && Py_ISALPHA(Py_CHARMASK(c))) |
---|
522 | n/a | n++; |
---|
523 | n/a | } |
---|
524 | n/a | |
---|
525 | n/a | if (!PySequence_Check(arg) || PyBytes_Check(arg)) { |
---|
526 | n/a | levels[0] = 0; |
---|
527 | n/a | PyOS_snprintf(msgbuf, bufsize, |
---|
528 | n/a | toplevel ? "expected %d arguments, not %.50s" : |
---|
529 | n/a | "must be %d-item sequence, not %.50s", |
---|
530 | n/a | n, |
---|
531 | n/a | arg == Py_None ? "None" : arg->ob_type->tp_name); |
---|
532 | n/a | return msgbuf; |
---|
533 | n/a | } |
---|
534 | n/a | |
---|
535 | n/a | len = PySequence_Size(arg); |
---|
536 | n/a | if (len != n) { |
---|
537 | n/a | levels[0] = 0; |
---|
538 | n/a | if (toplevel) { |
---|
539 | n/a | PyOS_snprintf(msgbuf, bufsize, |
---|
540 | n/a | "expected %d arguments, not %" PY_FORMAT_SIZE_T "d", |
---|
541 | n/a | n, len); |
---|
542 | n/a | } |
---|
543 | n/a | else { |
---|
544 | n/a | PyOS_snprintf(msgbuf, bufsize, |
---|
545 | n/a | "must be sequence of length %d, " |
---|
546 | n/a | "not %" PY_FORMAT_SIZE_T "d", |
---|
547 | n/a | n, len); |
---|
548 | n/a | } |
---|
549 | n/a | return msgbuf; |
---|
550 | n/a | } |
---|
551 | n/a | |
---|
552 | n/a | format = *p_format; |
---|
553 | n/a | for (i = 0; i < n; i++) { |
---|
554 | n/a | const char *msg; |
---|
555 | n/a | PyObject *item; |
---|
556 | n/a | item = PySequence_GetItem(arg, i); |
---|
557 | n/a | if (item == NULL) { |
---|
558 | n/a | PyErr_Clear(); |
---|
559 | n/a | levels[0] = i+1; |
---|
560 | n/a | levels[1] = 0; |
---|
561 | n/a | strncpy(msgbuf, "is not retrievable", bufsize); |
---|
562 | n/a | return msgbuf; |
---|
563 | n/a | } |
---|
564 | n/a | msg = convertitem(item, &format, p_va, flags, levels+1, |
---|
565 | n/a | msgbuf, bufsize, freelist); |
---|
566 | n/a | /* PySequence_GetItem calls tp->sq_item, which INCREFs */ |
---|
567 | n/a | Py_XDECREF(item); |
---|
568 | n/a | if (msg != NULL) { |
---|
569 | n/a | levels[0] = i+1; |
---|
570 | n/a | return msg; |
---|
571 | n/a | } |
---|
572 | n/a | } |
---|
573 | n/a | |
---|
574 | n/a | *p_format = format; |
---|
575 | n/a | return NULL; |
---|
576 | n/a | } |
---|
577 | n/a | |
---|
578 | n/a | |
---|
579 | n/a | /* Convert a single item. */ |
---|
580 | n/a | |
---|
581 | n/a | static const char * |
---|
582 | n/a | convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags, |
---|
583 | n/a | int *levels, char *msgbuf, size_t bufsize, freelist_t *freelist) |
---|
584 | n/a | { |
---|
585 | n/a | const char *msg; |
---|
586 | n/a | const char *format = *p_format; |
---|
587 | n/a | |
---|
588 | n/a | if (*format == '(' /* ')' */) { |
---|
589 | n/a | format++; |
---|
590 | n/a | msg = converttuple(arg, &format, p_va, flags, levels, msgbuf, |
---|
591 | n/a | bufsize, 0, freelist); |
---|
592 | n/a | if (msg == NULL) |
---|
593 | n/a | format++; |
---|
594 | n/a | } |
---|
595 | n/a | else { |
---|
596 | n/a | msg = convertsimple(arg, &format, p_va, flags, |
---|
597 | n/a | msgbuf, bufsize, freelist); |
---|
598 | n/a | if (msg != NULL) |
---|
599 | n/a | levels[0] = 0; |
---|
600 | n/a | } |
---|
601 | n/a | if (msg == NULL) |
---|
602 | n/a | *p_format = format; |
---|
603 | n/a | return msg; |
---|
604 | n/a | } |
---|
605 | n/a | |
---|
606 | n/a | |
---|
607 | n/a | |
---|
608 | n/a | /* Format an error message generated by convertsimple(). */ |
---|
609 | n/a | |
---|
610 | n/a | static const char * |
---|
611 | n/a | converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize) |
---|
612 | n/a | { |
---|
613 | n/a | assert(expected != NULL); |
---|
614 | n/a | assert(arg != NULL); |
---|
615 | n/a | if (expected[0] == '(') { |
---|
616 | n/a | PyOS_snprintf(msgbuf, bufsize, |
---|
617 | n/a | "%.100s", expected); |
---|
618 | n/a | } |
---|
619 | n/a | else { |
---|
620 | n/a | PyOS_snprintf(msgbuf, bufsize, |
---|
621 | n/a | "must be %.50s, not %.50s", expected, |
---|
622 | n/a | arg == Py_None ? "None" : arg->ob_type->tp_name); |
---|
623 | n/a | } |
---|
624 | n/a | return msgbuf; |
---|
625 | n/a | } |
---|
626 | n/a | |
---|
627 | n/a | #define CONV_UNICODE "(unicode conversion error)" |
---|
628 | n/a | |
---|
629 | n/a | /* Explicitly check for float arguments when integers are expected. |
---|
630 | n/a | Return 1 for error, 0 if ok. */ |
---|
631 | n/a | static int |
---|
632 | n/a | float_argument_error(PyObject *arg) |
---|
633 | n/a | { |
---|
634 | n/a | if (PyFloat_Check(arg)) { |
---|
635 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
636 | n/a | "integer argument expected, got float" ); |
---|
637 | n/a | return 1; |
---|
638 | n/a | } |
---|
639 | n/a | else |
---|
640 | n/a | return 0; |
---|
641 | n/a | } |
---|
642 | n/a | |
---|
643 | n/a | /* Convert a non-tuple argument. Return NULL if conversion went OK, |
---|
644 | n/a | or a string with a message describing the failure. The message is |
---|
645 | n/a | formatted as "must be <desired type>, not <actual type>". |
---|
646 | n/a | When failing, an exception may or may not have been raised. |
---|
647 | n/a | Don't call if a tuple is expected. |
---|
648 | n/a | |
---|
649 | n/a | When you add new format codes, please don't forget poor skipitem() below. |
---|
650 | n/a | */ |
---|
651 | n/a | |
---|
652 | n/a | static const char * |
---|
653 | n/a | convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, |
---|
654 | n/a | char *msgbuf, size_t bufsize, freelist_t *freelist) |
---|
655 | n/a | { |
---|
656 | n/a | /* For # codes */ |
---|
657 | n/a | #define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\ |
---|
658 | n/a | if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \ |
---|
659 | n/a | else q=va_arg(*p_va, int*); |
---|
660 | n/a | #define STORE_SIZE(s) \ |
---|
661 | n/a | if (flags & FLAG_SIZE_T) \ |
---|
662 | n/a | *q2=s; \ |
---|
663 | n/a | else { \ |
---|
664 | n/a | if (INT_MAX < s) { \ |
---|
665 | n/a | PyErr_SetString(PyExc_OverflowError, \ |
---|
666 | n/a | "size does not fit in an int"); \ |
---|
667 | n/a | return converterr("", arg, msgbuf, bufsize); \ |
---|
668 | n/a | } \ |
---|
669 | n/a | *q = (int)s; \ |
---|
670 | n/a | } |
---|
671 | n/a | #define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q) |
---|
672 | n/a | #define RETURN_ERR_OCCURRED return msgbuf |
---|
673 | n/a | |
---|
674 | n/a | const char *format = *p_format; |
---|
675 | n/a | char c = *format++; |
---|
676 | n/a | const char *sarg; |
---|
677 | n/a | |
---|
678 | n/a | switch (c) { |
---|
679 | n/a | |
---|
680 | n/a | case 'b': { /* unsigned byte -- very short int */ |
---|
681 | n/a | char *p = va_arg(*p_va, char *); |
---|
682 | n/a | long ival; |
---|
683 | n/a | if (float_argument_error(arg)) |
---|
684 | n/a | RETURN_ERR_OCCURRED; |
---|
685 | n/a | ival = PyLong_AsLong(arg); |
---|
686 | n/a | if (ival == -1 && PyErr_Occurred()) |
---|
687 | n/a | RETURN_ERR_OCCURRED; |
---|
688 | n/a | else if (ival < 0) { |
---|
689 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
690 | n/a | "unsigned byte integer is less than minimum"); |
---|
691 | n/a | RETURN_ERR_OCCURRED; |
---|
692 | n/a | } |
---|
693 | n/a | else if (ival > UCHAR_MAX) { |
---|
694 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
695 | n/a | "unsigned byte integer is greater than maximum"); |
---|
696 | n/a | RETURN_ERR_OCCURRED; |
---|
697 | n/a | } |
---|
698 | n/a | else |
---|
699 | n/a | *p = (unsigned char) ival; |
---|
700 | n/a | break; |
---|
701 | n/a | } |
---|
702 | n/a | |
---|
703 | n/a | case 'B': {/* byte sized bitfield - both signed and unsigned |
---|
704 | n/a | values allowed */ |
---|
705 | n/a | char *p = va_arg(*p_va, char *); |
---|
706 | n/a | long ival; |
---|
707 | n/a | if (float_argument_error(arg)) |
---|
708 | n/a | RETURN_ERR_OCCURRED; |
---|
709 | n/a | ival = PyLong_AsUnsignedLongMask(arg); |
---|
710 | n/a | if (ival == -1 && PyErr_Occurred()) |
---|
711 | n/a | RETURN_ERR_OCCURRED; |
---|
712 | n/a | else |
---|
713 | n/a | *p = (unsigned char) ival; |
---|
714 | n/a | break; |
---|
715 | n/a | } |
---|
716 | n/a | |
---|
717 | n/a | case 'h': {/* signed short int */ |
---|
718 | n/a | short *p = va_arg(*p_va, short *); |
---|
719 | n/a | long ival; |
---|
720 | n/a | if (float_argument_error(arg)) |
---|
721 | n/a | RETURN_ERR_OCCURRED; |
---|
722 | n/a | ival = PyLong_AsLong(arg); |
---|
723 | n/a | if (ival == -1 && PyErr_Occurred()) |
---|
724 | n/a | RETURN_ERR_OCCURRED; |
---|
725 | n/a | else if (ival < SHRT_MIN) { |
---|
726 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
727 | n/a | "signed short integer is less than minimum"); |
---|
728 | n/a | RETURN_ERR_OCCURRED; |
---|
729 | n/a | } |
---|
730 | n/a | else if (ival > SHRT_MAX) { |
---|
731 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
732 | n/a | "signed short integer is greater than maximum"); |
---|
733 | n/a | RETURN_ERR_OCCURRED; |
---|
734 | n/a | } |
---|
735 | n/a | else |
---|
736 | n/a | *p = (short) ival; |
---|
737 | n/a | break; |
---|
738 | n/a | } |
---|
739 | n/a | |
---|
740 | n/a | case 'H': { /* short int sized bitfield, both signed and |
---|
741 | n/a | unsigned allowed */ |
---|
742 | n/a | unsigned short *p = va_arg(*p_va, unsigned short *); |
---|
743 | n/a | long ival; |
---|
744 | n/a | if (float_argument_error(arg)) |
---|
745 | n/a | RETURN_ERR_OCCURRED; |
---|
746 | n/a | ival = PyLong_AsUnsignedLongMask(arg); |
---|
747 | n/a | if (ival == -1 && PyErr_Occurred()) |
---|
748 | n/a | RETURN_ERR_OCCURRED; |
---|
749 | n/a | else |
---|
750 | n/a | *p = (unsigned short) ival; |
---|
751 | n/a | break; |
---|
752 | n/a | } |
---|
753 | n/a | |
---|
754 | n/a | case 'i': {/* signed int */ |
---|
755 | n/a | int *p = va_arg(*p_va, int *); |
---|
756 | n/a | long ival; |
---|
757 | n/a | if (float_argument_error(arg)) |
---|
758 | n/a | RETURN_ERR_OCCURRED; |
---|
759 | n/a | ival = PyLong_AsLong(arg); |
---|
760 | n/a | if (ival == -1 && PyErr_Occurred()) |
---|
761 | n/a | RETURN_ERR_OCCURRED; |
---|
762 | n/a | else if (ival > INT_MAX) { |
---|
763 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
764 | n/a | "signed integer is greater than maximum"); |
---|
765 | n/a | RETURN_ERR_OCCURRED; |
---|
766 | n/a | } |
---|
767 | n/a | else if (ival < INT_MIN) { |
---|
768 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
769 | n/a | "signed integer is less than minimum"); |
---|
770 | n/a | RETURN_ERR_OCCURRED; |
---|
771 | n/a | } |
---|
772 | n/a | else |
---|
773 | n/a | *p = ival; |
---|
774 | n/a | break; |
---|
775 | n/a | } |
---|
776 | n/a | |
---|
777 | n/a | case 'I': { /* int sized bitfield, both signed and |
---|
778 | n/a | unsigned allowed */ |
---|
779 | n/a | unsigned int *p = va_arg(*p_va, unsigned int *); |
---|
780 | n/a | unsigned int ival; |
---|
781 | n/a | if (float_argument_error(arg)) |
---|
782 | n/a | RETURN_ERR_OCCURRED; |
---|
783 | n/a | ival = (unsigned int)PyLong_AsUnsignedLongMask(arg); |
---|
784 | n/a | if (ival == (unsigned int)-1 && PyErr_Occurred()) |
---|
785 | n/a | RETURN_ERR_OCCURRED; |
---|
786 | n/a | else |
---|
787 | n/a | *p = ival; |
---|
788 | n/a | break; |
---|
789 | n/a | } |
---|
790 | n/a | |
---|
791 | n/a | case 'n': /* Py_ssize_t */ |
---|
792 | n/a | { |
---|
793 | n/a | PyObject *iobj; |
---|
794 | n/a | Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *); |
---|
795 | n/a | Py_ssize_t ival = -1; |
---|
796 | n/a | if (float_argument_error(arg)) |
---|
797 | n/a | RETURN_ERR_OCCURRED; |
---|
798 | n/a | iobj = PyNumber_Index(arg); |
---|
799 | n/a | if (iobj != NULL) { |
---|
800 | n/a | ival = PyLong_AsSsize_t(iobj); |
---|
801 | n/a | Py_DECREF(iobj); |
---|
802 | n/a | } |
---|
803 | n/a | if (ival == -1 && PyErr_Occurred()) |
---|
804 | n/a | RETURN_ERR_OCCURRED; |
---|
805 | n/a | *p = ival; |
---|
806 | n/a | break; |
---|
807 | n/a | } |
---|
808 | n/a | case 'l': {/* long int */ |
---|
809 | n/a | long *p = va_arg(*p_va, long *); |
---|
810 | n/a | long ival; |
---|
811 | n/a | if (float_argument_error(arg)) |
---|
812 | n/a | RETURN_ERR_OCCURRED; |
---|
813 | n/a | ival = PyLong_AsLong(arg); |
---|
814 | n/a | if (ival == -1 && PyErr_Occurred()) |
---|
815 | n/a | RETURN_ERR_OCCURRED; |
---|
816 | n/a | else |
---|
817 | n/a | *p = ival; |
---|
818 | n/a | break; |
---|
819 | n/a | } |
---|
820 | n/a | |
---|
821 | n/a | case 'k': { /* long sized bitfield */ |
---|
822 | n/a | unsigned long *p = va_arg(*p_va, unsigned long *); |
---|
823 | n/a | unsigned long ival; |
---|
824 | n/a | if (PyLong_Check(arg)) |
---|
825 | n/a | ival = PyLong_AsUnsignedLongMask(arg); |
---|
826 | n/a | else |
---|
827 | n/a | return converterr("int", arg, msgbuf, bufsize); |
---|
828 | n/a | *p = ival; |
---|
829 | n/a | break; |
---|
830 | n/a | } |
---|
831 | n/a | |
---|
832 | n/a | case 'L': {/* long long */ |
---|
833 | n/a | long long *p = va_arg( *p_va, long long * ); |
---|
834 | n/a | long long ival; |
---|
835 | n/a | if (float_argument_error(arg)) |
---|
836 | n/a | RETURN_ERR_OCCURRED; |
---|
837 | n/a | ival = PyLong_AsLongLong(arg); |
---|
838 | n/a | if (ival == (long long)-1 && PyErr_Occurred()) |
---|
839 | n/a | RETURN_ERR_OCCURRED; |
---|
840 | n/a | else |
---|
841 | n/a | *p = ival; |
---|
842 | n/a | break; |
---|
843 | n/a | } |
---|
844 | n/a | |
---|
845 | n/a | case 'K': { /* long long sized bitfield */ |
---|
846 | n/a | unsigned long long *p = va_arg(*p_va, unsigned long long *); |
---|
847 | n/a | unsigned long long ival; |
---|
848 | n/a | if (PyLong_Check(arg)) |
---|
849 | n/a | ival = PyLong_AsUnsignedLongLongMask(arg); |
---|
850 | n/a | else |
---|
851 | n/a | return converterr("int", arg, msgbuf, bufsize); |
---|
852 | n/a | *p = ival; |
---|
853 | n/a | break; |
---|
854 | n/a | } |
---|
855 | n/a | |
---|
856 | n/a | case 'f': {/* float */ |
---|
857 | n/a | float *p = va_arg(*p_va, float *); |
---|
858 | n/a | double dval = PyFloat_AsDouble(arg); |
---|
859 | n/a | if (PyErr_Occurred()) |
---|
860 | n/a | RETURN_ERR_OCCURRED; |
---|
861 | n/a | else |
---|
862 | n/a | *p = (float) dval; |
---|
863 | n/a | break; |
---|
864 | n/a | } |
---|
865 | n/a | |
---|
866 | n/a | case 'd': {/* double */ |
---|
867 | n/a | double *p = va_arg(*p_va, double *); |
---|
868 | n/a | double dval = PyFloat_AsDouble(arg); |
---|
869 | n/a | if (PyErr_Occurred()) |
---|
870 | n/a | RETURN_ERR_OCCURRED; |
---|
871 | n/a | else |
---|
872 | n/a | *p = dval; |
---|
873 | n/a | break; |
---|
874 | n/a | } |
---|
875 | n/a | |
---|
876 | n/a | case 'D': {/* complex double */ |
---|
877 | n/a | Py_complex *p = va_arg(*p_va, Py_complex *); |
---|
878 | n/a | Py_complex cval; |
---|
879 | n/a | cval = PyComplex_AsCComplex(arg); |
---|
880 | n/a | if (PyErr_Occurred()) |
---|
881 | n/a | RETURN_ERR_OCCURRED; |
---|
882 | n/a | else |
---|
883 | n/a | *p = cval; |
---|
884 | n/a | break; |
---|
885 | n/a | } |
---|
886 | n/a | |
---|
887 | n/a | case 'c': {/* char */ |
---|
888 | n/a | char *p = va_arg(*p_va, char *); |
---|
889 | n/a | if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1) |
---|
890 | n/a | *p = PyBytes_AS_STRING(arg)[0]; |
---|
891 | n/a | else if (PyByteArray_Check(arg) && PyByteArray_Size(arg) == 1) |
---|
892 | n/a | *p = PyByteArray_AS_STRING(arg)[0]; |
---|
893 | n/a | else |
---|
894 | n/a | return converterr("a byte string of length 1", arg, msgbuf, bufsize); |
---|
895 | n/a | break; |
---|
896 | n/a | } |
---|
897 | n/a | |
---|
898 | n/a | case 'C': {/* unicode char */ |
---|
899 | n/a | int *p = va_arg(*p_va, int *); |
---|
900 | n/a | int kind; |
---|
901 | n/a | void *data; |
---|
902 | n/a | |
---|
903 | n/a | if (!PyUnicode_Check(arg)) |
---|
904 | n/a | return converterr("a unicode character", arg, msgbuf, bufsize); |
---|
905 | n/a | |
---|
906 | n/a | if (PyUnicode_READY(arg)) |
---|
907 | n/a | RETURN_ERR_OCCURRED; |
---|
908 | n/a | |
---|
909 | n/a | if (PyUnicode_GET_LENGTH(arg) != 1) |
---|
910 | n/a | return converterr("a unicode character", arg, msgbuf, bufsize); |
---|
911 | n/a | |
---|
912 | n/a | kind = PyUnicode_KIND(arg); |
---|
913 | n/a | data = PyUnicode_DATA(arg); |
---|
914 | n/a | *p = PyUnicode_READ(kind, data, 0); |
---|
915 | n/a | break; |
---|
916 | n/a | } |
---|
917 | n/a | |
---|
918 | n/a | case 'p': {/* boolean *p*redicate */ |
---|
919 | n/a | int *p = va_arg(*p_va, int *); |
---|
920 | n/a | int val = PyObject_IsTrue(arg); |
---|
921 | n/a | if (val > 0) |
---|
922 | n/a | *p = 1; |
---|
923 | n/a | else if (val == 0) |
---|
924 | n/a | *p = 0; |
---|
925 | n/a | else |
---|
926 | n/a | RETURN_ERR_OCCURRED; |
---|
927 | n/a | break; |
---|
928 | n/a | } |
---|
929 | n/a | |
---|
930 | n/a | /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w' codes all |
---|
931 | n/a | need to be cleaned up! */ |
---|
932 | n/a | |
---|
933 | n/a | case 'y': {/* any bytes-like object */ |
---|
934 | n/a | void **p = (void **)va_arg(*p_va, char **); |
---|
935 | n/a | const char *buf; |
---|
936 | n/a | Py_ssize_t count; |
---|
937 | n/a | if (*format == '*') { |
---|
938 | n/a | if (getbuffer(arg, (Py_buffer*)p, &buf) < 0) |
---|
939 | n/a | return converterr(buf, arg, msgbuf, bufsize); |
---|
940 | n/a | format++; |
---|
941 | n/a | if (addcleanup(p, freelist, cleanup_buffer)) { |
---|
942 | n/a | return converterr( |
---|
943 | n/a | "(cleanup problem)", |
---|
944 | n/a | arg, msgbuf, bufsize); |
---|
945 | n/a | } |
---|
946 | n/a | break; |
---|
947 | n/a | } |
---|
948 | n/a | count = convertbuffer(arg, (const void **)p, &buf); |
---|
949 | n/a | if (count < 0) |
---|
950 | n/a | return converterr(buf, arg, msgbuf, bufsize); |
---|
951 | n/a | if (*format == '#') { |
---|
952 | n/a | FETCH_SIZE; |
---|
953 | n/a | STORE_SIZE(count); |
---|
954 | n/a | format++; |
---|
955 | n/a | } else { |
---|
956 | n/a | if (strlen(*p) != (size_t)count) { |
---|
957 | n/a | PyErr_SetString(PyExc_ValueError, "embedded null byte"); |
---|
958 | n/a | RETURN_ERR_OCCURRED; |
---|
959 | n/a | } |
---|
960 | n/a | } |
---|
961 | n/a | break; |
---|
962 | n/a | } |
---|
963 | n/a | |
---|
964 | n/a | case 's': /* text string or bytes-like object */ |
---|
965 | n/a | case 'z': /* text string, bytes-like object or None */ |
---|
966 | n/a | { |
---|
967 | n/a | if (*format == '*') { |
---|
968 | n/a | /* "s*" or "z*" */ |
---|
969 | n/a | Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *); |
---|
970 | n/a | |
---|
971 | n/a | if (c == 'z' && arg == Py_None) |
---|
972 | n/a | PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0); |
---|
973 | n/a | else if (PyUnicode_Check(arg)) { |
---|
974 | n/a | Py_ssize_t len; |
---|
975 | n/a | sarg = PyUnicode_AsUTF8AndSize(arg, &len); |
---|
976 | n/a | if (sarg == NULL) |
---|
977 | n/a | return converterr(CONV_UNICODE, |
---|
978 | n/a | arg, msgbuf, bufsize); |
---|
979 | n/a | PyBuffer_FillInfo(p, arg, (void *)sarg, len, 1, 0); |
---|
980 | n/a | } |
---|
981 | n/a | else { /* any bytes-like object */ |
---|
982 | n/a | const char *buf; |
---|
983 | n/a | if (getbuffer(arg, p, &buf) < 0) |
---|
984 | n/a | return converterr(buf, arg, msgbuf, bufsize); |
---|
985 | n/a | } |
---|
986 | n/a | if (addcleanup(p, freelist, cleanup_buffer)) { |
---|
987 | n/a | return converterr( |
---|
988 | n/a | "(cleanup problem)", |
---|
989 | n/a | arg, msgbuf, bufsize); |
---|
990 | n/a | } |
---|
991 | n/a | format++; |
---|
992 | n/a | } else if (*format == '#') { /* a string or read-only bytes-like object */ |
---|
993 | n/a | /* "s#" or "z#" */ |
---|
994 | n/a | const void **p = (const void **)va_arg(*p_va, const char **); |
---|
995 | n/a | FETCH_SIZE; |
---|
996 | n/a | |
---|
997 | n/a | if (c == 'z' && arg == Py_None) { |
---|
998 | n/a | *p = NULL; |
---|
999 | n/a | STORE_SIZE(0); |
---|
1000 | n/a | } |
---|
1001 | n/a | else if (PyUnicode_Check(arg)) { |
---|
1002 | n/a | Py_ssize_t len; |
---|
1003 | n/a | sarg = PyUnicode_AsUTF8AndSize(arg, &len); |
---|
1004 | n/a | if (sarg == NULL) |
---|
1005 | n/a | return converterr(CONV_UNICODE, |
---|
1006 | n/a | arg, msgbuf, bufsize); |
---|
1007 | n/a | *p = sarg; |
---|
1008 | n/a | STORE_SIZE(len); |
---|
1009 | n/a | } |
---|
1010 | n/a | else { /* read-only bytes-like object */ |
---|
1011 | n/a | /* XXX Really? */ |
---|
1012 | n/a | const char *buf; |
---|
1013 | n/a | Py_ssize_t count = convertbuffer(arg, p, &buf); |
---|
1014 | n/a | if (count < 0) |
---|
1015 | n/a | return converterr(buf, arg, msgbuf, bufsize); |
---|
1016 | n/a | STORE_SIZE(count); |
---|
1017 | n/a | } |
---|
1018 | n/a | format++; |
---|
1019 | n/a | } else { |
---|
1020 | n/a | /* "s" or "z" */ |
---|
1021 | n/a | const char **p = va_arg(*p_va, const char **); |
---|
1022 | n/a | Py_ssize_t len; |
---|
1023 | n/a | sarg = NULL; |
---|
1024 | n/a | |
---|
1025 | n/a | if (c == 'z' && arg == Py_None) |
---|
1026 | n/a | *p = NULL; |
---|
1027 | n/a | else if (PyUnicode_Check(arg)) { |
---|
1028 | n/a | sarg = PyUnicode_AsUTF8AndSize(arg, &len); |
---|
1029 | n/a | if (sarg == NULL) |
---|
1030 | n/a | return converterr(CONV_UNICODE, |
---|
1031 | n/a | arg, msgbuf, bufsize); |
---|
1032 | n/a | if (strlen(sarg) != (size_t)len) { |
---|
1033 | n/a | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
---|
1034 | n/a | RETURN_ERR_OCCURRED; |
---|
1035 | n/a | } |
---|
1036 | n/a | *p = sarg; |
---|
1037 | n/a | } |
---|
1038 | n/a | else |
---|
1039 | n/a | return converterr(c == 'z' ? "str or None" : "str", |
---|
1040 | n/a | arg, msgbuf, bufsize); |
---|
1041 | n/a | } |
---|
1042 | n/a | break; |
---|
1043 | n/a | } |
---|
1044 | n/a | |
---|
1045 | n/a | case 'u': /* raw unicode buffer (Py_UNICODE *) */ |
---|
1046 | n/a | case 'Z': /* raw unicode buffer or None */ |
---|
1047 | n/a | { |
---|
1048 | n/a | Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **); |
---|
1049 | n/a | |
---|
1050 | n/a | if (*format == '#') { |
---|
1051 | n/a | /* "u#" or "Z#" */ |
---|
1052 | n/a | FETCH_SIZE; |
---|
1053 | n/a | |
---|
1054 | n/a | if (c == 'Z' && arg == Py_None) { |
---|
1055 | n/a | *p = NULL; |
---|
1056 | n/a | STORE_SIZE(0); |
---|
1057 | n/a | } |
---|
1058 | n/a | else if (PyUnicode_Check(arg)) { |
---|
1059 | n/a | Py_ssize_t len; |
---|
1060 | n/a | *p = PyUnicode_AsUnicodeAndSize(arg, &len); |
---|
1061 | n/a | if (*p == NULL) |
---|
1062 | n/a | RETURN_ERR_OCCURRED; |
---|
1063 | n/a | STORE_SIZE(len); |
---|
1064 | n/a | } |
---|
1065 | n/a | else |
---|
1066 | n/a | return converterr(c == 'Z' ? "str or None" : "str", |
---|
1067 | n/a | arg, msgbuf, bufsize); |
---|
1068 | n/a | format++; |
---|
1069 | n/a | } else { |
---|
1070 | n/a | /* "u" or "Z" */ |
---|
1071 | n/a | if (c == 'Z' && arg == Py_None) |
---|
1072 | n/a | *p = NULL; |
---|
1073 | n/a | else if (PyUnicode_Check(arg)) { |
---|
1074 | n/a | Py_ssize_t len; |
---|
1075 | n/a | *p = PyUnicode_AsUnicodeAndSize(arg, &len); |
---|
1076 | n/a | if (*p == NULL) |
---|
1077 | n/a | RETURN_ERR_OCCURRED; |
---|
1078 | n/a | if (wcslen(*p) != (size_t)len) { |
---|
1079 | n/a | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
---|
1080 | n/a | RETURN_ERR_OCCURRED; |
---|
1081 | n/a | } |
---|
1082 | n/a | } else |
---|
1083 | n/a | return converterr(c == 'Z' ? "str or None" : "str", |
---|
1084 | n/a | arg, msgbuf, bufsize); |
---|
1085 | n/a | } |
---|
1086 | n/a | break; |
---|
1087 | n/a | } |
---|
1088 | n/a | |
---|
1089 | n/a | case 'e': {/* encoded string */ |
---|
1090 | n/a | char **buffer; |
---|
1091 | n/a | const char *encoding; |
---|
1092 | n/a | PyObject *s; |
---|
1093 | n/a | int recode_strings; |
---|
1094 | n/a | Py_ssize_t size; |
---|
1095 | n/a | const char *ptr; |
---|
1096 | n/a | |
---|
1097 | n/a | /* Get 'e' parameter: the encoding name */ |
---|
1098 | n/a | encoding = (const char *)va_arg(*p_va, const char *); |
---|
1099 | n/a | if (encoding == NULL) |
---|
1100 | n/a | encoding = PyUnicode_GetDefaultEncoding(); |
---|
1101 | n/a | |
---|
1102 | n/a | /* Get output buffer parameter: |
---|
1103 | n/a | 's' (recode all objects via Unicode) or |
---|
1104 | n/a | 't' (only recode non-string objects) |
---|
1105 | n/a | */ |
---|
1106 | n/a | if (*format == 's') |
---|
1107 | n/a | recode_strings = 1; |
---|
1108 | n/a | else if (*format == 't') |
---|
1109 | n/a | recode_strings = 0; |
---|
1110 | n/a | else |
---|
1111 | n/a | return converterr( |
---|
1112 | n/a | "(unknown parser marker combination)", |
---|
1113 | n/a | arg, msgbuf, bufsize); |
---|
1114 | n/a | buffer = (char **)va_arg(*p_va, char **); |
---|
1115 | n/a | format++; |
---|
1116 | n/a | if (buffer == NULL) |
---|
1117 | n/a | return converterr("(buffer is NULL)", |
---|
1118 | n/a | arg, msgbuf, bufsize); |
---|
1119 | n/a | |
---|
1120 | n/a | /* Encode object */ |
---|
1121 | n/a | if (!recode_strings && |
---|
1122 | n/a | (PyBytes_Check(arg) || PyByteArray_Check(arg))) { |
---|
1123 | n/a | s = arg; |
---|
1124 | n/a | Py_INCREF(s); |
---|
1125 | n/a | if (PyBytes_Check(arg)) { |
---|
1126 | n/a | size = PyBytes_GET_SIZE(s); |
---|
1127 | n/a | ptr = PyBytes_AS_STRING(s); |
---|
1128 | n/a | } |
---|
1129 | n/a | else { |
---|
1130 | n/a | size = PyByteArray_GET_SIZE(s); |
---|
1131 | n/a | ptr = PyByteArray_AS_STRING(s); |
---|
1132 | n/a | } |
---|
1133 | n/a | } |
---|
1134 | n/a | else if (PyUnicode_Check(arg)) { |
---|
1135 | n/a | /* Encode object; use default error handling */ |
---|
1136 | n/a | s = PyUnicode_AsEncodedString(arg, |
---|
1137 | n/a | encoding, |
---|
1138 | n/a | NULL); |
---|
1139 | n/a | if (s == NULL) |
---|
1140 | n/a | return converterr("(encoding failed)", |
---|
1141 | n/a | arg, msgbuf, bufsize); |
---|
1142 | n/a | assert(PyBytes_Check(s)); |
---|
1143 | n/a | size = PyBytes_GET_SIZE(s); |
---|
1144 | n/a | ptr = PyBytes_AS_STRING(s); |
---|
1145 | n/a | if (ptr == NULL) |
---|
1146 | n/a | ptr = ""; |
---|
1147 | n/a | } |
---|
1148 | n/a | else { |
---|
1149 | n/a | return converterr( |
---|
1150 | n/a | recode_strings ? "str" : "str, bytes or bytearray", |
---|
1151 | n/a | arg, msgbuf, bufsize); |
---|
1152 | n/a | } |
---|
1153 | n/a | |
---|
1154 | n/a | /* Write output; output is guaranteed to be 0-terminated */ |
---|
1155 | n/a | if (*format == '#') { |
---|
1156 | n/a | /* Using buffer length parameter '#': |
---|
1157 | n/a | |
---|
1158 | n/a | - if *buffer is NULL, a new buffer of the |
---|
1159 | n/a | needed size is allocated and the data |
---|
1160 | n/a | copied into it; *buffer is updated to point |
---|
1161 | n/a | to the new buffer; the caller is |
---|
1162 | n/a | responsible for PyMem_Free()ing it after |
---|
1163 | n/a | usage |
---|
1164 | n/a | |
---|
1165 | n/a | - if *buffer is not NULL, the data is |
---|
1166 | n/a | copied to *buffer; *buffer_len has to be |
---|
1167 | n/a | set to the size of the buffer on input; |
---|
1168 | n/a | buffer overflow is signalled with an error; |
---|
1169 | n/a | buffer has to provide enough room for the |
---|
1170 | n/a | encoded string plus the trailing 0-byte |
---|
1171 | n/a | |
---|
1172 | n/a | - in both cases, *buffer_len is updated to |
---|
1173 | n/a | the size of the buffer /excluding/ the |
---|
1174 | n/a | trailing 0-byte |
---|
1175 | n/a | |
---|
1176 | n/a | */ |
---|
1177 | n/a | FETCH_SIZE; |
---|
1178 | n/a | |
---|
1179 | n/a | format++; |
---|
1180 | n/a | if (q == NULL && q2 == NULL) { |
---|
1181 | n/a | Py_DECREF(s); |
---|
1182 | n/a | return converterr( |
---|
1183 | n/a | "(buffer_len is NULL)", |
---|
1184 | n/a | arg, msgbuf, bufsize); |
---|
1185 | n/a | } |
---|
1186 | n/a | if (*buffer == NULL) { |
---|
1187 | n/a | *buffer = PyMem_NEW(char, size + 1); |
---|
1188 | n/a | if (*buffer == NULL) { |
---|
1189 | n/a | Py_DECREF(s); |
---|
1190 | n/a | PyErr_NoMemory(); |
---|
1191 | n/a | RETURN_ERR_OCCURRED; |
---|
1192 | n/a | } |
---|
1193 | n/a | if (addcleanup(*buffer, freelist, cleanup_ptr)) { |
---|
1194 | n/a | Py_DECREF(s); |
---|
1195 | n/a | return converterr( |
---|
1196 | n/a | "(cleanup problem)", |
---|
1197 | n/a | arg, msgbuf, bufsize); |
---|
1198 | n/a | } |
---|
1199 | n/a | } else { |
---|
1200 | n/a | if (size + 1 > BUFFER_LEN) { |
---|
1201 | n/a | Py_DECREF(s); |
---|
1202 | n/a | PyErr_Format(PyExc_ValueError, |
---|
1203 | n/a | "encoded string too long " |
---|
1204 | n/a | "(%zd, maximum length %zd)", |
---|
1205 | n/a | (Py_ssize_t)size, (Py_ssize_t)(BUFFER_LEN-1)); |
---|
1206 | n/a | RETURN_ERR_OCCURRED; |
---|
1207 | n/a | } |
---|
1208 | n/a | } |
---|
1209 | n/a | memcpy(*buffer, ptr, size+1); |
---|
1210 | n/a | STORE_SIZE(size); |
---|
1211 | n/a | } else { |
---|
1212 | n/a | /* Using a 0-terminated buffer: |
---|
1213 | n/a | |
---|
1214 | n/a | - the encoded string has to be 0-terminated |
---|
1215 | n/a | for this variant to work; if it is not, an |
---|
1216 | n/a | error raised |
---|
1217 | n/a | |
---|
1218 | n/a | - a new buffer of the needed size is |
---|
1219 | n/a | allocated and the data copied into it; |
---|
1220 | n/a | *buffer is updated to point to the new |
---|
1221 | n/a | buffer; the caller is responsible for |
---|
1222 | n/a | PyMem_Free()ing it after usage |
---|
1223 | n/a | |
---|
1224 | n/a | */ |
---|
1225 | n/a | if ((Py_ssize_t)strlen(ptr) != size) { |
---|
1226 | n/a | Py_DECREF(s); |
---|
1227 | n/a | return converterr( |
---|
1228 | n/a | "encoded string without null bytes", |
---|
1229 | n/a | arg, msgbuf, bufsize); |
---|
1230 | n/a | } |
---|
1231 | n/a | *buffer = PyMem_NEW(char, size + 1); |
---|
1232 | n/a | if (*buffer == NULL) { |
---|
1233 | n/a | Py_DECREF(s); |
---|
1234 | n/a | PyErr_NoMemory(); |
---|
1235 | n/a | RETURN_ERR_OCCURRED; |
---|
1236 | n/a | } |
---|
1237 | n/a | if (addcleanup(*buffer, freelist, cleanup_ptr)) { |
---|
1238 | n/a | Py_DECREF(s); |
---|
1239 | n/a | return converterr("(cleanup problem)", |
---|
1240 | n/a | arg, msgbuf, bufsize); |
---|
1241 | n/a | } |
---|
1242 | n/a | memcpy(*buffer, ptr, size+1); |
---|
1243 | n/a | } |
---|
1244 | n/a | Py_DECREF(s); |
---|
1245 | n/a | break; |
---|
1246 | n/a | } |
---|
1247 | n/a | |
---|
1248 | n/a | case 'S': { /* PyBytes object */ |
---|
1249 | n/a | PyObject **p = va_arg(*p_va, PyObject **); |
---|
1250 | n/a | if (PyBytes_Check(arg)) |
---|
1251 | n/a | *p = arg; |
---|
1252 | n/a | else |
---|
1253 | n/a | return converterr("bytes", arg, msgbuf, bufsize); |
---|
1254 | n/a | break; |
---|
1255 | n/a | } |
---|
1256 | n/a | |
---|
1257 | n/a | case 'Y': { /* PyByteArray object */ |
---|
1258 | n/a | PyObject **p = va_arg(*p_va, PyObject **); |
---|
1259 | n/a | if (PyByteArray_Check(arg)) |
---|
1260 | n/a | *p = arg; |
---|
1261 | n/a | else |
---|
1262 | n/a | return converterr("bytearray", arg, msgbuf, bufsize); |
---|
1263 | n/a | break; |
---|
1264 | n/a | } |
---|
1265 | n/a | |
---|
1266 | n/a | case 'U': { /* PyUnicode object */ |
---|
1267 | n/a | PyObject **p = va_arg(*p_va, PyObject **); |
---|
1268 | n/a | if (PyUnicode_Check(arg)) { |
---|
1269 | n/a | if (PyUnicode_READY(arg) == -1) |
---|
1270 | n/a | RETURN_ERR_OCCURRED; |
---|
1271 | n/a | *p = arg; |
---|
1272 | n/a | } |
---|
1273 | n/a | else |
---|
1274 | n/a | return converterr("str", arg, msgbuf, bufsize); |
---|
1275 | n/a | break; |
---|
1276 | n/a | } |
---|
1277 | n/a | |
---|
1278 | n/a | case 'O': { /* object */ |
---|
1279 | n/a | PyTypeObject *type; |
---|
1280 | n/a | PyObject **p; |
---|
1281 | n/a | if (*format == '!') { |
---|
1282 | n/a | type = va_arg(*p_va, PyTypeObject*); |
---|
1283 | n/a | p = va_arg(*p_va, PyObject **); |
---|
1284 | n/a | format++; |
---|
1285 | n/a | if (PyType_IsSubtype(arg->ob_type, type)) |
---|
1286 | n/a | *p = arg; |
---|
1287 | n/a | else |
---|
1288 | n/a | return converterr(type->tp_name, arg, msgbuf, bufsize); |
---|
1289 | n/a | |
---|
1290 | n/a | } |
---|
1291 | n/a | else if (*format == '&') { |
---|
1292 | n/a | typedef int (*converter)(PyObject *, void *); |
---|
1293 | n/a | converter convert = va_arg(*p_va, converter); |
---|
1294 | n/a | void *addr = va_arg(*p_va, void *); |
---|
1295 | n/a | int res; |
---|
1296 | n/a | format++; |
---|
1297 | n/a | if (! (res = (*convert)(arg, addr))) |
---|
1298 | n/a | return converterr("(unspecified)", |
---|
1299 | n/a | arg, msgbuf, bufsize); |
---|
1300 | n/a | if (res == Py_CLEANUP_SUPPORTED && |
---|
1301 | n/a | addcleanup(addr, freelist, convert) == -1) |
---|
1302 | n/a | return converterr("(cleanup problem)", |
---|
1303 | n/a | arg, msgbuf, bufsize); |
---|
1304 | n/a | } |
---|
1305 | n/a | else { |
---|
1306 | n/a | p = va_arg(*p_va, PyObject **); |
---|
1307 | n/a | *p = arg; |
---|
1308 | n/a | } |
---|
1309 | n/a | break; |
---|
1310 | n/a | } |
---|
1311 | n/a | |
---|
1312 | n/a | |
---|
1313 | n/a | case 'w': { /* "w*": memory buffer, read-write access */ |
---|
1314 | n/a | void **p = va_arg(*p_va, void **); |
---|
1315 | n/a | |
---|
1316 | n/a | if (*format != '*') |
---|
1317 | n/a | return converterr( |
---|
1318 | n/a | "(invalid use of 'w' format character)", |
---|
1319 | n/a | arg, msgbuf, bufsize); |
---|
1320 | n/a | format++; |
---|
1321 | n/a | |
---|
1322 | n/a | /* Caller is interested in Py_buffer, and the object |
---|
1323 | n/a | supports it directly. */ |
---|
1324 | n/a | if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) { |
---|
1325 | n/a | PyErr_Clear(); |
---|
1326 | n/a | return converterr("read-write bytes-like object", |
---|
1327 | n/a | arg, msgbuf, bufsize); |
---|
1328 | n/a | } |
---|
1329 | n/a | if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) { |
---|
1330 | n/a | PyBuffer_Release((Py_buffer*)p); |
---|
1331 | n/a | return converterr("contiguous buffer", arg, msgbuf, bufsize); |
---|
1332 | n/a | } |
---|
1333 | n/a | if (addcleanup(p, freelist, cleanup_buffer)) { |
---|
1334 | n/a | return converterr( |
---|
1335 | n/a | "(cleanup problem)", |
---|
1336 | n/a | arg, msgbuf, bufsize); |
---|
1337 | n/a | } |
---|
1338 | n/a | break; |
---|
1339 | n/a | } |
---|
1340 | n/a | |
---|
1341 | n/a | default: |
---|
1342 | n/a | return converterr("(impossible<bad format char>)", arg, msgbuf, bufsize); |
---|
1343 | n/a | |
---|
1344 | n/a | } |
---|
1345 | n/a | |
---|
1346 | n/a | *p_format = format; |
---|
1347 | n/a | return NULL; |
---|
1348 | n/a | |
---|
1349 | n/a | #undef FETCH_SIZE |
---|
1350 | n/a | #undef STORE_SIZE |
---|
1351 | n/a | #undef BUFFER_LEN |
---|
1352 | n/a | #undef RETURN_ERR_OCCURRED |
---|
1353 | n/a | } |
---|
1354 | n/a | |
---|
1355 | n/a | static Py_ssize_t |
---|
1356 | n/a | convertbuffer(PyObject *arg, const void **p, const char **errmsg) |
---|
1357 | n/a | { |
---|
1358 | n/a | PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer; |
---|
1359 | n/a | Py_ssize_t count; |
---|
1360 | n/a | Py_buffer view; |
---|
1361 | n/a | |
---|
1362 | n/a | *errmsg = NULL; |
---|
1363 | n/a | *p = NULL; |
---|
1364 | n/a | if (pb != NULL && pb->bf_releasebuffer != NULL) { |
---|
1365 | n/a | *errmsg = "read-only bytes-like object"; |
---|
1366 | n/a | return -1; |
---|
1367 | n/a | } |
---|
1368 | n/a | |
---|
1369 | n/a | if (getbuffer(arg, &view, errmsg) < 0) |
---|
1370 | n/a | return -1; |
---|
1371 | n/a | count = view.len; |
---|
1372 | n/a | *p = view.buf; |
---|
1373 | n/a | PyBuffer_Release(&view); |
---|
1374 | n/a | return count; |
---|
1375 | n/a | } |
---|
1376 | n/a | |
---|
1377 | n/a | static int |
---|
1378 | n/a | getbuffer(PyObject *arg, Py_buffer *view, const char **errmsg) |
---|
1379 | n/a | { |
---|
1380 | n/a | if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) { |
---|
1381 | n/a | *errmsg = "bytes-like object"; |
---|
1382 | n/a | return -1; |
---|
1383 | n/a | } |
---|
1384 | n/a | if (!PyBuffer_IsContiguous(view, 'C')) { |
---|
1385 | n/a | PyBuffer_Release(view); |
---|
1386 | n/a | *errmsg = "contiguous buffer"; |
---|
1387 | n/a | return -1; |
---|
1388 | n/a | } |
---|
1389 | n/a | return 0; |
---|
1390 | n/a | } |
---|
1391 | n/a | |
---|
1392 | n/a | /* Support for keyword arguments donated by |
---|
1393 | n/a | Geoff Philbrick <philbric@delphi.hks.com> */ |
---|
1394 | n/a | |
---|
1395 | n/a | /* Return false (0) for error, else true. */ |
---|
1396 | n/a | int |
---|
1397 | n/a | PyArg_ParseTupleAndKeywords(PyObject *args, |
---|
1398 | n/a | PyObject *keywords, |
---|
1399 | n/a | const char *format, |
---|
1400 | n/a | char **kwlist, ...) |
---|
1401 | n/a | { |
---|
1402 | n/a | int retval; |
---|
1403 | n/a | va_list va; |
---|
1404 | n/a | |
---|
1405 | n/a | if ((args == NULL || !PyTuple_Check(args)) || |
---|
1406 | n/a | (keywords != NULL && !PyDict_Check(keywords)) || |
---|
1407 | n/a | format == NULL || |
---|
1408 | n/a | kwlist == NULL) |
---|
1409 | n/a | { |
---|
1410 | n/a | PyErr_BadInternalCall(); |
---|
1411 | n/a | return 0; |
---|
1412 | n/a | } |
---|
1413 | n/a | |
---|
1414 | n/a | va_start(va, kwlist); |
---|
1415 | n/a | retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0); |
---|
1416 | n/a | va_end(va); |
---|
1417 | n/a | return retval; |
---|
1418 | n/a | } |
---|
1419 | n/a | |
---|
1420 | n/a | int |
---|
1421 | n/a | _PyArg_ParseTupleAndKeywords_SizeT(PyObject *args, |
---|
1422 | n/a | PyObject *keywords, |
---|
1423 | n/a | const char *format, |
---|
1424 | n/a | char **kwlist, ...) |
---|
1425 | n/a | { |
---|
1426 | n/a | int retval; |
---|
1427 | n/a | va_list va; |
---|
1428 | n/a | |
---|
1429 | n/a | if ((args == NULL || !PyTuple_Check(args)) || |
---|
1430 | n/a | (keywords != NULL && !PyDict_Check(keywords)) || |
---|
1431 | n/a | format == NULL || |
---|
1432 | n/a | kwlist == NULL) |
---|
1433 | n/a | { |
---|
1434 | n/a | PyErr_BadInternalCall(); |
---|
1435 | n/a | return 0; |
---|
1436 | n/a | } |
---|
1437 | n/a | |
---|
1438 | n/a | va_start(va, kwlist); |
---|
1439 | n/a | retval = vgetargskeywords(args, keywords, format, |
---|
1440 | n/a | kwlist, &va, FLAG_SIZE_T); |
---|
1441 | n/a | va_end(va); |
---|
1442 | n/a | return retval; |
---|
1443 | n/a | } |
---|
1444 | n/a | |
---|
1445 | n/a | |
---|
1446 | n/a | int |
---|
1447 | n/a | PyArg_VaParseTupleAndKeywords(PyObject *args, |
---|
1448 | n/a | PyObject *keywords, |
---|
1449 | n/a | const char *format, |
---|
1450 | n/a | char **kwlist, va_list va) |
---|
1451 | n/a | { |
---|
1452 | n/a | int retval; |
---|
1453 | n/a | va_list lva; |
---|
1454 | n/a | |
---|
1455 | n/a | if ((args == NULL || !PyTuple_Check(args)) || |
---|
1456 | n/a | (keywords != NULL && !PyDict_Check(keywords)) || |
---|
1457 | n/a | format == NULL || |
---|
1458 | n/a | kwlist == NULL) |
---|
1459 | n/a | { |
---|
1460 | n/a | PyErr_BadInternalCall(); |
---|
1461 | n/a | return 0; |
---|
1462 | n/a | } |
---|
1463 | n/a | |
---|
1464 | n/a | va_copy(lva, va); |
---|
1465 | n/a | |
---|
1466 | n/a | retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0); |
---|
1467 | n/a | va_end(lva); |
---|
1468 | n/a | return retval; |
---|
1469 | n/a | } |
---|
1470 | n/a | |
---|
1471 | n/a | int |
---|
1472 | n/a | _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args, |
---|
1473 | n/a | PyObject *keywords, |
---|
1474 | n/a | const char *format, |
---|
1475 | n/a | char **kwlist, va_list va) |
---|
1476 | n/a | { |
---|
1477 | n/a | int retval; |
---|
1478 | n/a | va_list lva; |
---|
1479 | n/a | |
---|
1480 | n/a | if ((args == NULL || !PyTuple_Check(args)) || |
---|
1481 | n/a | (keywords != NULL && !PyDict_Check(keywords)) || |
---|
1482 | n/a | format == NULL || |
---|
1483 | n/a | kwlist == NULL) |
---|
1484 | n/a | { |
---|
1485 | n/a | PyErr_BadInternalCall(); |
---|
1486 | n/a | return 0; |
---|
1487 | n/a | } |
---|
1488 | n/a | |
---|
1489 | n/a | va_copy(lva, va); |
---|
1490 | n/a | |
---|
1491 | n/a | retval = vgetargskeywords(args, keywords, format, |
---|
1492 | n/a | kwlist, &lva, FLAG_SIZE_T); |
---|
1493 | n/a | va_end(lva); |
---|
1494 | n/a | return retval; |
---|
1495 | n/a | } |
---|
1496 | n/a | |
---|
1497 | n/a | int |
---|
1498 | n/a | _PyArg_ParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords, |
---|
1499 | n/a | struct _PyArg_Parser *parser, ...) |
---|
1500 | n/a | { |
---|
1501 | n/a | int retval; |
---|
1502 | n/a | va_list va; |
---|
1503 | n/a | |
---|
1504 | n/a | va_start(va, parser); |
---|
1505 | n/a | retval = vgetargskeywordsfast(args, keywords, parser, &va, 0); |
---|
1506 | n/a | va_end(va); |
---|
1507 | n/a | return retval; |
---|
1508 | n/a | } |
---|
1509 | n/a | |
---|
1510 | n/a | int |
---|
1511 | n/a | _PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords, |
---|
1512 | n/a | struct _PyArg_Parser *parser, ...) |
---|
1513 | n/a | { |
---|
1514 | n/a | int retval; |
---|
1515 | n/a | va_list va; |
---|
1516 | n/a | |
---|
1517 | n/a | va_start(va, parser); |
---|
1518 | n/a | retval = vgetargskeywordsfast(args, keywords, parser, &va, FLAG_SIZE_T); |
---|
1519 | n/a | va_end(va); |
---|
1520 | n/a | return retval; |
---|
1521 | n/a | } |
---|
1522 | n/a | |
---|
1523 | n/a | int |
---|
1524 | n/a | _PyArg_ParseStackAndKeywords(PyObject **args, Py_ssize_t nargs, PyObject *kwnames, |
---|
1525 | n/a | struct _PyArg_Parser *parser, ...) |
---|
1526 | n/a | { |
---|
1527 | n/a | int retval; |
---|
1528 | n/a | va_list va; |
---|
1529 | n/a | |
---|
1530 | n/a | va_start(va, parser); |
---|
1531 | n/a | retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, 0); |
---|
1532 | n/a | va_end(va); |
---|
1533 | n/a | return retval; |
---|
1534 | n/a | } |
---|
1535 | n/a | |
---|
1536 | n/a | int |
---|
1537 | n/a | _PyArg_ParseStackAndKeywords_SizeT(PyObject **args, Py_ssize_t nargs, PyObject *kwnames, |
---|
1538 | n/a | struct _PyArg_Parser *parser, ...) |
---|
1539 | n/a | { |
---|
1540 | n/a | int retval; |
---|
1541 | n/a | va_list va; |
---|
1542 | n/a | |
---|
1543 | n/a | va_start(va, parser); |
---|
1544 | n/a | retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, FLAG_SIZE_T); |
---|
1545 | n/a | va_end(va); |
---|
1546 | n/a | return retval; |
---|
1547 | n/a | } |
---|
1548 | n/a | |
---|
1549 | n/a | |
---|
1550 | n/a | int |
---|
1551 | n/a | _PyArg_VaParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords, |
---|
1552 | n/a | struct _PyArg_Parser *parser, va_list va) |
---|
1553 | n/a | { |
---|
1554 | n/a | int retval; |
---|
1555 | n/a | va_list lva; |
---|
1556 | n/a | |
---|
1557 | n/a | va_copy(lva, va); |
---|
1558 | n/a | |
---|
1559 | n/a | retval = vgetargskeywordsfast(args, keywords, parser, &lva, 0); |
---|
1560 | n/a | va_end(lva); |
---|
1561 | n/a | return retval; |
---|
1562 | n/a | } |
---|
1563 | n/a | |
---|
1564 | n/a | int |
---|
1565 | n/a | _PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords, |
---|
1566 | n/a | struct _PyArg_Parser *parser, va_list va) |
---|
1567 | n/a | { |
---|
1568 | n/a | int retval; |
---|
1569 | n/a | va_list lva; |
---|
1570 | n/a | |
---|
1571 | n/a | va_copy(lva, va); |
---|
1572 | n/a | |
---|
1573 | n/a | retval = vgetargskeywordsfast(args, keywords, parser, &lva, FLAG_SIZE_T); |
---|
1574 | n/a | va_end(lva); |
---|
1575 | n/a | return retval; |
---|
1576 | n/a | } |
---|
1577 | n/a | |
---|
1578 | n/a | int |
---|
1579 | n/a | PyArg_ValidateKeywordArguments(PyObject *kwargs) |
---|
1580 | n/a | { |
---|
1581 | n/a | if (!PyDict_Check(kwargs)) { |
---|
1582 | n/a | PyErr_BadInternalCall(); |
---|
1583 | n/a | return 0; |
---|
1584 | n/a | } |
---|
1585 | n/a | if (!_PyDict_HasOnlyStringKeys(kwargs)) { |
---|
1586 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
1587 | n/a | "keyword arguments must be strings"); |
---|
1588 | n/a | return 0; |
---|
1589 | n/a | } |
---|
1590 | n/a | return 1; |
---|
1591 | n/a | } |
---|
1592 | n/a | |
---|
1593 | n/a | #define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':') |
---|
1594 | n/a | |
---|
1595 | n/a | static int |
---|
1596 | n/a | vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format, |
---|
1597 | n/a | char **kwlist, va_list *p_va, int flags) |
---|
1598 | n/a | { |
---|
1599 | n/a | char msgbuf[512]; |
---|
1600 | n/a | int levels[32]; |
---|
1601 | n/a | const char *fname, *msg, *custom_msg; |
---|
1602 | n/a | int min = INT_MAX; |
---|
1603 | n/a | int max = INT_MAX; |
---|
1604 | n/a | int i, pos, len; |
---|
1605 | n/a | int skip = 0; |
---|
1606 | n/a | Py_ssize_t nargs, nkwargs; |
---|
1607 | n/a | PyObject *current_arg; |
---|
1608 | n/a | freelistentry_t static_entries[STATIC_FREELIST_ENTRIES]; |
---|
1609 | n/a | freelist_t freelist; |
---|
1610 | n/a | |
---|
1611 | n/a | freelist.entries = static_entries; |
---|
1612 | n/a | freelist.first_available = 0; |
---|
1613 | n/a | freelist.entries_malloced = 0; |
---|
1614 | n/a | |
---|
1615 | n/a | assert(args != NULL && PyTuple_Check(args)); |
---|
1616 | n/a | assert(kwargs == NULL || PyDict_Check(kwargs)); |
---|
1617 | n/a | assert(format != NULL); |
---|
1618 | n/a | assert(kwlist != NULL); |
---|
1619 | n/a | assert(p_va != NULL); |
---|
1620 | n/a | |
---|
1621 | n/a | /* grab the function name or custom error msg first (mutually exclusive) */ |
---|
1622 | n/a | fname = strchr(format, ':'); |
---|
1623 | n/a | if (fname) { |
---|
1624 | n/a | fname++; |
---|
1625 | n/a | custom_msg = NULL; |
---|
1626 | n/a | } |
---|
1627 | n/a | else { |
---|
1628 | n/a | custom_msg = strchr(format,';'); |
---|
1629 | n/a | if (custom_msg) |
---|
1630 | n/a | custom_msg++; |
---|
1631 | n/a | } |
---|
1632 | n/a | |
---|
1633 | n/a | /* scan kwlist and count the number of positional-only parameters */ |
---|
1634 | n/a | for (pos = 0; kwlist[pos] && !*kwlist[pos]; pos++) { |
---|
1635 | n/a | } |
---|
1636 | n/a | /* scan kwlist and get greatest possible nbr of args */ |
---|
1637 | n/a | for (len = pos; kwlist[len]; len++) { |
---|
1638 | n/a | if (!*kwlist[len]) { |
---|
1639 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
1640 | n/a | "Empty keyword parameter name"); |
---|
1641 | n/a | return cleanreturn(0, &freelist); |
---|
1642 | n/a | } |
---|
1643 | n/a | } |
---|
1644 | n/a | |
---|
1645 | n/a | if (len > STATIC_FREELIST_ENTRIES) { |
---|
1646 | n/a | freelist.entries = PyMem_NEW(freelistentry_t, len); |
---|
1647 | n/a | if (freelist.entries == NULL) { |
---|
1648 | n/a | PyErr_NoMemory(); |
---|
1649 | n/a | return 0; |
---|
1650 | n/a | } |
---|
1651 | n/a | freelist.entries_malloced = 1; |
---|
1652 | n/a | } |
---|
1653 | n/a | |
---|
1654 | n/a | nargs = PyTuple_GET_SIZE(args); |
---|
1655 | n/a | nkwargs = (kwargs == NULL) ? 0 : PyDict_GET_SIZE(kwargs); |
---|
1656 | n/a | if (nargs + nkwargs > len) { |
---|
1657 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1658 | n/a | "%s%s takes at most %d argument%s (%zd given)", |
---|
1659 | n/a | (fname == NULL) ? "function" : fname, |
---|
1660 | n/a | (fname == NULL) ? "" : "()", |
---|
1661 | n/a | len, |
---|
1662 | n/a | (len == 1) ? "" : "s", |
---|
1663 | n/a | nargs + nkwargs); |
---|
1664 | n/a | return cleanreturn(0, &freelist); |
---|
1665 | n/a | } |
---|
1666 | n/a | |
---|
1667 | n/a | /* convert tuple args and keyword args in same loop, using kwlist to drive process */ |
---|
1668 | n/a | for (i = 0; i < len; i++) { |
---|
1669 | n/a | if (*format == '|') { |
---|
1670 | n/a | if (min != INT_MAX) { |
---|
1671 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
1672 | n/a | "Invalid format string (| specified twice)"); |
---|
1673 | n/a | return cleanreturn(0, &freelist); |
---|
1674 | n/a | } |
---|
1675 | n/a | |
---|
1676 | n/a | min = i; |
---|
1677 | n/a | format++; |
---|
1678 | n/a | |
---|
1679 | n/a | if (max != INT_MAX) { |
---|
1680 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
1681 | n/a | "Invalid format string ($ before |)"); |
---|
1682 | n/a | return cleanreturn(0, &freelist); |
---|
1683 | n/a | } |
---|
1684 | n/a | } |
---|
1685 | n/a | if (*format == '$') { |
---|
1686 | n/a | if (max != INT_MAX) { |
---|
1687 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
1688 | n/a | "Invalid format string ($ specified twice)"); |
---|
1689 | n/a | return cleanreturn(0, &freelist); |
---|
1690 | n/a | } |
---|
1691 | n/a | |
---|
1692 | n/a | max = i; |
---|
1693 | n/a | format++; |
---|
1694 | n/a | |
---|
1695 | n/a | if (max < pos) { |
---|
1696 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
1697 | n/a | "Empty parameter name after $"); |
---|
1698 | n/a | return cleanreturn(0, &freelist); |
---|
1699 | n/a | } |
---|
1700 | n/a | if (skip) { |
---|
1701 | n/a | /* Now we know the minimal and the maximal numbers of |
---|
1702 | n/a | * positional arguments and can raise an exception with |
---|
1703 | n/a | * informative message (see below). */ |
---|
1704 | n/a | break; |
---|
1705 | n/a | } |
---|
1706 | n/a | if (max < nargs) { |
---|
1707 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1708 | n/a | "Function takes %s %d positional arguments" |
---|
1709 | n/a | " (%d given)", |
---|
1710 | n/a | (min != INT_MAX) ? "at most" : "exactly", |
---|
1711 | n/a | max, nargs); |
---|
1712 | n/a | return cleanreturn(0, &freelist); |
---|
1713 | n/a | } |
---|
1714 | n/a | } |
---|
1715 | n/a | if (IS_END_OF_FORMAT(*format)) { |
---|
1716 | n/a | PyErr_Format(PyExc_SystemError, |
---|
1717 | n/a | "More keyword list entries (%d) than " |
---|
1718 | n/a | "format specifiers (%d)", len, i); |
---|
1719 | n/a | return cleanreturn(0, &freelist); |
---|
1720 | n/a | } |
---|
1721 | n/a | if (!skip) { |
---|
1722 | n/a | if (i < nargs) { |
---|
1723 | n/a | current_arg = PyTuple_GET_ITEM(args, i); |
---|
1724 | n/a | } |
---|
1725 | n/a | else if (nkwargs && i >= pos) { |
---|
1726 | n/a | current_arg = PyDict_GetItemString(kwargs, kwlist[i]); |
---|
1727 | n/a | if (current_arg) |
---|
1728 | n/a | --nkwargs; |
---|
1729 | n/a | } |
---|
1730 | n/a | else { |
---|
1731 | n/a | current_arg = NULL; |
---|
1732 | n/a | } |
---|
1733 | n/a | |
---|
1734 | n/a | if (current_arg) { |
---|
1735 | n/a | msg = convertitem(current_arg, &format, p_va, flags, |
---|
1736 | n/a | levels, msgbuf, sizeof(msgbuf), &freelist); |
---|
1737 | n/a | if (msg) { |
---|
1738 | n/a | seterror(i+1, msg, levels, fname, custom_msg); |
---|
1739 | n/a | return cleanreturn(0, &freelist); |
---|
1740 | n/a | } |
---|
1741 | n/a | continue; |
---|
1742 | n/a | } |
---|
1743 | n/a | |
---|
1744 | n/a | if (i < min) { |
---|
1745 | n/a | if (i < pos) { |
---|
1746 | n/a | assert (min == INT_MAX); |
---|
1747 | n/a | assert (max == INT_MAX); |
---|
1748 | n/a | skip = 1; |
---|
1749 | n/a | /* At that moment we still don't know the minimal and |
---|
1750 | n/a | * the maximal numbers of positional arguments. Raising |
---|
1751 | n/a | * an exception is deferred until we encounter | and $ |
---|
1752 | n/a | * or the end of the format. */ |
---|
1753 | n/a | } |
---|
1754 | n/a | else { |
---|
1755 | n/a | PyErr_Format(PyExc_TypeError, "Required argument " |
---|
1756 | n/a | "'%s' (pos %d) not found", |
---|
1757 | n/a | kwlist[i], i+1); |
---|
1758 | n/a | return cleanreturn(0, &freelist); |
---|
1759 | n/a | } |
---|
1760 | n/a | } |
---|
1761 | n/a | /* current code reports success when all required args |
---|
1762 | n/a | * fulfilled and no keyword args left, with no further |
---|
1763 | n/a | * validation. XXX Maybe skip this in debug build ? |
---|
1764 | n/a | */ |
---|
1765 | n/a | if (!nkwargs && !skip) { |
---|
1766 | n/a | return cleanreturn(1, &freelist); |
---|
1767 | n/a | } |
---|
1768 | n/a | } |
---|
1769 | n/a | |
---|
1770 | n/a | /* We are into optional args, skip thru to any remaining |
---|
1771 | n/a | * keyword args */ |
---|
1772 | n/a | msg = skipitem(&format, p_va, flags); |
---|
1773 | n/a | if (msg) { |
---|
1774 | n/a | PyErr_Format(PyExc_SystemError, "%s: '%s'", msg, |
---|
1775 | n/a | format); |
---|
1776 | n/a | return cleanreturn(0, &freelist); |
---|
1777 | n/a | } |
---|
1778 | n/a | } |
---|
1779 | n/a | |
---|
1780 | n/a | if (skip) { |
---|
1781 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1782 | n/a | "Function takes %s %d positional arguments" |
---|
1783 | n/a | " (%d given)", |
---|
1784 | n/a | (Py_MIN(pos, min) < i) ? "at least" : "exactly", |
---|
1785 | n/a | Py_MIN(pos, min), nargs); |
---|
1786 | n/a | return cleanreturn(0, &freelist); |
---|
1787 | n/a | } |
---|
1788 | n/a | |
---|
1789 | n/a | if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) { |
---|
1790 | n/a | PyErr_Format(PyExc_SystemError, |
---|
1791 | n/a | "more argument specifiers than keyword list entries " |
---|
1792 | n/a | "(remaining format:'%s')", format); |
---|
1793 | n/a | return cleanreturn(0, &freelist); |
---|
1794 | n/a | } |
---|
1795 | n/a | |
---|
1796 | n/a | if (nkwargs > 0) { |
---|
1797 | n/a | PyObject *key; |
---|
1798 | n/a | Py_ssize_t j; |
---|
1799 | n/a | /* make sure there are no arguments given by name and position */ |
---|
1800 | n/a | for (i = pos; i < nargs; i++) { |
---|
1801 | n/a | current_arg = PyDict_GetItemString(kwargs, kwlist[i]); |
---|
1802 | n/a | if (current_arg) { |
---|
1803 | n/a | /* arg present in tuple and in dict */ |
---|
1804 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1805 | n/a | "Argument given by name ('%s') " |
---|
1806 | n/a | "and position (%d)", |
---|
1807 | n/a | kwlist[i], i+1); |
---|
1808 | n/a | return cleanreturn(0, &freelist); |
---|
1809 | n/a | } |
---|
1810 | n/a | } |
---|
1811 | n/a | /* make sure there are no extraneous keyword arguments */ |
---|
1812 | n/a | j = 0; |
---|
1813 | n/a | while (PyDict_Next(kwargs, &j, &key, NULL)) { |
---|
1814 | n/a | int match = 0; |
---|
1815 | n/a | if (!PyUnicode_Check(key)) { |
---|
1816 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
1817 | n/a | "keywords must be strings"); |
---|
1818 | n/a | return cleanreturn(0, &freelist); |
---|
1819 | n/a | } |
---|
1820 | n/a | for (i = pos; i < len; i++) { |
---|
1821 | n/a | if (_PyUnicode_EqualToASCIIString(key, kwlist[i])) { |
---|
1822 | n/a | match = 1; |
---|
1823 | n/a | break; |
---|
1824 | n/a | } |
---|
1825 | n/a | } |
---|
1826 | n/a | if (!match) { |
---|
1827 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1828 | n/a | "'%U' is an invalid keyword " |
---|
1829 | n/a | "argument for this function", |
---|
1830 | n/a | key); |
---|
1831 | n/a | return cleanreturn(0, &freelist); |
---|
1832 | n/a | } |
---|
1833 | n/a | } |
---|
1834 | n/a | } |
---|
1835 | n/a | |
---|
1836 | n/a | return cleanreturn(1, &freelist); |
---|
1837 | n/a | } |
---|
1838 | n/a | |
---|
1839 | n/a | |
---|
1840 | n/a | /* List of static parsers. */ |
---|
1841 | n/a | static struct _PyArg_Parser *static_arg_parsers = NULL; |
---|
1842 | n/a | |
---|
1843 | n/a | static int |
---|
1844 | n/a | parser_init(struct _PyArg_Parser *parser) |
---|
1845 | n/a | { |
---|
1846 | n/a | const char * const *keywords; |
---|
1847 | n/a | const char *format, *msg; |
---|
1848 | n/a | int i, len, min, max, nkw; |
---|
1849 | n/a | PyObject *kwtuple; |
---|
1850 | n/a | |
---|
1851 | n/a | assert(parser->format != NULL); |
---|
1852 | n/a | assert(parser->keywords != NULL); |
---|
1853 | n/a | if (parser->kwtuple != NULL) { |
---|
1854 | n/a | return 1; |
---|
1855 | n/a | } |
---|
1856 | n/a | |
---|
1857 | n/a | /* grab the function name or custom error msg first (mutually exclusive) */ |
---|
1858 | n/a | parser->fname = strchr(parser->format, ':'); |
---|
1859 | n/a | if (parser->fname) { |
---|
1860 | n/a | parser->fname++; |
---|
1861 | n/a | parser->custom_msg = NULL; |
---|
1862 | n/a | } |
---|
1863 | n/a | else { |
---|
1864 | n/a | parser->custom_msg = strchr(parser->format,';'); |
---|
1865 | n/a | if (parser->custom_msg) |
---|
1866 | n/a | parser->custom_msg++; |
---|
1867 | n/a | } |
---|
1868 | n/a | |
---|
1869 | n/a | keywords = parser->keywords; |
---|
1870 | n/a | /* scan keywords and count the number of positional-only parameters */ |
---|
1871 | n/a | for (i = 0; keywords[i] && !*keywords[i]; i++) { |
---|
1872 | n/a | } |
---|
1873 | n/a | parser->pos = i; |
---|
1874 | n/a | /* scan keywords and get greatest possible nbr of args */ |
---|
1875 | n/a | for (; keywords[i]; i++) { |
---|
1876 | n/a | if (!*keywords[i]) { |
---|
1877 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
1878 | n/a | "Empty keyword parameter name"); |
---|
1879 | n/a | return 0; |
---|
1880 | n/a | } |
---|
1881 | n/a | } |
---|
1882 | n/a | len = i; |
---|
1883 | n/a | |
---|
1884 | n/a | min = max = INT_MAX; |
---|
1885 | n/a | format = parser->format; |
---|
1886 | n/a | for (i = 0; i < len; i++) { |
---|
1887 | n/a | if (*format == '|') { |
---|
1888 | n/a | if (min != INT_MAX) { |
---|
1889 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
1890 | n/a | "Invalid format string (| specified twice)"); |
---|
1891 | n/a | return 0; |
---|
1892 | n/a | } |
---|
1893 | n/a | if (max != INT_MAX) { |
---|
1894 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
1895 | n/a | "Invalid format string ($ before |)"); |
---|
1896 | n/a | return 0; |
---|
1897 | n/a | } |
---|
1898 | n/a | min = i; |
---|
1899 | n/a | format++; |
---|
1900 | n/a | } |
---|
1901 | n/a | if (*format == '$') { |
---|
1902 | n/a | if (max != INT_MAX) { |
---|
1903 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
1904 | n/a | "Invalid format string ($ specified twice)"); |
---|
1905 | n/a | return 0; |
---|
1906 | n/a | } |
---|
1907 | n/a | if (i < parser->pos) { |
---|
1908 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
1909 | n/a | "Empty parameter name after $"); |
---|
1910 | n/a | return 0; |
---|
1911 | n/a | } |
---|
1912 | n/a | max = i; |
---|
1913 | n/a | format++; |
---|
1914 | n/a | } |
---|
1915 | n/a | if (IS_END_OF_FORMAT(*format)) { |
---|
1916 | n/a | PyErr_Format(PyExc_SystemError, |
---|
1917 | n/a | "More keyword list entries (%d) than " |
---|
1918 | n/a | "format specifiers (%d)", len, i); |
---|
1919 | n/a | return 0; |
---|
1920 | n/a | } |
---|
1921 | n/a | |
---|
1922 | n/a | msg = skipitem(&format, NULL, 0); |
---|
1923 | n/a | if (msg) { |
---|
1924 | n/a | PyErr_Format(PyExc_SystemError, "%s: '%s'", msg, |
---|
1925 | n/a | format); |
---|
1926 | n/a | return 0; |
---|
1927 | n/a | } |
---|
1928 | n/a | } |
---|
1929 | n/a | parser->min = Py_MIN(min, len); |
---|
1930 | n/a | parser->max = Py_MIN(max, len); |
---|
1931 | n/a | |
---|
1932 | n/a | if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) { |
---|
1933 | n/a | PyErr_Format(PyExc_SystemError, |
---|
1934 | n/a | "more argument specifiers than keyword list entries " |
---|
1935 | n/a | "(remaining format:'%s')", format); |
---|
1936 | n/a | return 0; |
---|
1937 | n/a | } |
---|
1938 | n/a | |
---|
1939 | n/a | nkw = len - parser->pos; |
---|
1940 | n/a | kwtuple = PyTuple_New(nkw); |
---|
1941 | n/a | if (kwtuple == NULL) { |
---|
1942 | n/a | return 0; |
---|
1943 | n/a | } |
---|
1944 | n/a | keywords = parser->keywords + parser->pos; |
---|
1945 | n/a | for (i = 0; i < nkw; i++) { |
---|
1946 | n/a | PyObject *str = PyUnicode_FromString(keywords[i]); |
---|
1947 | n/a | if (str == NULL) { |
---|
1948 | n/a | Py_DECREF(kwtuple); |
---|
1949 | n/a | return 0; |
---|
1950 | n/a | } |
---|
1951 | n/a | PyUnicode_InternInPlace(&str); |
---|
1952 | n/a | PyTuple_SET_ITEM(kwtuple, i, str); |
---|
1953 | n/a | } |
---|
1954 | n/a | parser->kwtuple = kwtuple; |
---|
1955 | n/a | |
---|
1956 | n/a | assert(parser->next == NULL); |
---|
1957 | n/a | parser->next = static_arg_parsers; |
---|
1958 | n/a | static_arg_parsers = parser; |
---|
1959 | n/a | return 1; |
---|
1960 | n/a | } |
---|
1961 | n/a | |
---|
1962 | n/a | static void |
---|
1963 | n/a | parser_clear(struct _PyArg_Parser *parser) |
---|
1964 | n/a | { |
---|
1965 | n/a | Py_CLEAR(parser->kwtuple); |
---|
1966 | n/a | } |
---|
1967 | n/a | |
---|
1968 | n/a | static PyObject* |
---|
1969 | n/a | find_keyword(PyObject *kwargs, PyObject *kwnames, PyObject **kwstack, PyObject *key) |
---|
1970 | n/a | { |
---|
1971 | n/a | Py_ssize_t i, nkwargs; |
---|
1972 | n/a | |
---|
1973 | n/a | if (kwargs != NULL) { |
---|
1974 | n/a | return PyDict_GetItem(kwargs, key); |
---|
1975 | n/a | } |
---|
1976 | n/a | nkwargs = PyTuple_GET_SIZE(kwnames); |
---|
1977 | n/a | for (i=0; i < nkwargs; i++) { |
---|
1978 | n/a | PyObject *kwname = PyTuple_GET_ITEM(kwnames, i); |
---|
1979 | n/a | |
---|
1980 | n/a | /* ptr==ptr should match in most cases since keyword keys |
---|
1981 | n/a | should be interned strings */ |
---|
1982 | n/a | if (kwname == key) { |
---|
1983 | n/a | return kwstack[i]; |
---|
1984 | n/a | } |
---|
1985 | n/a | if (!PyUnicode_Check(kwname)) { |
---|
1986 | n/a | /* ignore non-string keyword keys: |
---|
1987 | n/a | an error will be raised below */ |
---|
1988 | n/a | continue; |
---|
1989 | n/a | } |
---|
1990 | n/a | if (_PyUnicode_EQ(kwname, key)) { |
---|
1991 | n/a | return kwstack[i]; |
---|
1992 | n/a | } |
---|
1993 | n/a | } |
---|
1994 | n/a | return NULL; |
---|
1995 | n/a | } |
---|
1996 | n/a | |
---|
1997 | n/a | static int |
---|
1998 | n/a | vgetargskeywordsfast_impl(PyObject **args, Py_ssize_t nargs, |
---|
1999 | n/a | PyObject *kwargs, PyObject *kwnames, |
---|
2000 | n/a | struct _PyArg_Parser *parser, |
---|
2001 | n/a | va_list *p_va, int flags) |
---|
2002 | n/a | { |
---|
2003 | n/a | PyObject *kwtuple; |
---|
2004 | n/a | char msgbuf[512]; |
---|
2005 | n/a | int levels[32]; |
---|
2006 | n/a | const char *format; |
---|
2007 | n/a | const char *msg; |
---|
2008 | n/a | PyObject *keyword; |
---|
2009 | n/a | int i, pos, len; |
---|
2010 | n/a | Py_ssize_t nkwargs; |
---|
2011 | n/a | PyObject *current_arg; |
---|
2012 | n/a | freelistentry_t static_entries[STATIC_FREELIST_ENTRIES]; |
---|
2013 | n/a | freelist_t freelist; |
---|
2014 | n/a | PyObject **kwstack = NULL; |
---|
2015 | n/a | |
---|
2016 | n/a | freelist.entries = static_entries; |
---|
2017 | n/a | freelist.first_available = 0; |
---|
2018 | n/a | freelist.entries_malloced = 0; |
---|
2019 | n/a | |
---|
2020 | n/a | assert(kwargs == NULL || PyDict_Check(kwargs)); |
---|
2021 | n/a | assert(kwargs == NULL || kwnames == NULL); |
---|
2022 | n/a | assert(p_va != NULL); |
---|
2023 | n/a | |
---|
2024 | n/a | if (parser == NULL) { |
---|
2025 | n/a | PyErr_BadInternalCall(); |
---|
2026 | n/a | return 0; |
---|
2027 | n/a | } |
---|
2028 | n/a | |
---|
2029 | n/a | if (kwnames != NULL && !PyTuple_Check(kwnames)) { |
---|
2030 | n/a | PyErr_BadInternalCall(); |
---|
2031 | n/a | return 0; |
---|
2032 | n/a | } |
---|
2033 | n/a | |
---|
2034 | n/a | if (!parser_init(parser)) { |
---|
2035 | n/a | return 0; |
---|
2036 | n/a | } |
---|
2037 | n/a | |
---|
2038 | n/a | kwtuple = parser->kwtuple; |
---|
2039 | n/a | pos = parser->pos; |
---|
2040 | n/a | len = pos + PyTuple_GET_SIZE(kwtuple); |
---|
2041 | n/a | |
---|
2042 | n/a | if (len > STATIC_FREELIST_ENTRIES) { |
---|
2043 | n/a | freelist.entries = PyMem_NEW(freelistentry_t, len); |
---|
2044 | n/a | if (freelist.entries == NULL) { |
---|
2045 | n/a | PyErr_NoMemory(); |
---|
2046 | n/a | return 0; |
---|
2047 | n/a | } |
---|
2048 | n/a | freelist.entries_malloced = 1; |
---|
2049 | n/a | } |
---|
2050 | n/a | |
---|
2051 | n/a | if (kwargs != NULL) { |
---|
2052 | n/a | nkwargs = PyDict_GET_SIZE(kwargs); |
---|
2053 | n/a | } |
---|
2054 | n/a | else if (kwnames != NULL) { |
---|
2055 | n/a | nkwargs = PyTuple_GET_SIZE(kwnames); |
---|
2056 | n/a | kwstack = args + nargs; |
---|
2057 | n/a | } |
---|
2058 | n/a | else { |
---|
2059 | n/a | nkwargs = 0; |
---|
2060 | n/a | } |
---|
2061 | n/a | if (nargs + nkwargs > len) { |
---|
2062 | n/a | PyErr_Format(PyExc_TypeError, |
---|
2063 | n/a | "%s%s takes at most %d argument%s (%zd given)", |
---|
2064 | n/a | (parser->fname == NULL) ? "function" : parser->fname, |
---|
2065 | n/a | (parser->fname == NULL) ? "" : "()", |
---|
2066 | n/a | len, |
---|
2067 | n/a | (len == 1) ? "" : "s", |
---|
2068 | n/a | nargs + nkwargs); |
---|
2069 | n/a | return cleanreturn(0, &freelist); |
---|
2070 | n/a | } |
---|
2071 | n/a | if (parser->max < nargs) { |
---|
2072 | n/a | PyErr_Format(PyExc_TypeError, |
---|
2073 | n/a | "Function takes %s %d positional arguments (%d given)", |
---|
2074 | n/a | (parser->min != INT_MAX) ? "at most" : "exactly", |
---|
2075 | n/a | parser->max, nargs); |
---|
2076 | n/a | return cleanreturn(0, &freelist); |
---|
2077 | n/a | } |
---|
2078 | n/a | |
---|
2079 | n/a | format = parser->format; |
---|
2080 | n/a | /* convert tuple args and keyword args in same loop, using kwtuple to drive process */ |
---|
2081 | n/a | for (i = 0; i < len; i++) { |
---|
2082 | n/a | if (*format == '|') { |
---|
2083 | n/a | format++; |
---|
2084 | n/a | } |
---|
2085 | n/a | if (*format == '$') { |
---|
2086 | n/a | format++; |
---|
2087 | n/a | } |
---|
2088 | n/a | assert(!IS_END_OF_FORMAT(*format)); |
---|
2089 | n/a | |
---|
2090 | n/a | if (i < nargs) { |
---|
2091 | n/a | current_arg = args[i]; |
---|
2092 | n/a | } |
---|
2093 | n/a | else if (nkwargs && i >= pos) { |
---|
2094 | n/a | keyword = PyTuple_GET_ITEM(kwtuple, i - pos); |
---|
2095 | n/a | current_arg = find_keyword(kwargs, kwnames, kwstack, keyword); |
---|
2096 | n/a | if (current_arg) |
---|
2097 | n/a | --nkwargs; |
---|
2098 | n/a | } |
---|
2099 | n/a | else { |
---|
2100 | n/a | current_arg = NULL; |
---|
2101 | n/a | } |
---|
2102 | n/a | |
---|
2103 | n/a | if (current_arg) { |
---|
2104 | n/a | msg = convertitem(current_arg, &format, p_va, flags, |
---|
2105 | n/a | levels, msgbuf, sizeof(msgbuf), &freelist); |
---|
2106 | n/a | if (msg) { |
---|
2107 | n/a | seterror(i+1, msg, levels, parser->fname, parser->custom_msg); |
---|
2108 | n/a | return cleanreturn(0, &freelist); |
---|
2109 | n/a | } |
---|
2110 | n/a | continue; |
---|
2111 | n/a | } |
---|
2112 | n/a | |
---|
2113 | n/a | if (i < parser->min) { |
---|
2114 | n/a | /* Less arguments than required */ |
---|
2115 | n/a | if (i < pos) { |
---|
2116 | n/a | Py_ssize_t min = Py_MIN(pos, parser->min); |
---|
2117 | n/a | PyErr_Format(PyExc_TypeError, |
---|
2118 | n/a | "Function takes %s %d positional arguments" |
---|
2119 | n/a | " (%d given)", |
---|
2120 | n/a | min < parser->max ? "at least" : "exactly", |
---|
2121 | n/a | min, nargs); |
---|
2122 | n/a | } |
---|
2123 | n/a | else { |
---|
2124 | n/a | keyword = PyTuple_GET_ITEM(kwtuple, i - pos); |
---|
2125 | n/a | PyErr_Format(PyExc_TypeError, "Required argument " |
---|
2126 | n/a | "'%U' (pos %d) not found", |
---|
2127 | n/a | keyword, i+1); |
---|
2128 | n/a | } |
---|
2129 | n/a | return cleanreturn(0, &freelist); |
---|
2130 | n/a | } |
---|
2131 | n/a | /* current code reports success when all required args |
---|
2132 | n/a | * fulfilled and no keyword args left, with no further |
---|
2133 | n/a | * validation. XXX Maybe skip this in debug build ? |
---|
2134 | n/a | */ |
---|
2135 | n/a | if (!nkwargs) { |
---|
2136 | n/a | return cleanreturn(1, &freelist); |
---|
2137 | n/a | } |
---|
2138 | n/a | |
---|
2139 | n/a | /* We are into optional args, skip thru to any remaining |
---|
2140 | n/a | * keyword args */ |
---|
2141 | n/a | msg = skipitem(&format, p_va, flags); |
---|
2142 | n/a | assert(msg == NULL); |
---|
2143 | n/a | } |
---|
2144 | n/a | |
---|
2145 | n/a | assert(IS_END_OF_FORMAT(*format) || (*format == '|') || (*format == '$')); |
---|
2146 | n/a | |
---|
2147 | n/a | if (nkwargs > 0) { |
---|
2148 | n/a | Py_ssize_t j; |
---|
2149 | n/a | /* make sure there are no arguments given by name and position */ |
---|
2150 | n/a | for (i = pos; i < nargs; i++) { |
---|
2151 | n/a | keyword = PyTuple_GET_ITEM(kwtuple, i - pos); |
---|
2152 | n/a | current_arg = find_keyword(kwargs, kwnames, kwstack, keyword); |
---|
2153 | n/a | if (current_arg) { |
---|
2154 | n/a | /* arg present in tuple and in dict */ |
---|
2155 | n/a | PyErr_Format(PyExc_TypeError, |
---|
2156 | n/a | "Argument given by name ('%U') " |
---|
2157 | n/a | "and position (%d)", |
---|
2158 | n/a | keyword, i+1); |
---|
2159 | n/a | return cleanreturn(0, &freelist); |
---|
2160 | n/a | } |
---|
2161 | n/a | } |
---|
2162 | n/a | /* make sure there are no extraneous keyword arguments */ |
---|
2163 | n/a | j = 0; |
---|
2164 | n/a | while (1) { |
---|
2165 | n/a | int match; |
---|
2166 | n/a | if (kwargs != NULL) { |
---|
2167 | n/a | if (!PyDict_Next(kwargs, &j, &keyword, NULL)) |
---|
2168 | n/a | break; |
---|
2169 | n/a | } |
---|
2170 | n/a | else { |
---|
2171 | n/a | if (j >= PyTuple_GET_SIZE(kwnames)) |
---|
2172 | n/a | break; |
---|
2173 | n/a | keyword = PyTuple_GET_ITEM(kwnames, j); |
---|
2174 | n/a | j++; |
---|
2175 | n/a | } |
---|
2176 | n/a | |
---|
2177 | n/a | if (!PyUnicode_Check(keyword)) { |
---|
2178 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
2179 | n/a | "keywords must be strings"); |
---|
2180 | n/a | return cleanreturn(0, &freelist); |
---|
2181 | n/a | } |
---|
2182 | n/a | match = PySequence_Contains(kwtuple, keyword); |
---|
2183 | n/a | if (match <= 0) { |
---|
2184 | n/a | if (!match) { |
---|
2185 | n/a | PyErr_Format(PyExc_TypeError, |
---|
2186 | n/a | "'%U' is an invalid keyword " |
---|
2187 | n/a | "argument for this function", |
---|
2188 | n/a | keyword); |
---|
2189 | n/a | } |
---|
2190 | n/a | return cleanreturn(0, &freelist); |
---|
2191 | n/a | } |
---|
2192 | n/a | } |
---|
2193 | n/a | } |
---|
2194 | n/a | |
---|
2195 | n/a | return cleanreturn(1, &freelist); |
---|
2196 | n/a | } |
---|
2197 | n/a | |
---|
2198 | n/a | static int |
---|
2199 | n/a | vgetargskeywordsfast(PyObject *args, PyObject *keywords, |
---|
2200 | n/a | struct _PyArg_Parser *parser, va_list *p_va, int flags) |
---|
2201 | n/a | { |
---|
2202 | n/a | PyObject **stack; |
---|
2203 | n/a | Py_ssize_t nargs; |
---|
2204 | n/a | |
---|
2205 | n/a | if (args == NULL |
---|
2206 | n/a | || !PyTuple_Check(args) |
---|
2207 | n/a | || (keywords != NULL && !PyDict_Check(keywords))) |
---|
2208 | n/a | { |
---|
2209 | n/a | PyErr_BadInternalCall(); |
---|
2210 | n/a | return 0; |
---|
2211 | n/a | } |
---|
2212 | n/a | |
---|
2213 | n/a | stack = &PyTuple_GET_ITEM(args, 0); |
---|
2214 | n/a | nargs = PyTuple_GET_SIZE(args); |
---|
2215 | n/a | return vgetargskeywordsfast_impl(stack, nargs, keywords, NULL, |
---|
2216 | n/a | parser, p_va, flags); |
---|
2217 | n/a | } |
---|
2218 | n/a | |
---|
2219 | n/a | |
---|
2220 | n/a | static const char * |
---|
2221 | n/a | skipitem(const char **p_format, va_list *p_va, int flags) |
---|
2222 | n/a | { |
---|
2223 | n/a | const char *format = *p_format; |
---|
2224 | n/a | char c = *format++; |
---|
2225 | n/a | |
---|
2226 | n/a | switch (c) { |
---|
2227 | n/a | |
---|
2228 | n/a | /* |
---|
2229 | n/a | * codes that take a single data pointer as an argument |
---|
2230 | n/a | * (the type of the pointer is irrelevant) |
---|
2231 | n/a | */ |
---|
2232 | n/a | |
---|
2233 | n/a | case 'b': /* byte -- very short int */ |
---|
2234 | n/a | case 'B': /* byte as bitfield */ |
---|
2235 | n/a | case 'h': /* short int */ |
---|
2236 | n/a | case 'H': /* short int as bitfield */ |
---|
2237 | n/a | case 'i': /* int */ |
---|
2238 | n/a | case 'I': /* int sized bitfield */ |
---|
2239 | n/a | case 'l': /* long int */ |
---|
2240 | n/a | case 'k': /* long int sized bitfield */ |
---|
2241 | n/a | case 'L': /* long long */ |
---|
2242 | n/a | case 'K': /* long long sized bitfield */ |
---|
2243 | n/a | case 'n': /* Py_ssize_t */ |
---|
2244 | n/a | case 'f': /* float */ |
---|
2245 | n/a | case 'd': /* double */ |
---|
2246 | n/a | case 'D': /* complex double */ |
---|
2247 | n/a | case 'c': /* char */ |
---|
2248 | n/a | case 'C': /* unicode char */ |
---|
2249 | n/a | case 'p': /* boolean predicate */ |
---|
2250 | n/a | case 'S': /* string object */ |
---|
2251 | n/a | case 'Y': /* string object */ |
---|
2252 | n/a | case 'U': /* unicode string object */ |
---|
2253 | n/a | { |
---|
2254 | n/a | if (p_va != NULL) { |
---|
2255 | n/a | (void) va_arg(*p_va, void *); |
---|
2256 | n/a | } |
---|
2257 | n/a | break; |
---|
2258 | n/a | } |
---|
2259 | n/a | |
---|
2260 | n/a | /* string codes */ |
---|
2261 | n/a | |
---|
2262 | n/a | case 'e': /* string with encoding */ |
---|
2263 | n/a | { |
---|
2264 | n/a | if (p_va != NULL) { |
---|
2265 | n/a | (void) va_arg(*p_va, const char *); |
---|
2266 | n/a | } |
---|
2267 | n/a | if (!(*format == 's' || *format == 't')) |
---|
2268 | n/a | /* after 'e', only 's' and 't' is allowed */ |
---|
2269 | n/a | goto err; |
---|
2270 | n/a | format++; |
---|
2271 | n/a | /* explicit fallthrough to string cases */ |
---|
2272 | n/a | } |
---|
2273 | n/a | |
---|
2274 | n/a | case 's': /* string */ |
---|
2275 | n/a | case 'z': /* string or None */ |
---|
2276 | n/a | case 'y': /* bytes */ |
---|
2277 | n/a | case 'u': /* unicode string */ |
---|
2278 | n/a | case 'Z': /* unicode string or None */ |
---|
2279 | n/a | case 'w': /* buffer, read-write */ |
---|
2280 | n/a | { |
---|
2281 | n/a | if (p_va != NULL) { |
---|
2282 | n/a | (void) va_arg(*p_va, char **); |
---|
2283 | n/a | } |
---|
2284 | n/a | if (*format == '#') { |
---|
2285 | n/a | if (p_va != NULL) { |
---|
2286 | n/a | if (flags & FLAG_SIZE_T) |
---|
2287 | n/a | (void) va_arg(*p_va, Py_ssize_t *); |
---|
2288 | n/a | else |
---|
2289 | n/a | (void) va_arg(*p_va, int *); |
---|
2290 | n/a | } |
---|
2291 | n/a | format++; |
---|
2292 | n/a | } else if ((c == 's' || c == 'z' || c == 'y') && *format == '*') { |
---|
2293 | n/a | format++; |
---|
2294 | n/a | } |
---|
2295 | n/a | break; |
---|
2296 | n/a | } |
---|
2297 | n/a | |
---|
2298 | n/a | case 'O': /* object */ |
---|
2299 | n/a | { |
---|
2300 | n/a | if (*format == '!') { |
---|
2301 | n/a | format++; |
---|
2302 | n/a | if (p_va != NULL) { |
---|
2303 | n/a | (void) va_arg(*p_va, PyTypeObject*); |
---|
2304 | n/a | (void) va_arg(*p_va, PyObject **); |
---|
2305 | n/a | } |
---|
2306 | n/a | } |
---|
2307 | n/a | else if (*format == '&') { |
---|
2308 | n/a | typedef int (*converter)(PyObject *, void *); |
---|
2309 | n/a | if (p_va != NULL) { |
---|
2310 | n/a | (void) va_arg(*p_va, converter); |
---|
2311 | n/a | (void) va_arg(*p_va, void *); |
---|
2312 | n/a | } |
---|
2313 | n/a | format++; |
---|
2314 | n/a | } |
---|
2315 | n/a | else { |
---|
2316 | n/a | if (p_va != NULL) { |
---|
2317 | n/a | (void) va_arg(*p_va, PyObject **); |
---|
2318 | n/a | } |
---|
2319 | n/a | } |
---|
2320 | n/a | break; |
---|
2321 | n/a | } |
---|
2322 | n/a | |
---|
2323 | n/a | case '(': /* bypass tuple, not handled at all previously */ |
---|
2324 | n/a | { |
---|
2325 | n/a | const char *msg; |
---|
2326 | n/a | for (;;) { |
---|
2327 | n/a | if (*format==')') |
---|
2328 | n/a | break; |
---|
2329 | n/a | if (IS_END_OF_FORMAT(*format)) |
---|
2330 | n/a | return "Unmatched left paren in format " |
---|
2331 | n/a | "string"; |
---|
2332 | n/a | msg = skipitem(&format, p_va, flags); |
---|
2333 | n/a | if (msg) |
---|
2334 | n/a | return msg; |
---|
2335 | n/a | } |
---|
2336 | n/a | format++; |
---|
2337 | n/a | break; |
---|
2338 | n/a | } |
---|
2339 | n/a | |
---|
2340 | n/a | case ')': |
---|
2341 | n/a | return "Unmatched right paren in format string"; |
---|
2342 | n/a | |
---|
2343 | n/a | default: |
---|
2344 | n/a | err: |
---|
2345 | n/a | return "impossible<bad format char>"; |
---|
2346 | n/a | |
---|
2347 | n/a | } |
---|
2348 | n/a | |
---|
2349 | n/a | *p_format = format; |
---|
2350 | n/a | return NULL; |
---|
2351 | n/a | } |
---|
2352 | n/a | |
---|
2353 | n/a | |
---|
2354 | n/a | static int |
---|
2355 | n/a | unpack_stack(PyObject **args, Py_ssize_t nargs, const char *name, |
---|
2356 | n/a | Py_ssize_t min, Py_ssize_t max, va_list vargs) |
---|
2357 | n/a | { |
---|
2358 | n/a | Py_ssize_t i; |
---|
2359 | n/a | PyObject **o; |
---|
2360 | n/a | |
---|
2361 | n/a | assert(min >= 0); |
---|
2362 | n/a | assert(min <= max); |
---|
2363 | n/a | |
---|
2364 | n/a | if (nargs < min) { |
---|
2365 | n/a | if (name != NULL) |
---|
2366 | n/a | PyErr_Format( |
---|
2367 | n/a | PyExc_TypeError, |
---|
2368 | n/a | "%s expected %s%zd arguments, got %zd", |
---|
2369 | n/a | name, (min == max ? "" : "at least "), min, nargs); |
---|
2370 | n/a | else |
---|
2371 | n/a | PyErr_Format( |
---|
2372 | n/a | PyExc_TypeError, |
---|
2373 | n/a | "unpacked tuple should have %s%zd elements," |
---|
2374 | n/a | " but has %zd", |
---|
2375 | n/a | (min == max ? "" : "at least "), min, nargs); |
---|
2376 | n/a | return 0; |
---|
2377 | n/a | } |
---|
2378 | n/a | |
---|
2379 | n/a | if (nargs == 0) { |
---|
2380 | n/a | return 1; |
---|
2381 | n/a | } |
---|
2382 | n/a | |
---|
2383 | n/a | if (nargs > max) { |
---|
2384 | n/a | if (name != NULL) |
---|
2385 | n/a | PyErr_Format( |
---|
2386 | n/a | PyExc_TypeError, |
---|
2387 | n/a | "%s expected %s%zd arguments, got %zd", |
---|
2388 | n/a | name, (min == max ? "" : "at most "), max, nargs); |
---|
2389 | n/a | else |
---|
2390 | n/a | PyErr_Format( |
---|
2391 | n/a | PyExc_TypeError, |
---|
2392 | n/a | "unpacked tuple should have %s%zd elements," |
---|
2393 | n/a | " but has %zd", |
---|
2394 | n/a | (min == max ? "" : "at most "), max, nargs); |
---|
2395 | n/a | return 0; |
---|
2396 | n/a | } |
---|
2397 | n/a | |
---|
2398 | n/a | for (i = 0; i < nargs; i++) { |
---|
2399 | n/a | o = va_arg(vargs, PyObject **); |
---|
2400 | n/a | *o = args[i]; |
---|
2401 | n/a | } |
---|
2402 | n/a | return 1; |
---|
2403 | n/a | } |
---|
2404 | n/a | |
---|
2405 | n/a | int |
---|
2406 | n/a | PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...) |
---|
2407 | n/a | { |
---|
2408 | n/a | PyObject **stack; |
---|
2409 | n/a | Py_ssize_t nargs; |
---|
2410 | n/a | int retval; |
---|
2411 | n/a | va_list vargs; |
---|
2412 | n/a | |
---|
2413 | n/a | if (!PyTuple_Check(args)) { |
---|
2414 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
2415 | n/a | "PyArg_UnpackTuple() argument list is not a tuple"); |
---|
2416 | n/a | return 0; |
---|
2417 | n/a | } |
---|
2418 | n/a | stack = &PyTuple_GET_ITEM(args, 0); |
---|
2419 | n/a | nargs = PyTuple_GET_SIZE(args); |
---|
2420 | n/a | |
---|
2421 | n/a | #ifdef HAVE_STDARG_PROTOTYPES |
---|
2422 | n/a | va_start(vargs, max); |
---|
2423 | n/a | #else |
---|
2424 | n/a | va_start(vargs); |
---|
2425 | n/a | #endif |
---|
2426 | n/a | retval = unpack_stack(stack, nargs, name, min, max, vargs); |
---|
2427 | n/a | va_end(vargs); |
---|
2428 | n/a | return retval; |
---|
2429 | n/a | } |
---|
2430 | n/a | |
---|
2431 | n/a | int |
---|
2432 | n/a | _PyArg_UnpackStack(PyObject **args, Py_ssize_t nargs, const char *name, |
---|
2433 | n/a | Py_ssize_t min, Py_ssize_t max, ...) |
---|
2434 | n/a | { |
---|
2435 | n/a | int retval; |
---|
2436 | n/a | va_list vargs; |
---|
2437 | n/a | |
---|
2438 | n/a | #ifdef HAVE_STDARG_PROTOTYPES |
---|
2439 | n/a | va_start(vargs, max); |
---|
2440 | n/a | #else |
---|
2441 | n/a | va_start(vargs); |
---|
2442 | n/a | #endif |
---|
2443 | n/a | retval = unpack_stack(args, nargs, name, min, max, vargs); |
---|
2444 | n/a | va_end(vargs); |
---|
2445 | n/a | return retval; |
---|
2446 | n/a | } |
---|
2447 | n/a | |
---|
2448 | n/a | |
---|
2449 | n/a | #undef _PyArg_NoKeywords |
---|
2450 | n/a | #undef _PyArg_NoStackKeywords |
---|
2451 | n/a | #undef _PyArg_NoPositional |
---|
2452 | n/a | |
---|
2453 | n/a | /* For type constructors that don't take keyword args |
---|
2454 | n/a | * |
---|
2455 | n/a | * Sets a TypeError and returns 0 if the args/kwargs is |
---|
2456 | n/a | * not empty, returns 1 otherwise |
---|
2457 | n/a | */ |
---|
2458 | n/a | int |
---|
2459 | n/a | _PyArg_NoKeywords(const char *funcname, PyObject *kwargs) |
---|
2460 | n/a | { |
---|
2461 | n/a | if (kwargs == NULL) { |
---|
2462 | n/a | return 1; |
---|
2463 | n/a | } |
---|
2464 | n/a | if (!PyDict_CheckExact(kwargs)) { |
---|
2465 | n/a | PyErr_BadInternalCall(); |
---|
2466 | n/a | return 0; |
---|
2467 | n/a | } |
---|
2468 | n/a | if (PyDict_GET_SIZE(kwargs) == 0) { |
---|
2469 | n/a | return 1; |
---|
2470 | n/a | } |
---|
2471 | n/a | |
---|
2472 | n/a | PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments", |
---|
2473 | n/a | funcname); |
---|
2474 | n/a | return 0; |
---|
2475 | n/a | } |
---|
2476 | n/a | |
---|
2477 | n/a | |
---|
2478 | n/a | int |
---|
2479 | n/a | _PyArg_NoStackKeywords(const char *funcname, PyObject *kwnames) |
---|
2480 | n/a | { |
---|
2481 | n/a | if (kwnames == NULL) { |
---|
2482 | n/a | return 1; |
---|
2483 | n/a | } |
---|
2484 | n/a | assert(PyTuple_CheckExact(kwnames)); |
---|
2485 | n/a | if (PyTuple_GET_SIZE(kwnames) == 0) { |
---|
2486 | n/a | return 1; |
---|
2487 | n/a | } |
---|
2488 | n/a | |
---|
2489 | n/a | PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments", |
---|
2490 | n/a | funcname); |
---|
2491 | n/a | return 0; |
---|
2492 | n/a | } |
---|
2493 | n/a | |
---|
2494 | n/a | |
---|
2495 | n/a | int |
---|
2496 | n/a | _PyArg_NoPositional(const char *funcname, PyObject *args) |
---|
2497 | n/a | { |
---|
2498 | n/a | if (args == NULL) |
---|
2499 | n/a | return 1; |
---|
2500 | n/a | if (!PyTuple_CheckExact(args)) { |
---|
2501 | n/a | PyErr_BadInternalCall(); |
---|
2502 | n/a | return 0; |
---|
2503 | n/a | } |
---|
2504 | n/a | if (PyTuple_GET_SIZE(args) == 0) |
---|
2505 | n/a | return 1; |
---|
2506 | n/a | |
---|
2507 | n/a | PyErr_Format(PyExc_TypeError, "%s does not take positional arguments", |
---|
2508 | n/a | funcname); |
---|
2509 | n/a | return 0; |
---|
2510 | n/a | } |
---|
2511 | n/a | |
---|
2512 | n/a | void |
---|
2513 | n/a | _PyArg_Fini(void) |
---|
2514 | n/a | { |
---|
2515 | n/a | struct _PyArg_Parser *tmp, *s = static_arg_parsers; |
---|
2516 | n/a | while (s) { |
---|
2517 | n/a | tmp = s->next; |
---|
2518 | n/a | s->next = NULL; |
---|
2519 | n/a | parser_clear(s); |
---|
2520 | n/a | s = tmp; |
---|
2521 | n/a | } |
---|
2522 | n/a | static_arg_parsers = NULL; |
---|
2523 | n/a | } |
---|
2524 | n/a | |
---|
2525 | n/a | #ifdef __cplusplus |
---|
2526 | n/a | }; |
---|
2527 | n/a | #endif |
---|