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

guido.van.rossum python-3000-checkins at python.org
Tue Nov 6 01:20:26 CET 2007


Author: guido.van.rossum
Date: Tue Nov  6 01:20:26 2007
New Revision: 58869

Modified:
   python/branches/py3k-pep3137/Lib/test/test_bytes.py
   python/branches/py3k-pep3137/Objects/bytesobject.c
Log:
stringlib's partition shares nullbytes in some cases.
Undo this, we don't want the nullbytes to be shared.



Modified: python/branches/py3k-pep3137/Lib/test/test_bytes.py
==============================================================================
--- python/branches/py3k-pep3137/Lib/test/test_bytes.py	(original)
+++ python/branches/py3k-pep3137/Lib/test/test_bytes.py	Tue Nov  6 01:20:26 2007
@@ -749,6 +749,28 @@
         self.assertEqual([ord(b[i:i+1]) for i in range(len(b))],
                          [0, 65, 127, 128, 255])
 
+    def test_partition_buffer_doesnt_share_nullstring(self):
+        a, b, c = buffer(b"x").partition(b"y")
+        self.assertEqual(b, b"")
+        self.assertEqual(c, b"")
+        self.assert_(b is not c)
+        b += b"!"
+        self.assertEqual(c, b"")
+        a, b, c = buffer(b"x").partition(b"y")
+        self.assertEqual(b, b"")
+        self.assertEqual(c, b"")
+        # Same for rpartition
+        b, c, a = buffer(b"x").rpartition(b"y")
+        self.assertEqual(b, b"")
+        self.assertEqual(c, b"")
+        self.assert_(b is not c)
+        b += b"!"
+        self.assertEqual(c, b"")
+        c, b, a = buffer(b"x").rpartition(b"y")
+        self.assertEqual(b, b"")
+        self.assertEqual(c, b"")
+
+
     # Optimizations:
     # __iter__? (optimization)
     # __reversed__? (optimization)

Modified: python/branches/py3k-pep3137/Objects/bytesobject.c
==============================================================================
--- python/branches/py3k-pep3137/Objects/bytesobject.c	(original)
+++ python/branches/py3k-pep3137/Objects/bytesobject.c	Tue Nov  6 01:20:26 2007
@@ -2251,6 +2251,31 @@
     return NULL;
 }
 
+/* stringlib's partition shares nullbytes in some cases.
+   undo this, we don't want the nullbytes to be shared. */
+static PyObject *
+make_nullbytes_unique(PyObject *result)
+{
+    if (result != NULL) {
+        int i;
+        assert(PyTuple_Check(result));
+        assert(PyTuple_GET_SIZE(result) == 3);
+        for (i = 0; i < 3; i++) {
+            if (PyTuple_GET_ITEM(result, i) == (PyObject *)nullbytes) {
+                PyObject *new = PyBytes_FromStringAndSize(NULL, 0);
+                if (new == NULL) {
+                    Py_DECREF(result);
+                    result = NULL;
+                    break;
+                }
+                Py_DECREF(nullbytes);
+                PyTuple_SET_ITEM(result, i, new);
+            }
+        }
+    }
+    return result;
+}
+
 PyDoc_STRVAR(partition__doc__,
 "B.partition(sep) -> (head, sep, tail)\n\
 \n\
@@ -2275,7 +2300,7 @@
             );
 
     Py_DECREF(bytesep);
-    return result;
+    return make_nullbytes_unique(result);
 }
 
 PyDoc_STRVAR(rpartition__doc__,
@@ -2303,7 +2328,7 @@
             );
 
     Py_DECREF(bytesep);
-    return result;
+    return make_nullbytes_unique(result);
 }
 
 Py_LOCAL_INLINE(PyObject *)


More information about the Python-3000-checkins mailing list