blob: 4ae7e578f24b111657fb72a59846bedf9f10cc3c [file] [log] [blame]
Eric Smith8c663262007-08-25 02:26:07 +00001/*
2 string_format.h -- implementation of string.format().
3
4 It uses the Objects/stringlib conventions, so that it can be
5 compiled for both unicode and string objects.
6*/
7
8
9/* Defines for more efficiently reallocating the string buffer */
10#define INITIAL_SIZE_INCREMENT 100
11#define SIZE_MULTIPLIER 2
12#define MAX_SIZE_INCREMENT 3200
13
14
15/************************************************************************/
16/*********** Global data structures and forward declarations *********/
17/************************************************************************/
18
19/*
20 A SubString consists of the characters between two string or
21 unicode pointers.
22*/
23typedef struct {
24 STRINGLIB_CHAR *ptr;
25 STRINGLIB_CHAR *end;
26} SubString;
27
28
29/* forward declaration for recursion */
30static PyObject *
31build_string(SubString *input, PyObject *args, PyObject *kwargs,
32 int *recursion_level);
33
34
35
36/************************************************************************/
37/************************** Utility functions ************************/
38/************************************************************************/
39
40/* fill in a SubString from a pointer and length */
41Py_LOCAL_INLINE(void)
42SubString_init(SubString *str, STRINGLIB_CHAR *p, Py_ssize_t len)
43{
44 str->ptr = p;
45 if (p == NULL)
46 str->end = NULL;
47 else
48 str->end = str->ptr + len;
49}
50
51Py_LOCAL_INLINE(PyObject *)
52SubString_new_object(SubString *str)
53{
54 return STRINGLIB_NEW(str->ptr, str->end - str->ptr);
55}
56
57/************************************************************************/
58/*********** Error handling and exception generation **************/
59/************************************************************************/
60
61/*
62 Most of our errors are value errors, because to Python, the
63 format string is a "value". Also, it's convenient to return
64 a NULL when we are erroring out.
65
66 XXX: need better error handling, per PEP 3101.
67*/
68static void *
69SetError(const char *s)
70{
71 /* PyErr_Format always returns NULL */
72 return PyErr_Format(PyExc_ValueError, "%s in format string", s);
73}
74
Eric Smith8c663262007-08-25 02:26:07 +000075/************************************************************************/
76/*********** Output string management functions ****************/
77/************************************************************************/
78
79typedef struct {
80 STRINGLIB_CHAR *ptr;
81 STRINGLIB_CHAR *end;
82 PyObject *obj;
83 Py_ssize_t size_increment;
84} OutputString;
85
86/* initialize an OutputString object, reserving size characters */
87static int
88output_initialize(OutputString *output, Py_ssize_t size)
89{
90 output->obj = STRINGLIB_NEW(NULL, size);
91 if (output->obj == NULL)
92 return 0;
93
94 output->ptr = STRINGLIB_STR(output->obj);
95 output->end = STRINGLIB_LEN(output->obj) + output->ptr;
96 output->size_increment = INITIAL_SIZE_INCREMENT;
97
98 return 1;
99}
100
101/*
102 output_extend reallocates the output string buffer.
103 It returns a status: 0 for a failed reallocation,
104 1 for success.
105*/
106
107static int
108output_extend(OutputString *output, Py_ssize_t count)
109{
110 STRINGLIB_CHAR *startptr = STRINGLIB_STR(output->obj);
111 Py_ssize_t curlen = output->ptr - startptr;
112 Py_ssize_t maxlen = curlen + count + output->size_increment;
113
114 if (STRINGLIB_RESIZE(&output->obj, maxlen) < 0)
115 return 0;
116 startptr = STRINGLIB_STR(output->obj);
117 output->ptr = startptr + curlen;
118 output->end = startptr + maxlen;
119 if (output->size_increment < MAX_SIZE_INCREMENT)
120 output->size_increment *= SIZE_MULTIPLIER;
121 return 1;
122}
123
124/*
125 output_data dumps characters into our output string
126 buffer.
127
128 In some cases, it has to reallocate the string.
129
130 It returns a status: 0 for a failed reallocation,
131 1 for success.
132*/
133static int
134output_data(OutputString *output, const STRINGLIB_CHAR *s, Py_ssize_t count)
135{
136 if ((count > output->end - output->ptr) && !output_extend(output, count))
137 return 0;
138 memcpy(output->ptr, s, count * sizeof(STRINGLIB_CHAR));
139 output->ptr += count;
140 return 1;
141}
142
143/************************************************************************/
144/*********** Format string parsing -- integers and identifiers *********/
145/************************************************************************/
146
Eric Smith7ade6482007-08-26 22:27:13 +0000147static Py_ssize_t
148get_integer(const SubString *str)
Eric Smith8c663262007-08-25 02:26:07 +0000149{
Eric Smith7ade6482007-08-26 22:27:13 +0000150 Py_ssize_t accumulator = 0;
151 Py_ssize_t digitval;
152 Py_ssize_t oldaccumulator;
153 STRINGLIB_CHAR *p;
Eric Smith8c663262007-08-25 02:26:07 +0000154
Eric Smith7ade6482007-08-26 22:27:13 +0000155 /* empty string is an error */
156 if (str->ptr >= str->end)
157 return -1;
Eric Smith8c663262007-08-25 02:26:07 +0000158
Eric Smith7ade6482007-08-26 22:27:13 +0000159 for (p = str->ptr; p < str->end; p++) {
160 digitval = STRINGLIB_TODECIMAL(*p);
Eric Smith8c663262007-08-25 02:26:07 +0000161 if (digitval < 0)
Eric Smith7ade6482007-08-26 22:27:13 +0000162 return -1;
Eric Smith8c663262007-08-25 02:26:07 +0000163 /*
164 This trick was copied from old Unicode format code. It's cute,
165 but would really suck on an old machine with a slow divide
166 implementation. Fortunately, in the normal case we do not
167 expect too many digits.
168 */
169 oldaccumulator = accumulator;
170 accumulator *= 10;
171 if ((accumulator+10)/10 != oldaccumulator+1) {
172 PyErr_Format(PyExc_ValueError,
173 "Too many decimal digits in format string");
174 return -1;
175 }
176 accumulator += digitval;
177 }
Eric Smith7ade6482007-08-26 22:27:13 +0000178 return accumulator;
Eric Smith8c663262007-08-25 02:26:07 +0000179}
180
181/************************************************************************/
182/******** Functions to get field objects and specification strings ******/
183/************************************************************************/
184
Eric Smith7ade6482007-08-26 22:27:13 +0000185/* do the equivalent of obj.name */
Eric Smith8c663262007-08-25 02:26:07 +0000186static PyObject *
Eric Smith7ade6482007-08-26 22:27:13 +0000187getattr(PyObject *obj, SubString *name)
Eric Smith8c663262007-08-25 02:26:07 +0000188{
Eric Smith7ade6482007-08-26 22:27:13 +0000189 PyObject *newobj;
190 PyObject *str = STRINGLIB_NEW(name->ptr, name->end - name->ptr);
191 if (str == NULL)
192 return NULL;
193 newobj = PyObject_GetAttr(obj, str);
194 Py_DECREF(str);
195 return newobj;
Eric Smith8c663262007-08-25 02:26:07 +0000196}
197
Eric Smith7ade6482007-08-26 22:27:13 +0000198/* do the equivalent of obj[idx], where obj is a sequence */
199static PyObject *
200getitem_sequence(PyObject *obj, Py_ssize_t idx)
201{
202 return PySequence_GetItem(obj, idx);
203}
204
205/* do the equivalent of obj[idx], where obj is not a sequence */
206static PyObject *
207getitem_idx(PyObject *obj, Py_ssize_t idx)
208{
209 PyObject *newobj;
210 PyObject *idx_obj = PyInt_FromSsize_t(idx);
211 if (idx_obj == NULL)
212 return NULL;
213 newobj = PyObject_GetItem(obj, idx_obj);
214 Py_DECREF(idx_obj);
215 return newobj;
216}
217
218/* do the equivalent of obj[name] */
219static PyObject *
220getitem_str(PyObject *obj, SubString *name)
221{
222 PyObject *newobj;
223 PyObject *str = STRINGLIB_NEW(name->ptr, name->end - name->ptr);
224 if (str == NULL)
225 return NULL;
226 newobj = PyObject_GetItem(obj, str);
227 Py_DECREF(str);
228 return newobj;
229}
230
231typedef struct {
232 /* the entire string we're parsing. we assume that someone else
233 is managing its lifetime, and that it will exist for the
234 lifetime of the iterator. can be empty */
235 SubString str;
236
237 /* pointer to where we are inside field_name */
238 STRINGLIB_CHAR *ptr;
239} FieldNameIterator;
240
241
242static int
243FieldNameIterator_init(FieldNameIterator *self, STRINGLIB_CHAR *ptr,
244 Py_ssize_t len)
245{
246 SubString_init(&self->str, ptr, len);
247 self->ptr = self->str.ptr;
248 return 1;
249}
250
251static int
252_FieldNameIterator_attr(FieldNameIterator *self, SubString *name)
253{
254 STRINGLIB_CHAR c;
255
256 name->ptr = self->ptr;
257
258 /* return everything until '.' or '[' */
259 while (self->ptr < self->str.end) {
260 switch (c = *self->ptr++) {
261 case '[':
262 case '.':
263 /* backup so that we this character will be seen next time */
264 self->ptr--;
265 break;
266 default:
267 continue;
268 }
269 break;
270 }
271 /* end of string is okay */
272 name->end = self->ptr;
273 return 1;
274}
275
276static int
277_FieldNameIterator_item(FieldNameIterator *self, SubString *name)
278{
279 STRINGLIB_CHAR c;
280
281 name->ptr = self->ptr;
282
283 /* return everything until ']' */
284 while (self->ptr < self->str.end) {
285 switch (c = *self->ptr++) {
286 case ']':
287 break;
288 default:
289 continue;
290 }
291 break;
292 }
293 /* end of string is okay */
294 /* don't include the ']' */
295 name->end = self->ptr-1;
296 return 1;
297}
298
299/* returns 0 on error, 1 on non-error termination, and 2 if it returns a value */
300static int
301FieldNameIterator_next(FieldNameIterator *self, int *is_attribute,
302 Py_ssize_t *name_idx, SubString *name)
303{
304 /* check at end of input */
305 if (self->ptr >= self->str.end)
306 return 1;
307
308 switch (*self->ptr++) {
309 case '.':
310 *is_attribute = 1;
311 if (_FieldNameIterator_attr(self, name) == 0) {
312 return 0;
313 }
314 *name_idx = -1;
315 break;
316 case '[':
317 *is_attribute = 0;
318 if (_FieldNameIterator_item(self, name) == 0) {
319 return 0;
320 }
321 *name_idx = get_integer(name);
322 break;
323 default:
324 /* interal error, can't get here */
325 assert(0);
326 return 0;
327 }
328
329 /* empty string is an error */
330 if (name->ptr == name->end) {
331 PyErr_SetString(PyExc_ValueError, "Empty attribute in format string");
332 return 0;
333 }
334
335 return 2;
336}
337
338
339/* input: field_name
340 output: 'first' points to the part before the first '[' or '.'
341 'first_idx' is -1 if 'first' is not an integer, otherwise
342 it's the value of first converted to an integer
343 'rest' is an iterator to return the rest
344*/
345static int
346field_name_split(STRINGLIB_CHAR *ptr, Py_ssize_t len, SubString *first,
347 Py_ssize_t *first_idx, FieldNameIterator *rest)
348{
349 STRINGLIB_CHAR c;
350 STRINGLIB_CHAR *p = ptr;
351 STRINGLIB_CHAR *end = ptr + len;
352
353 /* find the part up until the first '.' or '[' */
354 while (p < end) {
355 switch (c = *p++) {
356 case '[':
357 case '.':
358 /* backup so that we this character is available to the
359 "rest" iterator */
360 p--;
361 break;
362 default:
363 continue;
364 }
365 break;
366 }
367
368 /* set up the return values */
369 SubString_init(first, ptr, p - ptr);
370 FieldNameIterator_init(rest, p, end - p);
371
372 /* see if "first" is an integer, in which case it's used as an index */
373 *first_idx = get_integer(first);
374
375 /* zero length string is an error */
376 if (first->ptr >= first->end) {
377 PyErr_SetString(PyExc_ValueError, "empty field name");
378 goto error;
379 }
380
381 return 1;
382error:
383 return 0;
384}
385
386
Eric Smith8c663262007-08-25 02:26:07 +0000387/*
388 get_field_object returns the object inside {}, before the
389 format_spec. It handles getindex and getattr lookups and consumes
390 the entire input string.
391*/
392static PyObject *
393get_field_object(SubString *input, PyObject *args, PyObject *kwargs)
394{
Eric Smith7ade6482007-08-26 22:27:13 +0000395 PyObject *obj = NULL;
396 int ok;
397 int is_attribute;
398 SubString name;
399 SubString first;
Eric Smith8c663262007-08-25 02:26:07 +0000400 Py_ssize_t index;
Eric Smith7ade6482007-08-26 22:27:13 +0000401 FieldNameIterator rest;
Eric Smith8c663262007-08-25 02:26:07 +0000402
Eric Smith7ade6482007-08-26 22:27:13 +0000403 if (!field_name_split(input->ptr, input->end - input->ptr, &first,
404 &index, &rest)) {
405 goto error;
406 }
Eric Smith8c663262007-08-25 02:26:07 +0000407
Eric Smith7ade6482007-08-26 22:27:13 +0000408 if (index == -1) {
409 /* look up in kwargs */
410 PyObject *key = STRINGLIB_NEW(first.ptr, first.end - first.ptr);
411 if (key == NULL)
412 goto error;
413 if ((kwargs == NULL) || (obj = PyDict_GetItem(kwargs, key)) == NULL) {
414 PyErr_SetString(PyExc_ValueError, "Keyword argument not found "
415 "in format string");
416 Py_DECREF(key);
417 goto error;
Eric Smith8c663262007-08-25 02:26:07 +0000418 }
Neal Norwitz247b5152007-08-27 03:22:50 +0000419 Py_INCREF(obj);
Eric Smith7ade6482007-08-26 22:27:13 +0000420 } else {
421 /* look up in args */
422 obj = PySequence_GetItem(args, index);
423 if (obj == NULL) {
424 /* translate IndexError to a ValueError */
425 PyErr_SetString(PyExc_ValueError, "Not enough positional arguments "
426 "in format string");
427 goto error;
Eric Smith8c663262007-08-25 02:26:07 +0000428 }
429 }
Eric Smith7ade6482007-08-26 22:27:13 +0000430
431 /* iterate over the rest of the field_name */
432 while ((ok = FieldNameIterator_next(&rest, &is_attribute, &index,
433 &name)) == 2) {
434 PyObject *tmp;
435
436 if (is_attribute)
437 /* getattr lookup "." */
438 tmp = getattr(obj, &name);
439 else
440 /* getitem lookup "[]" */
441 if (index == -1)
442 tmp = getitem_str(obj, &name);
443 else
444 if (PySequence_Check(obj))
445 tmp = getitem_sequence(obj, index);
446 else
447 /* not a sequence */
448 tmp = getitem_idx(obj, index);
449 if (tmp == NULL)
450 goto error;
451
452 /* assign to obj */
453 Py_DECREF(obj);
454 obj = tmp;
Eric Smith8c663262007-08-25 02:26:07 +0000455 }
Eric Smith7ade6482007-08-26 22:27:13 +0000456 /* end of iterator, this is the non-error case */
457 if (ok == 1)
458 return obj;
459error:
460 Py_XDECREF(obj);
Eric Smith8c663262007-08-25 02:26:07 +0000461 return NULL;
462}
463
464/************************************************************************/
465/***************** Field rendering functions **************************/
466/************************************************************************/
467
468/*
469 render_field() is the main function in this section. It takes the
470 field object and field specification string generated by
471 get_field_and_spec, and renders the field into the output string.
472
473 format() does the actual calling of the objects __format__ method.
474*/
475
476
477/* returns fieldobj.__format__(format_spec) */
478static PyObject *
479format(PyObject *fieldobj, SubString *format_spec)
480{
481 static PyObject *format_str = NULL;
482 PyObject *meth;
483 PyObject *spec = NULL;
484 PyObject *result = NULL;
485
486 /* Initialize cached value */
487 if (format_str == NULL) {
488 /* Initialize static variable needed by _PyType_Lookup */
489 format_str = PyUnicode_FromString("__format__");
490 if (format_str == NULL)
491 return NULL;
492 }
493
494 /* Make sure the type is initialized. float gets initialized late */
495 if (Py_Type(fieldobj)->tp_dict == NULL)
496 if (PyType_Ready(Py_Type(fieldobj)) < 0)
497 return NULL;
498
499 /* we need to create an object out of the pointers we have */
500 spec = SubString_new_object(format_spec);
501 if (spec == NULL)
502 goto done;
503
504 /* Find the (unbound!) __format__ method (a borrowed reference) */
505 meth = _PyType_Lookup(Py_Type(fieldobj), format_str);
506 if (meth == NULL) {
507 PyErr_Format(PyExc_TypeError,
508 "Type %.100s doesn't define __format__",
509 Py_Type(fieldobj)->tp_name);
510 goto done;
511 }
512
513 /* And call it, binding it to the value */
514 result = PyObject_CallFunctionObjArgs(meth, fieldobj, spec, NULL);
515 if (result == NULL)
516 goto done;
517
518 if (!STRINGLIB_CHECK(result)) {
519 PyErr_SetString(PyExc_TypeError,
520 "__format__ method did not return "
521 STRINGLIB_TYPE_NAME);
522 Py_DECREF(result);
523 result = NULL;
524 goto done;
525 }
526
527done:
528 Py_XDECREF(spec);
529 return result;
530}
531
532/*
533 render_field calls fieldobj.__format__(format_spec) method, and
534 appends to the output.
535*/
536static int
537render_field(PyObject *fieldobj, SubString *format_spec, OutputString *output)
538{
539 int ok = 0;
540 PyObject *result = format(fieldobj, format_spec);
541
542 if (result == NULL)
543 goto done;
544
545 ok = output_data(output,
546 STRINGLIB_STR(result), STRINGLIB_LEN(result));
547done:
548 Py_XDECREF(result);
549 return ok;
550}
551
552static int
553parse_field(SubString *str, SubString *field_name, SubString *format_spec,
554 STRINGLIB_CHAR *conversion)
555{
556 STRINGLIB_CHAR c = 0;
557
558 /* initialize these, as they may be empty */
559 *conversion = '\0';
560 SubString_init(format_spec, NULL, 0);
561
562 /* search for the field name. it's terminated by the end of the
563 string, or a ':' or '!' */
564 field_name->ptr = str->ptr;
565 while (str->ptr < str->end) {
566 switch (c = *(str->ptr++)) {
567 case ':':
568 case '!':
569 break;
570 default:
571 continue;
572 }
573 break;
574 }
575
576 if (c == '!' || c == ':') {
577 /* we have a format specifier and/or a conversion */
578 /* don't include the last character */
579 field_name->end = str->ptr-1;
580
581 /* the format specifier is the rest of the string */
582 format_spec->ptr = str->ptr;
583 format_spec->end = str->end;
584
585 /* see if there's a conversion specifier */
586 if (c == '!') {
587 /* there must be another character present */
588 if (format_spec->ptr >= format_spec->end) {
589 PyErr_SetString(PyExc_ValueError,
590 "end of format while looking for conversion "
591 "specifier");
592 return 0;
593 }
594 *conversion = *(format_spec->ptr++);
595
596 /* if there is another character, it must be a colon */
597 if (format_spec->ptr < format_spec->end) {
598 c = *(format_spec->ptr++);
599 if (c != ':') {
600 PyErr_SetString(PyExc_ValueError,
601 "expected ':' after format specifier");
602 return 0;
603 }
604 }
605 }
606
607 return 1;
608
609 } else {
610 /* end of string, there's no format_spec or conversion */
611 field_name->end = str->ptr;
612 return 1;
613 }
614}
615
616/************************************************************************/
617/******* Output string allocation and escape-to-markup processing ******/
618/************************************************************************/
619
620/* MarkupIterator breaks the string into pieces of either literal
621 text, or things inside {} that need to be marked up. it is
622 designed to make it easy to wrap a Python iterator around it, for
623 use with the Formatter class */
624
625typedef struct {
626 SubString str;
627 int in_markup;
628} MarkupIterator;
629
630static int
631MarkupIterator_init(MarkupIterator *self, STRINGLIB_CHAR *ptr, Py_ssize_t len)
632{
633 SubString_init(&self->str, ptr, len);
634 self->in_markup = 0;
635 return 1;
636}
637
638/* returns 0 on error, 1 on non-error termination, and 2 if it got a
639 string (or something to be expanded) */
640static int
641MarkupIterator_next(MarkupIterator *self, int *is_markup, SubString *literal,
642 SubString *field_name, SubString *format_spec,
643 STRINGLIB_CHAR *conversion,
644 int *format_spec_needs_expanding)
645{
646 int at_end;
647 STRINGLIB_CHAR c = 0;
648 STRINGLIB_CHAR *start;
649 int count;
650 Py_ssize_t len;
651
652 *format_spec_needs_expanding = 0;
653
654 /* no more input, end of iterator */
655 if (self->str.ptr >= self->str.end)
656 return 1;
657
658 *is_markup = self->in_markup;
659 start = self->str.ptr;
660
661 if (self->in_markup) {
662
663 /* prepare for next iteration */
664 self->in_markup = 0;
665
666 /* this is markup, find the end of the string by counting nested
667 braces. note that this prohibits escaped braces, so that
668 format_specs cannot have braces in them. */
669 count = 1;
670
671 /* we know we can't have a zero length string, so don't worry
672 about that case */
673 while (self->str.ptr < self->str.end) {
674 switch (c = *(self->str.ptr++)) {
675 case '{':
676 /* the format spec needs to be recursively expanded.
677 this is an optimization, and not strictly needed */
678 *format_spec_needs_expanding = 1;
679 count++;
680 break;
681 case '}':
682 count--;
683 if (count <= 0) {
684 /* we're done. parse and get out */
685 literal->ptr = start;
686 literal->end = self->str.ptr-1;
687
688 if (parse_field(literal, field_name, format_spec,
689 conversion) == 0)
690 return 0;
691
692 /* success */
693 return 2;
694 }
695 break;
696 }
697 }
698 /* end of string while searching for matching '}' */
699 PyErr_SetString(PyExc_ValueError, "unmatched '{' in format");
700 return 0;
701
702 } else {
703 /* literal text, read until the end of string, an escaped { or },
704 or an unescaped { */
705 while (self->str.ptr < self->str.end) {
706 switch (c = *(self->str.ptr++)) {
707 case '{':
708 case '}':
709 self->in_markup = 1;
710 break;
711 default:
712 continue;
713 }
714 break;
715 }
716
717 at_end = self->str.ptr >= self->str.end;
718 len = self->str.ptr - start;
719
Neal Norwitz3ef6a572007-08-25 17:08:59 +0000720 if ((c == '}') && (at_end || (c != *self->str.ptr))) {
721 SetError("Single } encountered");
722 return 0;
723 }
724 if (at_end && c == '{') {
725 SetError("Single { encountered");
726 return 0;
727 }
Eric Smith8c663262007-08-25 02:26:07 +0000728 if (!at_end) {
729 if (c == *self->str.ptr) {
730 /* escaped } or {, skip it in the input */
731 self->str.ptr++;
732 self->in_markup = 0;
733 } else
734 len--;
735 }
736
737 /* this is just plain text, return it */
738 literal->ptr = start;
739 literal->end = start + len;
740 return 2;
741 }
742}
743
744
745/* do the !r or !s conversion on obj */
746static PyObject *
747do_conversion(PyObject *obj, STRINGLIB_CHAR conversion)
748{
749 /* XXX in pre-3.0, do we need to convert this to unicode, since it
750 might have returned a string? */
751 switch (conversion) {
752 case 'r':
753 return PyObject_Repr(obj);
754 case 's':
755 return PyObject_Unicode(obj);
756 default:
757 PyErr_Format(PyExc_ValueError,
758 "Unknown converion specifier %c",
759 conversion);
760 return NULL;
761 }
762}
763
764/* given:
765
766 {field_name!conversion:format_spec}
767
768 compute the result and write it to output.
769 format_spec_needs_expanding is an optimization. if it's false,
770 just output the string directly, otherwise recursively expand the
771 format_spec string. */
772
773static int
774output_markup(SubString *field_name, SubString *format_spec,
775 int format_spec_needs_expanding, STRINGLIB_CHAR conversion,
776 OutputString *output, PyObject *args, PyObject *kwargs,
777 int *recursion_level)
778{
779 PyObject *tmp = NULL;
780 PyObject *fieldobj = NULL;
781 SubString expanded_format_spec;
782 SubString *actual_format_spec;
783 int result = 0;
784
785 /* convert field_name to an object */
786 fieldobj = get_field_object(field_name, args, kwargs);
787 if (fieldobj == NULL)
788 goto done;
789
790 if (conversion != '\0') {
791 tmp = do_conversion(fieldobj, conversion);
792 if (tmp == NULL)
793 goto done;
794
795 /* do the assignment, transferring ownership: fieldobj = tmp */
796 Py_DECREF(fieldobj);
797 fieldobj = tmp;
798 tmp = NULL;
799 }
800
801 /* if needed, recurively compute the format_spec */
802 if (format_spec_needs_expanding) {
803 tmp = build_string(format_spec, args, kwargs, recursion_level);
804 if (tmp == NULL)
805 goto done;
806
807 /* note that in the case we're expanding the format string,
808 tmp must be kept around until after the call to
809 render_field. */
810 SubString_init(&expanded_format_spec,
811 STRINGLIB_STR(tmp), STRINGLIB_LEN(tmp));
812 actual_format_spec = &expanded_format_spec;
813 } else
814 actual_format_spec = format_spec;
815
816 if (render_field(fieldobj, actual_format_spec, output) == 0)
817 goto done;
818
819 result = 1;
820
821done:
822 Py_XDECREF(fieldobj);
823 Py_XDECREF(tmp);
824
825 return result;
826}
827
828/*
829 do_markup is the top-level loop for the format() function. It
830 searches through the format string for escapes to markup codes, and
831 calls other functions to move non-markup text to the output,
832 and to perform the markup to the output.
833*/
834static int
835do_markup(SubString *input, PyObject *args, PyObject *kwargs,
836 OutputString *output, int *recursion_level)
837{
838 MarkupIterator iter;
839 int is_markup;
840 int format_spec_needs_expanding;
841 int result;
842 SubString str;
843 SubString field_name;
844 SubString format_spec;
845 STRINGLIB_CHAR conversion;
846
847 MarkupIterator_init(&iter, input->ptr, input->end - input->ptr);
848 while ((result = MarkupIterator_next(&iter, &is_markup, &str, &field_name,
849 &format_spec, &conversion,
850 &format_spec_needs_expanding)) == 2) {
851 if (is_markup) {
852 if (!output_markup(&field_name, &format_spec,
853 format_spec_needs_expanding, conversion, output,
854 args, kwargs, recursion_level))
855 return 0;
856 } else {
857 if (!output_data(output, str.ptr, str.end-str.ptr))
858 return 0;
859 }
860 }
861 return result;
862}
863
864
865/*
866 build_string allocates the output string and then
867 calls do_markup to do the heavy lifting.
868*/
869static PyObject *
870build_string(SubString *input, PyObject *args, PyObject *kwargs,
871 int *recursion_level)
872{
873 OutputString output;
874 PyObject *result = NULL;
875 Py_ssize_t count;
876
877 output.obj = NULL; /* needed so cleanup code always works */
878
879 /* check the recursion level */
880 (*recursion_level)--;
881 if (*recursion_level < 0) {
882 PyErr_SetString(PyExc_ValueError,
883 "Max string recursion exceeded");
884 goto done;
885 }
886
887 /* initial size is the length of the format string, plus the size
888 increment. seems like a reasonable default */
889 if (!output_initialize(&output,
890 input->end - input->ptr +
891 INITIAL_SIZE_INCREMENT))
892 goto done;
893
894 if (!do_markup(input, args, kwargs, &output, recursion_level)) {
895 goto done;
896 }
897
898 count = output.ptr - STRINGLIB_STR(output.obj);
899 if (STRINGLIB_RESIZE(&output.obj, count) < 0) {
900 goto done;
901 }
902
903 /* transfer ownership to result */
904 result = output.obj;
905 output.obj = NULL;
906
907done:
908 (*recursion_level)++;
909 Py_XDECREF(output.obj);
910 return result;
911}
912
913/************************************************************************/
914/*********** main routine ***********************************************/
915/************************************************************************/
916
917/* this is the main entry point */
918static PyObject *
919do_string_format(PyObject *self, PyObject *args, PyObject *kwargs)
920{
921 SubString input;
922
923 /* PEP 3101 says only 2 levels, so that
924 "{0:{1}}".format('abc', 's') # works
925 "{0:{1:{2}}}".format('abc', 's', '') # fails
926 */
927 int recursion_level = 2;
928
929 SubString_init(&input, STRINGLIB_STR(self), STRINGLIB_LEN(self));
930 return build_string(&input, args, kwargs, &recursion_level);
931}