[Python-checkins] cpython: Use sequence repetition instead of bytes constructor with integer argument.

serhiy.storchaka python-checkins at python.org
Sun Sep 11 07:41:18 EDT 2016


https://hg.python.org/cpython/rev/aac69574afde
changeset:   103620:aac69574afde
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sun Sep 11 14:41:02 2016 +0300
summary:
  Use sequence repetition instead of bytes constructor with integer argument.

files:
  Lib/base64.py                 |   2 +-
  Lib/gzip.py                   |   4 ++--
  Lib/hmac.py                   |   2 +-
  Lib/pickletools.py            |   2 +-
  Lib/test/test_bufio.py        |   2 +-
  Lib/test/test_bz2.py          |   4 ++--
  Lib/test/test_gzip.py         |   4 ++--
  Lib/test/test_io.py           |   2 +-
  Lib/test/test_ipaddress.py    |  10 +++++-----
  Lib/test/test_lzma.py         |   4 ++--
  Lib/test/test_socketserver.py |   2 +-
  Lib/test/test_wsgiref.py      |   2 +-
  12 files changed, 20 insertions(+), 20 deletions(-)


diff --git a/Lib/base64.py b/Lib/base64.py
--- a/Lib/base64.py
+++ b/Lib/base64.py
@@ -155,7 +155,7 @@
     leftover = len(s) % 5
     # Pad the last quantum with zero bits if necessary
     if leftover:
-        s = s + bytes(5 - leftover)  # Don't use += !
+        s = s + b'\0' * (5 - leftover)  # Don't use += !
     encoded = bytearray()
     from_bytes = int.from_bytes
     b32tab2 = _b32tab2
diff --git a/Lib/gzip.py b/Lib/gzip.py
--- a/Lib/gzip.py
+++ b/Lib/gzip.py
@@ -357,10 +357,10 @@
             if offset < self.offset:
                 raise OSError('Negative seek in write mode')
             count = offset - self.offset
-            chunk = bytes(1024)
+            chunk = b'\0' * 1024
             for i in range(count // 1024):
                 self.write(chunk)
-            self.write(bytes(count % 1024))
+            self.write(b'\0' * (count % 1024))
         elif self.mode == READ:
             self._check_not_closed()
             return self._buffer.seek(offset, whence)
diff --git a/Lib/hmac.py b/Lib/hmac.py
--- a/Lib/hmac.py
+++ b/Lib/hmac.py
@@ -77,7 +77,7 @@
         if len(key) > blocksize:
             key = self.digest_cons(key).digest()
 
-        key = key + bytes(blocksize - len(key))
+        key = key.ljust(blocksize, b'\0')
         self.outer.update(key.translate(trans_5C))
         self.inner.update(key.translate(trans_36))
         if msg is not None:
diff --git a/Lib/pickletools.py b/Lib/pickletools.py
--- a/Lib/pickletools.py
+++ b/Lib/pickletools.py
@@ -707,7 +707,7 @@
     >>> enc = s.encode('utf-8')
     >>> enc
     b'abcd\xea\xaf\x8d'
-    >>> n = bytes([len(enc)]) + bytes(7)  # little-endian 8-byte length
+    >>> n = bytes([len(enc)]) + b'\0' * 7  # little-endian 8-byte length
     >>> t = read_unicodestring8(io.BytesIO(n + enc + b'junk'))
     >>> s == t
     True
diff --git a/Lib/test/test_bufio.py b/Lib/test/test_bufio.py
--- a/Lib/test/test_bufio.py
+++ b/Lib/test/test_bufio.py
@@ -59,7 +59,7 @@
         self.drive_one(b"1234567890\00\01\02\03\04\05\06")
 
     def test_nullpat(self):
-        self.drive_one(bytes(1000))
+        self.drive_one(b'\0' * 1000)
 
 
 class CBufferSizeTest(BufferSizeTest, unittest.TestCase):
diff --git a/Lib/test/test_bz2.py b/Lib/test/test_bz2.py
--- a/Lib/test/test_bz2.py
+++ b/Lib/test/test_bz2.py
@@ -562,11 +562,11 @@
 
     def testDecompressLimited(self):
         """Decompressed data buffering should be limited"""
-        bomb = bz2.compress(bytes(int(2e6)), compresslevel=9)
+        bomb = bz2.compress(b'\0' * int(2e6), compresslevel=9)
         self.assertLess(len(bomb), _compression.BUFFER_SIZE)
 
         decomp = BZ2File(BytesIO(bomb))
-        self.assertEqual(bytes(1), decomp.read(1))
+        self.assertEqual(decomp.read(1), b'\0')
         max_decomp = 1 + DEFAULT_BUFFER_SIZE
         self.assertLessEqual(decomp._buffer.raw.tell(), max_decomp,
             "Excessive amount of data was decompressed")
diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py
--- a/Lib/test/test_gzip.py
+++ b/Lib/test/test_gzip.py
@@ -434,12 +434,12 @@
 
     def test_decompress_limited(self):
         """Decompressed data buffering should be limited"""
-        bomb = gzip.compress(bytes(int(2e6)), compresslevel=9)
+        bomb = gzip.compress(b'\0' * int(2e6), compresslevel=9)
         self.assertLess(len(bomb), io.DEFAULT_BUFFER_SIZE)
 
         bomb = io.BytesIO(bomb)
         decomp = gzip.GzipFile(fileobj=bomb)
-        self.assertEqual(bytes(1), decomp.read(1))
+        self.assertEqual(decomp.read(1), b'\0')
         max_decomp = 1 + io.DEFAULT_BUFFER_SIZE
         self.assertLessEqual(decomp._buffer.raw.tell(), max_decomp,
             "Excessive amount of data was decompressed")
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -1812,7 +1812,7 @@
             with self.subTest(method):
                 pair = self.tp(self.BytesIO(b"abcdef"), self.MockRawIO())
 
-                data = byteslike(5)
+                data = byteslike(b'\0' * 5)
                 self.assertEqual(getattr(pair, method)(data), 5)
                 self.assertEqual(bytes(data), b"abcde")
 
diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py
--- a/Lib/test/test_ipaddress.py
+++ b/Lib/test/test_ipaddress.py
@@ -119,7 +119,7 @@
 
     def test_bad_packed_length(self):
         def assertBadLength(length):
-            addr = bytes(length)
+            addr = b'\0' * length
             msg = "%r (len %d != 4) is not permitted as an IPv4 address"
             with self.assertAddressError(re.escape(msg % (addr, length))):
                 self.factory(addr)
@@ -139,11 +139,11 @@
         self.assertInstancesEqual(3232235521, "::c0a8:1")
 
     def test_packed(self):
-        addr = bytes(12) + bytes.fromhex("00000000")
+        addr = b'\0'*12 + bytes.fromhex("00000000")
         self.assertInstancesEqual(addr, "::")
-        addr = bytes(12) + bytes.fromhex("c0a80001")
+        addr = b'\0'*12 + bytes.fromhex("c0a80001")
         self.assertInstancesEqual(addr, "::c0a8:1")
-        addr = bytes.fromhex("c0a80001") + bytes(12)
+        addr = bytes.fromhex("c0a80001") + b'\0'*12
         self.assertInstancesEqual(addr, "c0a8:1::")
 
     def test_negative_ints_rejected(self):
@@ -158,7 +158,7 @@
 
     def test_bad_packed_length(self):
         def assertBadLength(length):
-            addr = bytes(length)
+            addr = b'\0' * length
             msg = "%r (len %d != 16) is not permitted as an IPv6 address"
             with self.assertAddressError(re.escape(msg % (addr, length))):
                 self.factory(addr)
diff --git a/Lib/test/test_lzma.py b/Lib/test/test_lzma.py
--- a/Lib/test/test_lzma.py
+++ b/Lib/test/test_lzma.py
@@ -928,11 +928,11 @@
 
     def test_decompress_limited(self):
         """Decompressed data buffering should be limited"""
-        bomb = lzma.compress(bytes(int(2e6)), preset=6)
+        bomb = lzma.compress(b'\0' * int(2e6), preset=6)
         self.assertLess(len(bomb), _compression.BUFFER_SIZE)
 
         decomp = LZMAFile(BytesIO(bomb))
-        self.assertEqual(bytes(1), decomp.read(1))
+        self.assertEqual(decomp.read(1), b'\0')
         max_decomp = 1 + DEFAULT_BUFFER_SIZE
         self.assertLessEqual(decomp._buffer.raw.tell(), max_decomp,
             "Excessive amount of data was decompressed")
diff --git a/Lib/test/test_socketserver.py b/Lib/test/test_socketserver.py
--- a/Lib/test/test_socketserver.py
+++ b/Lib/test/test_socketserver.py
@@ -406,7 +406,7 @@
                 self.server.sent1 = self.wfile.write(b'write data\n')
                 # Should be sent immediately, without requiring flush()
                 self.server.received = self.rfile.readline()
-                big_chunk = bytes(test.support.SOCK_MAX_SIZE)
+                big_chunk = b'\0' * test.support.SOCK_MAX_SIZE
                 self.server.sent2 = self.wfile.write(big_chunk)
 
         server = socketserver.TCPServer((HOST, 0), Handler)
diff --git a/Lib/test/test_wsgiref.py b/Lib/test/test_wsgiref.py
--- a/Lib/test/test_wsgiref.py
+++ b/Lib/test/test_wsgiref.py
@@ -258,7 +258,7 @@
 
         def app(environ, start_response):
             start_response("200 OK", [])
-            return [bytes(support.SOCK_MAX_SIZE)]
+            return [b'\0' * support.SOCK_MAX_SIZE]
 
         class WsgiHandler(NoLogRequestHandler, WSGIRequestHandler):
             pass

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list