Mercurial > cpython-lru_cache-weakref
diff Objects/stringobject.c @ 6286:82cd2baa4136 legacy-trunk
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.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Mon, 08 Sep 1997 18:30:11 +0000 |
| parents | d77d3b8e86b2 |
| children | 09963a2a24db |
line wrap: on
line diff
--- a/Objects/stringobject.c Mon Sep 08 16:07:11 1997 +0000 +++ b/Objects/stringobject.c Mon Sep 08 18:30:11 1997 +0000 @@ -464,7 +464,8 @@ 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 @@ 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 @@ char *keystart; int keylen; PyObject *key; + int pcount = 1; if (dict == NULL) { PyErr_SetString(PyExc_TypeError, @@ -762,11 +765,16 @@ ++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 @@ 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) {
