Mercurial > cpython-lru_cache-weakref
annotate Objects/stringlib/string_format.h @ 45852:30c4e1ca8421
Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61431 | vinay.sajip | 2008-03-16 22:35:58 +0100 (So, 16 Mär 2008) | 1 line
Clarified documentation on use of shutdown().
........
r61433 | mark.summerfield | 2008-03-17 09:28:15 +0100 (Mo, 17 Mär 2008) | 5 lines
Added a footnote to each pointing out that for XML output if an encoding
string is given it should conform to the appropriate XML standards---for
example, "UTF-8" is okay, but "UTF8" is not.
........
r61434 | eric.smith | 2008-03-17 12:01:01 +0100 (Mo, 17 Mär 2008) | 7 lines
Issue 2264: empty float presentation type needs to have at least one digit past the decimal point.
Added "Z" format_char to PyOS_ascii_formatd to support empty float presentation type.
Renamed buf_size in PyOS_ascii_formatd to more accurately reflect it's meaning.
Modified format.__float__ to use the new "Z" format as the default.
Added test cases.
........
r61435 | eric.smith | 2008-03-17 13:14:29 +0100 (Mo, 17 Mär 2008) | 2 lines
Reformated lines > 79 chars.
Deleted unused macro ISXDIGIT.
........
r61436 | jeffrey.yasskin | 2008-03-17 15:40:53 +0100 (Mo, 17 Mär 2008) | 13 lines
Allow Gnu gcc's to build python on OSX by removing -Wno-long-double,
-no-cpp-precomp, and -mno-fused-madd from configure.
* r22183 added -no-cpp-precomp, which
http://gcc.gnu.org/ml/gcc/2005-12/msg00368.html claims hasn't been
needed since gcc-3.1.
* r25607 added -Wno-long-double to avoid a warning in
Include/objimpl.h (issue 525481). The long double is still there,
but OSX 10.4's gcc no longer warns about it.
* r33666 fixed issue 775892 on OSX 10.3 by adding -mno-fused-madd,
which changed the sign of some float 0s. Tim Peters said it wasn't
a real issue anyway, and it no longer causes test failures.
Fixes issue #1779871.
........
r61439 | martin.v.loewis | 2008-03-17 17:31:57 +0100 (Mo, 17 Mär 2008) | 2 lines
Add Trent Nelson.
........
r61444 | travis.oliphant | 2008-03-17 18:36:12 +0100 (Mo, 17 Mär 2008) | 1 line
Add necessary headers to back-port new buffer protocol to Python 2.6
........
r61449 | gregory.p.smith | 2008-03-17 19:48:05 +0100 (Mo, 17 Mär 2008) | 8 lines
Force zlib.crc32 and zlib.adler32 to return a signed integer on all platforms
regardless of the native sizeof(long) used in the integer object.
This somewhat odd behavior of returning a signed is maintained in 2.x for
compatibility reasons of always returning an integer rather than a long object.
Fixes Issue1202 for Python 2.6
........
r61450 | neal.norwitz | 2008-03-17 20:02:45 +0100 (Mo, 17 Mär 2008) | 3 lines
Use a buffer large enough to ensure we don't overrun, even if the value
is outside the range we expect.
........
r61453 | steven.bethard | 2008-03-17 20:33:11 +0100 (Mo, 17 Mär 2008) | 1 line
Document unicode.isnumeric() and unicode.isdecimal() (issue2326)
........
r61458 | neal.norwitz | 2008-03-17 21:22:43 +0100 (Mo, 17 Mär 2008) | 5 lines
Issue 2321: reduce memory usage (increase the memory that is returned
to the system) by using pymalloc for the data of unicode objects.
Will backport.
........
r61465 | martin.v.loewis | 2008-03-17 22:55:30 +0100 (Mo, 17 Mär 2008) | 2 lines
Add David Wolever.
........
r61468 | gregory.p.smith | 2008-03-18 01:20:01 +0100 (Di, 18 Mär 2008) | 3 lines
Fix the IOError message text when opening a file with an invalid filename.
Error reported by Ilan Schnell.
........
r61471 | brett.cannon | 2008-03-18 02:00:07 +0100 (Di, 18 Mär 2008) | 2 lines
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
........
r61472 | jeffrey.yasskin | 2008-03-18 02:09:59 +0100 (Di, 18 Mär 2008) | 2 lines
Fix build on platforms that don't have intptr_t. Patch by Joseph Armbruster.
........
r61473 | brett.cannon | 2008-03-18 02:50:25 +0100 (Di, 18 Mär 2008) | 2 lines
Convert test_dummy_threading and test_dbm to unittest.
........
r61474 | brett.cannon | 2008-03-18 02:58:56 +0100 (Di, 18 Mär 2008) | 2 lines
Move test_extcall to doctest.
........
r61480 | brett.cannon | 2008-03-18 04:46:22 +0100 (Di, 18 Mär 2008) | 2 lines
test_errno was a no-op test; now it actually tests things and uses unittest.
........
r61483 | brett.cannon | 2008-03-18 05:09:00 +0100 (Di, 18 Mär 2008) | 3 lines
Remove our implementation of memmove() and strerror(); both are in the C89
standard library.
........
r61484 | brett.cannon | 2008-03-18 05:16:06 +0100 (Di, 18 Mär 2008) | 2 lines
The output directory for tests that compare against stdout is now gone!
........
r61488 | jeffrey.yasskin | 2008-03-18 05:29:35 +0100 (Di, 18 Mär 2008) | 2 lines
Block the "socket.ssl() is deprecated" warning from test_socket_ssl.
........
r61495 | jeffrey.yasskin | 2008-03-18 05:56:06 +0100 (Di, 18 Mär 2008) | 4 lines
Speed test_thread up from 51.328s to 0.081s by reducing its sleep times. We
still sleep at all to make it likely that all threads are active at the same
time.
........
r61496 | jeffrey.yasskin | 2008-03-18 06:12:41 +0100 (Di, 18 Mär 2008) | 4 lines
Speed up test_dict by about 10x by only checking selected dict literal sizes,
instead of every integer from 0 to 400. Exhaustive testing wastes time without
providing enough more assurance that the code is correct.
........
r61498 | neal.norwitz | 2008-03-18 06:20:29 +0100 (Di, 18 Mär 2008) | 1 line
Try increasing the timeout to reduce the flakiness of this test.
........
r61503 | brett.cannon | 2008-03-18 06:43:04 +0100 (Di, 18 Mär 2008) | 2 lines
Improve the error message for a test that failed on the S-390 Debian buildbot.
........
r61504 | jeffrey.yasskin | 2008-03-18 06:45:40 +0100 (Di, 18 Mär 2008) | 3 lines
Add a -S/--slow flag to regrtest to have it print the 10 slowest tests with
their times.
........
r61507 | neal.norwitz | 2008-03-18 07:03:46 +0100 (Di, 18 Mär 2008) | 1 line
Add some info to the failure messages
........
r61509 | trent.nelson | 2008-03-18 08:02:12 +0100 (Di, 18 Mär 2008) | 1 line
Issue 2286: bump up the stack size of the 64-bit debug python_d.exe to 2100000. The default value of 200000 causes a stack overflow at 1965 iterations of r_object() in marshal.c, 35 iterations before the 2000 limit enforced by MAX_MARSHAL_STACK_DEPTH.
........
r61510 | trent.nelson | 2008-03-18 08:32:47 +0100 (Di, 18 Mär 2008) | 5 lines
The behaviour of winsound.Beep() seems to differ between different versions of Windows when there's either:
a) no sound card entirely
b) legacy beep driver has been disabled
c) the legacy beep driver has been uninstalled
Sometimes RuntimeErrors are raised, sometimes they're not. If _have_soundcard() returns False, don't expect winsound.Beep() to raise a RuntimeError, as this clearly isn't the case, as demonstrated by the various Win32 XP buildbots.
........
r61515 | martin.v.loewis | 2008-03-18 13:20:15 +0100 (Di, 18 Mär 2008) | 2 lines
norwitz-amd64 (gentoo) has EREMOTEIO.
........
r61516 | martin.v.loewis | 2008-03-18 13:45:37 +0100 (Di, 18 Mär 2008) | 2 lines
Add more Linux error codes.
........
r61517 | martin.v.loewis | 2008-03-18 14:05:03 +0100 (Di, 18 Mär 2008) | 2 lines
Add WSA errors.
........
r61518 | martin.v.loewis | 2008-03-18 14:16:05 +0100 (Di, 18 Mär 2008) | 2 lines
Note that the stderr output of the test is intentional.
........
| author | Christian Heimes <christian@cheimes.de> |
|---|---|
| date | Tue, 18 Mar 2008 15:15:01 +0000 |
| parents | 09c9ceecdd56 |
| children | 3d9ad27d930b |
| 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 |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
44351
diff
changeset
|
9 /* Defines for Python 2.6 compatability */ |
|
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 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
34 /* forward declaration for recursion */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
35 static PyObject * |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
36 build_string(SubString *input, PyObject *args, PyObject *kwargs, |
|
43244
79080c4889bc
Simplified recursion logic. Modified variable name to match string.Formatter.
Eric Smith <eric@trueblade.com>
parents:
43241
diff
changeset
|
37 int recursion_depth); |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
38 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
39 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
40 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
41 /************************************************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
42 /************************** Utility functions ************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
43 /************************************************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
44 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
45 /* 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
|
46 Py_LOCAL_INLINE(void) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
47 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
|
48 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
49 str->ptr = p; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
50 if (p == NULL) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
51 str->end = NULL; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
52 else |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
53 str->end = str->ptr + len; |
|
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 |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
56 /* 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
|
57 Py_LOCAL_INLINE(PyObject *) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
58 SubString_new_object(SubString *str) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
59 { |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
60 if (str->ptr == NULL) { |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
61 Py_INCREF(Py_None); |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
62 return Py_None; |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
63 } |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
64 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
|
65 } |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
66 |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
67 /* 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
|
68 Py_LOCAL_INLINE(PyObject *) |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
69 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
|
70 { |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
71 if (str->ptr == NULL) { |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
72 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
|
73 } |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
74 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
|
75 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
76 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
77 /************************************************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
78 /*********** Output string management functions ****************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
79 /************************************************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
80 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
81 typedef struct { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
82 STRINGLIB_CHAR *ptr; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
83 STRINGLIB_CHAR *end; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
84 PyObject *obj; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
85 Py_ssize_t size_increment; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
86 } OutputString; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
87 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
88 /* initialize an OutputString object, reserving size characters */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
89 static int |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
90 output_initialize(OutputString *output, Py_ssize_t size) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
91 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
92 output->obj = STRINGLIB_NEW(NULL, size); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
93 if (output->obj == NULL) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
94 return 0; |
|
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 output->ptr = STRINGLIB_STR(output->obj); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
97 output->end = STRINGLIB_LEN(output->obj) + output->ptr; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
98 output->size_increment = INITIAL_SIZE_INCREMENT; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
99 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
100 return 1; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
101 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
102 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
103 /* |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
104 output_extend reallocates the output string buffer. |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
105 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
|
106 1 for success. |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
107 */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
108 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
109 static int |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
110 output_extend(OutputString *output, Py_ssize_t count) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
111 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
112 STRINGLIB_CHAR *startptr = STRINGLIB_STR(output->obj); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
113 Py_ssize_t curlen = output->ptr - startptr; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
114 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
|
115 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
116 if (STRINGLIB_RESIZE(&output->obj, maxlen) < 0) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
117 return 0; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
118 startptr = STRINGLIB_STR(output->obj); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
119 output->ptr = startptr + curlen; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
120 output->end = startptr + maxlen; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
121 if (output->size_increment < MAX_SIZE_INCREMENT) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
122 output->size_increment *= SIZE_MULTIPLIER; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
123 return 1; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
124 } |
|
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 output_data dumps characters into our output string |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
128 buffer. |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
129 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
130 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
|
131 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
132 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
|
133 1 for success. |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
134 */ |
|
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_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
|
137 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
138 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
|
139 return 0; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
140 memcpy(output->ptr, s, count * sizeof(STRINGLIB_CHAR)); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
141 output->ptr += count; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
142 return 1; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
143 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
144 |
|
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 /*********** Format string parsing -- integers and identifiers *********/ |
|
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 |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
149 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
|
150 get_integer(const SubString *str) |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
151 { |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
152 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
|
153 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
|
154 Py_ssize_t oldaccumulator; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
155 STRINGLIB_CHAR *p; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
156 |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
157 /* 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
|
158 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
|
159 return -1; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
160 |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
161 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
|
162 digitval = STRINGLIB_TODECIMAL(*p); |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
163 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
|
164 return -1; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
165 /* |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
166 This trick was copied from old Unicode format code. It's cute, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
167 but would really suck on an old machine with a slow divide |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
168 implementation. Fortunately, in the normal case we do not |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
169 expect too many digits. |
|
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 oldaccumulator = accumulator; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
172 accumulator *= 10; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
173 if ((accumulator+10)/10 != oldaccumulator+1) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
174 PyErr_Format(PyExc_ValueError, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
175 "Too many decimal digits in format string"); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
176 return -1; |
|
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 accumulator += digitval; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
179 } |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
180 return accumulator; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
181 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
182 |
|
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 /******** Functions to get field objects and specification strings ******/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
185 /************************************************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
186 |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
187 /* 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
|
188 static PyObject * |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
189 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
|
190 { |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
191 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
|
192 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
|
193 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
|
194 return NULL; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
195 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
|
196 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
|
197 return newobj; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
198 } |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
199 |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
200 /* 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
|
201 static PyObject * |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
202 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
|
203 { |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
204 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
|
205 } |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
206 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
207 /* 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
|
208 static PyObject * |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
209 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
|
210 { |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
211 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
|
212 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
|
213 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
|
214 return NULL; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
215 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
|
216 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
|
217 return newobj; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
218 } |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
219 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
220 /* do the equivalent of obj[name] */ |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
221 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
|
222 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
|
223 { |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
224 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
|
225 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
|
226 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
|
227 return NULL; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
228 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
|
229 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
|
230 return newobj; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
231 } |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
232 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
233 typedef struct { |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
234 /* 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
|
235 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
|
236 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
|
237 SubString str; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
238 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
239 /* 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
|
240 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
|
241 } FieldNameIterator; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
242 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
243 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
244 static int |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
245 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
|
246 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
|
247 { |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
248 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
|
249 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
|
250 return 1; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
251 } |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
252 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
253 static int |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
254 _FieldNameIterator_attr(FieldNameIterator *self, SubString *name) |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
255 { |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
256 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
|
257 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
258 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
|
259 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
260 /* 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
|
261 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
|
262 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
|
263 case '[': |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
264 case '.': |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
265 /* 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
|
266 self->ptr--; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
267 break; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
268 default: |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
269 continue; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
270 } |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
271 break; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
272 } |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
273 /* 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
|
274 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
|
275 return 1; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
276 } |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
277 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
278 static int |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
279 _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
|
280 { |
|
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
|
281 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
|
282 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
|
283 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
284 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
|
285 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
286 /* 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
|
287 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
|
288 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
|
289 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
|
290 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
|
291 break; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
292 default: |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
293 continue; |
|
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 break; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
296 } |
|
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
|
297 /* 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
|
298 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
|
299 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
|
300 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
|
301 } |
|
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
|
302 |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
303 /* 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
|
304 /* 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
|
305 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
|
306 return 1; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
307 } |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
308 |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
309 /* 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
|
310 static int |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
311 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
|
312 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
|
313 { |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
314 /* 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
|
315 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
|
316 return 1; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
317 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
318 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
|
319 case '.': |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
320 *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
|
321 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
|
322 return 0; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
323 *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
|
324 break; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
325 case '[': |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
326 *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
|
327 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
|
328 return 0; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
329 *name_idx = get_integer(name); |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
330 break; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
331 default: |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
332 /* interal error, can't get here */ |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
333 assert(0); |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
334 return 0; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
335 } |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
336 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
337 /* 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
|
338 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
|
339 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
|
340 return 0; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
341 } |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
342 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
343 return 2; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
344 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
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 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
347 /* 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
|
348 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
|
349 '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
|
350 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
|
351 '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
|
352 */ |
|
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 field_name_split(STRINGLIB_CHAR *ptr, Py_ssize_t len, SubString *first, |
|
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 *first_idx, FieldNameIterator *rest) |
|
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 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
|
358 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
|
359 STRINGLIB_CHAR *end = ptr + len; |
|
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 /* 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
|
362 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
|
363 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
|
364 case '[': |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
365 case '.': |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
366 /* 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
|
367 "rest" iterator */ |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
368 p--; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
369 break; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
370 default: |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
371 continue; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
372 } |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
373 break; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
374 } |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
375 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
376 /* 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
|
377 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
|
378 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
|
379 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
380 /* 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
|
381 *first_idx = get_integer(first); |
|
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 /* zero length 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 (first->ptr >= first->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 field name"); |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
386 goto error; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
387 } |
|
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 1; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
390 error: |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
391 return 0; |
|
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 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
394 |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
395 /* |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
396 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
|
397 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
|
398 the entire input string. |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
399 */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
400 static PyObject * |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
401 get_field_object(SubString *input, PyObject *args, PyObject *kwargs) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
402 { |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
403 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
|
404 int ok; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
405 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
|
406 SubString name; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
407 SubString first; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
408 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
|
409 FieldNameIterator rest; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
410 |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
411 if (!field_name_split(input->ptr, input->end - input->ptr, &first, |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
412 &index, &rest)) { |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
413 goto error; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
414 } |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
415 |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
416 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
|
417 /* 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
|
418 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
|
419 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
|
420 goto error; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
421 if ((kwargs == NULL) || (obj = PyDict_GetItem(kwargs, key)) == NULL) { |
|
43241
e946073dee38
Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents:
43206
diff
changeset
|
422 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
|
423 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
|
424 goto error; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
425 } |
|
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
|
426 Py_DECREF(key); |
|
42891
252a84280b73
This adds a leak, but fixes a crash. The leaking code is:
Neal Norwitz <nnorwitz@gmail.com>
parents:
42875
diff
changeset
|
427 Py_INCREF(obj); |
|
42929
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42926
diff
changeset
|
428 } |
|
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42926
diff
changeset
|
429 else { |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
430 /* 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
|
431 obj = PySequence_GetItem(args, index); |
|
43241
e946073dee38
Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents:
43206
diff
changeset
|
432 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
|
433 goto error; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
434 } |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
435 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
436 /* 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
|
437 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
|
438 &name)) == 2) { |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
439 PyObject *tmp; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
440 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
441 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
|
442 /* getattr lookup "." */ |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
443 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
|
444 else |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
445 /* getitem lookup "[]" */ |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
446 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
|
447 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
|
448 else |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
449 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
|
450 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
|
451 else |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
452 /* 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
|
453 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
|
454 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
|
455 goto error; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
456 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
457 /* 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
|
458 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
|
459 obj = tmp; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
460 } |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
461 /* 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
|
462 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
|
463 return obj; |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
464 error: |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42834
diff
changeset
|
465 Py_XDECREF(obj); |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
466 return NULL; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
467 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
468 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
469 /************************************************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
470 /***************** Field rendering functions **************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
471 /************************************************************************/ |
|
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 /* |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
474 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
|
475 field object and field specification string generated by |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
476 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
|
477 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
478 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
|
479 appends to the output. |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
480 */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
481 static int |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
482 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
|
483 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
484 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
|
485 PyObject *result = NULL; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
486 |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
44351
diff
changeset
|
487 /* we need to create an object out of the pointers we have */ |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
44351
diff
changeset
|
488 PyObject *format_spec_object = SubString_new_object_or_empty(format_spec); |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
44351
diff
changeset
|
489 if (format_spec_object == NULL) |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
44351
diff
changeset
|
490 goto done; |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
44351
diff
changeset
|
491 |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
44351
diff
changeset
|
492 result = PyObject_Format(fieldobj, format_spec_object); |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
493 if (result == NULL) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
494 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
495 |
|
45392
8ece5ca4a785
Port 60893 to py3k, without unicode test.
Eric Smith <eric@trueblade.com>
parents:
45382
diff
changeset
|
496 #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
|
497 assert(PyUnicode_Check(result)); |
|
45392
8ece5ca4a785
Port 60893 to py3k, without unicode test.
Eric Smith <eric@trueblade.com>
parents:
45382
diff
changeset
|
498 #else |
|
8ece5ca4a785
Port 60893 to py3k, without unicode test.
Eric Smith <eric@trueblade.com>
parents:
45382
diff
changeset
|
499 assert(PyString_Check(result) || PyUnicode_Check(result)); |
|
8ece5ca4a785
Port 60893 to py3k, without unicode test.
Eric Smith <eric@trueblade.com>
parents:
45382
diff
changeset
|
500 |
|
8ece5ca4a785
Port 60893 to py3k, without unicode test.
Eric Smith <eric@trueblade.com>
parents:
45382
diff
changeset
|
501 /* 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
|
502 be unicode */ |
|
8ece5ca4a785
Port 60893 to py3k, without unicode test.
Eric Smith <eric@trueblade.com>
parents:
45382
diff
changeset
|
503 { |
|
8ece5ca4a785
Port 60893 to py3k, without unicode test.
Eric Smith <eric@trueblade.com>
parents:
45382
diff
changeset
|
504 PyObject *tmp = STRINGLIB_TOSTR(result); |
|
8ece5ca4a785
Port 60893 to py3k, without unicode test.
Eric Smith <eric@trueblade.com>
parents:
45382
diff
changeset
|
505 if (tmp == NULL) |
|
8ece5ca4a785
Port 60893 to py3k, without unicode test.
Eric Smith <eric@trueblade.com>
parents:
45382
diff
changeset
|
506 goto done; |
|
8ece5ca4a785
Port 60893 to py3k, without unicode test.
Eric Smith <eric@trueblade.com>
parents:
45382
diff
changeset
|
507 Py_DECREF(result); |
|
8ece5ca4a785
Port 60893 to py3k, without unicode test.
Eric Smith <eric@trueblade.com>
parents:
45382
diff
changeset
|
508 result = tmp; |
|
8ece5ca4a785
Port 60893 to py3k, without unicode test.
Eric Smith <eric@trueblade.com>
parents:
45382
diff
changeset
|
509 } |
|
8ece5ca4a785
Port 60893 to py3k, without unicode test.
Eric Smith <eric@trueblade.com>
parents:
45382
diff
changeset
|
510 #endif |
|
8ece5ca4a785
Port 60893 to py3k, without unicode test.
Eric Smith <eric@trueblade.com>
parents:
45382
diff
changeset
|
511 |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
512 ok = output_data(output, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
513 STRINGLIB_STR(result), STRINGLIB_LEN(result)); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
514 done: |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
44351
diff
changeset
|
515 Py_DECREF(format_spec_object); |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
516 Py_XDECREF(result); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
517 return ok; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
518 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
519 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
520 static int |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
521 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
|
522 STRINGLIB_CHAR *conversion) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
523 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
524 STRINGLIB_CHAR c = 0; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
525 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
526 /* initialize these, as they may be empty */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
527 *conversion = '\0'; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
528 SubString_init(format_spec, NULL, 0); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
529 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
530 /* search for the field name. it's terminated by the end of the |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
531 string, or a ':' or '!' */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
532 field_name->ptr = str->ptr; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
533 while (str->ptr < str->end) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
534 switch (c = *(str->ptr++)) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
535 case ':': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
536 case '!': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
537 break; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
538 default: |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
539 continue; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
540 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
541 break; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
542 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
543 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
544 if (c == '!' || c == ':') { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
545 /* 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
|
546 /* don't include the last character */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
547 field_name->end = str->ptr-1; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
548 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
549 /* 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
|
550 format_spec->ptr = str->ptr; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
551 format_spec->end = str->end; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
552 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
553 /* see if there's a conversion specifier */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
554 if (c == '!') { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
555 /* there must be another character present */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
556 if (format_spec->ptr >= format_spec->end) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
557 PyErr_SetString(PyExc_ValueError, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
558 "end of format while looking for conversion " |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
559 "specifier"); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
560 return 0; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
561 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
562 *conversion = *(format_spec->ptr++); |
|
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 /* 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
|
565 if (format_spec->ptr < format_spec->end) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
566 c = *(format_spec->ptr++); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
567 if (c != ':') { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
568 PyErr_SetString(PyExc_ValueError, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
569 "expected ':' after format specifier"); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
570 return 0; |
|
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 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
573 } |
|
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 return 1; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
576 |
|
42929
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42926
diff
changeset
|
577 } |
|
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42926
diff
changeset
|
578 else { |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
579 /* 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
|
580 field_name->end = str->ptr; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
581 return 1; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
582 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
583 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
584 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
585 /************************************************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
586 /******* Output string allocation and escape-to-markup processing ******/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
587 /************************************************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
588 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
589 /* 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
|
590 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
|
591 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
|
592 use with the Formatter class */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
593 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
594 typedef struct { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
595 SubString str; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
596 } MarkupIterator; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
597 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
598 static int |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
599 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
|
600 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
601 SubString_init(&self->str, ptr, len); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
602 return 1; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
603 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
604 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
605 /* 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
|
606 string (or something to be expanded) */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
607 static int |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
608 MarkupIterator_next(MarkupIterator *self, SubString *literal, |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
609 SubString *field_name, SubString *format_spec, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
610 STRINGLIB_CHAR *conversion, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
611 int *format_spec_needs_expanding) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
612 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
613 int at_end; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
614 STRINGLIB_CHAR c = 0; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
615 STRINGLIB_CHAR *start; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
616 int count; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
617 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
|
618 int markup_follows = 0; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
619 |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
620 /* 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
|
621 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
|
622 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
|
623 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
|
624 *conversion = '\0'; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
625 *format_spec_needs_expanding = 0; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
626 |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
627 /* 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
|
628 path. */ |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
629 if (self->str.ptr >= self->str.end) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
630 return 1; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
631 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
632 start = self->str.ptr; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
633 |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
634 /* 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
|
635 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
|
636 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
|
637 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
|
638 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
|
639 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
|
640 the second consecutive brace. */ |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
641 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
|
642 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
|
643 case '{': |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
644 case '}': |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
645 markup_follows = 1; |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
646 break; |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
647 default: |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
648 continue; |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
649 } |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
650 break; |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
651 } |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
652 |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
653 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
|
654 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
|
655 |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
656 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
|
657 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
|
658 "in format string"); |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
659 return 0; |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
660 } |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
661 if (at_end && c == '{') { |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
662 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
|
663 "in format string"); |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
664 return 0; |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
665 } |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
666 if (!at_end) { |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
667 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
|
668 /* 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
|
669 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
|
670 self->str.ptr++; |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
671 markup_follows = 0; |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
672 } |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
673 else |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
674 len--; |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
675 } |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
676 |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
677 /* record the literal text */ |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
678 literal->ptr = start; |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
679 literal->end = start + len; |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
680 |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
681 if (!markup_follows) |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
682 return 2; |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
683 |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
684 /* 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
|
685 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
|
686 format_specs cannot have braces in them. */ |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
687 count = 1; |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
688 |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
689 start = self->str.ptr; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
690 |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
691 /* 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
|
692 about that case */ |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
693 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
|
694 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
|
695 case '{': |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
696 /* 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
|
697 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
|
698 *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
|
699 count++; |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
700 break; |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
701 case '}': |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
702 count--; |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
703 if (count <= 0) { |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
704 /* 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
|
705 SubString s; |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
706 |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
707 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
|
708 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
|
709 return 0; |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
710 |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
711 /* a zero length field_name is an error */ |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
712 if (field_name->ptr == field_name->end) { |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
713 PyErr_SetString(PyExc_ValueError, "zero length field name " |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
714 "in format"); |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
715 return 0; |
|
42817
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 |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
718 /* success */ |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
719 return 2; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
720 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
721 break; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
722 } |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
723 } |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
724 |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
725 /* 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
|
726 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
|
727 return 0; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
728 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
729 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
730 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
731 /* do the !r or !s conversion on obj */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
732 static PyObject * |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
733 do_conversion(PyObject *obj, STRINGLIB_CHAR conversion) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
734 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
735 /* 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
|
736 might have returned a string? */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
737 switch (conversion) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
738 case 'r': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
739 return PyObject_Repr(obj); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
740 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
|
741 return STRINGLIB_TOSTR(obj); |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
742 default: |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
743 PyErr_Format(PyExc_ValueError, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
744 "Unknown converion specifier %c", |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
745 conversion); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
746 return NULL; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
747 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
748 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
749 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
750 /* given: |
|
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 {field_name!conversion:format_spec} |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
753 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
754 compute the result and write it to output. |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
755 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
|
756 just output the string directly, otherwise recursively expand the |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
757 format_spec string. */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
758 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
759 static int |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
760 output_markup(SubString *field_name, SubString *format_spec, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
761 int format_spec_needs_expanding, STRINGLIB_CHAR conversion, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
762 OutputString *output, PyObject *args, PyObject *kwargs, |
|
43244
79080c4889bc
Simplified recursion logic. Modified variable name to match string.Formatter.
Eric Smith <eric@trueblade.com>
parents:
43241
diff
changeset
|
763 int recursion_depth) |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
764 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
765 PyObject *tmp = NULL; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
766 PyObject *fieldobj = NULL; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
767 SubString expanded_format_spec; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
768 SubString *actual_format_spec; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
769 int result = 0; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
770 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
771 /* convert field_name to an object */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
772 fieldobj = get_field_object(field_name, args, kwargs); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
773 if (fieldobj == NULL) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
774 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
775 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
776 if (conversion != '\0') { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
777 tmp = do_conversion(fieldobj, conversion); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
778 if (tmp == NULL) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
779 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
780 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
781 /* do the assignment, transferring ownership: fieldobj = tmp */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
782 Py_DECREF(fieldobj); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
783 fieldobj = tmp; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
784 tmp = NULL; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
785 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
786 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
787 /* if needed, recurively compute the format_spec */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
788 if (format_spec_needs_expanding) { |
|
43244
79080c4889bc
Simplified recursion logic. Modified variable name to match string.Formatter.
Eric Smith <eric@trueblade.com>
parents:
43241
diff
changeset
|
789 tmp = build_string(format_spec, args, kwargs, recursion_depth-1); |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
790 if (tmp == NULL) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
791 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
792 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
793 /* 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
|
794 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
|
795 render_field. */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
796 SubString_init(&expanded_format_spec, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
797 STRINGLIB_STR(tmp), STRINGLIB_LEN(tmp)); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
798 actual_format_spec = &expanded_format_spec; |
|
42929
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42926
diff
changeset
|
799 } |
|
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42926
diff
changeset
|
800 else |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
801 actual_format_spec = format_spec; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
802 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
803 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
|
804 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
805 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
806 result = 1; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
807 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
808 done: |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
809 Py_XDECREF(fieldobj); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
810 Py_XDECREF(tmp); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
811 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
812 return result; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
813 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
814 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
815 /* |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
44351
diff
changeset
|
816 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
|
817 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
|
818 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
|
819 and to perform the markup to the output. |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
820 */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
821 static int |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
822 do_markup(SubString *input, PyObject *args, PyObject *kwargs, |
|
43244
79080c4889bc
Simplified recursion logic. Modified variable name to match string.Formatter.
Eric Smith <eric@trueblade.com>
parents:
43241
diff
changeset
|
823 OutputString *output, int recursion_depth) |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
824 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
825 MarkupIterator iter; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
826 int format_spec_needs_expanding; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
827 int result; |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
828 SubString literal; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
829 SubString field_name; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
830 SubString format_spec; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
831 STRINGLIB_CHAR conversion; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
832 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
833 MarkupIterator_init(&iter, input->ptr, input->end - input->ptr); |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
834 while ((result = MarkupIterator_next(&iter, &literal, &field_name, |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
835 &format_spec, &conversion, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
836 &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
|
837 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
|
838 return 0; |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
839 if (field_name.ptr != field_name.end) |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
840 if (!output_markup(&field_name, &format_spec, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
841 format_spec_needs_expanding, conversion, output, |
|
43244
79080c4889bc
Simplified recursion logic. Modified variable name to match string.Formatter.
Eric Smith <eric@trueblade.com>
parents:
43241
diff
changeset
|
842 args, kwargs, recursion_depth)) |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
843 return 0; |
|
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 return result; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
846 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
847 |
|
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 /* |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
850 build_string allocates the output string and then |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
851 calls do_markup to do the heavy lifting. |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
852 */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
853 static PyObject * |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
854 build_string(SubString *input, PyObject *args, PyObject *kwargs, |
|
43244
79080c4889bc
Simplified recursion logic. Modified variable name to match string.Formatter.
Eric Smith <eric@trueblade.com>
parents:
43241
diff
changeset
|
855 int recursion_depth) |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
856 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
857 OutputString output; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
858 PyObject *result = NULL; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
859 Py_ssize_t count; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
860 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
861 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
|
862 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
863 /* 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
|
864 if (recursion_depth <= 0) { |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
865 PyErr_SetString(PyExc_ValueError, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
866 "Max string recursion exceeded"); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
867 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
868 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
869 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
870 /* 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
|
871 increment. seems like a reasonable default */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
872 if (!output_initialize(&output, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
873 input->end - input->ptr + |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
874 INITIAL_SIZE_INCREMENT)) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
875 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
876 |
|
43244
79080c4889bc
Simplified recursion logic. Modified variable name to match string.Formatter.
Eric Smith <eric@trueblade.com>
parents:
43241
diff
changeset
|
877 if (!do_markup(input, args, kwargs, &output, recursion_depth)) { |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
878 goto done; |
|
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 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
881 count = output.ptr - STRINGLIB_STR(output.obj); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
882 if (STRINGLIB_RESIZE(&output.obj, count) < 0) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
883 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
884 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
885 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
886 /* transfer ownership to result */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
887 result = output.obj; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
888 output.obj = NULL; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
889 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
890 done: |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
891 Py_XDECREF(output.obj); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
892 return result; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
893 } |
|
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 /************************************************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
896 /*********** main routine ***********************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
897 /************************************************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
898 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
899 /* this is the main entry point */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
900 static PyObject * |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
901 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
|
902 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
903 SubString input; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
904 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
905 /* PEP 3101 says only 2 levels, so that |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
906 "{0:{1}}".format('abc', 's') # works |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
907 "{0:{1:{2}}}".format('abc', 's', '') # fails |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
908 */ |
|
43244
79080c4889bc
Simplified recursion logic. Modified variable name to match string.Formatter.
Eric Smith <eric@trueblade.com>
parents:
43241
diff
changeset
|
909 int recursion_depth = 2; |
|
42817
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 SubString_init(&input, STRINGLIB_STR(self), STRINGLIB_LEN(self)); |
|
43244
79080c4889bc
Simplified recursion logic. Modified variable name to match string.Formatter.
Eric Smith <eric@trueblade.com>
parents:
43241
diff
changeset
|
912 return build_string(&input, args, kwargs, recursion_depth); |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
913 } |
|
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
|
914 |
|
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
|
915 |
|
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
|
916 |
|
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
|
917 /************************************************************************/ |
|
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
|
918 /*********** 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
|
919 /************************************************************************/ |
|
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
|
920 |
|
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
|
921 /* 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
|
922 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
|
923 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
|
924 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
|
925 |
|
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
|
926 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
|
927 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
|
928 |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
44351
diff
changeset
|
929 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
|
930 |
|
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
|
931 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
|
932 } 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
|
933 |
|
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
|
934 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
|
935 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
|
936 { |
|
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
|
937 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
|
938 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
|
939 } |
|
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
|
940 |
|
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
|
941 /* returns a tuple: |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
942 (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
|
943 |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
944 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
|
945 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
|
946 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
|
947 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
|
948 */ |
|
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
|
949 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
|
950 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
|
951 { |
|
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
|
952 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
|
953 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
|
954 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
|
955 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
|
956 int format_spec_needs_expanding; |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
957 int result = MarkupIterator_next(&it->it_markup, &literal, &field_name, |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
958 &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
|
959 &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
|
960 |
|
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
|
961 /* 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
|
962 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
|
963 assert(0 <= result && result <= 2); |
|
42929
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42926
diff
changeset
|
964 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
|
965 /* 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
|
966 return NULL; |
|
42929
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42926
diff
changeset
|
967 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
|
968 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
|
969 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
|
970 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
|
971 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
|
972 PyObject *tuple = NULL; |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
973 int has_field = field_name.ptr != field_name.end; |
|
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
|
974 |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
975 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
|
976 if (literal_str == NULL) |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
977 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
|
978 |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
979 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
|
980 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
|
981 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
|
982 |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
983 /* 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
|
984 format_spec (even if zero length), else return None */ |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
985 format_spec_str = (has_field ? |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
986 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
|
987 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
|
988 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
|
989 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
|
990 |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
991 /* 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
|
992 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
|
993 character */ |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
994 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
|
995 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
|
996 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
|
997 } |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
998 else |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
44351
diff
changeset
|
999 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
|
1000 if (conversion_str == NULL) |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
1001 goto done; |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
1002 |
|
42948
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42929
diff
changeset
|
1003 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
|
1004 conversion_str); |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
1005 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
|
1006 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
|
1007 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
|
1008 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
|
1009 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
|
1010 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
|
1011 } |
|
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
|
1012 } |
|
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
|
1013 |
|
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
|
1014 static PyMethodDef formatteriter_methods[] = { |
|
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
|
1015 {NULL, NULL} /* sentinel */ |
|
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
|
1016 }; |
|
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
|
1017 |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
44351
diff
changeset
|
1018 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
|
1019 PyVarObject_HEAD_INIT(&PyType_Type, 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
|
1020 "formatteriterator", /* tp_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
|
1021 sizeof(formatteriterobject), /* tp_basicsize */ |
|
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
|
1022 0, /* tp_itemsize */ |
|
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
|
1023 /* methods */ |
|
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
|
1024 (destructor)formatteriter_dealloc, /* tp_dealloc */ |
|
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
|
1025 0, /* tp_print */ |
|
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
|
1026 0, /* tp_getattr */ |
|
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
|
1027 0, /* tp_setattr */ |
|
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
|
1028 0, /* tp_compare */ |
|
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
|
1029 0, /* tp_repr */ |
|
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
|
1030 0, /* tp_as_number */ |
|
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
|
1031 0, /* tp_as_sequence */ |
|
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
|
1032 0, /* tp_as_mapping */ |
|
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
|
1033 0, /* tp_hash */ |
|
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
|
1034 0, /* tp_call */ |
|
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
|
1035 0, /* tp_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
|
1036 PyObject_GenericGetAttr, /* tp_getattro */ |
|
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
|
1037 0, /* tp_setattro */ |
|
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
|
1038 0, /* tp_as_buffer */ |
|
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
|
1039 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
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
|
1040 0, /* tp_doc */ |
|
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
|
1041 0, /* tp_traverse */ |
|
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
|
1042 0, /* tp_clear */ |
|
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
|
1043 0, /* tp_richcompare */ |
|
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
|
1044 0, /* tp_weaklistoffset */ |
|
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
|
1045 PyObject_SelfIter, /* tp_iter */ |
|
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
|
1046 (iternextfunc)formatteriter_next, /* tp_iternext */ |
|
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
|
1047 formatteriter_methods, /* tp_methods */ |
|
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
|
1048 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
|
1049 }; |
|
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
|
1050 |
|
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 /* 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
|
1052 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
|
1053 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
|
1054 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
|
1055 static PyObject * |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
44351
diff
changeset
|
1056 formatter_parser(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
|
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 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
|
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 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
|
1061 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
|
1062 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
|
1063 |
|
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 /* 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
|
1065 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
|
1066 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
|
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 /* 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
|
1069 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
|
1070 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
|
1071 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
|
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 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
|
1074 } |
|
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 |
|
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 /************************************************************************/ |
|
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 /*********** 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
|
1079 /************************************************************************/ |
|
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 |
|
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 /* 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
|
1083 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
|
1084 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
|
1085 |
|
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
|
1086 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
|
1087 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
|
1088 |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
44351
diff
changeset
|
1089 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
|
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 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
|
1092 } 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
|
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 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
|
1095 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
|
1096 { |
|
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
|
1097 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
|
1098 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
|
1099 } |
|
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
|
1100 |
|
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
|
1101 /* 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
|
1102 (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
|
1103 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
|
1104 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
|
1105 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
|
1106 */ |
|
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
|
1107 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
|
1108 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
|
1109 { |
|
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
|
1110 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
|
1111 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
|
1112 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
|
1113 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
|
1114 |
|
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 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
|
1116 &idx, &name); |
|
42929
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42926
diff
changeset
|
1117 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
|
1118 /* 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
|
1119 return NULL; |
|
42929
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42926
diff
changeset
|
1120 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
|
1121 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
|
1122 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
|
1123 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
|
1124 |
|
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
|
1125 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
|
1126 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
|
1127 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
|
1128 |
|
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
|
1129 /* 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
|
1130 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
|
1131 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
|
1132 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
|
1133 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
|
1134 if (obj == NULL) |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
1135 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
|
1136 |
|
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 /* 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
|
1138 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
|
1139 |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
1140 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
|
1141 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
|
1142 Py_XDECREF(obj); |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
1143 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
|
1144 } |
|
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
|
1145 } |
|
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 |
|
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
|
1147 static PyMethodDef fieldnameiter_methods[] = { |
|
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 {NULL, NULL} /* sentinel */ |
|
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 }; |
|
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 |
|
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 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
|
1152 PyVarObject_HEAD_INIT(&PyType_Type, 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
|
1153 "fieldnameiterator", /* tp_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
|
1154 sizeof(fieldnameiterobject), /* tp_basicsize */ |
|
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 0, /* tp_itemsize */ |
|
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 /* methods */ |
|
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
|
1157 (destructor)fieldnameiter_dealloc, /* tp_dealloc */ |
|
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 0, /* tp_print */ |
|
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 0, /* tp_getattr */ |
|
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
|
1160 0, /* tp_setattr */ |
|
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 0, /* tp_compare */ |
|
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
|
1162 0, /* tp_repr */ |
|
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
|
1163 0, /* tp_as_number */ |
|
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
|
1164 0, /* tp_as_sequence */ |
|
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 0, /* tp_as_mapping */ |
|
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
|
1166 0, /* tp_hash */ |
|
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
|
1167 0, /* tp_call */ |
|
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
|
1168 0, /* tp_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
|
1169 PyObject_GenericGetAttr, /* tp_getattro */ |
|
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
|
1170 0, /* tp_setattro */ |
|
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
|
1171 0, /* tp_as_buffer */ |
|
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
|
1172 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
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
|
1173 0, /* tp_doc */ |
|
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
|
1174 0, /* tp_traverse */ |
|
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
|
1175 0, /* tp_clear */ |
|
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
|
1176 0, /* tp_richcompare */ |
|
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
|
1177 0, /* tp_weaklistoffset */ |
|
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
|
1178 PyObject_SelfIter, /* tp_iter */ |
|
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
|
1179 (iternextfunc)fieldnameiter_next, /* tp_iternext */ |
|
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
|
1180 fieldnameiter_methods, /* tp_methods */ |
|
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
|
1181 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
|
1182 |
|
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
|
1183 /* 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
|
1184 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
|
1185 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
|
1186 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
|
1187 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
|
1188 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
|
1189 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
|
1190 static PyObject * |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
44351
diff
changeset
|
1191 formatter_field_name_split(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
|
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 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
|
1194 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
|
1195 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
|
1196 |
|
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 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
|
1198 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
|
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 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
|
1201 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
|
1202 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
|
1203 |
|
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
|
1204 /* 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
|
1205 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
|
1206 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
|
1207 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
|
1208 |
|
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 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
|
1210 STRINGLIB_LEN(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
|
1211 &first, &first_idx, &it->it_field)) |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
1212 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
|
1213 |
|
42929
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42926
diff
changeset
|
1214 /* 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
|
1215 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
|
1216 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
|
1217 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
|
1218 /* 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
|
1219 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
|
1220 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
|
1221 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
|
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 /* 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
|
1224 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
|
1225 |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
1226 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
|
1227 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
|
1228 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
|
1229 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
|
1230 } |
