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

Skip to content

Commit a351f77

Browse files
committed
Fixed failing unit tests due to str/bytes mismatch.
Changed "assert isinstance(...)" in hmac.py to raise proper TypeError.
1 parent 7f757ed commit a351f77

4 files changed

Lines changed: 22 additions & 19 deletions

File tree

Lib/hmac.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def __init__(self, key, msg = None, digestmod = None):
3939
if key is _secret_backdoor_key: # cheap
4040
return
4141

42-
assert isinstance(key, bytes), repr(key)
42+
if not isinstance(key, bytes):
43+
raise TypeError("expected bytes, but got %r" % type(key).__name__)
4344

4445
if digestmod is None:
4546
import hashlib
@@ -84,7 +85,8 @@ def __init__(self, key, msg = None, digestmod = None):
8485
def update(self, msg):
8586
"""Update this hashing object with the string msg.
8687
"""
87-
assert isinstance(msg, bytes), repr(msg)
88+
if not isinstance(msg, bytes):
89+
raise TypeError("expected bytes, but got %r" % type(msg).__name__)
8890
self.inner.update(msg)
8991

9092
def copy(self):

Lib/test/test_largefile.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ def test_seek(self):
3131
print('create large file via seek (may be sparse file) ...')
3232
f = open(TESTFN, 'wb')
3333
try:
34-
f.write('z')
34+
f.write(b'z')
3535
f.seek(0)
3636
f.seek(size)
37-
f.write('a')
37+
f.write(b'a')
3838
f.flush()
3939
if verbose:
4040
print('check file size with os.fstat')
@@ -53,7 +53,7 @@ def test_seek_read(self):
5353
f = open(TESTFN, 'rb')
5454
try:
5555
self.assertEqual(f.tell(), 0)
56-
self.assertEqual(f.read(1), 'z')
56+
self.assertEqual(f.read(1), b'z')
5757
self.assertEqual(f.tell(), 1)
5858
f.seek(0)
5959
self.assertEqual(f.tell(), 0)
@@ -76,9 +76,9 @@ def test_seek_read(self):
7676
f.seek(size)
7777
self.assertEqual(f.tell(), size)
7878
# the 'a' that was written at the end of file above
79-
self.assertEqual(f.read(1), 'a')
79+
self.assertEqual(f.read(1), b'a')
8080
f.seek(-size-1, 1)
81-
self.assertEqual(f.read(1), 'z')
81+
self.assertEqual(f.read(1), b'z')
8282
self.assertEqual(f.tell(), 1)
8383
finally:
8484
f.close()
@@ -97,7 +97,7 @@ def test_lseek(self):
9797
self.assertEqual(os.lseek(f.fileno(), -size-1, 2), 0)
9898
self.assertEqual(os.lseek(f.fileno(), size, 0), size)
9999
# the 'a' that was written at the end of file above
100-
self.assertEqual(f.read(1), 'a')
100+
self.assertEqual(f.read(1), b'a')
101101
finally:
102102
f.close()
103103

@@ -155,7 +155,7 @@ def main_test():
155155
f.seek(2147483649)
156156
# Seeking is not enough of a test: you must write and
157157
# flush, too!
158-
f.write("x")
158+
f.write(b'x')
159159
f.flush()
160160
except (IOError, OverflowError):
161161
f.close()

Lib/test/test_pep247.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,37 @@ def check_hash_module(module, key=None):
1212

1313
if key is not None:
1414
obj1 = module.new(key)
15-
obj2 = module.new(key, "string")
15+
obj2 = module.new(key, b"string")
1616

17-
h1 = module.new(key, "string").digest()
18-
obj3 = module.new(key) ; obj3.update("string") ; h2 = obj3.digest()
17+
h1 = module.new(key, b"string").digest()
18+
obj3 = module.new(key) ; obj3.update(b"string") ; h2 = obj3.digest()
1919
assert h1 == h2, "Hashes must match"
2020

2121
else:
2222
obj1 = module.new()
23-
obj2 = module.new("string")
23+
obj2 = module.new(b"string")
2424

25-
h1 = module.new("string").digest()
26-
obj3 = module.new() ; obj3.update("string") ; h2 = obj3.digest()
25+
h1 = module.new(b"string").digest()
26+
obj3 = module.new() ; obj3.update(b"string") ; h2 = obj3.digest()
2727
assert h1 == h2, "Hashes must match"
2828

2929
assert hasattr(obj1, 'digest_size'), "Objects must have digest_size attr"
3030
if module.digest_size is not None:
3131
assert obj1.digest_size == module.digest_size, "digest_size must match"
3232
assert obj1.digest_size == len(h1), "digest_size must match actual size"
33-
obj1.update("string")
33+
obj1.update(b"string")
3434
obj_copy = obj1.copy()
3535
assert obj1.digest() == obj_copy.digest(), "Copied objects must match"
3636
assert obj1.hexdigest() == obj_copy.hexdigest(), \
3737
"Copied objects must match"
3838
digest, hexdigest = obj1.digest(), obj1.hexdigest()
3939
hd2 = ""
4040
for byte in digest:
41-
hd2 += "%02x" % ord(byte)
41+
hd2 += "%02x" % byte
4242
assert hd2 == hexdigest, "hexdigest doesn't appear correct"
4343

4444
print('Module', module.__name__, 'seems to comply with PEP 247')
4545

4646

4747
if __name__ == '__main__':
48-
check_hash_module(hmac, key='abc')
48+
check_hash_module(hmac, key=b'abc')

Lib/test/test_pty.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ def test_fork(self):
157157
break
158158
if not data:
159159
break
160-
sys.stdout.write(data.replace('\r\n', '\n').decode('ascii'))
160+
sys.stdout.write(str(data.replace(b'\r\n', b'\n'),
161+
encoding='ascii'))
161162

162163
##line = os.read(master_fd, 80)
163164
##lines = line.replace('\r\n', '\n').split('\n')

0 commit comments

Comments
 (0)