annotate Objects/stringlib/string_format.h @ 80015:102df748572d 3.2

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