Commit 03a36c8d authored by Guido van Rossum's avatar Guido van Rossum

Fix a leak and a buglet discovered by Thomas.

Get rid of silly lambdas in the unit test suite.
Add a TODO list to the unit test suite (TDD style).
parent 02f76cd9
......@@ -41,28 +41,29 @@ class BytesTest(unittest.TestCase):
return self.i
b = bytes([C(), C(1), C(254), C(255)])
self.assertEqual(list(b), [0, 1, 254, 255])
self.assertRaises(ValueError, lambda: bytes([C(-1)]))
self.assertRaises(ValueError, lambda: bytes([C(256)]))
self.assertRaises(ValueError, bytes, [C(-1)])
self.assertRaises(ValueError, bytes, [C(256)])
def test_constructor_type_errors(self):
self.assertRaises(TypeError, bytes, 0)
class C:
pass
self.assertRaises(TypeError, lambda: bytes(["0"]))
self.assertRaises(TypeError, lambda: bytes([0.0]))
self.assertRaises(TypeError, lambda: bytes([None]))
self.assertRaises(TypeError, lambda: bytes([C()]))
self.assertRaises(TypeError, bytes, ["0"])
self.assertRaises(TypeError, bytes, [0.0])
self.assertRaises(TypeError, bytes, [None])
self.assertRaises(TypeError, bytes, [C()])
def test_constructor_value_errors(self):
self.assertRaises(ValueError, lambda: bytes([-1]))
self.assertRaises(ValueError, lambda: bytes([-sys.maxint]))
self.assertRaises(ValueError, lambda: bytes([-sys.maxint-1]))
self.assertRaises(ValueError, lambda: bytes([-sys.maxint-2]))
self.assertRaises(ValueError, lambda: bytes([-10**100]))
self.assertRaises(ValueError, lambda: bytes([256]))
self.assertRaises(ValueError, lambda: bytes([257]))
self.assertRaises(ValueError, lambda: bytes([sys.maxint]))
self.assertRaises(ValueError, lambda: bytes([sys.maxint+1]))
self.assertRaises(ValueError, lambda: bytes([10**100]))
self.assertRaises(ValueError, bytes, [-1])
self.assertRaises(ValueError, bytes, [-sys.maxint])
self.assertRaises(ValueError, bytes, [-sys.maxint-1])
self.assertRaises(ValueError, bytes, [-sys.maxint-2])
self.assertRaises(ValueError, bytes, [-10**100])
self.assertRaises(ValueError, bytes, [256])
self.assertRaises(ValueError, bytes, [257])
self.assertRaises(ValueError, bytes, [sys.maxint])
self.assertRaises(ValueError, bytes, [sys.maxint+1])
self.assertRaises(ValueError, bytes, [10**100])
def test_repr(self):
self.assertEqual(repr(bytes()), "bytes()")
......@@ -99,6 +100,37 @@ class BytesTest(unittest.TestCase):
self.failUnless(bytes.__doc__ != None)
self.failUnless(bytes.__doc__.startswith("bytes("))
# XXX More stuff to test and build (TDD):
# constructor from str: bytes(<str>) == bytes(map(ord, <str>))?
# encoding constructor: bytes(<unicode>[, <encoding>[, <errors>]])
# default encoding Latin-1? (Matching ord)
# slicing
# extended slicing?
# item assignment
# slice assignment
# extended slice assignment?
# __contains__ with simple int arg
# __contains__ with another bytes arg?
# find/index? (int or bytes arg?)
# count? (int arg)
# concatenation (+)
# repeat?
# extend?
# append?
# insert?
# pop?
# __reversed__?
# reverse? (inplace)
# NOT sort!
# __iter__? (optimization)
# __str__? (could return "".join(map(chr, self))
# decode
# buffer API
# check that regexp searches work
# (I suppose re.sub() returns a string)
# file.readinto
# file.write
def test_main():
test.test_support.run_unittest(BytesTest)
......
......@@ -130,7 +130,7 @@ bytes_init(PyBytesObject *self, PyObject *args, PyObject *kwds)
/* Get the iterator */
it = PyObject_GetIter(arg);
if (it == NULL)
return 0;
return -1;
iternext = *it->ob_type->tp_iternext;
/* Run the iterator to exhaustion */
......@@ -151,6 +151,7 @@ bytes_init(PyBytesObject *self, PyObject *args, PyObject *kwds)
/* Interpret it as an int (__index__) */
value = PyNumber_Index(item);
Py_DECREF(item);
if (value == -1 && PyErr_Occurred())
goto error;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment