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

Skip to content

Commit a280ca7

Browse files
committed
Conform to strict str/bytes distinction.
1 parent 6665cef commit a280ca7

2 files changed

Lines changed: 8 additions & 8 deletions

File tree

Lib/tarfile.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def itn(n, digits=8, format=DEFAULT_FORMAT):
214214
# encoding, the following digits-1 bytes are a big-endian
215215
# representation. This allows values up to (256**(digits-1))-1.
216216
if 0 <= n < 8 ** (digits - 1):
217-
s = bytes("%0*o" % (digits - 1, n)) + NUL
217+
s = bytes("%0*o" % (digits - 1, n), "ascii") + NUL
218218
else:
219219
if format != GNU_FORMAT or n >= 256 ** (digits - 1):
220220
raise ValueError("overflow in number field")
@@ -604,7 +604,7 @@ def read(self, size):
604604
def getcomptype(self):
605605
if self.buf.startswith(b"\037\213\010"):
606606
return "gz"
607-
if self.buf.startswith("BZh91"):
607+
if self.buf.startswith(b"BZh91"):
608608
return "bz2"
609609
return "tar"
610610

@@ -1108,7 +1108,7 @@ def _create_header(info, format, encoding, errors):
11081108

11091109
buf = struct.pack("%ds" % BLOCKSIZE, b"".join(parts))
11101110
chksum = calc_chksums(buf[-BLOCKSIZE:])[0]
1111-
buf = buf[:-364] + bytes("%06o\0" % chksum) + buf[-357:]
1111+
buf = buf[:-364] + bytes("%06o\0" % chksum, "ascii") + buf[-357:]
11121112
return buf
11131113

11141114
@staticmethod
@@ -1155,7 +1155,7 @@ def _create_pax_generic_header(cls, pax_headers, type):
11551155
if n == p:
11561156
break
11571157
p = n
1158-
records += bytes(str(p)) + b" " + keyword + b"=" + value + b"\n"
1158+
records += bytes(str(p), "ascii") + b" " + keyword + b"=" + value + b"\n"
11591159

11601160
# We use a hardcoded "././@PaxHeader" name like star does
11611161
# instead of the one that POSIX recommends.

Lib/test/test_tarfile.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ def test_tar_size(self):
481481
tar = tarfile.open(tmpname, self.mode)
482482
path = os.path.join(TEMPDIR, "file")
483483
fobj = open(path, "wb")
484-
fobj.write("aaa")
484+
fobj.write(b"aaa")
485485
fobj.close()
486486
tar.add(path)
487487
tar.close()
@@ -499,7 +499,7 @@ def test_file_size(self):
499499
self.assertEqual(tarinfo.size, 0)
500500

501501
fobj = open(path, "wb")
502-
fobj.write("aaa")
502+
fobj.write(b"aaa")
503503
fobj.close()
504504
tarinfo = tar.gettarinfo(path)
505505
self.assertEqual(tarinfo.size, 3)
@@ -603,7 +603,7 @@ def test_stream_padding(self):
603603
data = fobj.read()
604604
fobj.close()
605605

606-
self.assert_(data.count("\0") == tarfile.RECORDSIZE,
606+
self.assert_(data.count(b"\0") == tarfile.RECORDSIZE,
607607
"incorrect zero padding")
608608

609609

@@ -693,7 +693,7 @@ def setUp(self):
693693
self.bar = os.path.join(TEMPDIR, "bar")
694694

695695
fobj = open(self.foo, "wb")
696-
fobj.write("foo")
696+
fobj.write(b"foo")
697697
fobj.close()
698698

699699
os.link(self.foo, self.bar)

0 commit comments

Comments
 (0)