blob: 2799141f32a95145d4464bf15e1950769d5bac78 [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 }
Eric Smith7ade6482007-08-26 22:27:13 +0000419 } else {
420 /* look up in args */
421 obj = PySequence_GetItem(args, index);
422 if (obj == NULL) {
423 /* translate IndexError to a ValueError */
424 PyErr_SetString(PyExc_ValueError, "Not enough positional arguments "
425 "in format string");
426 goto error;
Eric Smith8c663262007-08-25 02:26:07 +0000427 }
428 }
Eric Smith7ade6482007-08-26 22:27:13 +0000429
430 /* iterate over the rest of the field_name */
431 while ((ok = FieldNameIterator_next(&rest, &is_attribute, &index,
432 &name)) == 2) {
433 PyObject *tmp;
434
435 if (is_attribute)
436 /* getattr lookup "." */
437 tmp = getattr(obj, &name);
438 else
439 /* getitem lookup "[]" */
440 if (index == -1)
441 tmp = getitem_str(obj, &name);
442 else
443 if (PySequence_Check(obj))
444 tmp = getitem_sequence(obj, index);
445 else
446 /* not a sequence */
447 tmp = getitem_idx(obj, index);
448 if (tmp == NULL)
449 goto error;
450
451 /* assign to obj */
452 Py_DECREF(obj);
453 obj = tmp;
Eric Smith8c663262007-08-25 02:26:07 +0000454 }
Eric Smith7ade6482007-08-26 22:27:13 +0000455 /* end of iterator, this is the non-error case */
456 if (ok == 1)
457 return obj;
458error:
459 Py_XDECREF(obj);
Eric Smith8c663262007-08-25 02:26:07 +0000460 return NULL;
461}
462
463/************************************************************************/
464/***************** Field rendering functions **************************/
465/************************************************************************/
466
467/*
468 render_field() is the main function in this section. It takes the
469 field object and field specification string generated by
470 get_field_and_spec, and renders the field into the output string.
471
472 format() does the actual calling of the objects __format__ method.
473*/
474
475
476/* returns fieldobj.__format__(format_spec) */
477static PyObject *
478format(PyObject *fieldobj, SubString *format_spec)
479{
480 static PyObject *format_str = NULL;
481 PyObject *meth;
482 PyObject *spec = NULL;
483 PyObject *result = NULL;
484
485 /* Initialize cached value */
486 if (format_str == NULL) {
487 /* Initialize static variable needed by _PyType_Lookup */
488 format_str = PyUnicode_FromString("__format__");
489 if (format_str == NULL)
490 return NULL;
491 }
492
493 /* Make sure the type is initialized. float gets initialized late */
494 if (Py_Type(fieldobj)->tp_dict == NULL)
495 if (PyType_Ready(Py_Type(fieldobj)) < 0)
496 return NULL;
497
498 /* we need to create an object out of the pointers we have */
499 spec = SubString_new_object(format_spec);
500 if (spec == NULL)
501 goto done;
502
503 /* Find the (unbound!) __format__ method (a borrowed reference) */
504 meth = _PyType_Lookup(Py_Type(fieldobj), format_str);
505 if (meth == NULL) {
506 PyErr_Format(PyExc_TypeError,
507 "Type %.100s doesn't define __format__",
508 Py_Type(fieldobj)->tp_name);
509 goto done;
510 }
511
512 /* And call it, binding it to the value */
513 result = PyObject_CallFunctionObjArgs(meth, fieldobj, spec, NULL);
514 if (result == NULL)
515 goto done;
516
517 if (!STRINGLIB_CHECK(result)) {
518 PyErr_SetString(PyExc_TypeError,
519 "__format__ method did not return "
520 STRINGLIB_TYPE_NAME);
521 Py_DECREF(result);
522 result = NULL;
523 goto done;
524 }
525
526done:
527 Py_XDECREF(spec);
528 return result;
529}
530
531/*
532 render_field calls fieldobj.__format__(format_spec) method, and
533 appends to the output.
534*/
535static int
536render_field(PyObject *fieldobj, SubString *format_spec, OutputString *output)
537{
538 int ok = 0;
539 PyObject *result = format(fieldobj, format_spec);
540
541 if (result == NULL)
542 goto done;
543
544 ok = output_data(output,
545 STRINGLIB_STR(result), STRINGLIB_LEN(result));
546done:
547 Py_XDECREF(result);
548 return ok;
549}
550
551static int
552parse_field(SubString *str, SubString *field_name, SubString *format_spec,
553 STRINGLIB_CHAR *conversion)
554{
555 STRINGLIB_CHAR c = 0;
556
557 /* initialize these, as they may be empty */
558 *conversion = '\0';
559 SubString_init(format_spec, NULL, 0);
560
561 /* search for the field name. it's terminated by the end of the
562 string, or a ':' or '!' */
563 field_name->ptr = str->ptr;
564 while (str->ptr < str->end) {
565 switch (c = *(str->ptr++)) {
566 case ':':
567 case '!':
568 break;
569 default:
570 continue;
571 }
572 break;
573 }
574
575 if (c == '!' || c == ':') {
576 /* we have a format specifier and/or a conversion */
577 /* don't include the last character */
578 field_name->end = str->ptr-1;
579
580 /* the format specifier is the rest of the string */
581 format_spec->ptr = str->ptr;
582 format_spec->end = str->end;
583
584 /* see if there's a conversion specifier */
585 if (c == '!') {
586 /* there must be another character present */
587 if (format_spec->ptr >= format_spec->end) {
588 PyErr_SetString(PyExc_ValueError,
589 "end of format while looking for conversion "
590 "specifier");
591 return 0;
592 }
593 *conversion = *(format_spec->ptr++);
594
595 /* if there is another character, it must be a colon */
596 if (format_spec->ptr < format_spec->end) {
597 c = *(format_spec->ptr++);
598 if (c != ':') {
599 PyErr_SetString(PyExc_ValueError,
600 "expected ':' after format specifier");
601 return 0;
602 }
603 }
604 }
605
606 return 1;
607
608 } else {
609 /* end of string, there's no format_spec or conversion */
610 field_name->end = str->ptr;
611 return 1;
612 }
613}
614
615/************************************************************************/
616/******* Output string allocation and escape-to-markup processing ******/
617/************************************************************************/
618
619/* MarkupIterator breaks the string into pieces of either literal
620 text, or things inside {} that need to be marked up. it is
621 designed to make it easy to wrap a Python iterator around it, for
622 use with the Formatter class */
623
624typedef struct {
625 SubString str;
626 int in_markup;
627} MarkupIterator;
628
629static int
630MarkupIterator_init(MarkupIterator *self, STRINGLIB_CHAR *ptr, Py_ssize_t len)
631{
632 SubString_init(&self->str, ptr, len);
633 self->in_markup = 0;
634 return 1;
635}
636
637/* returns 0 on error, 1 on non-error termination, and 2 if it got a
638 string (or something to be expanded) */
639static int
640MarkupIterator_next(MarkupIterator *self, int *is_markup, SubString *literal,
641 SubString *field_name, SubString *format_spec,
642 STRINGLIB_CHAR *conversion,
643 int *format_spec_needs_expanding)
644{
645 int at_end;
646 STRINGLIB_CHAR c = 0;
647 STRINGLIB_CHAR *start;
648 int count;
649 Py_ssize_t len;
650
651 *format_spec_needs_expanding = 0;
652
653 /* no more input, end of iterator */
654 if (self->str.ptr >= self->str.end)
655 return 1;
656
657 *is_markup = self->in_markup;
658 start = self->str.ptr;
659
660 if (self->in_markup) {
661
662 /* prepare for next iteration */
663 self->in_markup = 0;
664
665 /* this is markup, find the end of the string by counting nested
666 braces. note that this prohibits escaped braces, so that
667 format_specs cannot have braces in them. */
668 count = 1;
669
670 /* we know we can't have a zero length string, so don't worry
671 about that case */
672 while (self->str.ptr < self->str.end) {
673 switch (c = *(self->str.ptr++)) {
674 case '{':
675 /* the format spec needs to be recursively expanded.
676 this is an optimization, and not strictly needed */
677 *format_spec_needs_expanding = 1;
678 count++;
679 break;
680 case '}':
681 count--;
682 if (count <= 0) {
683 /* we're done. parse and get out */
684 literal->ptr = start;
685 literal->end = self->str.ptr-1;
686
687 if (parse_field(literal, field_name, format_spec,
688 conversion) == 0)
689 return 0;
690
691 /* success */
692 return 2;
693 }
694 break;
695 }
696 }
697 /* end of string while searching for matching '}' */
698 PyErr_SetString(PyExc_ValueError, "unmatched '{' in format");
699 return 0;
700
701 } else {
702 /* literal text, read until the end of string, an escaped { or },
703 or an unescaped { */
704 while (self->str.ptr < self->str.end) {
705 switch (c = *(self->str.ptr++)) {
706 case '{':
707 case '}':
708 self->in_markup = 1;
709 break;
710 default:
711 continue;
712 }
713 break;
714 }
715
716 at_end = self->str.ptr >= self->str.end;
717 len = self->str.ptr - start;
718
Neal Norwitz3ef6a572007-08-25 17:08:59 +0000719 if ((c == '}') && (at_end || (c != *self->str.ptr))) {
720 SetError("Single } encountered");
721 return 0;
722 }
723 if (at_end && c == '{') {
724 SetError("Single { encountered");
725 return 0;
726 }
Eric Smith8c663262007-08-25 02:26:07 +0000727 if (!at_end) {
728 if (c == *self->str.ptr) {
729 /* escaped } or {, skip it in the input */
730 self->str.ptr++;
731 self->in_markup = 0;
732 } else
733 len--;
734 }
735
736 /* this is just plain text, return it */
737 literal->ptr = start;
738 literal->end = start + len;
739 return 2;
740 }
741}
742
743
744/* do the !r or !s conversion on obj */
745static PyObject *
746do_conversion(PyObject *obj, STRINGLIB_CHAR conversion)
747{
748 /* XXX in pre-3.0, do we need to convert this to unicode, since it
749 might have returned a string? */
750 switch (conversion) {
751 case 'r':
752 return PyObject_Repr(obj);
753 case 's':
754 return PyObject_Unicode(obj);
755 default:
756 PyErr_Format(PyExc_ValueError,
757 "Unknown converion specifier %c",
758 conversion);
759 return NULL;
760 }
761}
762
763/* given:
764
765 {field_name!conversion:format_spec}
766
767 compute the result and write it to output.
768 format_spec_needs_expanding is an optimization. if it's false,
769 just output the string directly, otherwise recursively expand the
770 format_spec string. */
771
772static int
773output_markup(SubString *field_name, SubString *format_spec,
774 int format_spec_needs_expanding, STRINGLIB_CHAR conversion,
775 OutputString *output, PyObject *args, PyObject *kwargs,
776 int *recursion_level)
777{
778 PyObject *tmp = NULL;
779 PyObject *fieldobj = NULL;
780 SubString expanded_format_spec;
781 SubString *actual_format_spec;
782 int result = 0;
783
784 /* convert field_name to an object */
785 fieldobj = get_field_object(field_name, args, kwargs);
786 if (fieldobj == NULL)
787 goto done;
788
789 if (conversion != '\0') {
790 tmp = do_conversion(fieldobj, conversion);
791 if (tmp == NULL)
792 goto done;
793
794 /* do the assignment, transferring ownership: fieldobj = tmp */
795 Py_DECREF(fieldobj);
796 fieldobj = tmp;
797 tmp = NULL;
798 }
799
800 /* if needed, recurively compute the format_spec */
801 if (format_spec_needs_expanding) {
802 tmp = build_string(format_spec, args, kwargs, recursion_level);
803 if (tmp == NULL)
804 goto done;
805
806 /* note that in the case we're expanding the format string,
807 tmp must be kept around until after the call to
808 render_field. */
809 SubString_init(&expanded_format_spec,
810 STRINGLIB_STR(tmp), STRINGLIB_LEN(tmp));
811 actual_format_spec = &expanded_format_spec;
812 } else
813 actual_format_spec = format_spec;
814
815 if (render_field(fieldobj, actual_format_spec, output) == 0)
816 goto done;
817
818 result = 1;
819
820done:
821 Py_XDECREF(fieldobj);
822 Py_XDECREF(tmp);
823
824 return result;
825}
826
827/*
828 do_markup is the top-level loop for the format() function. It
829 searches through the format string for escapes to markup codes, and
830 calls other functions to move non-markup text to the output,
831 and to perform the markup to the output.
832*/
833static int
834do_markup(SubString *input, PyObject *args, PyObject *kwargs,
835 OutputString *output, int *recursion_level)
836{
837 MarkupIterator iter;
838 int is_markup;
839 int format_spec_needs_expanding;
840 int result;
841 SubString str;
842 SubString field_name;
843 SubString format_spec;
844 STRINGLIB_CHAR conversion;
845
846 MarkupIterator_init(&iter, input->ptr, input->end - input->ptr);
847 while ((result = MarkupIterator_next(&iter, &is_markup, &str, &field_name,
848 &format_spec, &conversion,
849 &format_spec_needs_expanding)) == 2) {
850 if (is_markup) {
851 if (!output_markup(&field_name, &format_spec,
852 format_spec_needs_expanding, conversion, output,
853 args, kwargs, recursion_level))
854 return 0;
855 } else {
856 if (!output_data(output, str.ptr, str.end-str.ptr))
857 return 0;
858 }
859 }
860 return result;
861}
862
863
864/*
865 build_string allocates the output string and then
866 calls do_markup to do the heavy lifting.
867*/
868static PyObject *
869build_string(SubString *input, PyObject *args, PyObject *kwargs,
870 int *recursion_level)
871{
872 OutputString output;
873 PyObject *result = NULL;
874 Py_ssize_t count;
875
876 output.obj = NULL; /* needed so cleanup code always works */
877
878 /* check the recursion level */
879 (*recursion_level)--;
880 if (*recursion_level < 0) {
881 PyErr_SetString(PyExc_ValueError,
882 "Max string recursion exceeded");
883 goto done;
884 }
885
886 /* initial size is the length of the format string, plus the size
887 increment. seems like a reasonable default */
888 if (!output_initialize(&output,
889 input->end - input->ptr +
890 INITIAL_SIZE_INCREMENT))
891 goto done;
892
893 if (!do_markup(input, args, kwargs, &output, recursion_level)) {
894 goto done;
895 }
896
897 count = output.ptr - STRINGLIB_STR(output.obj);
898 if (STRINGLIB_RESIZE(&output.obj, count) < 0) {
899 goto done;
900 }
901
902 /* transfer ownership to result */
903 result = output.obj;
904 output.obj = NULL;
905
906done:
907 (*recursion_level)++;
908 Py_XDECREF(output.obj);
909 return result;
910}
911
912/************************************************************************/
913/*********** main routine ***********************************************/
914/************************************************************************/
915
916/* this is the main entry point */
917static PyObject *
918do_string_format(PyObject *self, PyObject *args, PyObject *kwargs)
919{
920 SubString input;
921
922 /* PEP 3101 says only 2 levels, so that
923 "{0:{1}}".format('abc', 's') # works
924 "{0:{1:{2}}}".format('abc', 's', '') # fails
925 */
926 int recursion_level = 2;
927
928 SubString_init(&input, STRINGLIB_STR(self), STRINGLIB_LEN(self));
929 return build_string(&input, args, kwargs, &recursion_level);
930}