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