[Python-3000-checkins] r58764 - in python/branches/py3k-pep3137: Objects/unicodeobject.c Python/codecs.c

guido.van.rossum python-3000-checkins at python.org
Fri Nov 2 03:40:13 CET 2007


Author: guido.van.rossum
Date: Fri Nov  2 03:40:12 2007
New Revision: 58764

Modified:
   python/branches/py3k-pep3137/Objects/unicodeobject.c
   python/branches/py3k-pep3137/Python/codecs.c
Log:
Move the encode() type check into codecs.c, where it catches more cases.


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 03:40:12 2007
@@ -1223,13 +1223,6 @@
     if (v == NULL)
         goto onError;
     if (!PyString_Check(v)) {
-        if (PyBytes_Check(v)) {
-            /* Turn it into PyString */
-            PyObject *s = PyString_FromStringAndSize(
-                    PyBytes_AS_STRING(v), Py_Size(v));
-            Py_DECREF(v);
-            return s;
-        }
         PyErr_Format(PyExc_TypeError,
                      "encoder did not return a bytes object "
                      "(type=%.400s, encoding=%.20s, errors=%.20s)",

Modified: python/branches/py3k-pep3137/Python/codecs.c
==============================================================================
--- python/branches/py3k-pep3137/Python/codecs.c	(original)
+++ python/branches/py3k-pep3137/Python/codecs.c	Fri Nov  2 03:40:12 2007
@@ -14,7 +14,7 @@
 /* --- Codec Registry ----------------------------------------------------- */
 
 /* Import the standard encodings package which will register the first
-   codec search function. 
+   codec search function.
 
    This is done in a lazy way so that the Unicode implementation does
    not downgrade startup time of scripts not needing it.
@@ -87,7 +87,7 @@
    characters. This makes encodings looked up through this mechanism
    effectively case-insensitive.
 
-   If no codec is found, a LookupError is set and NULL returned. 
+   If no codec is found, a LookupError is set and NULL returned.
 
    As side effect, this tries to load the encodings package, if not
    yet done. This is part of the lazy load strategy for the encodings
@@ -125,7 +125,7 @@
 	Py_DECREF(v);
 	return result;
     }
-    
+
     /* Next, scan the search functions in order of registration */
     args = PyTuple_New(1);
     if (args == NULL)
@@ -144,7 +144,7 @@
 
     for (i = 0; i < len; i++) {
 	PyObject *func;
-	
+
 	func = PyList_GetItem(interp->codec_search_path, i);
 	if (func == NULL)
 	    goto onError;
@@ -188,7 +188,7 @@
 		     const char *errors)
 {
     PyObject *args;
-    
+
     args = PyTuple_New(1 + (errors != NULL));
     if (args == NULL)
 	return NULL;
@@ -196,7 +196,7 @@
     PyTuple_SET_ITEM(args,0,object);
     if (errors) {
 	PyObject *v;
-	
+
 	v = PyUnicode_FromString(errors);
 	if (v == NULL) {
 	    Py_DECREF(args);
@@ -271,10 +271,10 @@
     return streamcodec;
 }
 
-/* Convenience APIs to query the Codec registry. 
-   
+/* Convenience APIs to query the Codec registry.
+
    All APIs return a codec object with incremented refcount.
-   
+
  */
 
 PyObject *PyCodec_Encoder(const char *encoding)
@@ -324,7 +324,7 @@
 {
     PyObject *encoder = NULL;
     PyObject *args = NULL, *result = NULL;
-    PyObject *v;
+    PyObject *v = NULL;
 
     encoder = PyCodec_Encoder(encoding);
     if (encoder == NULL)
@@ -341,23 +341,26 @@
     if (!PyTuple_Check(result) ||
 	PyTuple_GET_SIZE(result) != 2) {
 	PyErr_SetString(PyExc_TypeError,
-			"encoder must return a tuple (object,integer)");
+			"encoder must return a tuple (object, integer)");
 	goto onError;
     }
     v = PyTuple_GET_ITEM(result, 0);
-    Py_INCREF(v);
+    if (PyBytes_Check(v))
+        v = PyString_FromStringAndSize(PyBytes_AS_STRING(v), Py_Size(v));
+    else if (PyString_Check(v))
+        Py_INCREF(v);
+    else {
+        PyErr_SetString(PyExc_TypeError,
+                        "encoding must return a tuple(bytes, integer)");
+        v = NULL;
+    }
     /* We don't check or use the second (integer) entry. */
 
-    Py_DECREF(args);
-    Py_DECREF(encoder);
-    Py_DECREF(result);
-    return v;
-
  onError:
     Py_XDECREF(result);
     Py_XDECREF(args);
     Py_XDECREF(encoder);
-    return NULL;
+    return v;
 }
 
 /* Decode an object (usually a Python string) using the given encoding
@@ -380,11 +383,11 @@
     args = args_tuple(object, errors);
     if (args == NULL)
 	goto onError;
-    
+
     result = PyEval_CallObject(decoder,args);
     if (result == NULL)
 	goto onError;
-    if (!PyTuple_Check(result) || 
+    if (!PyTuple_Check(result) ||
 	PyTuple_GET_SIZE(result) != 2) {
 	PyErr_SetString(PyExc_TypeError,
 			"decoder must return a tuple (object,integer)");
@@ -398,7 +401,7 @@
     Py_DECREF(decoder);
     Py_DECREF(result);
     return v;
-	
+
  onError:
     Py_XDECREF(args);
     Py_XDECREF(decoder);


More information about the Python-3000-checkins mailing list