[Python-checkins] r61756 - in python/branches/trunk-bytearray: Objects/bytesobject.c Objects/stringobject.c Python/bltinmodule.c

christian.heimes python-checkins at python.org
Sat Mar 22 21:43:39 CET 2008


Author: christian.heimes
Date: Sat Mar 22 21:43:38 2008
New Revision: 61756

Modified:
   python/branches/trunk-bytearray/Objects/bytesobject.c
   python/branches/trunk-bytearray/Objects/stringobject.c
   python/branches/trunk-bytearray/Python/bltinmodule.c
Log:
Added PyBytes support to several places:
str + bytearray
ord(bytearray)
bytearray(str, encoding)


Modified: python/branches/trunk-bytearray/Objects/bytesobject.c
==============================================================================
--- python/branches/trunk-bytearray/Objects/bytesobject.c	(original)
+++ python/branches/trunk-bytearray/Objects/bytesobject.c	Sat Mar 22 21:43:38 2008
@@ -714,13 +714,19 @@
     }
 
     if (PyString_Check(arg)) {
-        PyObject *new;
+        PyObject *new, *encoded;
         if (encoding != NULL) {
-            PyErr_SetString(PyExc_TypeError,
-                            "str argument with an encoding");
-            return -1;
+            encoded = PyCodec_Encode(arg, encoding, errors);
+            if (encoded == NULL)
+                return -1;
+            assert(PyString_Check(encoded));
+        }
+        else {
+            encoded = arg;
+            Py_INCREF(arg);
         }
         new = bytes_iconcat(self, arg);
+        Py_DECREF(encoded);
         if (new == NULL)
             return -1;
         Py_DECREF(new);

Modified: python/branches/trunk-bytearray/Objects/stringobject.c
==============================================================================
--- python/branches/trunk-bytearray/Objects/stringobject.c	(original)
+++ python/branches/trunk-bytearray/Objects/stringobject.c	Sat Mar 22 21:43:38 2008
@@ -953,6 +953,8 @@
 		if (PyUnicode_Check(bb))
 		    return PyUnicode_Concat((PyObject *)a, bb);
 #endif
+		if (PyBytes_Check(bb))
+		    return PyBytes_Concat((PyObject *)a, bb);
 		PyErr_Format(PyExc_TypeError,
 			     "cannot concatenate 'str' and '%.200s' objects",
 			     Py_TYPE(bb)->tp_name);

Modified: python/branches/trunk-bytearray/Python/bltinmodule.c
==============================================================================
--- python/branches/trunk-bytearray/Python/bltinmodule.c	(original)
+++ python/branches/trunk-bytearray/Python/bltinmodule.c	Sat Mar 22 21:43:38 2008
@@ -1434,6 +1434,13 @@
 			ord = (long)((unsigned char)*PyString_AS_STRING(obj));
 			return PyInt_FromLong(ord);
 		}
+	} else if (PyBytes_Check(obj)) {
+		size = PyBytes_GET_SIZE(obj);
+		if (size == 1) {
+			ord = (long)((unsigned char)*PyBytes_AS_STRING(obj));
+			return PyInt_FromLong(ord);
+		}
+
 #ifdef Py_USING_UNICODE
 	} else if (PyUnicode_Check(obj)) {
 		size = PyUnicode_GET_SIZE(obj);


More information about the Python-checkins mailing list