Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e06b6b8

Browse files
committed
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).
1 parent 5f6f27d commit e06b6b8

2 files changed

Lines changed: 50 additions & 17 deletions

File tree

Lib/test/test_bytes.py

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,29 @@ def __index__(self):
4141
return self.i
4242
b = bytes([C(), C(1), C(254), C(255)])
4343
self.assertEqual(list(b), [0, 1, 254, 255])
44-
self.assertRaises(ValueError, lambda: bytes([C(-1)]))
45-
self.assertRaises(ValueError, lambda: bytes([C(256)]))
44+
self.assertRaises(ValueError, bytes, [C(-1)])
45+
self.assertRaises(ValueError, bytes, [C(256)])
4646

4747
def test_constructor_type_errors(self):
48+
self.assertRaises(TypeError, bytes, 0)
4849
class C:
4950
pass
50-
self.assertRaises(TypeError, lambda: bytes(["0"]))
51-
self.assertRaises(TypeError, lambda: bytes([0.0]))
52-
self.assertRaises(TypeError, lambda: bytes([None]))
53-
self.assertRaises(TypeError, lambda: bytes([C()]))
51+
self.assertRaises(TypeError, bytes, ["0"])
52+
self.assertRaises(TypeError, bytes, [0.0])
53+
self.assertRaises(TypeError, bytes, [None])
54+
self.assertRaises(TypeError, bytes, [C()])
5455

5556
def test_constructor_value_errors(self):
56-
self.assertRaises(ValueError, lambda: bytes([-1]))
57-
self.assertRaises(ValueError, lambda: bytes([-sys.maxint]))
58-
self.assertRaises(ValueError, lambda: bytes([-sys.maxint-1]))
59-
self.assertRaises(ValueError, lambda: bytes([-sys.maxint-2]))
60-
self.assertRaises(ValueError, lambda: bytes([-10**100]))
61-
self.assertRaises(ValueError, lambda: bytes([256]))
62-
self.assertRaises(ValueError, lambda: bytes([257]))
63-
self.assertRaises(ValueError, lambda: bytes([sys.maxint]))
64-
self.assertRaises(ValueError, lambda: bytes([sys.maxint+1]))
65-
self.assertRaises(ValueError, lambda: bytes([10**100]))
57+
self.assertRaises(ValueError, bytes, [-1])
58+
self.assertRaises(ValueError, bytes, [-sys.maxint])
59+
self.assertRaises(ValueError, bytes, [-sys.maxint-1])
60+
self.assertRaises(ValueError, bytes, [-sys.maxint-2])
61+
self.assertRaises(ValueError, bytes, [-10**100])
62+
self.assertRaises(ValueError, bytes, [256])
63+
self.assertRaises(ValueError, bytes, [257])
64+
self.assertRaises(ValueError, bytes, [sys.maxint])
65+
self.assertRaises(ValueError, bytes, [sys.maxint+1])
66+
self.assertRaises(ValueError, bytes, [10**100])
6667

6768
def test_repr(self):
6869
self.assertEqual(repr(bytes()), "bytes()")
@@ -99,6 +100,37 @@ def test_doc(self):
99100
self.failUnless(bytes.__doc__ != None)
100101
self.failUnless(bytes.__doc__.startswith("bytes("))
101102

103+
# XXX More stuff to test and build (TDD):
104+
# constructor from str: bytes(<str>) == bytes(map(ord, <str>))?
105+
# encoding constructor: bytes(<unicode>[, <encoding>[, <errors>]])
106+
# default encoding Latin-1? (Matching ord)
107+
# slicing
108+
# extended slicing?
109+
# item assignment
110+
# slice assignment
111+
# extended slice assignment?
112+
# __contains__ with simple int arg
113+
# __contains__ with another bytes arg?
114+
# find/index? (int or bytes arg?)
115+
# count? (int arg)
116+
# concatenation (+)
117+
# repeat?
118+
# extend?
119+
# append?
120+
# insert?
121+
# pop?
122+
# __reversed__?
123+
# reverse? (inplace)
124+
# NOT sort!
125+
# __iter__? (optimization)
126+
# __str__? (could return "".join(map(chr, self))
127+
# decode
128+
# buffer API
129+
# check that regexp searches work
130+
# (I suppose re.sub() returns a string)
131+
# file.readinto
132+
# file.write
133+
102134

103135
def test_main():
104136
test.test_support.run_unittest(BytesTest)

Objects/bytesobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ bytes_init(PyBytesObject *self, PyObject *args, PyObject *kwds)
130130
/* Get the iterator */
131131
it = PyObject_GetIter(arg);
132132
if (it == NULL)
133-
return 0;
133+
return -1;
134134
iternext = *it->ob_type->tp_iternext;
135135

136136
/* Run the iterator to exhaustion */
@@ -151,6 +151,7 @@ bytes_init(PyBytesObject *self, PyObject *args, PyObject *kwds)
151151

152152
/* Interpret it as an int (__index__) */
153153
value = PyNumber_Index(item);
154+
Py_DECREF(item);
154155
if (value == -1 && PyErr_Occurred())
155156
goto error;
156157

0 commit comments

Comments
 (0)