Christian Heimes added the comment:

Here it is.

patch + unit test

Added file: http://bugs.python.org/file8670/py3k_pystring_ssize.patch

__________________________________
Tracker <[EMAIL PROTECTED]>
<http://bugs.python.org/issue1365>
__________________________________
Index: Objects/stringobject.c
===================================================================
--- Objects/stringobject.c	(Revision 58729)
+++ Objects/stringobject.c	(Arbeitskopie)
@@ -3113,6 +3113,26 @@
 		return NULL;
 	}
 
+	/* Is it an int? */
+	size = PyNumber_AsSsize_t(x, PyExc_ValueError);
+	if (size == -1 && PyErr_Occurred()) {
+		PyErr_Clear();
+	}
+	else {
+		if (size < 0) {
+			PyErr_SetString(PyExc_ValueError, "negative count");
+			return NULL;
+		}
+		new = PyString_FromStringAndSize(NULL, size);
+		if (new == NULL) {
+			return NULL;
+		}
+		if (size > 0) {
+			memset(((PyStringObject*)new)->ob_sval, 0, size);
+		}
+		return new;
+	}
+
 	/* Use the modern buffer interface */
 	if (PyObject_CheckBuffer(x)) {
 		Py_buffer view;
Index: Lib/test/test_bytes.py
===================================================================
--- Lib/test/test_bytes.py	(Revision 58729)
+++ Lib/test/test_bytes.py	(Arbeitskopie)
@@ -50,6 +50,15 @@
         self.assertRaises(ValueError, bytes, [C(-1)])
         self.assertRaises(ValueError, bytes, [C(256)])
 
+    def test_from_ssize(self):
+        self.assertEqual(bytes(0), b'')
+        self.assertEqual(bytes(1), b'\x00')
+        self.assertEqual(bytes(5), b'\x00\x00\x00\x00\x00')
+        self.assertRaises(ValueError, bytes, -1)
+
+        self.assertEqual(bytes('0', 'ascii'), b'0')
+        self.assertEqual(bytes(b'0'), b'0')
+
     def test_constructor_type_errors(self):
         self.assertRaises(TypeError, bytes, 0.0)
         class C:
_______________________________________________
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to