[Python-checkins] r80585 - python/branches/py3k/Objects/exceptions.c

victor.stinner python-checkins at python.org
Wed Apr 28 19:26:19 CEST 2010


Author: victor.stinner
Date: Wed Apr 28 19:26:19 2010
New Revision: 80585

Log:
Don't decode/recode the unicode filename in SyntaxError_str()

 * Rewrite my_basename() to use unicode
 * Use '%U' format


Modified:
   python/branches/py3k/Objects/exceptions.c

Modified: python/branches/py3k/Objects/exceptions.c
==============================================================================
--- python/branches/py3k/Objects/exceptions.c	(original)
+++ python/branches/py3k/Objects/exceptions.c	Wed Apr 28 19:26:19 2010
@@ -959,20 +959,27 @@
 /* This is called "my_basename" instead of just "basename" to avoid name
    conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
    defined, and Python does define that. */
-static char *
-my_basename(char *name)
+static PyObject*
+my_basename(PyObject *name)
 {
-    char *cp = name;
-    char *result = name;
+    Py_UNICODE *unicode;
+    Py_ssize_t i, size, offset;
 
-    if (name == NULL)
-        return "???";
-    while (*cp != '\0') {
-        if (*cp == SEP)
-            result = cp + 1;
-        ++cp;
+    unicode = PyUnicode_AS_UNICODE(name);
+    size = PyUnicode_GET_SIZE(name);
+    offset = 0;
+    for(i=0; i < size; i++) {
+        if (unicode[i] == SEP)
+            offset = i + 1;
+    }
+    if (offset != 0) {
+        return PyUnicode_FromUnicode(
+            PyUnicode_AS_UNICODE(name) + offset,
+            size - offset);
+    } else {
+        Py_INCREF(name);
+        return name;
     }
-    return result;
 }
 
 
@@ -980,7 +987,8 @@
 SyntaxError_str(PySyntaxErrorObject *self)
 {
     int have_lineno = 0;
-    char *filename = 0;
+    PyObject *filename;
+    PyObject *result;
     /* Below, we always ignore overflow errors, just printing -1.
        Still, we cannot allow an OverflowError to be raised, so
        we need to call PyLong_AsLongAndOverflow. */
@@ -990,7 +998,11 @@
        lineno here */
 
     if (self->filename && PyUnicode_Check(self->filename)) {
-	    filename = _PyUnicode_AsString(self->filename);
+        filename = my_basename(self->filename);
+        if (filename == NULL)
+            return NULL;
+    } else {
+        filename = NULL;
     }
     have_lineno = (self->lineno != NULL) && PyLong_CheckExact(self->lineno);
 
@@ -998,18 +1010,20 @@
         return PyObject_Str(self->msg ? self->msg : Py_None);
 
     if (filename && have_lineno)
-        return PyUnicode_FromFormat("%S (%s, line %ld)",
+        result = PyUnicode_FromFormat("%S (%U, line %ld)",
                    self->msg ? self->msg : Py_None,
-                   my_basename(filename),
+                   filename,
 		   PyLong_AsLongAndOverflow(self->lineno, &overflow));
     else if (filename)
-        return PyUnicode_FromFormat("%S (%s)",
+        result = PyUnicode_FromFormat("%S (%U)",
                    self->msg ? self->msg : Py_None,
-                   my_basename(filename));
+                   filename);
     else /* only have_lineno */
-        return PyUnicode_FromFormat("%S (line %ld)",
+        result = PyUnicode_FromFormat("%S (line %ld)",
                    self->msg ? self->msg : Py_None,
                    PyLong_AsLongAndOverflow(self->lineno, &overflow));
+    Py_XDECREF(filename);
+    return result;
 }
 
 static PyMemberDef SyntaxError_members[] = {


More information about the Python-checkins mailing list