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.
This commit is contained in:
Guido van Rossum 1997-09-08 18:30:11 +00:00
parent 9905ef9669
commit 045e688f6f
1 changed files with 17 additions and 8 deletions

View File

@ -464,7 +464,8 @@ string_buffer_getreadbuf(self, index, ptr)
const void **ptr; const void **ptr;
{ {
if ( index != 0 ) { if ( index != 0 ) {
PyErr_SetString(PyExc_SystemError, "Accessing non-existent string segment"); PyErr_SetString(PyExc_SystemError,
"Accessing non-existent string segment");
return -1; return -1;
} }
*ptr = (void *)self->ob_sval; *ptr = (void *)self->ob_sval;
@ -477,7 +478,8 @@ string_buffer_getwritebuf(self, index, ptr)
int index; int index;
const void **ptr; 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; return -1;
} }
@ -753,6 +755,7 @@ PyString_Format(format, args)
char *keystart; char *keystart;
int keylen; int keylen;
PyObject *key; PyObject *key;
int pcount = 1;
if (dict == NULL) { if (dict == NULL) {
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
@ -762,11 +765,16 @@ PyString_Format(format, args)
++fmt; ++fmt;
--fmtcnt; --fmtcnt;
keystart = fmt; keystart = fmt;
while (--fmtcnt >= 0 && *fmt != ')') /* Skip over balanced parentheses */
while (pcount > 0 && --fmtcnt >= 0) {
if (*fmt == ')')
--pcount;
else if (*fmt == '(')
++pcount;
fmt++; fmt++;
keylen = fmt - keystart; }
++fmt; keylen = fmt - keystart - 1;
if (fmtcnt < 0) { if (fmtcnt < 0 || pcount > 0) {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"incomplete format key"); "incomplete format key");
goto error; goto error;
@ -945,8 +953,9 @@ PyString_Format(format, args)
goto error; goto error;
break; break;
default: default:
PyErr_SetString(PyExc_ValueError, PyErr_Format(PyExc_ValueError,
"unsupported format character"); "unsupported format character '%c' (0x%x)",
c, c);
goto error; goto error;
} }
if (sign) { if (sign) {