From: Marc-André Lemburg <mal@egenix.com>
Date: Thu, 8 Jun 2000 17:54:00 +0000 (+0000)
Subject: Marc-Andre Lemburg <mal@lemburg.com>:
X-Git-Tag: v2.0b1~1532
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d4ab4a5905d238c27b6d93823f1959c69c6e15c6;p=python

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().
---

diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index c7f9803b16..5548748cac 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -108,14 +108,19 @@ Unicode Integration Proposal (see file Misc/unicode.txt).
 # define BYTEORDER_IS_LITTLE_ENDIAN
 #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 */
-static PyUnicodeObject *unicode_empty = NULL;
+static PyUnicodeObject *unicode_empty;
 
 /* Free list for Unicode objects */
-static PyUnicodeObject *unicode_freelist = NULL;
-static int unicode_freelist_size = 0;
+static PyUnicodeObject *unicode_freelist;
+static int unicode_freelist_size;
 
 /* Default encoding to use and assume when NULL is passed as encoding
    parameter; it is initialized by _PyUnicode_Init().
@@ -4262,22 +4267,33 @@ static int
 formatchar(Py_UNICODE *buf,
 	   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];
+    }
 
-    else if (PyString_Check(v))
-	buf[0] = (Py_UNICODE) PyString_AS_STRING(v)[0];
+    else if (PyString_Check(v)) {
+	if (PyString_GET_SIZE(v) != 1) 
+	    goto onError;
+	buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0];
+    }
 
     else {
 	/* Integer input truncated to a character */
         long x;
 	x = PyInt_AsLong(v);
 	if (x == -1 && PyErr_Occurred())
-	    return -1;
+	    goto onError;
 	buf[0] = (char) x;
     }
     buf[1] = '\0';
     return 1;
+
+ onError:
+    PyErr_SetString(PyExc_TypeError,
+		    "%c requires int or char");
+    return -1;
 }
 
 PyObject *PyUnicode_Format(PyObject *format,
@@ -4709,6 +4725,8 @@ void _PyUnicode_Init()
 		      "sizeof(Py_UNICODE) != 2 bytes");
 
     /* Init the implementation */
+    unicode_freelist = NULL;
+    unicode_freelist_size = 0;
     unicode_empty = _PyUnicode_New(0);
     strcpy(unicode_default_encoding, "ascii");
 }
@@ -4728,5 +4746,8 @@ _PyUnicode_Fini()
 	Py_XDECREF(v->utf8str);
 	PyObject_DEL(v);
     }
+    unicode_freelist = NULL;
+    unicode_freelist_size = 0;
     Py_XDECREF(unicode_empty);
+    unicode_empty = NULL;
 }