New submission from Christian Heimes:
str(bytes()) == repr(bytes()) and str(buffer()) == repr(buffer()) is
causing a bunch bugs which are extremely hard to understand. On several
occasions I didn't understand the problem until I removed a str() call
or made str(bytes()) and str(buffer()) raise an exception.
----------
assignee: gvanrossum
components: Interpreter Core
files: py3k-pep3137_remove_str_bytes.patch
keywords: patch, py3k
messages: 57124
nosy: gvanrossum, tiran
priority: high
severity: normal
status: open
title: py3k-pep3137: str(bytes()) and str(buffer()) should raise TypeError patch
type: behavior
versions: Python 3.0
Added file: http://bugs.python.org/file8694/py3k-pep3137_remove_str_bytes.patch
__________________________________
Tracker <[EMAIL PROTECTED]>
<http://bugs.python.org/issue1392>
__________________________________
Index: Objects/bytesobject.c
===================================================================
--- Objects/bytesobject.c (Revision 58843)
+++ Objects/bytesobject.c (Arbeitskopie)
@@ -923,6 +923,14 @@
}
static PyObject *
+bytes_str(PyObject *op)
+{
+ PyErr_SetString(PyExc_TypeError,
+ "str() argument must not be a buffer instance.");
+ return NULL;
+}
+
+static PyObject *
bytes_richcompare(PyObject *self, PyObject *other, int op)
{
Py_ssize_t self_size, other_size;
@@ -3063,7 +3071,7 @@
&bytes_as_mapping, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
- (reprfunc)bytes_repr, /* tp_str */
+ (reprfunc)bytes_str, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
&bytes_as_buffer, /* tp_as_buffer */
Index: Objects/stringobject.c
===================================================================
--- Objects/stringobject.c (Revision 58843)
+++ Objects/stringobject.c (Arbeitskopie)
@@ -679,6 +679,14 @@
return PyString_Repr(op, 1);
}
+static PyObject *
+string_str(PyObject *op)
+{
+ PyErr_SetString(PyExc_TypeError,
+ "str() argument must not be a byte instance.");
+ return NULL;
+}
+
static Py_ssize_t
string_length(PyStringObject *a)
{
@@ -3096,7 +3104,7 @@
&string_as_mapping, /* tp_as_mapping */
(hashfunc)string_hash, /* tp_hash */
0, /* tp_call */
- string_repr, /* tp_str */
+ (reprfunc)string_str, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
&string_as_buffer, /* tp_as_buffer */
Index: Lib/test/test_bytes.py
===================================================================
--- Lib/test/test_bytes.py (Revision 58843)
+++ Lib/test/test_bytes.py (Arbeitskopie)
@@ -86,16 +86,22 @@
self.assertRaises(ValueError, buffer, [sys.maxint+1])
self.assertRaises(ValueError, buffer, [10**100])
- def test_repr_str(self):
- for f in str, repr:
- self.assertEqual(f(buffer()), "buffer(b'')")
- self.assertEqual(f(buffer([0])), "buffer(b'\\x00')")
- self.assertEqual(f(buffer([0, 1, 254, 255])),
- "buffer(b'\\x00\\x01\\xfe\\xff')")
- self.assertEqual(f(b"abc"), "b'abc'")
- self.assertEqual(f(b"'"), '''b"'"''')
- self.assertEqual(f(b"'\""), r"""b'\'"'""")
+ def test_repr(self):
+ self.assertEqual(repr(buffer()), "buffer(b'')")
+ self.assertEqual(repr(buffer([0])), "buffer(b'\\x00')")
+ self.assertEqual(repr(buffer([0, 1, 254, 255])),
+ "buffer(b'\\x00\\x01\\xfe\\xff')")
+ self.assertEqual(repr(b"abc"), "b'abc'")
+ self.assertEqual(repr(b"'"), '''b"'"''')
+ self.assertEqual(repr(b"'\""), r"""b'\'"'""")
+ def test_str(self):
+ self.assertRaises(TypeError, str, buffer())
+ self.assertRaises(TypeError, str, buffer([0]))
+ self.assertRaises(TypeError, str, buffer([0, 1, 254, 255]))
+ self.assertRaises(TypeError, str, b"abc")
+ self.assertRaises(TypeError, str, b"'")
+ self.assertRaises(TypeError, str, b"'\"")
def test_compare(self):
b1 = buffer([1, 2, 3])
@@ -370,11 +376,6 @@
b = buffer(buf)
self.assertEqual(b, buffer(sample))
- def test_to_str(self):
- self.assertEqual(str(b''), "b''")
- self.assertEqual(str(b'x'), "b'x'")
- self.assertEqual(str(b'\x80'), "b'\\x80'")
-
def test_from_int(self):
b = buffer(0)
self.assertEqual(b, buffer())
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com