|
72475
|
1 /*
|
|
|
2 unicode_format.h -- implementation of str.format().
|
|
|
3 */
|
|
|
4
|
|
|
5
|
|
|
6 /* Defines for more efficiently reallocating the string buffer */
|
|
|
7 #define INITIAL_SIZE_INCREMENT 100
|
|
|
8 #define SIZE_MULTIPLIER 2
|
|
|
9 #define MAX_SIZE_INCREMENT 3200
|
|
|
10
|
|
|
11
|
|
|
12 /************************************************************************/
|
|
|
13 /*********** Global data structures and forward declarations *********/
|
|
|
14 /************************************************************************/
|
|
|
15
|
|
|
16 /*
|
|
|
17 A SubString consists of the characters between two string or
|
|
|
18 unicode pointers.
|
|
|
19 */
|
|
|
20 typedef struct {
|
|
|
21 PyObject *str; /* borrowed reference */
|
|
|
22 Py_ssize_t start, end;
|
|
|
23 } SubString;
|
|
|
24
|
|
|
25
|
|
|
26 typedef enum {
|
|
|
27 ANS_INIT,
|
|
|
28 ANS_AUTO,
|
|
|
29 ANS_MANUAL
|
|
|
30 } AutoNumberState; /* Keep track if we're auto-numbering fields */
|
|
|
31
|
|
|
32 /* Keeps track of our auto-numbering state, and which number field we're on */
|
|
|
33 typedef struct {
|
|
|
34 AutoNumberState an_state;
|
|
|
35 int an_field_number;
|
|
|
36 } AutoNumber;
|
|
|
37
|
|
|
38
|
|
|
39 /* forward declaration for recursion */
|
|
|
40 static PyObject *
|
|
|
41 build_string(SubString *input, PyObject *args, PyObject *kwargs,
|
|
|
42 int recursion_depth, AutoNumber *auto_number);
|
|
|
43
|
|
|
44
|
|
|
45
|
|
|
46 /************************************************************************/
|
|
|
47 /************************** Utility functions ************************/
|
|
|
48 /************************************************************************/
|
|
|
49
|
|
|
50 static void
|
|
|
51 AutoNumber_Init(AutoNumber *auto_number)
|
|
|
52 {
|
|
|
53 auto_number->an_state = ANS_INIT;
|
|
|
54 auto_number->an_field_number = 0;
|
|
|
55 }
|
|
|
56
|
|
|
57 /* fill in a SubString from a pointer and length */
|
|
|
58 Py_LOCAL_INLINE(void)
|
|
|
59 SubString_init(SubString *str, PyObject *s, int start, int end)
|
|
|
60 {
|
|
|
61 str->str = s;
|
|
|
62 str->start = start;
|
|
|
63 str->end = end;
|
|
|
64 }
|
|
|
65
|
|
|
66 /* return a new string. if str->str is NULL, return None */
|
|
|
67 Py_LOCAL_INLINE(PyObject *)
|
|
|
68 SubString_new_object(SubString *str)
|
|
|
69 {
|
|
|
70 if (str->str == NULL) {
|
|
|
71 Py_INCREF(Py_None);
|
|
|
72 return Py_None;
|
|
|
73 }
|
|
|
74 return PyUnicode_Substring(str->str, str->start, str->end);
|
|
|
75 }
|
|
|
76
|
|
|
77 /* return a new string. if str->str is NULL, return None */
|
|
|
78 Py_LOCAL_INLINE(PyObject *)
|
|
|
79 SubString_new_object_or_empty(SubString *str)
|
|
|
80 {
|
|
|
81 if (str->str == NULL) {
|
|
|
82 return PyUnicode_FromUnicode(NULL, 0);
|
|
|
83 }
|
|
|
84 return SubString_new_object(str);
|
|
|
85 }
|
|
|
86
|
|
|
87 /* Return 1 if an error has been detected switching between automatic
|
|
|
88 field numbering and manual field specification, else return 0. Set
|
|
|
89 ValueError on error. */
|
|
|
90 static int
|
|
|
91 autonumber_state_error(AutoNumberState state, int field_name_is_empty)
|
|
|
92 {
|
|
|
93 if (state == ANS_MANUAL) {
|
|
|
94 if (field_name_is_empty) {
|
|
|
95 PyErr_SetString(PyExc_ValueError, "cannot switch from "
|
|
|
96 "manual field specification to "
|
|
|
97 "automatic field numbering");
|
|
|
98 return 1;
|
|
|
99 }
|
|
|
100 }
|
|
|
101 else {
|
|
|
102 if (!field_name_is_empty) {
|
|
|
103 PyErr_SetString(PyExc_ValueError, "cannot switch from "
|
|
|
104 "automatic field numbering to "
|
|
|
105 "manual field specification");
|
|
|
106 return 1;
|
|
|
107 }
|
|
|
108 }
|
|
|
109 return 0;
|
|
|
110 }
|
|
|
111
|
|
|
112
|
|
|
113 /************************************************************************/
|
|
|
114 /*********** Output string management functions ****************/
|
|
|
115 /************************************************************************/
|
|
|
116
|
|
|
117 typedef struct {
|
|
|
118 char *data;
|
|
|
119 Py_UCS4 maxchar;
|
|
|
120 unsigned int kind;
|
|
|
121 Py_ssize_t pos, size;
|
|
|
122 Py_ssize_t size_increment;
|
|
|
123 } OutputString;
|
|
|
124
|
|
|
125 /* initialize an OutputString object, reserving size characters */
|
|
|
126 static int
|
|
|
127 output_initialize(OutputString *output, Py_ssize_t size)
|
|
|
128 {
|
|
|
129 output->data = PyMem_Malloc(size);
|
|
|
130 if (output->data == NULL) {
|
|
|
131 PyErr_NoMemory();
|
|
|
132 return 0;
|
|
|
133 }
|
|
|
134
|
|
|
135 output->maxchar = 127;
|
|
|
136 output->kind = PyUnicode_1BYTE_KIND;
|
|
|
137 output->pos = 0;
|
|
|
138 output->size = size;
|
|
|
139 output->size_increment = INITIAL_SIZE_INCREMENT;
|
|
|
140
|
|
|
141 return 1;
|
|
|
142 }
|
|
|
143
|
|
|
144 /*
|
|
|
145 output_extend reallocates the output string buffer.
|
|
|
146 It returns a status: 0 for a failed reallocation,
|
|
|
147 1 for success.
|
|
|
148 */
|
|
|
149
|
|
|
150 static int
|
|
|
151 output_extend(OutputString *output, Py_ssize_t count)
|
|
|
152 {
|
|
|
153 Py_ssize_t maxlen = output->size + count + output->size_increment;
|
|
|
154
|
|
|
155 output->data = PyMem_Realloc(output->data, maxlen << (output->kind-1));
|
|
|
156 output->size = maxlen;
|
|
|
157 if (output->data == 0) {
|
|
|
158 PyErr_NoMemory();
|
|
|
159 return 0;
|
|
|
160 }
|
|
|
161 if (output->size_increment < MAX_SIZE_INCREMENT)
|
|
|
162 output->size_increment *= SIZE_MULTIPLIER;
|
|
|
163 return 1;
|
|
|
164 }
|
|
|
165
|
|
|
166 static int
|
|
|
167 output_widen(OutputString *output, Py_UCS4 maxchar)
|
|
|
168 {
|
|
|
169 int kind;
|
|
|
170 void *data;
|
|
|
171 Py_ssize_t i;
|
|
|
172 if (maxchar <= output->maxchar)
|
|
|
173 return 1;
|
|
|
174 if (maxchar < 256) {
|
|
|
175 output->maxchar = 255;
|
|
|
176 return 1;
|
|
|
177 }
|
|
|
178 if (maxchar < 65536) {
|
|
|
179 output->maxchar = 65535;
|
|
|
180 kind = 2;
|
|
|
181 }
|
|
|
182 else {
|
|
|
183 output->maxchar = 1<<21;
|
|
|
184 kind = 3;
|
|
|
185 }
|
|
|
186 data = PyMem_Malloc(output->size << (kind-1));
|
|
|
187 if (data == 0)
|
|
|
188 return 0;
|
|
|
189 for (i = 0; i < output->size; i++)
|
|
|
190 PyUnicode_WRITE(kind, data, i,
|
|
|
191 PyUnicode_READ(output->kind, output->data, i));
|
|
|
192 PyMem_Free(output->data);
|
|
|
193 output->data = data;
|
|
|
194 output->kind = kind;
|
|
|
195 return 1;
|
|
|
196 }
|
|
|
197
|
|
|
198 /*
|
|
|
199 output_data dumps characters into our output string
|
|
|
200 buffer.
|
|
|
201
|
|
|
202 In some cases, it has to reallocate the string.
|
|
|
203
|
|
|
204 It returns a status: 0 for a failed reallocation,
|
|
|
205 1 for success.
|
|
|
206 */
|
|
|
207 static int
|
|
|
208 output_data(OutputString *output, PyObject *s, Py_ssize_t start, Py_ssize_t end)
|
|
|
209 {
|
|
|
210 Py_ssize_t i;
|
|
|
211 int kind;
|
|
|
212 if ((output->pos + end - start > output->size) &&
|
|
|
213 !output_extend(output, end - start))
|
|
|
214 return 0;
|
|
|
215 kind = PyUnicode_KIND(s);
|
|
|
216 if (PyUnicode_MAX_CHAR_VALUE(s) > output->maxchar) {
|
|
|
217 Py_UCS4 maxchar = output->maxchar;
|
|
|
218 for (i = start; i < end; i++)
|
|
|
219 if (PyUnicode_READ(kind, PyUnicode_DATA(s), i) > maxchar)
|
|
|
220 maxchar = PyUnicode_READ(kind, PyUnicode_DATA(s), i);
|
|
|
221 if (!output_widen(output, maxchar))
|
|
|
222 return 0;
|
|
|
223 }
|
|
|
224 for (i = start; i < end; i++)
|
|
|
225 PyUnicode_WRITE(output->kind, output->data, output->pos++,
|
|
|
226 PyUnicode_READ(kind, PyUnicode_DATA(s), i));
|
|
|
227 return 1;
|
|
|
228 }
|
|
|
229
|
|
|
230 /************************************************************************/
|
|
|
231 /*********** Format string parsing -- integers and identifiers *********/
|
|
|
232 /************************************************************************/
|
|
|
233
|
|
|
234 static Py_ssize_t
|
|
|
235 get_integer(const SubString *str)
|
|
|
236 {
|
|
|
237 Py_ssize_t accumulator = 0;
|
|
|
238 Py_ssize_t digitval;
|
|
|
239 Py_ssize_t i;
|
|
|
240
|
|
|
241 /* empty string is an error */
|
|
|
242 if (str->start >= str->end)
|
|
|
243 return -1;
|
|
|
244
|
|
|
245 for (i = str->start; i < str->end; i++) {
|
|
|
246 digitval = Py_UNICODE_TODECIMAL(PyUnicode_READ_CHAR(str->str, i));
|
|
|
247 if (digitval < 0)
|
|
|
248 return -1;
|
|
|
249 /*
|
|
|
250 Detect possible overflow before it happens:
|
|
|
251
|
|
|
252 accumulator * 10 + digitval > PY_SSIZE_T_MAX if and only if
|
|
|
253 accumulator > (PY_SSIZE_T_MAX - digitval) / 10.
|
|
|
254 */
|
|
|
255 if (accumulator > (PY_SSIZE_T_MAX - digitval) / 10) {
|
|
|
256 PyErr_Format(PyExc_ValueError,
|
|
|
257 "Too many decimal digits in format string");
|
|
|
258 return -1;
|
|
|
259 }
|
|
|
260 accumulator = accumulator * 10 + digitval;
|
|
|
261 }
|
|
|
262 return accumulator;
|
|
|
263 }
|
|
|
264
|
|
|
265 /************************************************************************/
|
|
|
266 /******** Functions to get field objects and specification strings ******/
|
|
|
267 /************************************************************************/
|
|
|
268
|
|
|
269 /* do the equivalent of obj.name */
|
|
|
270 static PyObject *
|
|
|
271 getattr(PyObject *obj, SubString *name)
|
|
|
272 {
|
|
|
273 PyObject *newobj;
|
|
|
274 PyObject *str = SubString_new_object(name);
|
|
|
275 if (str == NULL)
|
|
|
276 return NULL;
|
|
|
277 newobj = PyObject_GetAttr(obj, str);
|
|
|
278 Py_DECREF(str);
|
|
|
279 return newobj;
|
|
|
280 }
|
|
|
281
|
|
|
282 /* do the equivalent of obj[idx], where obj is a sequence */
|
|
|
283 static PyObject *
|
|
|
284 getitem_sequence(PyObject *obj, Py_ssize_t idx)
|
|
|
285 {
|
|
|
286 return PySequence_GetItem(obj, idx);
|
|
|
287 }
|
|
|
288
|
|
|
289 /* do the equivalent of obj[idx], where obj is not a sequence */
|
|
|
290 static PyObject *
|
|
|
291 getitem_idx(PyObject *obj, Py_ssize_t idx)
|
|
|
292 {
|
|
|
293 PyObject *newobj;
|
|
|
294 PyObject *idx_obj = PyLong_FromSsize_t(idx);
|
|
|
295 if (idx_obj == NULL)
|
|
|
296 return NULL;
|
|
|
297 newobj = PyObject_GetItem(obj, idx_obj);
|
|
|
298 Py_DECREF(idx_obj);
|
|
|
299 return newobj;
|
|
|
300 }
|
|
|
301
|
|
|
302 /* do the equivalent of obj[name] */
|
|
|
303 static PyObject *
|
|
|
304 getitem_str(PyObject *obj, SubString *name)
|
|
|
305 {
|
|
|
306 PyObject *newobj;
|
|
|
307 PyObject *str = SubString_new_object(name);
|
|
|
308 if (str == NULL)
|
|
|
309 return NULL;
|
|
|
310 newobj = PyObject_GetItem(obj, str);
|
|
|
311 Py_DECREF(str);
|
|
|
312 return newobj;
|
|
|
313 }
|
|
|
314
|
|
|
315 typedef struct {
|
|
|
316 /* the entire string we're parsing. we assume that someone else
|
|
|
317 is managing its lifetime, and that it will exist for the
|
|
|
318 lifetime of the iterator. can be empty */
|
|
|
319 SubString str;
|
|
|
320
|
|
|
321 /* index to where we are inside field_name */
|
|
|
322 Py_ssize_t index;
|
|
|
323 } FieldNameIterator;
|
|
|
324
|
|
|
325
|
|
|
326 static int
|
|
|
327 FieldNameIterator_init(FieldNameIterator *self, PyObject *s,
|
|
|
328 Py_ssize_t start, Py_ssize_t end)
|
|
|
329 {
|
|
|
330 SubString_init(&self->str, s, start, end);
|
|
|
331 self->index = start;
|
|
|
332 return 1;
|
|
|
333 }
|
|
|
334
|
|
|
335 static int
|
|
|
336 _FieldNameIterator_attr(FieldNameIterator *self, SubString *name)
|
|
|
337 {
|
|
|
338 Py_UCS4 c;
|
|
|
339
|
|
|
340 name->str = self->str.str;
|
|
|
341 name->start = self->index;
|
|
|
342
|
|
|
343 /* return everything until '.' or '[' */
|
|
|
344 while (self->index < self->str.end) {
|
|
|
345 c = PyUnicode_READ_CHAR(self->str.str, self->index++);
|
|
|
346 switch (c) {
|
|
|
347 case '[':
|
|
|
348 case '.':
|
|
|
349 /* backup so that we this character will be seen next time */
|
|
|
350 self->index--;
|
|
|
351 break;
|
|
|
352 default:
|
|
|
353 continue;
|
|
|
354 }
|
|
|
355 break;
|
|
|
356 }
|
|
|
357 /* end of string is okay */
|
|
|
358 name->end = self->index;
|
|
|
359 return 1;
|
|
|
360 }
|
|
|
361
|
|
|
362 static int
|
|
|
363 _FieldNameIterator_item(FieldNameIterator *self, SubString *name)
|
|
|
364 {
|
|
|
365 int bracket_seen = 0;
|
|
|
366 Py_UCS4 c;
|
|
|
367
|
|
|
368 name->str = self->str.str;
|
|
|
369 name->start = self->index;
|
|
|
370
|
|
|
371 /* return everything until ']' */
|
|
|
372 while (self->index < self->str.end) {
|
|
|
373 c = PyUnicode_READ_CHAR(self->str.str, self->index++);
|
|
|
374 switch (c) {
|
|
|
375 case ']':
|
|
|
376 bracket_seen = 1;
|
|
|
377 break;
|
|
|
378 default:
|
|
|
379 continue;
|
|
|
380 }
|
|
|
381 break;
|
|
|
382 }
|
|
|
383 /* make sure we ended with a ']' */
|
|
|
384 if (!bracket_seen) {
|
|
|
385 PyErr_SetString(PyExc_ValueError, "Missing ']' in format string");
|
|
|
386 return 0;
|
|
|
387 }
|
|
|
388
|
|
|
389 /* end of string is okay */
|
|
|
390 /* don't include the ']' */
|
|
|
391 name->end = self->index-1;
|
|
|
392 return 1;
|
|
|
393 }
|
|
|
394
|
|
|
395 /* returns 0 on error, 1 on non-error termination, and 2 if it returns a value */
|
|
|
396 static int
|
|
|
397 FieldNameIterator_next(FieldNameIterator *self, int *is_attribute,
|
|
|
398 Py_ssize_t *name_idx, SubString *name)
|
|
|
399 {
|
|
|
400 /* check at end of input */
|
|
|
401 if (self->index >= self->str.end)
|
|
|
402 return 1;
|
|
|
403
|
|
|
404 switch (PyUnicode_READ_CHAR(self->str.str, self->index++)) {
|
|
|
405 case '.':
|
|
|
406 *is_attribute = 1;
|
|
|
407 if (_FieldNameIterator_attr(self, name) == 0)
|
|
|
408 return 0;
|
|
|
409 *name_idx = -1;
|
|
|
410 break;
|
|
|
411 case '[':
|
|
|
412 *is_attribute = 0;
|
|
|
413 if (_FieldNameIterator_item(self, name) == 0)
|
|
|
414 return 0;
|
|
|
415 *name_idx = get_integer(name);
|
|
|
416 if (*name_idx == -1 && PyErr_Occurred())
|
|
|
417 return 0;
|
|
|
418 break;
|
|
|
419 default:
|
|
|
420 /* Invalid character follows ']' */
|
|
|
421 PyErr_SetString(PyExc_ValueError, "Only '.' or '[' may "
|
|
|
422 "follow ']' in format field specifier");
|
|
|
423 return 0;
|
|
|
424 }
|
|
|
425
|
|
|
426 /* empty string is an error */
|
|
|
427 if (name->start == name->end) {
|
|
|
428 PyErr_SetString(PyExc_ValueError, "Empty attribute in format string");
|
|
|
429 return 0;
|
|
|
430 }
|
|
|
431
|
|
|
432 return 2;
|
|
|
433 }
|
|
|
434
|
|
|
435
|
|
|
436 /* input: field_name
|
|
|
437 output: 'first' points to the part before the first '[' or '.'
|
|
|
438 'first_idx' is -1 if 'first' is not an integer, otherwise
|
|
|
439 it's the value of first converted to an integer
|
|
|
440 'rest' is an iterator to return the rest
|
|
|
441 */
|
|
|
442 static int
|
|
|
443 field_name_split(PyObject *str, Py_ssize_t start, Py_ssize_t end, SubString *first,
|
|
|
444 Py_ssize_t *first_idx, FieldNameIterator *rest,
|
|
|
445 AutoNumber *auto_number)
|
|
|
446 {
|
|
|
447 Py_UCS4 c;
|
|
|
448 Py_ssize_t i = start;
|
|
|
449 int field_name_is_empty;
|
|
|
450 int using_numeric_index;
|
|
|
451
|
|
|
452 /* find the part up until the first '.' or '[' */
|
|
|
453 while (i < end) {
|
|
|
454 switch (c = PyUnicode_READ_CHAR(str, i++)) {
|
|
|
455 case '[':
|
|
|
456 case '.':
|
|
|
457 /* backup so that we this character is available to the
|
|
|
458 "rest" iterator */
|
|
|
459 i--;
|
|
|
460 break;
|
|
|
461 default:
|
|
|
462 continue;
|
|
|
463 }
|
|
|
464 break;
|
|
|
465 }
|
|
|
466
|
|
|
467 /* set up the return values */
|
|
|
468 SubString_init(first, str, start, i);
|
|
|
469 FieldNameIterator_init(rest, str, i, end);
|
|
|
470
|
|
|
471 /* see if "first" is an integer, in which case it's used as an index */
|
|
|
472 *first_idx = get_integer(first);
|
|
|
473 if (*first_idx == -1 && PyErr_Occurred())
|
|
|
474 return 0;
|
|
|
475
|
|
|
476 field_name_is_empty = first->start >= first->end;
|
|
|
477
|
|
|
478 /* If the field name is omitted or if we have a numeric index
|
|
|
479 specified, then we're doing numeric indexing into args. */
|
|
|
480 using_numeric_index = field_name_is_empty || *first_idx != -1;
|
|
|
481
|
|
|
482 /* We always get here exactly one time for each field we're
|
|
|
483 processing. And we get here in field order (counting by left
|
|
|
484 braces). So this is the perfect place to handle automatic field
|
|
|
485 numbering if the field name is omitted. */
|
|
|
486
|
|
|
487 /* Check if we need to do the auto-numbering. It's not needed if
|
|
|
488 we're called from string.Format routines, because it's handled
|
|
|
489 in that class by itself. */
|
|
|
490 if (auto_number) {
|
|
|
491 /* Initialize our auto numbering state if this is the first
|
|
|
492 time we're either auto-numbering or manually numbering. */
|
|
|
493 if (auto_number->an_state == ANS_INIT && using_numeric_index)
|
|
|
494 auto_number->an_state = field_name_is_empty ?
|
|
|
495 ANS_AUTO : ANS_MANUAL;
|
|
|
496
|
|
|
497 /* Make sure our state is consistent with what we're doing
|
|
|
498 this time through. Only check if we're using a numeric
|
|
|
499 index. */
|
|
|
500 if (using_numeric_index)
|
|
|
501 if (autonumber_state_error(auto_number->an_state,
|
|
|
502 field_name_is_empty))
|
|
|
503 return 0;
|
|
|
504 /* Zero length field means we want to do auto-numbering of the
|
|
|
505 fields. */
|
|
|
506 if (field_name_is_empty)
|
|
|
507 *first_idx = (auto_number->an_field_number)++;
|
|
|
508 }
|
|
|
509
|
|
|
510 return 1;
|
|
|
511 }
|
|
|
512
|
|
|
513
|
|
|
514 /*
|
|
|
515 get_field_object returns the object inside {}, before the
|
|
|
516 format_spec. It handles getindex and getattr lookups and consumes
|
|
|
517 the entire input string.
|
|
|
518 */
|
|
|
519 static PyObject *
|
|
|
520 get_field_object(SubString *input, PyObject *args, PyObject *kwargs,
|
|
|
521 AutoNumber *auto_number)
|
|
|
522 {
|
|
|
523 PyObject *obj = NULL;
|
|
|
524 int ok;
|
|
|
525 int is_attribute;
|
|
|
526 SubString name;
|
|
|
527 SubString first;
|
|
|
528 Py_ssize_t index;
|
|
|
529 FieldNameIterator rest;
|
|
|
530
|
|
|
531 if (!field_name_split(input->str, input->start, input->end, &first,
|
|
|
532 &index, &rest, auto_number)) {
|
|
|
533 goto error;
|
|
|
534 }
|
|
|
535
|
|
|
536 if (index == -1) {
|
|
|
537 /* look up in kwargs */
|
|
|
538 PyObject *key = SubString_new_object(&first);
|
|
|
539 if (key == NULL)
|
|
|
540 goto error;
|
|
|
541
|
|
|
542 /* Use PyObject_GetItem instead of PyDict_GetItem because this
|
|
|
543 code is no longer just used with kwargs. It might be passed
|
|
|
544 a non-dict when called through format_map. */
|
|
|
545 if ((kwargs == NULL) || (obj = PyObject_GetItem(kwargs, key)) == NULL) {
|
|
|
546 PyErr_SetObject(PyExc_KeyError, key);
|
|
|
547 Py_DECREF(key);
|
|
|
548 goto error;
|
|
|
549 }
|
|
|
550 Py_DECREF(key);
|
|
|
551 }
|
|
|
552 else {
|
|
|
553 /* If args is NULL, we have a format string with a positional field
|
|
|
554 with only kwargs to retrieve it from. This can only happen when
|
|
|
555 used with format_map(), where positional arguments are not
|
|
|
556 allowed. */
|
|
|
557 if (args == NULL) {
|
|
|
558 PyErr_SetString(PyExc_ValueError, "Format string contains "
|
|
|
559 "positional fields");
|
|
|
560 goto error;
|
|
|
561 }
|
|
|
562
|
|
|
563 /* look up in args */
|
|
|
564 obj = PySequence_GetItem(args, index);
|
|
|
565 if (obj == NULL)
|
|
|
566 goto error;
|
|
|
567 }
|
|
|
568
|
|
|
569 /* iterate over the rest of the field_name */
|
|
|
570 while ((ok = FieldNameIterator_next(&rest, &is_attribute, &index,
|
|
|
571 &name)) == 2) {
|
|
|
572 PyObject *tmp;
|
|
|
573
|
|
|
574 if (is_attribute)
|
|
|
575 /* getattr lookup "." */
|
|
|
576 tmp = getattr(obj, &name);
|
|
|
577 else
|
|
|
578 /* getitem lookup "[]" */
|
|
|
579 if (index == -1)
|
|
|
580 tmp = getitem_str(obj, &name);
|
|
|
581 else
|
|
|
582 if (PySequence_Check(obj))
|
|
|
583 tmp = getitem_sequence(obj, index);
|
|
|
584 else
|
|
|
585 /* not a sequence */
|
|
|
586 tmp = getitem_idx(obj, index);
|
|
|
587 if (tmp == NULL)
|
|
|
588 goto error;
|
|
|
589
|
|
|
590 /* assign to obj */
|
|
|
591 Py_DECREF(obj);
|
|
|
592 obj = tmp;
|
|
|
593 }
|
|
|
594 /* end of iterator, this is the non-error case */
|
|
|
595 if (ok == 1)
|
|
|
596 return obj;
|
|
|
597 error:
|
|
|
598 Py_XDECREF(obj);
|
|
|
599 return NULL;
|
|
|
600 }
|
|
|
601
|
|
|
602 /************************************************************************/
|
|
|
603 /***************** Field rendering functions **************************/
|
|
|
604 /************************************************************************/
|
|
|
605
|
|
|
606 /*
|
|
|
607 render_field() is the main function in this section. It takes the
|
|
|
608 field object and field specification string generated by
|
|
|
609 get_field_and_spec, and renders the field into the output string.
|
|
|
610
|
|
|
611 render_field calls fieldobj.__format__(format_spec) method, and
|
|
|
612 appends to the output.
|
|
|
613 */
|
|
|
614 static int
|
|
|
615 render_field(PyObject *fieldobj, SubString *format_spec, OutputString *output)
|
|
|
616 {
|
|
|
617 int ok = 0;
|
|
|
618 PyObject *result = NULL;
|
|
|
619 PyObject *format_spec_object = NULL;
|
|
|
620 PyObject *(*formatter)(PyObject *, PyObject *, Py_ssize_t, Py_ssize_t) = NULL;
|
|
|
621
|
|
|
622 /* If we know the type exactly, skip the lookup of __format__ and just
|
|
|
623 call the formatter directly. */
|
|
|
624 if (PyUnicode_CheckExact(fieldobj))
|
|
|
625 formatter = _PyUnicode_FormatAdvanced;
|
|
|
626 else if (PyLong_CheckExact(fieldobj))
|
|
|
627 formatter =_PyLong_FormatAdvanced;
|
|
|
628 else if (PyFloat_CheckExact(fieldobj))
|
|
|
629 formatter = _PyFloat_FormatAdvanced;
|
|
|
630
|
|
|
631 /* XXX: for 2.6, convert format_spec to the appropriate type
|
|
|
632 (unicode, str) */
|
|
|
633
|
|
|
634 if (formatter) {
|
|
|
635 /* we know exactly which formatter will be called when __format__ is
|
|
|
636 looked up, so call it directly, instead. */
|
|
|
637 result = formatter(fieldobj, format_spec->str,
|
|
|
638 format_spec->start, format_spec->end);
|
|
|
639 }
|
|
|
640 else {
|
|
|
641 /* We need to create an object out of the pointers we have, because
|
|
|
642 __format__ takes a string/unicode object for format_spec. */
|
|
|
643 if (format_spec->str)
|
|
|
644 format_spec_object = PyUnicode_Substring(format_spec->str,
|
|
|
645 format_spec->start,
|
|
|
646 format_spec->end);
|
|
|
647 else
|
|
|
648 format_spec_object = PyUnicode_New(0, 0);
|
|
|
649 if (format_spec_object == NULL)
|
|
|
650 goto done;
|
|
|
651
|
|
|
652 result = PyObject_Format(fieldobj, format_spec_object);
|
|
|
653 }
|
|
|
654 if (result == NULL || PyUnicode_READY(result) == -1)
|
|
|
655 goto done;
|
|
|
656
|
|
|
657 assert(PyUnicode_Check(result));
|
|
|
658 ok = output_data(output, result, 0, PyUnicode_GET_LENGTH(result));
|
|
|
659 done:
|
|
|
660 Py_XDECREF(format_spec_object);
|
|
|
661 Py_XDECREF(result);
|
|
|
662 return ok;
|
|
|
663 }
|
|
|
664
|
|
|
665 static int
|
|
|
666 parse_field(SubString *str, SubString *field_name, SubString *format_spec,
|
|
|
667 Py_UCS4 *conversion)
|
|
|
668 {
|
|
|
669 /* Note this function works if the field name is zero length,
|
|
|
670 which is good. Zero length field names are handled later, in
|
|
|
671 field_name_split. */
|
|
|
672
|
|
|
673 Py_UCS4 c = 0;
|
|
|
674
|
|
|
675 /* initialize these, as they may be empty */
|
|
|
676 *conversion = '\0';
|
|
|
677 SubString_init(format_spec, NULL, 0, 0);
|
|
|
678
|
|
|
679 /* Search for the field name. it's terminated by the end of
|
|
|
680 the string, or a ':' or '!' */
|
|
|
681 field_name->str = str->str;
|
|
|
682 field_name->start = str->start;
|
|
|
683 while (str->start < str->end) {
|
|
|
684 switch ((c = PyUnicode_READ_CHAR(str->str, str->start++))) {
|
|
|
685 case ':':
|
|
|
686 case '!':
|
|
|
687 break;
|
|
|
688 default:
|
|
|
689 continue;
|
|
|
690 }
|
|
|
691 break;
|
|
|
692 }
|
|
|
693
|
|
|
694 if (c == '!' || c == ':') {
|
|
|
695 /* we have a format specifier and/or a conversion */
|
|
|
696 /* don't include the last character */
|
|
|
697 field_name->end = str->start-1;
|
|
|
698
|
|
|
699 /* the format specifier is the rest of the string */
|
|
|
700 format_spec->str = str->str;
|
|
|
701 format_spec->start = str->start;
|
|
|
702 format_spec->end = str->end;
|
|
|
703
|
|
|
704 /* see if there's a conversion specifier */
|
|
|
705 if (c == '!') {
|
|
|
706 /* there must be another character present */
|
|
|
707 if (format_spec->start >= format_spec->end) {
|
|
|
708 PyErr_SetString(PyExc_ValueError,
|
|
|
709 "end of format while looking for conversion "
|
|
|
710 "specifier");
|
|
|
711 return 0;
|
|
|
712 }
|
|
|
713 *conversion = PyUnicode_READ_CHAR(format_spec->str, format_spec->start++);
|
|
|
714
|
|
|
715 /* if there is another character, it must be a colon */
|
|
|
716 if (format_spec->start < format_spec->end) {
|
|
|
717 c = PyUnicode_READ_CHAR(format_spec->str, format_spec->start++);
|
|
|
718 if (c != ':') {
|
|
|
719 PyErr_SetString(PyExc_ValueError,
|
|
|
720 "expected ':' after format specifier");
|
|
|
721 return 0;
|
|
|
722 }
|
|
|
723 }
|
|
|
724 }
|
|
|
725 }
|
|
|
726 else
|
|
|
727 /* end of string, there's no format_spec or conversion */
|
|
|
728 field_name->end = str->start;
|
|
|
729
|
|
|
730 return 1;
|
|
|
731 }
|
|
|
732
|
|
|
733 /************************************************************************/
|
|
|
734 /******* Output string allocation and escape-to-markup processing ******/
|
|
|
735 /************************************************************************/
|
|
|
736
|
|
|
737 /* MarkupIterator breaks the string into pieces of either literal
|
|
|
738 text, or things inside {} that need to be marked up. it is
|
|
|
739 designed to make it easy to wrap a Python iterator around it, for
|
|
|
740 use with the Formatter class */
|
|
|
741
|
|
|
742 typedef struct {
|
|
|
743 SubString str;
|
|
|
744 } MarkupIterator;
|
|
|
745
|
|
|
746 static int
|
|
|
747 MarkupIterator_init(MarkupIterator *self, PyObject *str,
|
|
|
748 Py_ssize_t start, Py_ssize_t end)
|
|
|
749 {
|
|
|
750 SubString_init(&self->str, str, start, end);
|
|
|
751 return 1;
|
|
|
752 }
|
|
|
753
|
|
|
754 /* returns 0 on error, 1 on non-error termination, and 2 if it got a
|
|
|
755 string (or something to be expanded) */
|
|
|
756 static int
|
|
|
757 MarkupIterator_next(MarkupIterator *self, SubString *literal,
|
|
|
758 int *field_present, SubString *field_name,
|
|
|
759 SubString *format_spec, Py_UCS4 *conversion,
|
|
|
760 int *format_spec_needs_expanding)
|
|
|
761 {
|
|
|
762 int at_end;
|
|
|
763 Py_UCS4 c = 0;
|
|
|
764 Py_ssize_t start;
|
|
|
765 int count;
|
|
|
766 Py_ssize_t len;
|
|
|
767 int markup_follows = 0;
|
|
|
768
|
|
|
769 /* initialize all of the output variables */
|
|
|
770 SubString_init(literal, NULL, 0, 0);
|
|
|
771 SubString_init(field_name, NULL, 0, 0);
|
|
|
772 SubString_init(format_spec, NULL, 0, 0);
|
|
|
773 *conversion = '\0';
|
|
|
774 *format_spec_needs_expanding = 0;
|
|
|
775 *field_present = 0;
|
|
|
776
|
|
|
777 /* No more input, end of iterator. This is the normal exit
|
|
|
778 path. */
|
|
|
779 if (self->str.start >= self->str.end)
|
|
|
780 return 1;
|
|
|
781
|
|
|
782 start = self->str.start;
|
|
|
783
|
|
|
784 /* First read any literal text. Read until the end of string, an
|
|
|
785 escaped '{' or '}', or an unescaped '{'. In order to never
|
|
|
786 allocate memory and so I can just pass pointers around, if
|
|
|
787 there's an escaped '{' or '}' then we'll return the literal
|
|
|
788 including the brace, but no format object. The next time
|
|
|
789 through, we'll return the rest of the literal, skipping past
|
|
|
790 the second consecutive brace. */
|
|
|
791 while (self->str.start < self->str.end) {
|
|
|
792 switch (c = PyUnicode_READ_CHAR(self->str.str, self->str.start++)) {
|
|
|
793 case '{':
|
|
|
794 case '}':
|
|
|
795 markup_follows = 1;
|
|
|
796 break;
|
|
|
797 default:
|
|
|
798 continue;
|
|
|
799 }
|
|
|
800 break;
|
|
|
801 }
|
|
|
802
|
|
|
803 at_end = self->str.start >= self->str.end;
|
|
|
804 len = self->str.start - start;
|
|
|
805
|
|
|
806 if ((c == '}') && (at_end ||
|
|
|
807 (c != PyUnicode_READ_CHAR(self->str.str,
|
|
|
808 self->str.start)))) {
|
|
|
809 PyErr_SetString(PyExc_ValueError, "Single '}' encountered "
|
|
|
810 "in format string");
|
|
|
811 return 0;
|
|
|
812 }
|
|
|
813 if (at_end && c == '{') {
|
|
|
814 PyErr_SetString(PyExc_ValueError, "Single '{' encountered "
|
|
|
815 "in format string");
|
|
|
816 return 0;
|
|
|
817 }
|
|
|
818 if (!at_end) {
|
|
|
819 if (c == PyUnicode_READ_CHAR(self->str.str, self->str.start)) {
|
|
|
820 /* escaped } or {, skip it in the input. there is no
|
|
|
821 markup object following us, just this literal text */
|
|
|
822 self->str.start++;
|
|
|
823 markup_follows = 0;
|
|
|
824 }
|
|
|
825 else
|
|
|
826 len--;
|
|
|
827 }
|
|
|
828
|
|
|
829 /* record the literal text */
|
|
|
830 literal->str = self->str.str;
|
|
|
831 literal->start = start;
|
|
|
832 literal->end = start + len;
|
|
|
833
|
|
|
834 if (!markup_follows)
|
|
|
835 return 2;
|
|
|
836
|
|
|
837 /* this is markup, find the end of the string by counting nested
|
|
|
838 braces. note that this prohibits escaped braces, so that
|
|
|
839 format_specs cannot have braces in them. */
|
|
|
840 *field_present = 1;
|
|
|
841 count = 1;
|
|
|
842
|
|
|
843 start = self->str.start;
|
|
|
844
|
|
|
845 /* we know we can't have a zero length string, so don't worry
|
|
|
846 about that case */
|
|
|
847 while (self->str.start < self->str.end) {
|
|
|
848 switch (c = PyUnicode_READ_CHAR(self->str.str, self->str.start++)) {
|
|
|
849 case '{':
|
|
|
850 /* the format spec needs to be recursively expanded.
|
|
|
851 this is an optimization, and not strictly needed */
|
|
|
852 *format_spec_needs_expanding = 1;
|
|
|
853 count++;
|
|
|
854 break;
|
|
|
855 case '}':
|
|
|
856 count--;
|
|
|
857 if (count <= 0) {
|
|
|
858 /* we're done. parse and get out */
|
|
|
859 SubString s;
|
|
|
860
|
|
|
861 SubString_init(&s, self->str.str, start, self->str.start - 1);
|
|
|
862 if (parse_field(&s, field_name, format_spec, conversion) == 0)
|
|
|
863 return 0;
|
|
|
864
|
|
|
865 /* success */
|
|
|
866 return 2;
|
|
|
867 }
|
|
|
868 break;
|
|
|
869 }
|
|
|
870 }
|
|
|
871
|
|
|
872 /* end of string while searching for matching '}' */
|
|
|
873 PyErr_SetString(PyExc_ValueError, "unmatched '{' in format");
|
|
|
874 return 0;
|
|
|
875 }
|
|
|
876
|
|
|
877
|
|
|
878 /* do the !r or !s conversion on obj */
|
|
|
879 static PyObject *
|
|
|
880 do_conversion(PyObject *obj, Py_UCS4 conversion)
|
|
|
881 {
|
|
|
882 /* XXX in pre-3.0, do we need to convert this to unicode, since it
|
|
|
883 might have returned a string? */
|
|
|
884 switch (conversion) {
|
|
|
885 case 'r':
|
|
|
886 return PyObject_Repr(obj);
|
|
|
887 case 's':
|
|
|
888 return PyObject_Str(obj);
|
|
|
889 case 'a':
|
|
|
890 return PyObject_ASCII(obj);
|
|
|
891 default:
|
|
|
892 if (conversion > 32 && conversion < 127) {
|
|
|
893 /* It's the ASCII subrange; casting to char is safe
|
|
|
894 (assuming the execution character set is an ASCII
|
|
|
895 superset). */
|
|
|
896 PyErr_Format(PyExc_ValueError,
|
|
|
897 "Unknown conversion specifier %c",
|
|
|
898 (char)conversion);
|
|
|
899 } else
|
|
|
900 PyErr_Format(PyExc_ValueError,
|
|
|
901 "Unknown conversion specifier \\x%x",
|
|
|
902 (unsigned int)conversion);
|
|
|
903 return NULL;
|
|
|
904 }
|
|
|
905 }
|
|
|
906
|
|
|
907 /* given:
|
|
|
908
|
|
|
909 {field_name!conversion:format_spec}
|
|
|
910
|
|
|
911 compute the result and write it to output.
|
|
|
912 format_spec_needs_expanding is an optimization. if it's false,
|
|
|
913 just output the string directly, otherwise recursively expand the
|
|
|
914 format_spec string.
|
|
|
915
|
|
|
916 field_name is allowed to be zero length, in which case we
|
|
|
917 are doing auto field numbering.
|
|
|
918 */
|
|
|
919
|
|
|
920 static int
|
|
|
921 output_markup(SubString *field_name, SubString *format_spec,
|
|
|
922 int format_spec_needs_expanding, Py_UCS4 conversion,
|
|
|
923 OutputString *output, PyObject *args, PyObject *kwargs,
|
|
|
924 int recursion_depth, AutoNumber *auto_number)
|
|
|
925 {
|
|
|
926 PyObject *tmp = NULL;
|
|
|
927 PyObject *fieldobj = NULL;
|
|
|
928 SubString expanded_format_spec;
|
|
|
929 SubString *actual_format_spec;
|
|
|
930 int result = 0;
|
|
|
931
|
|
|
932 /* convert field_name to an object */
|
|
|
933 fieldobj = get_field_object(field_name, args, kwargs, auto_number);
|
|
|
934 if (fieldobj == NULL)
|
|
|
935 goto done;
|
|
|
936
|
|
|
937 if (conversion != '\0') {
|
|
|
938 tmp = do_conversion(fieldobj, conversion);
|
|
|
939 if (tmp == NULL || PyUnicode_READY(tmp) == -1)
|
|
|
940 goto done;
|
|
|
941
|
|
|
942 /* do the assignment, transferring ownership: fieldobj = tmp */
|
|
|
943 Py_DECREF(fieldobj);
|
|
|
944 fieldobj = tmp;
|
|
|
945 tmp = NULL;
|
|
|
946 }
|
|
|
947
|
|
|
948 /* if needed, recurively compute the format_spec */
|
|
|
949 if (format_spec_needs_expanding) {
|
|
|
950 tmp = build_string(format_spec, args, kwargs, recursion_depth-1,
|
|
|
951 auto_number);
|
|
|
952 if (tmp == NULL || PyUnicode_READY(tmp) == -1)
|
|
|
953 goto done;
|
|
|
954
|
|
|
955 /* note that in the case we're expanding the format string,
|
|
|
956 tmp must be kept around until after the call to
|
|
|
957 render_field. */
|
|
|
958 SubString_init(&expanded_format_spec, tmp, 0, PyUnicode_GET_LENGTH(tmp));
|
|
|
959 actual_format_spec = &expanded_format_spec;
|
|
|
960 }
|
|
|
961 else
|
|
|
962 actual_format_spec = format_spec;
|
|
|
963
|
|
|
964 if (render_field(fieldobj, actual_format_spec, output) == 0)
|
|
|
965 goto done;
|
|
|
966
|
|
|
967 result = 1;
|
|
|
968
|
|
|
969 done:
|
|
|
970 Py_XDECREF(fieldobj);
|
|
|
971 Py_XDECREF(tmp);
|
|
|
972
|
|
|
973 return result;
|
|
|
974 }
|
|
|
975
|
|
|
976 /*
|
|
|
977 do_markup is the top-level loop for the format() method. It
|
|
|
978 searches through the format string for escapes to markup codes, and
|
|
|
979 calls other functions to move non-markup text to the output,
|
|
|
980 and to perform the markup to the output.
|
|
|
981 */
|
|
|
982 static int
|
|
|
983 do_markup(SubString *input, PyObject *args, PyObject *kwargs,
|
|
|
984 OutputString *output, int recursion_depth, AutoNumber *auto_number)
|
|
|
985 {
|
|
|
986 MarkupIterator iter;
|
|
|
987 int format_spec_needs_expanding;
|
|
|
988 int result;
|
|
|
989 int field_present;
|
|
|
990 SubString literal;
|
|
|
991 SubString field_name;
|
|
|
992 SubString format_spec;
|
|
|
993 Py_UCS4 conversion;
|
|
|
994
|
|
|
995 MarkupIterator_init(&iter, input->str, input->start, input->end);
|
|
|
996 while ((result = MarkupIterator_next(&iter, &literal, &field_present,
|
|
|
997 &field_name, &format_spec,
|
|
|
998 &conversion,
|
|
|
999 &format_spec_needs_expanding)) == 2) {
|
|
|
1000 if (!output_data(output, literal.str, literal.start, literal.end))
|
|
|
1001 return 0;
|
|
|
1002 if (field_present)
|
|
|
1003 if (!output_markup(&field_name, &format_spec,
|
|
|
1004 format_spec_needs_expanding, conversion, output,
|
|
|
1005 args, kwargs, recursion_depth, auto_number))
|
|
|
1006 return 0;
|
|
|
1007 }
|
|
|
1008 return result;
|
|
|
1009 }
|
|
|
1010
|
|
|
1011
|
|
|
1012 /*
|
|
|
1013 build_string allocates the output string and then
|
|
|
1014 calls do_markup to do the heavy lifting.
|
|
|
1015 */
|
|
|
1016 static PyObject *
|
|
|
1017 build_string(SubString *input, PyObject *args, PyObject *kwargs,
|
|
|
1018 int recursion_depth, AutoNumber *auto_number)
|
|
|
1019 {
|
|
|
1020 OutputString output;
|
|
|
1021 PyObject *result = NULL;
|
|
|
1022
|
|
|
1023 output.data = NULL; /* needed so cleanup code always works */
|
|
|
1024
|
|
|
1025 /* check the recursion level */
|
|
|
1026 if (recursion_depth <= 0) {
|
|
|
1027 PyErr_SetString(PyExc_ValueError,
|
|
|
1028 "Max string recursion exceeded");
|
|
|
1029 goto done;
|
|
|
1030 }
|
|
|
1031
|
|
|
1032 /* initial size is the length of the format string, plus the size
|
|
|
1033 increment. seems like a reasonable default */
|
|
|
1034 if (!output_initialize(&output,
|
|
|
1035 input->end - input->start +
|
|
|
1036 INITIAL_SIZE_INCREMENT))
|
|
|
1037 goto done;
|
|
|
1038
|
|
|
1039 if (!do_markup(input, args, kwargs, &output, recursion_depth,
|
|
|
1040 auto_number)) {
|
|
|
1041 goto done;
|
|
|
1042 }
|
|
|
1043
|
|
|
1044 result = PyUnicode_New(output.pos, output.maxchar);
|
|
|
1045 if (!result)
|
|
|
1046 goto done;
|
|
|
1047 memcpy(PyUnicode_DATA(result), output.data, output.pos << (output.kind-1));
|
|
|
1048
|
|
|
1049 done:
|
|
|
1050 if (output.data)
|
|
|
1051 PyMem_Free(output.data);
|
|
|
1052 return result;
|
|
|
1053 }
|
|
|
1054
|
|
|
1055 /************************************************************************/
|
|
|
1056 /*********** main routine ***********************************************/
|
|
|
1057 /************************************************************************/
|
|
|
1058
|
|
|
1059 /* this is the main entry point */
|
|
|
1060 static PyObject *
|
|
|
1061 do_string_format(PyObject *self, PyObject *args, PyObject *kwargs)
|
|
|
1062 {
|
|
|
1063 SubString input;
|
|
|
1064
|
|
|
1065 /* PEP 3101 says only 2 levels, so that
|
|
|
1066 "{0:{1}}".format('abc', 's') # works
|
|
|
1067 "{0:{1:{2}}}".format('abc', 's', '') # fails
|
|
|
1068 */
|
|
|
1069 int recursion_depth = 2;
|
|
|
1070
|
|
|
1071 AutoNumber auto_number;
|
|
|
1072
|
|
|
1073 if (PyUnicode_READY(self) == -1)
|
|
|
1074 return NULL;
|
|
|
1075
|
|
|
1076 AutoNumber_Init(&auto_number);
|
|
|
1077 SubString_init(&input, self, 0, PyUnicode_GET_LENGTH(self));
|
|
|
1078 return build_string(&input, args, kwargs, recursion_depth, &auto_number);
|
|
|
1079 }
|
|
|
1080
|
|
|
1081 static PyObject *
|
|
|
1082 do_string_format_map(PyObject *self, PyObject *obj)
|
|
|
1083 {
|
|
|
1084 return do_string_format(self, NULL, obj);
|
|
|
1085 }
|
|
|
1086
|
|
|
1087
|
|
|
1088 /************************************************************************/
|
|
|
1089 /*********** formatteriterator ******************************************/
|
|
|
1090 /************************************************************************/
|
|
|
1091
|
|
|
1092 /* This is used to implement string.Formatter.vparse(). It exists so
|
|
|
1093 Formatter can share code with the built in unicode.format() method.
|
|
|
1094 It's really just a wrapper around MarkupIterator that is callable
|
|
|
1095 from Python. */
|
|
|
1096
|
|
|
1097 typedef struct {
|
|
|
1098 PyObject_HEAD
|
|
|
1099
|
|
|
1100 PyUnicodeObject *str;
|
|
|
1101
|
|
|
1102 MarkupIterator it_markup;
|
|
|
1103 } formatteriterobject;
|
|
|
1104
|
|
|
1105 static void
|
|
|
1106 formatteriter_dealloc(formatteriterobject *it)
|
|
|
1107 {
|
|
|
1108 Py_XDECREF(it->str);
|
|
|
1109 PyObject_FREE(it);
|
|
|
1110 }
|
|
|
1111
|
|
|
1112 /* returns a tuple:
|
|
|
1113 (literal, field_name, format_spec, conversion)
|
|
|
1114
|
|
|
1115 literal is any literal text to output. might be zero length
|
|
|
1116 field_name is the string before the ':'. might be None
|
|
|
1117 format_spec is the string after the ':'. mibht be None
|
|
|
1118 conversion is either None, or the string after the '!'
|
|
|
1119 */
|
|
|
1120 static PyObject *
|
|
|
1121 formatteriter_next(formatteriterobject *it)
|
|
|
1122 {
|
|
|
1123 SubString literal;
|
|
|
1124 SubString field_name;
|
|
|
1125 SubString format_spec;
|
|
|
1126 Py_UCS4 conversion;
|
|
|
1127 int format_spec_needs_expanding;
|
|
|
1128 int field_present;
|
|
|
1129 int result = MarkupIterator_next(&it->it_markup, &literal, &field_present,
|
|
|
1130 &field_name, &format_spec, &conversion,
|
|
|
1131 &format_spec_needs_expanding);
|
|
|
1132
|
|
|
1133 /* all of the SubString objects point into it->str, so no
|
|
|
1134 memory management needs to be done on them */
|
|
|
1135 assert(0 <= result && result <= 2);
|
|
|
1136 if (result == 0 || result == 1)
|
|
|
1137 /* if 0, error has already been set, if 1, iterator is empty */
|
|
|
1138 return NULL;
|
|
|
1139 else {
|
|
|
1140 PyObject *literal_str = NULL;
|
|
|
1141 PyObject *field_name_str = NULL;
|
|
|
1142 PyObject *format_spec_str = NULL;
|
|
|
1143 PyObject *conversion_str = NULL;
|
|
|
1144 PyObject *tuple = NULL;
|
|
|
1145
|
|
|
1146 literal_str = SubString_new_object(&literal);
|
|
|
1147 if (literal_str == NULL)
|
|
|
1148 goto done;
|
|
|
1149
|
|
|
1150 field_name_str = SubString_new_object(&field_name);
|
|
|
1151 if (field_name_str == NULL)
|
|
|
1152 goto done;
|
|
|
1153
|
|
|
1154 /* if field_name is non-zero length, return a string for
|
|
|
1155 format_spec (even if zero length), else return None */
|
|
|
1156 format_spec_str = (field_present ?
|
|
|
1157 SubString_new_object_or_empty :
|
|
|
1158 SubString_new_object)(&format_spec);
|
|
|
1159 if (format_spec_str == NULL)
|
|
|
1160 goto done;
|
|
|
1161
|
|
|
1162 /* if the conversion is not specified, return a None,
|
|
|
1163 otherwise create a one length string with the conversion
|
|
|
1164 character */
|
|
|
1165 if (conversion == '\0') {
|
|
|
1166 conversion_str = Py_None;
|
|
|
1167 Py_INCREF(conversion_str);
|
|
|
1168 }
|
|
|
1169 else
|
|
|
1170 conversion_str = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
|
|
|
1171 &conversion, 1);
|
|
|
1172 if (conversion_str == NULL)
|
|
|
1173 goto done;
|
|
|
1174
|
|
|
1175 tuple = PyTuple_Pack(4, literal_str, field_name_str, format_spec_str,
|
|
|
1176 conversion_str);
|
|
|
1177 done:
|
|
|
1178 Py_XDECREF(literal_str);
|
|
|
1179 Py_XDECREF(field_name_str);
|
|
|
1180 Py_XDECREF(format_spec_str);
|
|
|
1181 Py_XDECREF(conversion_str);
|
|
|
1182 return tuple;
|
|
|
1183 }
|
|
|
1184 }
|
|
|
1185
|
|
|
1186 static PyMethodDef formatteriter_methods[] = {
|
|
|
1187 {NULL, NULL} /* sentinel */
|
|
|
1188 };
|
|
|
1189
|
|
|
1190 static PyTypeObject PyFormatterIter_Type = {
|
|
|
1191 PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
1192 "formatteriterator", /* tp_name */
|
|
|
1193 sizeof(formatteriterobject), /* tp_basicsize */
|
|
|
1194 0, /* tp_itemsize */
|
|
|
1195 /* methods */
|
|
|
1196 (destructor)formatteriter_dealloc, /* tp_dealloc */
|
|
|
1197 0, /* tp_print */
|
|
|
1198 0, /* tp_getattr */
|
|
|
1199 0, /* tp_setattr */
|
|
|
1200 0, /* tp_reserved */
|
|
|
1201 0, /* tp_repr */
|
|
|
1202 0, /* tp_as_number */
|
|
|
1203 0, /* tp_as_sequence */
|
|
|
1204 0, /* tp_as_mapping */
|
|
|
1205 0, /* tp_hash */
|
|
|
1206 0, /* tp_call */
|
|
|
1207 0, /* tp_str */
|
|
|
1208 PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
1209 0, /* tp_setattro */
|
|
|
1210 0, /* tp_as_buffer */
|
|
|
1211 Py_TPFLAGS_DEFAULT, /* tp_flags */
|
|
|
1212 0, /* tp_doc */
|
|
|
1213 0, /* tp_traverse */
|
|
|
1214 0, /* tp_clear */
|
|
|
1215 0, /* tp_richcompare */
|
|
|
1216 0, /* tp_weaklistoffset */
|
|
|
1217 PyObject_SelfIter, /* tp_iter */
|
|
|
1218 (iternextfunc)formatteriter_next, /* tp_iternext */
|
|
|
1219 formatteriter_methods, /* tp_methods */
|
|
|
1220 0,
|
|
|
1221 };
|
|
|
1222
|
|
|
1223 /* unicode_formatter_parser is used to implement
|
|
|
1224 string.Formatter.vformat. it parses a string and returns tuples
|
|
|
1225 describing the parsed elements. It's a wrapper around
|
|
|
1226 stringlib/string_format.h's MarkupIterator */
|
|
|
1227 static PyObject *
|
|
|
1228 formatter_parser(PyObject *ignored, PyUnicodeObject *self)
|
|
|
1229 {
|
|
|
1230 formatteriterobject *it;
|
|
|
1231
|
|
|
1232 if (!PyUnicode_Check(self)) {
|
|
|
1233 PyErr_Format(PyExc_TypeError, "expected str, got %s", Py_TYPE(self)->tp_name);
|
|
|
1234 return NULL;
|
|
|
1235 }
|
|
|
1236
|
|
|
1237 if (PyUnicode_READY(self) == -1)
|
|
|
1238 return NULL;
|
|
|
1239
|
|
|
1240 it = PyObject_New(formatteriterobject, &PyFormatterIter_Type);
|
|
|
1241 if (it == NULL)
|
|
|
1242 return NULL;
|
|
|
1243
|
|
|
1244 /* take ownership, give the object to the iterator */
|
|
|
1245 Py_INCREF(self);
|
|
|
1246 it->str = self;
|
|
|
1247
|
|
|
1248 /* initialize the contained MarkupIterator */
|
|
|
1249 MarkupIterator_init(&it->it_markup, (PyObject*)self, 0, PyUnicode_GET_LENGTH(self));
|
|
|
1250 return (PyObject *)it;
|
|
|
1251 }
|
|
|
1252
|
|
|
1253
|
|
|
1254 /************************************************************************/
|
|
|
1255 /*********** fieldnameiterator ******************************************/
|
|
|
1256 /************************************************************************/
|
|
|
1257
|
|
|
1258
|
|
|
1259 /* This is used to implement string.Formatter.vparse(). It parses the
|
|
|
1260 field name into attribute and item values. It's a Python-callable
|
|
|
1261 wrapper around FieldNameIterator */
|
|
|
1262
|
|
|
1263 typedef struct {
|
|
|
1264 PyObject_HEAD
|
|
|
1265
|
|
|
1266 PyUnicodeObject *str;
|
|
|
1267
|
|
|
1268 FieldNameIterator it_field;
|
|
|
1269 } fieldnameiterobject;
|
|
|
1270
|
|
|
1271 static void
|
|
|
1272 fieldnameiter_dealloc(fieldnameiterobject *it)
|
|
|
1273 {
|
|
|
1274 Py_XDECREF(it->str);
|
|
|
1275 PyObject_FREE(it);
|
|
|
1276 }
|
|
|
1277
|
|
|
1278 /* returns a tuple:
|
|
|
1279 (is_attr, value)
|
|
|
1280 is_attr is true if we used attribute syntax (e.g., '.foo')
|
|
|
1281 false if we used index syntax (e.g., '[foo]')
|
|
|
1282 value is an integer or string
|
|
|
1283 */
|
|
|
1284 static PyObject *
|
|
|
1285 fieldnameiter_next(fieldnameiterobject *it)
|
|
|
1286 {
|
|
|
1287 int result;
|
|
|
1288 int is_attr;
|
|
|
1289 Py_ssize_t idx;
|
|
|
1290 SubString name;
|
|
|
1291
|
|
|
1292 result = FieldNameIterator_next(&it->it_field, &is_attr,
|
|
|
1293 &idx, &name);
|
|
|
1294 if (result == 0 || result == 1)
|
|
|
1295 /* if 0, error has already been set, if 1, iterator is empty */
|
|
|
1296 return NULL;
|
|
|
1297 else {
|
|
|
1298 PyObject* result = NULL;
|
|
|
1299 PyObject* is_attr_obj = NULL;
|
|
|
1300 PyObject* obj = NULL;
|
|
|
1301
|
|
|
1302 is_attr_obj = PyBool_FromLong(is_attr);
|
|
|
1303 if (is_attr_obj == NULL)
|
|
|
1304 goto done;
|
|
|
1305
|
|
|
1306 /* either an integer or a string */
|
|
|
1307 if (idx != -1)
|
|
|
1308 obj = PyLong_FromSsize_t(idx);
|
|
|
1309 else
|
|
|
1310 obj = SubString_new_object(&name);
|
|
|
1311 if (obj == NULL)
|
|
|
1312 goto done;
|
|
|
1313
|
|
|
1314 /* return a tuple of values */
|
|
|
1315 result = PyTuple_Pack(2, is_attr_obj, obj);
|
|
|
1316
|
|
|
1317 done:
|
|
|
1318 Py_XDECREF(is_attr_obj);
|
|
|
1319 Py_XDECREF(obj);
|
|
|
1320 return result;
|
|
|
1321 }
|
|
|
1322 }
|
|
|
1323
|
|
|
1324 static PyMethodDef fieldnameiter_methods[] = {
|
|
|
1325 {NULL, NULL} /* sentinel */
|
|
|
1326 };
|
|
|
1327
|
|
|
1328 static PyTypeObject PyFieldNameIter_Type = {
|
|
|
1329 PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
1330 "fieldnameiterator", /* tp_name */
|
|
|
1331 sizeof(fieldnameiterobject), /* tp_basicsize */
|
|
|
1332 0, /* tp_itemsize */
|
|
|
1333 /* methods */
|
|
|
1334 (destructor)fieldnameiter_dealloc, /* tp_dealloc */
|
|
|
1335 0, /* tp_print */
|
|
|
1336 0, /* tp_getattr */
|
|
|
1337 0, /* tp_setattr */
|
|
|
1338 0, /* tp_reserved */
|
|
|
1339 0, /* tp_repr */
|
|
|
1340 0, /* tp_as_number */
|
|
|
1341 0, /* tp_as_sequence */
|
|
|
1342 0, /* tp_as_mapping */
|
|
|
1343 0, /* tp_hash */
|
|
|
1344 0, /* tp_call */
|
|
|
1345 0, /* tp_str */
|
|
|
1346 PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
1347 0, /* tp_setattro */
|
|
|
1348 0, /* tp_as_buffer */
|
|
|
1349 Py_TPFLAGS_DEFAULT, /* tp_flags */
|
|
|
1350 0, /* tp_doc */
|
|
|
1351 0, /* tp_traverse */
|
|
|
1352 0, /* tp_clear */
|
|
|
1353 0, /* tp_richcompare */
|
|
|
1354 0, /* tp_weaklistoffset */
|
|
|
1355 PyObject_SelfIter, /* tp_iter */
|
|
|
1356 (iternextfunc)fieldnameiter_next, /* tp_iternext */
|
|
|
1357 fieldnameiter_methods, /* tp_methods */
|
|
|
1358 0};
|
|
|
1359
|
|
|
1360 /* unicode_formatter_field_name_split is used to implement
|
|
|
1361 string.Formatter.vformat. it takes an PEP 3101 "field name", and
|
|
|
1362 returns a tuple of (first, rest): "first", the part before the
|
|
|
1363 first '.' or '['; and "rest", an iterator for the rest of the field
|
|
|
1364 name. it's a wrapper around stringlib/string_format.h's
|
|
|
1365 field_name_split. The iterator it returns is a
|
|
|
1366 FieldNameIterator */
|
|
|
1367 static PyObject *
|
|
|
1368 formatter_field_name_split(PyObject *ignored, PyUnicodeObject *self)
|
|
|
1369 {
|
|
|
1370 SubString first;
|
|
|
1371 Py_ssize_t first_idx;
|
|
|
1372 fieldnameiterobject *it;
|
|
|
1373
|
|
|
1374 PyObject *first_obj = NULL;
|
|
|
1375 PyObject *result = NULL;
|
|
|
1376
|
|
|
1377 if (!PyUnicode_Check(self)) {
|
|
|
1378 PyErr_Format(PyExc_TypeError, "expected str, got %s", Py_TYPE(self)->tp_name);
|
|
|
1379 return NULL;
|
|
|
1380 }
|
|
|
1381
|
|
|
1382 if (PyUnicode_READY(self) == -1)
|
|
|
1383 return NULL;
|
|
|
1384
|
|
|
1385 it = PyObject_New(fieldnameiterobject, &PyFieldNameIter_Type);
|
|
|
1386 if (it == NULL)
|
|
|
1387 return NULL;
|
|
|
1388
|
|
|
1389 /* take ownership, give the object to the iterator. this is
|
|
|
1390 just to keep the field_name alive */
|
|
|
1391 Py_INCREF(self);
|
|
|
1392 it->str = self;
|
|
|
1393
|
|
|
1394 /* Pass in auto_number = NULL. We'll return an empty string for
|
|
|
1395 first_obj in that case. */
|
|
|
1396 if (!field_name_split((PyObject*)self, 0, PyUnicode_GET_LENGTH(self),
|
|
|
1397 &first, &first_idx, &it->it_field, NULL))
|
|
|
1398 goto done;
|
|
|
1399
|
|
|
1400 /* first becomes an integer, if possible; else a string */
|
|
|
1401 if (first_idx != -1)
|
|
|
1402 first_obj = PyLong_FromSsize_t(first_idx);
|
|
|
1403 else
|
|
|
1404 /* convert "first" into a string object */
|
|
|
1405 first_obj = SubString_new_object(&first);
|
|
|
1406 if (first_obj == NULL)
|
|
|
1407 goto done;
|
|
|
1408
|
|
|
1409 /* return a tuple of values */
|
|
|
1410 result = PyTuple_Pack(2, first_obj, it);
|
|
|
1411
|
|
|
1412 done:
|
|
|
1413 Py_XDECREF(it);
|
|
|
1414 Py_XDECREF(first_obj);
|
|
|
1415 return result;
|
|
|
1416 }
|