[Python-3000-checkins] r55230 - python/branches/py3k-struni/Modules/_csv.c python/branches/py3k-struni/Modules/datetimemodule.c python/branches/py3k-struni/Modules/posixmodule.c

guido.van.rossum python-3000-checkins at python.org
Thu May 10 20:04:40 CEST 2007


Author: guido.van.rossum
Date: Thu May 10 20:04:33 2007
New Revision: 55230

Modified:
   python/branches/py3k-struni/Modules/_csv.c
   python/branches/py3k-struni/Modules/datetimemodule.c
   python/branches/py3k-struni/Modules/posixmodule.c
Log:
Fix some miscellaneous places that incorrectly insisted on str8.


Modified: python/branches/py3k-struni/Modules/_csv.c
==============================================================================
--- python/branches/py3k-struni/Modules/_csv.c	(original)
+++ python/branches/py3k-struni/Modules/_csv.c	Thu May 10 20:04:33 2007
@@ -235,19 +235,19 @@
 	if (src == NULL)
 		*target = dflt;
 	else {
-		if (src == Py_None || PyString_Size(src) == 0)
-			*target = '\0';
-		else if (!PyString_Check(src) || PyString_Size(src) != 1) {
-			PyErr_Format(PyExc_TypeError, 
-				     "\"%s\" must be an 1-character string", 
-				     name);
-			return -1;
-		}
-		else {
-			char *s = PyString_AsString(src);
-			if (s == NULL)
+		*target = '\0';
+		if (src != Py_None) {
+			const char *buf;
+			Py_ssize_t len;
+			if (PyObject_AsCharBuffer(src, &buf, &len) < 0 ||
+				len > 1) {
+				PyErr_Format(PyExc_TypeError,
+					"\"%s\" must be an 1-character string",
+					     name);
 				return -1;
-			*target = s[0];
+			}
+			if (len > 0)
+				*target = buf[0];
 		}
 	}
         return 0;

Modified: python/branches/py3k-struni/Modules/datetimemodule.c
==============================================================================
--- python/branches/py3k-struni/Modules/datetimemodule.c	(original)
+++ python/branches/py3k-struni/Modules/datetimemodule.c	Thu May 10 20:04:33 2007
@@ -1144,7 +1144,8 @@
 	PyObject *zreplacement = NULL;	/* py string, replacement for %z */
 	PyObject *Zreplacement = NULL;	/* py string, replacement for %Z */
 
-	char *pin;	/* pointer to next char in input format */
+	const char *pin;/* pointer to next char in input format */
+        Py_ssize_t flen;/* length of input format */
 	char ch;	/* next char in input format */
 
 	PyObject *newfmt = NULL;	/* py string, the output format */
@@ -1153,11 +1154,15 @@
 			   exclusive of trailing \0 */
 	int usednew;	/* number bytes used so far in output format buffer */
 
-	char *ptoappend; /* pointer to string to append to output buffer */
+	const char *ptoappend;/* pointer to string to append to output buffer */
 	int ntoappend;	/* # of bytes to append to output buffer */
 
 	assert(object && format && timetuple);
-	assert(PyString_Check(format));
+	assert(PyString_Check(format) || PyUnicode_Check(format));
+
+        /* Convert the input format to a C string and size */
+        if (PyObject_AsCharBuffer(format, &pin, &flen) < 0)
+		return NULL;
 
 	/* Give up if the year is before 1900.
 	 * Python strftime() plays games with the year, and different
@@ -1188,13 +1193,12 @@
 	 * a new format.  Since computing the replacements for those codes
 	 * is expensive, don't unless they're actually used.
 	 */
-	totalnew = PyString_Size(format) + 1;	/* realistic if no %z/%Z */
+	totalnew = flen + 1;	/* realistic if no %z/%Z */
 	newfmt = PyString_FromStringAndSize(NULL, totalnew);
 	if (newfmt == NULL) goto Done;
 	pnew = PyString_AsString(newfmt);
 	usednew = 0;
 
-	pin = PyString_AsString(format);
 	while ((ch = *pin++) != '\0') {
 		if (ch != '%') {
 			ptoappend = pin - 1;
@@ -2441,8 +2445,8 @@
 	PyObject *tuple;
 	static char *keywords[] = {"format", NULL};
 
-	if (! PyArg_ParseTupleAndKeywords(args, kw, "O!:strftime", keywords,
-					  &PyString_Type, &format))
+	if (! PyArg_ParseTupleAndKeywords(args, kw, "S:strftime", keywords,
+					  &format))
 		return NULL;
 
 	tuple = PyObject_CallMethod((PyObject *)self, "timetuple", "()");
@@ -3174,8 +3178,8 @@
 	PyObject *tuple;
 	static char *keywords[] = {"format", NULL};
 
-	if (! PyArg_ParseTupleAndKeywords(args, kw, "O!:strftime", keywords,
-					  &PyString_Type, &format))
+	if (! PyArg_ParseTupleAndKeywords(args, kw, "S:strftime", keywords,
+					  &format))
 		return NULL;
 
 	/* Python's strftime does insane things with the year part of the

Modified: python/branches/py3k-struni/Modules/posixmodule.c
==============================================================================
--- python/branches/py3k-struni/Modules/posixmodule.c	(original)
+++ python/branches/py3k-struni/Modules/posixmodule.c	Thu May 10 20:04:33 2007
@@ -6966,13 +6966,19 @@
         *valuep = PyInt_AS_LONG(arg);
         return 1;
     }
-    if (PyString_Check(arg)) {
+    else {
         /* look up the value in the table using a binary search */
         size_t lo = 0;
 		size_t mid;
         size_t hi = tablesize;
         int cmp;
-        char *confname = PyString_AS_STRING(arg);
+        const char *confname;
+        Py_ssize_t namelen;
+        if (PyObject_AsCharBuffer(arg, &confname, &namelen) < 0) {
+            PyErr_SetString(PyExc_TypeError,
+                            "configuration names must be strings or integers");
+            return 0;
+        }
         while (lo < hi) {
             mid = (lo + hi) / 2;
             cmp = strcmp(confname, table[mid].name);
@@ -6986,11 +6992,8 @@
             }
         }
         PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
+        return 0;
     }
-    else
-        PyErr_SetString(PyExc_TypeError,
-                        "configuration names must be strings or integers");
-    return 0;
 }
 
 


More information about the Python-3000-checkins mailing list