From e06b6b8ff5a380f5e107f2d28f23853bfe20021e Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sun, 23 Apr 2006 07:43:54 +0000 Subject: 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). --- Lib/test/test_bytes.py | 64 +++++++++++++++++++++++++++++++++++++------------- Objects/bytesobject.c | 3 ++- 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index fc911ac..1ba5e11 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -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() == bytes(map(ord, ))? + # encoding constructor: bytes([, [, ]]) + # 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) diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 8fc089b..f221395 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -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; -- cgit v0.12