diff options
author | Guido van Rossum <guido@python.org> | 1997-09-08 18:30:11 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1997-09-08 18:30:11 (GMT) |
commit | 045e688f6fc06c87cc93f84e42fb4767a04ba559 (patch) | |
tree | a62f872c07d4b676223a28f07bb6f26e2b1fe1f6 /Objects/stringobject.c | |
parent | 9905ef966939ff15de6514a4fb994ad0f030d7cf (diff) | |
download | cpython-045e688f6fc06c87cc93f84e42fb4767a04ba559.zip cpython-045e688f6fc06c87cc93f84e42fb4767a04ba559.tar.gz cpython-045e688f6fc06c87cc93f84e42fb4767a04ba559.tar.bz2 |
Patch submitted by Brad Howes (with one bug fixed by me): allow
arbitrary nested parens in a %(...)X style format.
#Also folded two lines and added more detail to the error message for
#unsupported format character.
Diffstat (limited to 'Objects/stringobject.c')
-rw-r--r-- | Objects/stringobject.c | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c index cb37038..5928128 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -464,7 +464,8 @@ string_buffer_getreadbuf(self, index, ptr) const void **ptr; { if ( index != 0 ) { - PyErr_SetString(PyExc_SystemError, "Accessing non-existent string segment"); + PyErr_SetString(PyExc_SystemError, + "Accessing non-existent string segment"); return -1; } *ptr = (void *)self->ob_sval; @@ -477,7 +478,8 @@ string_buffer_getwritebuf(self, index, ptr) int index; const void **ptr; { - PyErr_SetString(PyExc_TypeError, "Cannot use string as modifyable buffer"); + PyErr_SetString(PyExc_TypeError, + "Cannot use string as modifyable buffer"); return -1; } @@ -753,6 +755,7 @@ PyString_Format(format, args) char *keystart; int keylen; PyObject *key; + int pcount = 1; if (dict == NULL) { PyErr_SetString(PyExc_TypeError, @@ -762,11 +765,16 @@ PyString_Format(format, args) ++fmt; --fmtcnt; keystart = fmt; - while (--fmtcnt >= 0 && *fmt != ')') + /* Skip over balanced parentheses */ + while (pcount > 0 && --fmtcnt >= 0) { + if (*fmt == ')') + --pcount; + else if (*fmt == '(') + ++pcount; fmt++; - keylen = fmt - keystart; - ++fmt; - if (fmtcnt < 0) { + } + keylen = fmt - keystart - 1; + if (fmtcnt < 0 || pcount > 0) { PyErr_SetString(PyExc_ValueError, "incomplete format key"); goto error; @@ -945,8 +953,9 @@ PyString_Format(format, args) goto error; break; default: - PyErr_SetString(PyExc_ValueError, - "unsupported format character"); + PyErr_Format(PyExc_ValueError, + "unsupported format character '%c' (0x%x)", + c, c); goto error; } if (sign) { |