| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#if PY_VERSION_HEX < 0x03000000 |
| 11 |
#define PyLong_FromSsize_t _PyLong_FromSsize_t |
| 12 |
#endif |
| 13 |
|
| 14 |
|
| 15 |
#define INITIAL_SIZE_INCREMENT 100 |
| 16 |
#define SIZE_MULTIPLIER 2 |
| 17 |
#define MAX_SIZE_INCREMENT 3200 |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
typedef struct { |
| 29 |
STRINGLIB_CHAR *ptr; |
| 30 |
STRINGLIB_CHAR *end; |
| 31 |
} SubString; |
| 32 |
|
| 33 |
|
| 34 |
typedef enum { |
| 35 |
ANS_INIT, |
| 36 |
ANS_AUTO, |
| 37 |
ANS_MANUAL |
| 38 |
} AutoNumberState; |
| 39 |
|
| 40 |
|
| 41 |
typedef struct { |
| 42 |
AutoNumberState an_state; |
| 43 |
int an_field_number; |
| 44 |
} AutoNumber; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
static PyObject * |
| 49 |
build_string(SubString *input, PyObject *args, PyObject *kwargs, |
| 50 |
int recursion_depth, AutoNumber *auto_number); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
static void |
| 59 |
AutoNumber_Init(AutoNumber *auto_number) |
| 60 |
{ |
| 61 |
auto_number->an_state = ANS_INIT; |
| 62 |
auto_number->an_field_number = 0; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
Py_LOCAL_INLINE(void) |
| 67 |
SubString_init(SubString *str, STRINGLIB_CHAR *p, Py_ssize_t len) |
| 68 |
{ |
| 69 |
str->ptr = p; |
| 70 |
if (p == NULL) |
| 71 |
str->end = NULL; |
| 72 |
else |
| 73 |
str->end = str->ptr + len; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
Py_LOCAL_INLINE(PyObject *) |
| 78 |
SubString_new_object(SubString *str) |
| 79 |
{ |
| 80 |
if (str->ptr == NULL) { |
| 81 |
Py_INCREF(Py_None); |
| 82 |
return Py_None; |
| 83 |
} |
| 84 |
return STRINGLIB_NEW(str->ptr, str->end - str->ptr); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
Py_LOCAL_INLINE(PyObject *) |
| 89 |
SubString_new_object_or_empty(SubString *str) |
| 90 |
{ |
| 91 |
if (str->ptr == NULL) { |
| 92 |
return STRINGLIB_NEW(NULL, 0); |
| 93 |
} |
| 94 |
return STRINGLIB_NEW(str->ptr, str->end - str->ptr); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
static int |
| 101 |
autonumber_state_error(AutoNumberState state, int field_name_is_empty) |
| 102 |
{ |
| 103 |
if (state == ANS_MANUAL) { |
| 104 |
if (field_name_is_empty) { |
| 105 |
PyErr_SetString(PyExc_ValueError, "cannot switch from " |
| 106 |
"manual field specification to " |
| 107 |
"automatic field numbering"); |
| 108 |
return 1; |
| 109 |
} |
| 110 |
} |
| 111 |
else { |
| 112 |
if (!field_name_is_empty) { |
| 113 |
PyErr_SetString(PyExc_ValueError, "cannot switch from " |
| 114 |
"automatic field numbering to " |
| 115 |
"manual field specification"); |
| 116 |
return 1; |
| 117 |
} |
| 118 |
} |
| 119 |
return 0; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
typedef struct { |
| 128 |
STRINGLIB_CHAR *ptr; |
| 129 |
STRINGLIB_CHAR *end; |
| 130 |
PyObject *obj; |
| 131 |
Py_ssize_t size_increment; |
| 132 |
} OutputString; |
| 133 |
|
| 134 |
|
| 135 |
static int |
| 136 |
output_initialize(OutputString *output, Py_ssize_t size) |
| 137 |
{ |
| 138 |
output->obj = STRINGLIB_NEW(NULL, size); |
| 139 |
if (output->obj == NULL) |
| 140 |
return 0; |
| 141 |
|
| 142 |
output->ptr = STRINGLIB_STR(output->obj); |
| 143 |
output->end = STRINGLIB_LEN(output->obj) + output->ptr; |
| 144 |
output->size_increment = INITIAL_SIZE_INCREMENT; |
| 145 |
|
| 146 |
return 1; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
static int |
| 156 |
output_extend(OutputString *output, Py_ssize_t count) |
| 157 |
{ |
| 158 |
STRINGLIB_CHAR *startptr = STRINGLIB_STR(output->obj); |
| 159 |
Py_ssize_t curlen = output->ptr - startptr; |
| 160 |
Py_ssize_t maxlen = curlen + count + output->size_increment; |
| 161 |
|
| 162 |
if (STRINGLIB_RESIZE(&output->obj, maxlen) < 0) |
| 163 |
return 0; |
| 164 |
startptr = STRINGLIB_STR(output->obj); |
| 165 |
output->ptr = startptr + curlen; |
| 166 |
output->end = startptr + maxlen; |
| 167 |
if (output->size_increment < MAX_SIZE_INCREMENT) |
| 168 |
output->size_increment *= SIZE_MULTIPLIER; |
| 169 |
return 1; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
static int |
| 182 |
output_data(OutputString *output, const STRINGLIB_CHAR *s, Py_ssize_t count) |
| 183 |
{ |
| 184 |
if ((count > output->end - output->ptr) && !output_extend(output, count)) |
| 185 |
return 0; |
| 186 |
memcpy(output->ptr, s, count * sizeof(STRINGLIB_CHAR)); |
| 187 |
output->ptr += count; |
| 188 |
return 1; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
static Py_ssize_t |
| 196 |
get_integer(const SubString *str) |
| 197 |
{ |
| 198 |
Py_ssize_t accumulator = 0; |
| 199 |
Py_ssize_t digitval; |
| 200 |
STRINGLIB_CHAR *p; |
| 201 |
|
| 202 |
|
| 203 |
if (str->ptr >= str->end) |
| 204 |
return -1; |
| 205 |
|
| 206 |
for (p = str->ptr; p < str->end; p++) { |
| 207 |
digitval = STRINGLIB_TODECIMAL(*p); |
| 208 |
if (digitval < 0) |
| 209 |
return -1; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
if (accumulator > (PY_SSIZE_T_MAX - digitval) / 10) { |
| 217 |
PyErr_Format(PyExc_ValueError, |
| 218 |
"Too many decimal digits in format string"); |
| 219 |
return -1; |
| 220 |
} |
| 221 |
accumulator = accumulator * 10 + digitval; |
| 222 |
} |
| 223 |
return accumulator; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
static PyObject * |
| 232 |
getattr(PyObject *obj, SubString *name) |
| 233 |
{ |
| 234 |
PyObject *newobj; |
| 235 |
PyObject *str = SubString_new_object(name); |
| 236 |
if (str == NULL) |
| 237 |
return NULL; |
| 238 |
newobj = PyObject_GetAttr(obj, str); |
| 239 |
Py_DECREF(str); |
| 240 |
return newobj; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
static PyObject * |
| 245 |
getitem_sequence(PyObject *obj, Py_ssize_t idx) |
| 246 |
{ |
| 247 |
return PySequence_GetItem(obj, idx); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
static PyObject * |
| 252 |
getitem_idx(PyObject *obj, Py_ssize_t idx) |
| 253 |
{ |
| 254 |
PyObject *newobj; |
| 255 |
PyObject *idx_obj = PyLong_FromSsize_t(idx); |
| 256 |
if (idx_obj == NULL) |
| 257 |
return NULL; |
| 258 |
newobj = PyObject_GetItem(obj, idx_obj); |
| 259 |
Py_DECREF(idx_obj); |
| 260 |
return newobj; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
static PyObject * |
| 265 |
getitem_str(PyObject *obj, SubString *name) |
| 266 |
{ |
| 267 |
PyObject *newobj; |
| 268 |
PyObject *str = SubString_new_object(name); |
| 269 |
if (str == NULL) |
| 270 |
return NULL; |
| 271 |
newobj = PyObject_GetItem(obj, str); |
| 272 |
Py_DECREF(str); |
| 273 |
return newobj; |
| 274 |
} |
| 275 |
|
| 276 |
typedef struct { |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
SubString str; |
| 281 |
|
| 282 |
|
| 283 |
STRINGLIB_CHAR *ptr; |
| 284 |
} FieldNameIterator; |
| 285 |
|
| 286 |
|
| 287 |
static int |
| 288 |
FieldNameIterator_init(FieldNameIterator *self, STRINGLIB_CHAR *ptr, |
| 289 |
Py_ssize_t len) |
| 290 |
{ |
| 291 |
SubString_init(&self->str, ptr, len); |
| 292 |
self->ptr = self->str.ptr; |
| 293 |
return 1; |
| 294 |
} |
| 295 |
|
| 296 |
static int |
| 297 |
_FieldNameIterator_attr(FieldNameIterator *self, SubString *name) |
| 298 |
{ |
| 299 |
STRINGLIB_CHAR c; |
| 300 |
|
| 301 |
name->ptr = self->ptr; |
| 302 |
|
| 303 |
|
| 304 |
while (self->ptr < self->str.end) { |
| 305 |
switch (c = *self->ptr++) { |
| 306 |
case '[': |
| 307 |
case '.': |
| 308 |
|
| 309 |
self->ptr--; |
| 310 |
break; |
| 311 |
default: |
| 312 |
continue; |
| 313 |
} |
| 314 |
break; |
| 315 |
} |
| 316 |
|
| 317 |
name->end = self->ptr; |
| 318 |
return 1; |
| 319 |
} |
| 320 |
|
| 321 |
static int |
| 322 |
_FieldNameIterator_item(FieldNameIterator *self, SubString *name) |
| 323 |
{ |
| 324 |
int bracket_seen = 0; |
| 325 |
STRINGLIB_CHAR c; |
| 326 |
|
| 327 |
name->ptr = self->ptr; |
| 328 |
|
| 329 |
|
| 330 |
while (self->ptr < self->str.end) { |
| 331 |
switch (c = *self->ptr++) { |
| 332 |
case ']': |
| 333 |
bracket_seen = 1; |
| 334 |
break; |
| 335 |
default: |
| 336 |
continue; |
| 337 |
} |
| 338 |
break; |
| 339 |
} |
| 340 |
|
| 341 |
if (!bracket_seen) { |
| 342 |
PyErr_SetString(PyExc_ValueError, "Missing ']' in format string"); |
| 343 |
return 0; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
name->end = self->ptr-1; |
| 349 |
return 1; |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
static int |
| 354 |
FieldNameIterator_next(FieldNameIterator *self, int *is_attribute, |
| 355 |
Py_ssize_t *name_idx, SubString *name) |
| 356 |
{ |
| 357 |
|
| 358 |
if (self->ptr >= self->str.end) |
| 359 |
return 1; |
| 360 |
|
| 361 |
switch (*self->ptr++) { |
| 362 |
case '.': |
| 363 |
*is_attribute = 1; |
| 364 |
if (_FieldNameIterator_attr(self, name) == 0) |
| 365 |
return 0; |
| 366 |
*name_idx = -1; |
| 367 |
break; |
| 368 |
case '[': |
| 369 |
*is_attribute = 0; |
| 370 |
if (_FieldNameIterator_item(self, name) == 0) |
| 371 |
return 0; |
| 372 |
*name_idx = get_integer(name); |
| 373 |
if (*name_idx == -1 && PyErr_Occurred()) |
| 374 |
return 0; |
| 375 |
break; |
| 376 |
default: |
| 377 |
|
| 378 |
PyErr_SetString(PyExc_ValueError, "Only '.' or '[' may " |
| 379 |
"follow ']' in format field specifier"); |
| 380 |
return 0; |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
if (name->ptr == name->end) { |
| 385 |
PyErr_SetString(PyExc_ValueError, "Empty attribute in format string"); |
| 386 |
return 0; |
| 387 |
} |
| 388 |
|
| 389 |
return 2; |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
static int |
| 400 |
field_name_split(STRINGLIB_CHAR *ptr, Py_ssize_t len, SubString *first, |
| 401 |
Py_ssize_t *first_idx, FieldNameIterator *rest, |
| 402 |
AutoNumber *auto_number) |
| 403 |
{ |
| 404 |
STRINGLIB_CHAR c; |
| 405 |
STRINGLIB_CHAR *p = ptr; |
| 406 |
STRINGLIB_CHAR *end = ptr + len; |
| 407 |
int field_name_is_empty; |
| 408 |
int using_numeric_index; |
| 409 |
|
| 410 |
|
| 411 |
while (p < end) { |
| 412 |
switch (c = *p++) { |
| 413 |
case '[': |
| 414 |
case '.': |
| 415 |
|
| 416 |
|
| 417 |
p--; |
| 418 |
break; |
| 419 |
default: |
| 420 |
continue; |
| 421 |
} |
| 422 |
break; |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
SubString_init(first, ptr, p - ptr); |
| 427 |
FieldNameIterator_init(rest, p, end - p); |
| 428 |
|
| 429 |
|
| 430 |
*first_idx = get_integer(first); |
| 431 |
if (*first_idx == -1 && PyErr_Occurred()) |
| 432 |
return 0; |
| 433 |
|
| 434 |
field_name_is_empty = first->ptr >= first->end; |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
using_numeric_index = field_name_is_empty || *first_idx != -1; |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
if (auto_number) { |
| 449 |
|
| 450 |
|
| 451 |
if (auto_number->an_state == ANS_INIT && using_numeric_index) |
| 452 |
auto_number->an_state = field_name_is_empty ? |
| 453 |
ANS_AUTO : ANS_MANUAL; |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
if (using_numeric_index) |
| 459 |
if (autonumber_state_error(auto_number->an_state, |
| 460 |
field_name_is_empty)) |
| 461 |
return 0; |
| 462 |
|
| 463 |
|
| 464 |
if (field_name_is_empty) |
| 465 |
*first_idx = (auto_number->an_field_number)++; |
| 466 |
} |
| 467 |
|
| 468 |
return 1; |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
static PyObject * |
| 478 |
get_field_object(SubString *input, PyObject *args, PyObject *kwargs, |
| 479 |
AutoNumber *auto_number) |
| 480 |
{ |
| 481 |
PyObject *obj = NULL; |
| 482 |
int ok; |
| 483 |
int is_attribute; |
| 484 |
SubString name; |
| 485 |
SubString first; |
| 486 |
Py_ssize_t index; |
| 487 |
FieldNameIterator rest; |
| 488 |
|
| 489 |
if (!field_name_split(input->ptr, input->end - input->ptr, &first, |
| 490 |
&index, &rest, auto_number)) { |
| 491 |
goto error; |
| 492 |
} |
| 493 |
|
| 494 |
if (index == -1) { |
| 495 |
|
| 496 |
PyObject *key = SubString_new_object(&first); |
| 497 |
if (key == NULL) |
| 498 |
goto error; |
| 499 |
if ((kwargs == NULL) || (obj = PyDict_GetItem(kwargs, key)) == NULL) { |
| 500 |
PyErr_SetObject(PyExc_KeyError, key); |
| 501 |
Py_DECREF(key); |
| 502 |
goto error; |
| 503 |
} |
| 504 |
Py_DECREF(key); |
| 505 |
Py_INCREF(obj); |
| 506 |
} |
| 507 |
else { |
| 508 |
|
| 509 |
obj = PySequence_GetItem(args, index); |
| 510 |
if (obj == NULL) |
| 511 |
goto error; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
while ((ok = FieldNameIterator_next(&rest, &is_attribute, &index, |
| 516 |
&name)) == 2) { |
| 517 |
PyObject *tmp; |
| 518 |
|
| 519 |
if (is_attribute) |
| 520 |
|
| 521 |
tmp = getattr(obj, &name); |
| 522 |
else |
| 523 |
|
| 524 |
if (index == -1) |
| 525 |
tmp = getitem_str(obj, &name); |
| 526 |
else |
| 527 |
if (PySequence_Check(obj)) |
| 528 |
tmp = getitem_sequence(obj, index); |
| 529 |
else |
| 530 |
|
| 531 |
tmp = getitem_idx(obj, index); |
| 532 |
if (tmp == NULL) |
| 533 |
goto error; |
| 534 |
|
| 535 |
|
| 536 |
Py_DECREF(obj); |
| 537 |
obj = tmp; |
| 538 |
} |
| 539 |
|
| 540 |
if (ok == 1) |
| 541 |
return obj; |
| 542 |
error: |
| 543 |
Py_XDECREF(obj); |
| 544 |
return NULL; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
static int |
| 560 |
render_field(PyObject *fieldobj, SubString *format_spec, OutputString *output) |
| 561 |
{ |
| 562 |
int ok = 0; |
| 563 |
PyObject *result = NULL; |
| 564 |
PyObject *format_spec_object = NULL; |
| 565 |
PyObject *(*formatter)(PyObject *, STRINGLIB_CHAR *, Py_ssize_t) = NULL; |
| 566 |
STRINGLIB_CHAR* format_spec_start = format_spec->ptr ? |
| 567 |
format_spec->ptr : NULL; |
| 568 |
Py_ssize_t format_spec_len = format_spec->ptr ? |
| 569 |
format_spec->end - format_spec->ptr : 0; |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
#if STRINGLIB_IS_UNICODE |
| 574 |
if (PyUnicode_CheckExact(fieldobj)) |
| 575 |
formatter = _PyUnicode_FormatAdvanced; |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
#else |
| 582 |
if (PyString_CheckExact(fieldobj)) |
| 583 |
formatter = _PyBytes_FormatAdvanced; |
| 584 |
else if (PyInt_CheckExact(fieldobj)) |
| 585 |
formatter =_PyInt_FormatAdvanced; |
| 586 |
else if (PyLong_CheckExact(fieldobj)) |
| 587 |
formatter =_PyLong_FormatAdvanced; |
| 588 |
else if (PyFloat_CheckExact(fieldobj)) |
| 589 |
formatter = _PyFloat_FormatAdvanced; |
| 590 |
#endif |
| 591 |
|
| 592 |
if (formatter) { |
| 593 |
|
| 594 |
|
| 595 |
result = formatter(fieldobj, format_spec_start, format_spec_len); |
| 596 |
} |
| 597 |
else { |
| 598 |
|
| 599 |
|
| 600 |
format_spec_object = STRINGLIB_NEW(format_spec_start, |
| 601 |
format_spec_len); |
| 602 |
if (format_spec_object == NULL) |
| 603 |
goto done; |
| 604 |
|
| 605 |
result = PyObject_Format(fieldobj, format_spec_object); |
| 606 |
} |
| 607 |
if (result == NULL) |
| 608 |
goto done; |
| 609 |
|
| 610 |
#if PY_VERSION_HEX >= 0x03000000 |
| 611 |
assert(PyUnicode_Check(result)); |
| 612 |
#else |
| 613 |
assert(PyString_Check(result) || PyUnicode_Check(result)); |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
{ |
| 618 |
PyObject *tmp = STRINGLIB_TOSTR(result); |
| 619 |
if (tmp == NULL) |
| 620 |
goto done; |
| 621 |
Py_DECREF(result); |
| 622 |
result = tmp; |
| 623 |
} |
| 624 |
#endif |
| 625 |
|
| 626 |
ok = output_data(output, |
| 627 |
STRINGLIB_STR(result), STRINGLIB_LEN(result)); |
| 628 |
done: |
| 629 |
Py_XDECREF(format_spec_object); |
| 630 |
Py_XDECREF(result); |
| 631 |
return ok; |
| 632 |
} |
| 633 |
|
| 634 |
static int |
| 635 |
parse_field(SubString *str, SubString *field_name, SubString *format_spec, |
| 636 |
STRINGLIB_CHAR *conversion) |
| 637 |
{ |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
STRINGLIB_CHAR c = 0; |
| 643 |
|
| 644 |
|
| 645 |
*conversion = '\0'; |
| 646 |
SubString_init(format_spec, NULL, 0); |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
field_name->ptr = str->ptr; |
| 651 |
while (str->ptr < str->end) { |
| 652 |
switch (c = *(str->ptr++)) { |
| 653 |
case ':': |
| 654 |
case '!': |
| 655 |
break; |
| 656 |
default: |
| 657 |
continue; |
| 658 |
} |
| 659 |
break; |
| 660 |
} |
| 661 |
|
| 662 |
if (c == '!' || c == ':') { |
| 663 |
|
| 664 |
|
| 665 |
field_name->end = str->ptr-1; |
| 666 |
|
| 667 |
|
| 668 |
format_spec->ptr = str->ptr; |
| 669 |
format_spec->end = str->end; |
| 670 |
|
| 671 |
|
| 672 |
if (c == '!') { |
| 673 |
|
| 674 |
if (format_spec->ptr >= format_spec->end) { |
| 675 |
PyErr_SetString(PyExc_ValueError, |
| 676 |
"end of format while looking for conversion " |
| 677 |
"specifier"); |
| 678 |
return 0; |
| 679 |
} |
| 680 |
*conversion = *(format_spec->ptr++); |
| 681 |
|
| 682 |
|
| 683 |
if (format_spec->ptr < format_spec->end) { |
| 684 |
c = *(format_spec->ptr++); |
| 685 |
if (c != ':') { |
| 686 |
PyErr_SetString(PyExc_ValueError, |
| 687 |
"expected ':' after format specifier"); |
| 688 |
return 0; |
| 689 |
} |
| 690 |
} |
| 691 |
} |
| 692 |
} |
| 693 |
else |
| 694 |
|
| 695 |
field_name->end = str->ptr; |
| 696 |
|
| 697 |
return 1; |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
typedef struct { |
| 710 |
SubString str; |
| 711 |
} MarkupIterator; |
| 712 |
|
| 713 |
static int |
| 714 |
MarkupIterator_init(MarkupIterator *self, STRINGLIB_CHAR *ptr, Py_ssize_t len) |
| 715 |
{ |
| 716 |
SubString_init(&self->str, ptr, len); |
| 717 |
return 1; |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
static int |
| 723 |
MarkupIterator_next(MarkupIterator *self, SubString *literal, |
| 724 |
int *field_present, SubString *field_name, |
| 725 |
SubString *format_spec, STRINGLIB_CHAR *conversion, |
| 726 |
int *format_spec_needs_expanding) |
| 727 |
{ |
| 728 |
int at_end; |
| 729 |
STRINGLIB_CHAR c = 0; |
| 730 |
STRINGLIB_CHAR *start; |
| 731 |
int count; |
| 732 |
Py_ssize_t len; |
| 733 |
int markup_follows = 0; |
| 734 |
|
| 735 |
|
| 736 |
SubString_init(literal, NULL, 0); |
| 737 |
SubString_init(field_name, NULL, 0); |
| 738 |
SubString_init(format_spec, NULL, 0); |
| 739 |
*conversion = '\0'; |
| 740 |
*format_spec_needs_expanding = 0; |
| 741 |
*field_present = 0; |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
if (self->str.ptr >= self->str.end) |
| 746 |
return 1; |
| 747 |
|
| 748 |
start = self->str.ptr; |
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
while (self->str.ptr < self->str.end) { |
| 758 |
switch (c = *(self->str.ptr++)) { |
| 759 |
case '{': |
| 760 |
case '}': |
| 761 |
markup_follows = 1; |
| 762 |
break; |
| 763 |
default: |
| 764 |
continue; |
| 765 |
} |
| 766 |
break; |
| 767 |
} |
| 768 |
|
| 769 |
at_end = self->str.ptr >= self->str.end; |
| 770 |
len = self->str.ptr - start; |
| 771 |
|
| 772 |
if ((c == '}') && (at_end || (c != *self->str.ptr))) { |
| 773 |
PyErr_SetString(PyExc_ValueError, "Single '}' encountered " |
| 774 |
"in format string"); |
| 775 |
return 0; |
| 776 |
} |
| 777 |
if (at_end && c == '{') { |
| 778 |
PyErr_SetString(PyExc_ValueError, "Single '{' encountered " |
| 779 |
"in format string"); |
| 780 |
return 0; |
| 781 |
} |
| 782 |
if (!at_end) { |
| 783 |
if (c == *self->str.ptr) { |
| 784 |
|
| 785 |
|
| 786 |
self->str.ptr++; |
| 787 |
markup_follows = 0; |
| 788 |
} |
| 789 |
else |
| 790 |
len--; |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
literal->ptr = start; |
| 795 |
literal->end = start + len; |
| 796 |
|
| 797 |
if (!markup_follows) |
| 798 |
return 2; |
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
*field_present = 1; |
| 804 |
count = 1; |
| 805 |
|
| 806 |
start = self->str.ptr; |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
while (self->str.ptr < self->str.end) { |
| 811 |
switch (c = *(self->str.ptr++)) { |
| 812 |
case '{': |
| 813 |
|
| 814 |
|
| 815 |
*format_spec_needs_expanding = 1; |
| 816 |
count++; |
| 817 |
break; |
| 818 |
case '}': |
| 819 |
count--; |
| 820 |
if (count <= 0) { |
| 821 |
|
| 822 |
SubString s; |
| 823 |
|
| 824 |
SubString_init(&s, start, self->str.ptr - 1 - start); |
| 825 |
if (parse_field(&s, field_name, format_spec, conversion) == 0) |
| 826 |
return 0; |
| 827 |
|
| 828 |
|
| 829 |
return 2; |
| 830 |
} |
| 831 |
break; |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
|
| 836 |
PyErr_SetString(PyExc_ValueError, "unmatched '{' in format"); |
| 837 |
return 0; |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
static PyObject * |
| 843 |
do_conversion(PyObject *obj, STRINGLIB_CHAR conversion) |
| 844 |
{ |
| 845 |
|
| 846 |
|
| 847 |
switch (conversion) { |
| 848 |
case 'r': |
| 849 |
return PyObject_Repr(obj); |
| 850 |
case 's': |
| 851 |
return STRINGLIB_TOSTR(obj); |
| 852 |
default: |
| 853 |
if (conversion > 32 && conversion < 127) { |
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
PyErr_Format(PyExc_ValueError, |
| 858 |
"Unknown conversion specifier %c", |
| 859 |
(char)conversion); |
| 860 |
} else |
| 861 |
PyErr_Format(PyExc_ValueError, |
| 862 |
"Unknown conversion specifier \\x%x", |
| 863 |
(unsigned int)conversion); |
| 864 |
return NULL; |
| 865 |
} |
| 866 |
} |
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
static int |
| 882 |
output_markup(SubString *field_name, SubString *format_spec, |
| 883 |
int format_spec_needs_expanding, STRINGLIB_CHAR conversion, |
| 884 |
OutputString *output, PyObject *args, PyObject *kwargs, |
| 885 |
int recursion_depth, AutoNumber *auto_number) |
| 886 |
{ |
| 887 |
PyObject *tmp = NULL; |
| 888 |
PyObject *fieldobj = NULL; |
| 889 |
SubString expanded_format_spec; |
| 890 |
SubString *actual_format_spec; |
| 891 |
int result = 0; |
| 892 |
|
| 893 |
|
| 894 |
fieldobj = get_field_object(field_name, args, kwargs, auto_number); |
| 895 |
if (fieldobj == NULL) |
| 896 |
goto done; |
| 897 |
|
| 898 |
if (conversion != '\0') { |
| 899 |
tmp = do_conversion(fieldobj, conversion); |
| 900 |
if (tmp == NULL) |
| 901 |
goto done; |
| 902 |
|
| 903 |
|
| 904 |
Py_DECREF(fieldobj); |
| 905 |
fieldobj = tmp; |
| 906 |
tmp = NULL; |
| 907 |
} |
| 908 |
|
| 909 |
|
| 910 |
if (format_spec_needs_expanding) { |
| 911 |
tmp = build_string(format_spec, args, kwargs, recursion_depth-1, |
| 912 |
auto_number); |
| 913 |
if (tmp == NULL) |
| 914 |
goto done; |
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
SubString_init(&expanded_format_spec, |
| 920 |
STRINGLIB_STR(tmp), STRINGLIB_LEN(tmp)); |
| 921 |
actual_format_spec = &expanded_format_spec; |
| 922 |
} |
| 923 |
else |
| 924 |
actual_format_spec = format_spec; |
| 925 |
|
| 926 |
if (render_field(fieldobj, actual_format_spec, output) == 0) |
| 927 |
goto done; |
| 928 |
|
| 929 |
result = 1; |
| 930 |
|
| 931 |
done: |
| 932 |
Py_XDECREF(fieldobj); |
| 933 |
Py_XDECREF(tmp); |
| 934 |
|
| 935 |
return result; |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
static int |
| 945 |
do_markup(SubString *input, PyObject *args, PyObject *kwargs, |
| 946 |
OutputString *output, int recursion_depth, AutoNumber *auto_number) |
| 947 |
{ |
| 948 |
MarkupIterator iter; |
| 949 |
int format_spec_needs_expanding; |
| 950 |
int result; |
| 951 |
int field_present; |
| 952 |
SubString literal; |
| 953 |
SubString field_name; |
| 954 |
SubString format_spec; |
| 955 |
STRINGLIB_CHAR conversion; |
| 956 |
|
| 957 |
MarkupIterator_init(&iter, input->ptr, input->end - input->ptr); |
| 958 |
while ((result = MarkupIterator_next(&iter, &literal, &field_present, |
| 959 |
&field_name, &format_spec, |
| 960 |
&conversion, |
| 961 |
&format_spec_needs_expanding)) == 2) { |
| 962 |
if (!output_data(output, literal.ptr, literal.end - literal.ptr)) |
| 963 |
return 0; |
| 964 |
if (field_present) |
| 965 |
if (!output_markup(&field_name, &format_spec, |
| 966 |
format_spec_needs_expanding, conversion, output, |
| 967 |
args, kwargs, recursion_depth, auto_number)) |
| 968 |
return 0; |
| 969 |
} |
| 970 |
return result; |
| 971 |
} |
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
static PyObject * |
| 979 |
build_string(SubString *input, PyObject *args, PyObject *kwargs, |
| 980 |
int recursion_depth, AutoNumber *auto_number) |
| 981 |
{ |
| 982 |
OutputString output; |
| 983 |
PyObject *result = NULL; |
| 984 |
Py_ssize_t count; |
| 985 |
|
| 986 |
output.obj = NULL; |
| 987 |
|
| 988 |
|
| 989 |
if (recursion_depth <= 0) { |
| 990 |
PyErr_SetString(PyExc_ValueError, |
| 991 |
"Max string recursion exceeded"); |
| 992 |
goto done; |
| 993 |
} |
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
if (!output_initialize(&output, |
| 998 |
input->end - input->ptr + |
| 999 |
INITIAL_SIZE_INCREMENT)) |
| 1000 |
goto done; |
| 1001 |
|
| 1002 |
if (!do_markup(input, args, kwargs, &output, recursion_depth, |
| 1003 |
auto_number)) { |
| 1004 |
goto done; |
| 1005 |
} |
| 1006 |
|
| 1007 |
count = output.ptr - STRINGLIB_STR(output.obj); |
| 1008 |
if (STRINGLIB_RESIZE(&output.obj, count) < 0) { |
| 1009 |
goto done; |
| 1010 |
} |
| 1011 |
|
| 1012 |
|
| 1013 |
result = output.obj; |
| 1014 |
output.obj = NULL; |
| 1015 |
|
| 1016 |
done: |
| 1017 |
Py_XDECREF(output.obj); |
| 1018 |
return result; |
| 1019 |
} |
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
static PyObject * |
| 1027 |
do_string_format(PyObject *self, PyObject *args, PyObject *kwargs) |
| 1028 |
{ |
| 1029 |
SubString input; |
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
int recursion_depth = 2; |
| 1036 |
|
| 1037 |
AutoNumber auto_number; |
| 1038 |
|
| 1039 |
AutoNumber_Init(&auto_number); |
| 1040 |
SubString_init(&input, STRINGLIB_STR(self), STRINGLIB_LEN(self)); |
| 1041 |
return build_string(&input, args, kwargs, recursion_depth, &auto_number); |
| 1042 |
} |
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
typedef struct { |
| 1056 |
PyObject_HEAD |
| 1057 |
|
| 1058 |
STRINGLIB_OBJECT *str; |
| 1059 |
|
| 1060 |
MarkupIterator it_markup; |
| 1061 |
} formatteriterobject; |
| 1062 |
|
| 1063 |
static void |
| 1064 |
formatteriter_dealloc(formatteriterobject *it) |
| 1065 |
{ |
| 1066 |
Py_XDECREF(it->str); |
| 1067 |
PyObject_FREE(it); |
| 1068 |
} |
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
static PyObject * |
| 1079 |
formatteriter_next(formatteriterobject *it) |
| 1080 |
{ |
| 1081 |
SubString literal; |
| 1082 |
SubString field_name; |
| 1083 |
SubString format_spec; |
| 1084 |
STRINGLIB_CHAR conversion; |
| 1085 |
int format_spec_needs_expanding; |
| 1086 |
int field_present; |
| 1087 |
int result = MarkupIterator_next(&it->it_markup, &literal, &field_present, |
| 1088 |
&field_name, &format_spec, &conversion, |
| 1089 |
&format_spec_needs_expanding); |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
assert(0 <= result && result <= 2); |
| 1094 |
if (result == 0 || result == 1) |
| 1095 |
|
| 1096 |
return NULL; |
| 1097 |
else { |
| 1098 |
PyObject *literal_str = NULL; |
| 1099 |
PyObject *field_name_str = NULL; |
| 1100 |
PyObject *format_spec_str = NULL; |
| 1101 |
PyObject *conversion_str = NULL; |
| 1102 |
PyObject *tuple = NULL; |
| 1103 |
|
| 1104 |
literal_str = SubString_new_object(&literal); |
| 1105 |
if (literal_str == NULL) |
| 1106 |
goto done; |
| 1107 |
|
| 1108 |
field_name_str = SubString_new_object(&field_name); |
| 1109 |
if (field_name_str == NULL) |
| 1110 |
goto done; |
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
format_spec_str = (field_present ? |
| 1115 |
SubString_new_object_or_empty : |
| 1116 |
SubString_new_object)(&format_spec); |
| 1117 |
if (format_spec_str == NULL) |
| 1118 |
goto done; |
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
if (conversion == '\0') { |
| 1124 |
conversion_str = Py_None; |
| 1125 |
Py_INCREF(conversion_str); |
| 1126 |
} |
| 1127 |
else |
| 1128 |
conversion_str = STRINGLIB_NEW(&conversion, 1); |
| 1129 |
if (conversion_str == NULL) |
| 1130 |
goto done; |
| 1131 |
|
| 1132 |
tuple = PyTuple_Pack(4, literal_str, field_name_str, format_spec_str, |
| 1133 |
conversion_str); |
| 1134 |
done: |
| 1135 |
Py_XDECREF(literal_str); |
| 1136 |
Py_XDECREF(field_name_str); |
| 1137 |
Py_XDECREF(format_spec_str); |
| 1138 |
Py_XDECREF(conversion_str); |
| 1139 |
return tuple; |
| 1140 |
} |
| 1141 |
} |
| 1142 |
|
| 1143 |
static PyMethodDef formatteriter_methods[] = { |
| 1144 |
{NULL, NULL} |
| 1145 |
}; |
| 1146 |
|
| 1147 |
static PyTypeObject PyFormatterIter_Type = { |
| 1148 |
PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1149 |
"formatteriterator", |
| 1150 |
sizeof(formatteriterobject), |
| 1151 |
0, |
| 1152 |
|
| 1153 |
(destructor)formatteriter_dealloc, |
| 1154 |
0, |
| 1155 |
0, |
| 1156 |
0, |
| 1157 |
0, |
| 1158 |
0, |
| 1159 |
0, |
| 1160 |
0, |
| 1161 |
0, |
| 1162 |
0, |
| 1163 |
0, |
| 1164 |
0, |
| 1165 |
PyObject_GenericGetAttr, |
| 1166 |
0, |
| 1167 |
0, |
| 1168 |
Py_TPFLAGS_DEFAULT, |
| 1169 |
0, |
| 1170 |
0, |
| 1171 |
0, |
| 1172 |
0, |
| 1173 |
0, |
| 1174 |
PyObject_SelfIter, |
| 1175 |
(iternextfunc)formatteriter_next, |
| 1176 |
formatteriter_methods, |
| 1177 |
0, |
| 1178 |
}; |
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
static PyObject * |
| 1185 |
formatter_parser(STRINGLIB_OBJECT *self) |
| 1186 |
{ |
| 1187 |
formatteriterobject *it; |
| 1188 |
|
| 1189 |
it = PyObject_New(formatteriterobject, &PyFormatterIter_Type); |
| 1190 |
if (it == NULL) |
| 1191 |
return NULL; |
| 1192 |
|
| 1193 |
|
| 1194 |
Py_INCREF(self); |
| 1195 |
it->str = self; |
| 1196 |
|
| 1197 |
|
| 1198 |
MarkupIterator_init(&it->it_markup, |
| 1199 |
STRINGLIB_STR(self), |
| 1200 |
STRINGLIB_LEN(self)); |
| 1201 |
|
| 1202 |
return (PyObject *)it; |
| 1203 |
} |
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
typedef struct { |
| 1216 |
PyObject_HEAD |
| 1217 |
|
| 1218 |
STRINGLIB_OBJECT *str; |
| 1219 |
|
| 1220 |
FieldNameIterator it_field; |
| 1221 |
} fieldnameiterobject; |
| 1222 |
|
| 1223 |
static void |
| 1224 |
fieldnameiter_dealloc(fieldnameiterobject *it) |
| 1225 |
{ |
| 1226 |
Py_XDECREF(it->str); |
| 1227 |
PyObject_FREE(it); |
| 1228 |
} |
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
static PyObject * |
| 1237 |
fieldnameiter_next(fieldnameiterobject *it) |
| 1238 |
{ |
| 1239 |
int result; |
| 1240 |
int is_attr; |
| 1241 |
Py_ssize_t idx; |
| 1242 |
SubString name; |
| 1243 |
|
| 1244 |
result = FieldNameIterator_next(&it->it_field, &is_attr, |
| 1245 |
&idx, &name); |
| 1246 |
if (result == 0 || result == 1) |
| 1247 |
|
| 1248 |
return NULL; |
| 1249 |
else { |
| 1250 |
PyObject* result = NULL; |
| 1251 |
PyObject* is_attr_obj = NULL; |
| 1252 |
PyObject* obj = NULL; |
| 1253 |
|
| 1254 |
is_attr_obj = PyBool_FromLong(is_attr); |
| 1255 |
if (is_attr_obj == NULL) |
| 1256 |
goto done; |
| 1257 |
|
| 1258 |
|
| 1259 |
if (idx != -1) |
| 1260 |
obj = PyLong_FromSsize_t(idx); |
| 1261 |
else |
| 1262 |
obj = SubString_new_object(&name); |
| 1263 |
if (obj == NULL) |
| 1264 |
goto done; |
| 1265 |
|
| 1266 |
|
| 1267 |
result = PyTuple_Pack(2, is_attr_obj, obj); |
| 1268 |
|
| 1269 |
done: |
| 1270 |
Py_XDECREF(is_attr_obj); |
| 1271 |
Py_XDECREF(obj); |
| 1272 |
return result; |
| 1273 |
} |
| 1274 |
} |
| 1275 |
|
| 1276 |
static PyMethodDef fieldnameiter_methods[] = { |
| 1277 |
{NULL, NULL} |
| 1278 |
}; |
| 1279 |
|
| 1280 |
static PyTypeObject PyFieldNameIter_Type = { |
| 1281 |
PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1282 |
"fieldnameiterator", |
| 1283 |
sizeof(fieldnameiterobject), |
| 1284 |
0, |
| 1285 |
|
| 1286 |
(destructor)fieldnameiter_dealloc, |
| 1287 |
0, |
| 1288 |
0, |
| 1289 |
0, |
| 1290 |
0, |
| 1291 |
0, |
| 1292 |
0, |
| 1293 |
0, |
| 1294 |
0, |
| 1295 |
0, |
| 1296 |
0, |
| 1297 |
0, |
| 1298 |
PyObject_GenericGetAttr, |
| 1299 |
0, |
| 1300 |
0, |
| 1301 |
Py_TPFLAGS_DEFAULT, |
| 1302 |
0, |
| 1303 |
0, |
| 1304 |
0, |
| 1305 |
0, |
| 1306 |
0, |
| 1307 |
PyObject_SelfIter, |
| 1308 |
(iternextfunc)fieldnameiter_next, |
| 1309 |
fieldnameiter_methods, |
| 1310 |
0}; |
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
static PyObject * |
| 1320 |
formatter_field_name_split(STRINGLIB_OBJECT *self) |
| 1321 |
{ |
| 1322 |
SubString first; |
| 1323 |
Py_ssize_t first_idx; |
| 1324 |
fieldnameiterobject *it; |
| 1325 |
|
| 1326 |
PyObject *first_obj = NULL; |
| 1327 |
PyObject *result = NULL; |
| 1328 |
|
| 1329 |
it = PyObject_New(fieldnameiterobject, &PyFieldNameIter_Type); |
| 1330 |
if (it == NULL) |
| 1331 |
return NULL; |
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
Py_INCREF(self); |
| 1336 |
it->str = self; |
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
if (!field_name_split(STRINGLIB_STR(self), |
| 1341 |
STRINGLIB_LEN(self), |
| 1342 |
&first, &first_idx, &it->it_field, NULL)) |
| 1343 |
goto done; |
| 1344 |
|
| 1345 |
|
| 1346 |
if (first_idx != -1) |
| 1347 |
first_obj = PyLong_FromSsize_t(first_idx); |
| 1348 |
else |
| 1349 |
|
| 1350 |
first_obj = SubString_new_object(&first); |
| 1351 |
if (first_obj == NULL) |
| 1352 |
goto done; |
| 1353 |
|
| 1354 |
|
| 1355 |
result = PyTuple_Pack(2, first_obj, it); |
| 1356 |
|
| 1357 |
done: |
| 1358 |
Py_XDECREF(it); |
| 1359 |
Py_XDECREF(first_obj); |
| 1360 |
return result; |
| 1361 |
} |