Marc-Andre Lemburg <mal@lemburg.com>:

Fixed %c formatting to check for one character arguments. Thanks
to Finn Bock for finding this bug.

Added a fix for bug PR#348 which originated from not resetting
the globals correctly in _PyUnicode_Fini().
This commit is contained in:
Marc-André Lemburg 2000-06-08 17:54:00 +00:00
parent bfa36f5407
commit d4ab4a5905
1 changed files with 29 additions and 8 deletions

View File

@ -108,14 +108,19 @@ Unicode Integration Proposal (see file Misc/unicode.txt).
# define BYTEORDER_IS_LITTLE_ENDIAN # define BYTEORDER_IS_LITTLE_ENDIAN
#endif #endif
/* --- Globals ------------------------------------------------------------ */ /* --- Globals ------------------------------------------------------------
The globals are initialized by the _PyUnicode_Init() API and should
not be used before calling that API.
*/
/* The empty Unicode object */ /* The empty Unicode object */
static PyUnicodeObject *unicode_empty = NULL; static PyUnicodeObject *unicode_empty;
/* Free list for Unicode objects */ /* Free list for Unicode objects */
static PyUnicodeObject *unicode_freelist = NULL; static PyUnicodeObject *unicode_freelist;
static int unicode_freelist_size = 0; static int unicode_freelist_size;
/* Default encoding to use and assume when NULL is passed as encoding /* Default encoding to use and assume when NULL is passed as encoding
parameter; it is initialized by _PyUnicode_Init(). parameter; it is initialized by _PyUnicode_Init().
@ -4262,22 +4267,33 @@ static int
formatchar(Py_UNICODE *buf, formatchar(Py_UNICODE *buf,
PyObject *v) PyObject *v)
{ {
if (PyUnicode_Check(v)) if (PyUnicode_Check(v)) {
if (PyUnicode_GET_SIZE(v) != 1)
goto onError;
buf[0] = PyUnicode_AS_UNICODE(v)[0]; buf[0] = PyUnicode_AS_UNICODE(v)[0];
}
else if (PyString_Check(v)) else if (PyString_Check(v)) {
buf[0] = (Py_UNICODE) PyString_AS_STRING(v)[0]; if (PyString_GET_SIZE(v) != 1)
goto onError;
buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0];
}
else { else {
/* Integer input truncated to a character */ /* Integer input truncated to a character */
long x; long x;
x = PyInt_AsLong(v); x = PyInt_AsLong(v);
if (x == -1 && PyErr_Occurred()) if (x == -1 && PyErr_Occurred())
return -1; goto onError;
buf[0] = (char) x; buf[0] = (char) x;
} }
buf[1] = '\0'; buf[1] = '\0';
return 1; return 1;
onError:
PyErr_SetString(PyExc_TypeError,
"%c requires int or char");
return -1;
} }
PyObject *PyUnicode_Format(PyObject *format, PyObject *PyUnicode_Format(PyObject *format,
@ -4709,6 +4725,8 @@ void _PyUnicode_Init()
"sizeof(Py_UNICODE) != 2 bytes"); "sizeof(Py_UNICODE) != 2 bytes");
/* Init the implementation */ /* Init the implementation */
unicode_freelist = NULL;
unicode_freelist_size = 0;
unicode_empty = _PyUnicode_New(0); unicode_empty = _PyUnicode_New(0);
strcpy(unicode_default_encoding, "ascii"); strcpy(unicode_default_encoding, "ascii");
} }
@ -4728,5 +4746,8 @@ _PyUnicode_Fini()
Py_XDECREF(v->utf8str); Py_XDECREF(v->utf8str);
PyObject_DEL(v); PyObject_DEL(v);
} }
unicode_freelist = NULL;
unicode_freelist_size = 0;
Py_XDECREF(unicode_empty); Py_XDECREF(unicode_empty);
unicode_empty = NULL;
} }