[Python-3000-checkins] r58805 - in python/branches/py3k-pep3137: Lib/test/test_exceptions.py Objects/exceptions.c Objects/unicodeobject.c

christian.heimes python-3000-checkins at python.org
Fri Nov 2 22:05:32 CET 2007


Author: christian.heimes
Date: Fri Nov  2 22:05:31 2007
New Revision: 58805

Modified:
   python/branches/py3k-pep3137/Lib/test/test_exceptions.py
   python/branches/py3k-pep3137/Objects/exceptions.c
   python/branches/py3k-pep3137/Objects/unicodeobject.c
Log:
Changed UnicodeDecodeError.object type to PyString.
An object that supports the read buffer (e.g. PyBytes) is converted to PyString. UnicodeDecodeError.args isn't altered and still contains the original type. Is this fine with you, Guido?

Modified: python/branches/py3k-pep3137/Lib/test/test_exceptions.py
==============================================================================
--- python/branches/py3k-pep3137/Lib/test/test_exceptions.py	(original)
+++ python/branches/py3k-pep3137/Lib/test/test_exceptions.py	Fri Nov  2 22:05:31 2007
@@ -257,7 +257,13 @@
                                   'ordinal not in range'),
                 {'args' : ('ascii', buffer(b'\xff'), 0, 1,
                                            'ordinal not in range'),
-                 'encoding' : 'ascii', 'object' : buffer(b'\xff'),
+                 'encoding' : 'ascii', 'object' : b'\xff',
+                 'start' : 0, 'reason' : 'ordinal not in range'}),
+            (UnicodeDecodeError, ('ascii', b'\xff', 0, 1,
+                                  'ordinal not in range'),
+                {'args' : ('ascii', b'\xff', 0, 1,
+                                           'ordinal not in range'),
+                 'encoding' : 'ascii', 'object' : b'\xff',
                  'start' : 0, 'reason' : 'ordinal not in range'}),
             (UnicodeTranslateError, ("\u3042", 0, 1, "ouch"),
                 {'args' : ('\u3042', 0, 1, 'ouch'),

Modified: python/branches/py3k-pep3137/Objects/exceptions.c
==============================================================================
--- python/branches/py3k-pep3137/Objects/exceptions.c	(original)
+++ python/branches/py3k-pep3137/Objects/exceptions.c	Fri Nov  2 22:05:31 2007
@@ -1045,14 +1045,14 @@
                        "Unicode related error.");
 
 static PyObject *
-get_bytes(PyObject *attr, const char *name)
+get_string(PyObject *attr, const char *name)
 {
     if (!attr) {
         PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
         return NULL;
     }
 
-    if (!PyBytes_Check(attr)) {
+    if (!PyString_Check(attr)) {
         PyErr_Format(PyExc_TypeError, "%.200s attribute must be bytes", name);
         return NULL;
     }
@@ -1109,7 +1109,7 @@
 PyObject *
 PyUnicodeDecodeError_GetObject(PyObject *exc)
 {
-    return get_bytes(((PyUnicodeErrorObject *)exc)->object, "object");
+    return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
 }
 
 PyObject *
@@ -1141,10 +1141,10 @@
 PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
 {
     Py_ssize_t size;
-    PyObject *obj = get_bytes(((PyUnicodeErrorObject *)exc)->object, "object");
+    PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object");
     if (!obj)
         return -1;
-    size = PyBytes_GET_SIZE(obj);
+    size = PyString_GET_SIZE(obj);
     *start = ((PyUnicodeErrorObject *)exc)->start;
     if (*start<0)
         *start = 0;
@@ -1209,10 +1209,10 @@
 PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
 {
     Py_ssize_t size;
-    PyObject *obj = get_bytes(((PyUnicodeErrorObject *)exc)->object, "object");
+    PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object");
     if (!obj)
         return -1;
-    size = PyBytes_GET_SIZE(obj);
+    size = PyString_GET_SIZE(obj);
     *end = ((PyUnicodeErrorObject *)exc)->end;
     if (*end<1)
         *end = 1;
@@ -1299,31 +1299,6 @@
 
 
 static int
-UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds,
-                  PyTypeObject *objecttype)
-{
-    Py_CLEAR(self->encoding);
-    Py_CLEAR(self->object);
-    Py_CLEAR(self->reason);
-
-    if (!PyArg_ParseTuple(args, "O!O!nnO!",
-        &PyUnicode_Type, &self->encoding,
-        objecttype, &self->object,
-        &self->start,
-        &self->end,
-        &PyUnicode_Type, &self->reason)) {
-        self->encoding = self->object = self->reason = NULL;
-        return -1;
-    }
-
-    Py_INCREF(self->encoding);
-    Py_INCREF(self->object);
-    Py_INCREF(self->reason);
-
-    return 0;
-}
-
-static int
 UnicodeError_clear(PyUnicodeErrorObject *self)
 {
     Py_CLEAR(self->encoding);
@@ -1371,10 +1346,32 @@
 static int
 UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
 {
+    PyUnicodeErrorObject *err;
+
     if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
         return -1;
-    return UnicodeError_init((PyUnicodeErrorObject *)self, args,
-                             kwds, &PyUnicode_Type);
+
+    err = (PyUnicodeErrorObject *)self;
+
+    Py_CLEAR(err->encoding);
+    Py_CLEAR(err->object);
+    Py_CLEAR(err->reason);
+
+    if (!PyArg_ParseTuple(args, "O!O!nnO!",
+        &PyUnicode_Type, &err->encoding,
+        &PyUnicode_Type, &err->object,
+        &err->start,
+        &err->end,
+        &PyUnicode_Type, &err->reason)) {
+          err->encoding = err->object = err->reason = NULL;
+          return -1;
+    }
+
+    Py_INCREF(err->encoding);
+    Py_INCREF(err->object);
+    Py_INCREF(err->reason);
+
+    return 0;
 }
 
 static PyObject *
@@ -1439,10 +1436,44 @@
 static int
 UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
 {
+    PyUnicodeErrorObject *ude;
+    const char *data;
+    Py_ssize_t size;
+
     if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
         return -1;
-    return UnicodeError_init((PyUnicodeErrorObject *)self, args,
-                             kwds, &PyBytes_Type);
+
+    ude = (PyUnicodeErrorObject *)self;
+
+    Py_CLEAR(ude->encoding);
+    Py_CLEAR(ude->object);
+    Py_CLEAR(ude->reason);
+
+    if (!PyArg_ParseTuple(args, "O!OnnO!",
+         &PyUnicode_Type, &ude->encoding,
+         &ude->object,
+         &ude->start,
+         &ude->end,
+         &PyUnicode_Type, &ude->reason)) {
+             ude->encoding = ude->object = ude->reason = NULL;
+             return -1;
+    }
+
+    if (!PyString_Check(ude->object)) {
+        if (PyObject_AsReadBuffer(ude->object, (const void **)&data, &size)) {
+            ude->encoding = ude->object = ude->reason = NULL;
+            return -1;
+        }
+        ude->object = PyString_FromStringAndSize(data, size);
+    }
+    else {
+        Py_INCREF(ude->object);
+    }
+
+    Py_INCREF(ude->encoding);
+    Py_INCREF(ude->reason);
+
+    return 0;
 }
 
 static PyObject *
@@ -1451,7 +1482,8 @@
     PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
 
     if (uself->end==uself->start+1) {
-        int byte = (int)(PyBytes_AS_STRING(((PyUnicodeErrorObject *)self)->object)[uself->start]&0xff);
+        assert(PyString_Check(((PyUnicodeErrorObject *)self)->object));
+        int byte = (int)(PyString_AS_STRING(((PyUnicodeErrorObject *)self)->object)[uself->start]&0xff);
         return PyUnicode_FromFormat(
             "'%U' codec can't decode byte 0x%02x in position %zd: %U",
             ((PyUnicodeErrorObject *)self)->encoding,

Modified: python/branches/py3k-pep3137/Objects/unicodeobject.c
==============================================================================
--- python/branches/py3k-pep3137/Objects/unicodeobject.c	(original)
+++ python/branches/py3k-pep3137/Objects/unicodeobject.c	Fri Nov  2 22:05:31 2007
@@ -1409,11 +1409,11 @@
     inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject);
     if (!inputobj)
         goto onError;
-    if (!PyBytes_Check(inputobj)) {
+    if (!PyString_Check(inputobj)) {
 	PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes");
     }
-    *input = PyBytes_AS_STRING(inputobj);
-    insize = PyBytes_GET_SIZE(inputobj);
+    *input = PyString_AS_STRING(inputobj);
+    insize = PyString_GET_SIZE(inputobj);
     *inend = *input + insize;
     /* we can DECREF safely, as the exception has another reference,
        so the object won't go away. */


More information about the Python-3000-checkins mailing list