[Python-3000-checkins] r55208 - in python/branches/py3k-struni: Lib/test/test_bytes.py Objects/bytesobject.c

guido.van.rossum python-3000-checkins at python.org
Wed May 9 21:52:21 CEST 2007


Author: guido.van.rossum
Date: Wed May  9 21:52:16 2007
New Revision: 55208

Modified:
   python/branches/py3k-struni/Lib/test/test_bytes.py
   python/branches/py3k-struni/Objects/bytesobject.c
Log:
I don't know how come bytes.join() was a class method, but that's clearly
a mistake.  It's not a regular (instance) method. b".".join([b"a", b"b"])
now returns b"a.b" -- it used to return b"ab"!


Modified: python/branches/py3k-struni/Lib/test/test_bytes.py
==============================================================================
--- python/branches/py3k-struni/Lib/test/test_bytes.py	(original)
+++ python/branches/py3k-struni/Lib/test/test_bytes.py	Wed May  9 21:52:16 2007
@@ -466,13 +466,14 @@
         self.assertRaises(ValueError, bytes.fromhex, '12   \x00   34')
 
     def test_join(self):
-        self.assertEqual(bytes.join([]), bytes())
-        self.assertEqual(bytes.join([bytes()]), bytes())
+        self.assertEqual(b"".join([]), bytes())
+        self.assertEqual(b"".join([bytes()]), bytes())
         for part in [("abc",), ("a", "bc"), ("ab", "c"), ("a", "b", "c")]:
             lst = map(bytes, part)
-            self.assertEqual(bytes.join(lst), bytes("abc"))
-            self.assertEqual(bytes.join(tuple(lst)), bytes("abc"))
-            self.assertEqual(bytes.join(iter(lst)), bytes("abc"))
+            self.assertEqual(b"".join(lst), bytes("abc"))
+            self.assertEqual(b"".join(tuple(lst)), bytes("abc"))
+            self.assertEqual(b"".join(iter(lst)), bytes("abc"))
+        self.assertEqual(b".".join([b"ab", b"cd"]), b"ab.cd")
         # XXX more...
 
     def test_literal(self):

Modified: python/branches/py3k-struni/Objects/bytesobject.c
==============================================================================
--- python/branches/py3k-struni/Objects/bytesobject.c	(original)
+++ python/branches/py3k-struni/Objects/bytesobject.c	Wed May  9 21:52:16 2007
@@ -2580,15 +2580,16 @@
 }
 
 PyDoc_STRVAR(join_doc,
-"bytes.join(iterable_of_bytes) -> bytes\n\
+"B.join(iterable_of_bytes) -> bytes\n\
 \n\
-Concatenates any number of bytes objects.  Example:\n\
-bytes.join([bytes('ab'), bytes('pq'), bytes('rs')]) -> bytes('abpqrs').");
+Concatenates any number of bytes objects, with B in between each pair.\n\
+Example: b'.'.join([b'ab', b'pq', b'rs']) -> b'ab.pq.rs'.");
 
 static PyObject *
-bytes_join(PyObject *cls, PyObject *it)
+bytes_join(PyBytesObject *self, PyObject *it)
 {
     PyObject *seq;
+    Py_ssize_t mysize = self->ob_size;
     Py_ssize_t i;
     Py_ssize_t n;
     PyObject **items;
@@ -2613,6 +2614,8 @@
                          (long)i, obj->ob_type->tp_name);
             goto error;
         }
+        if (i > 0)
+            totalsize += mysize;
         totalsize += PyBytes_GET_SIZE(obj);
         if (totalsize < 0) {
             PyErr_NoMemory();
@@ -2628,6 +2631,10 @@
     for (i = 0; i < n; i++) {
         PyObject *obj = items[i];
         Py_ssize_t size = PyBytes_GET_SIZE(obj);
+        if (i > 0) {
+            memcpy(dest, self->ob_bytes, mysize);
+            dest += mysize;
+        }
         memcpy(dest, PyBytes_AS_STRING(obj), size);
         dest += size;
     }
@@ -2775,7 +2782,7 @@
     {"__alloc__", (PyCFunction)bytes_alloc, METH_NOARGS, alloc_doc},
     {"fromhex", (PyCFunction)bytes_fromhex, METH_VARARGS|METH_CLASS,
      fromhex_doc},
-    {"join", (PyCFunction)bytes_join, METH_O|METH_CLASS, join_doc},
+    {"join", (PyCFunction)bytes_join, METH_O, join_doc},
     {"__reduce__", (PyCFunction)bytes_reduce, METH_NOARGS, reduce_doc},
     {NULL}
 };


More information about the Python-3000-checkins mailing list