@@ -138,7 +138,6 @@ def randbits(k):
138138unicode = str
139139long = int
140140file = IOBase
141- PY2 = False
142141
143142text_type = str
144143bytes_type = bytes
@@ -706,7 +705,7 @@ def _get(section_dict, key, default=None):
706705__version_date__ = str(__version_date_info__[0]) + "." + str(
707706 __version_date_info__[1]).zfill(2) + "." + str(__version_date_info__[2]).zfill(2)
708707__revision__ = __version_info__[3]
709- __revision_id__ = "$Id$"
708+ __revision_id__ = "$Id: 53f125985089830620e38ced3a4588e2b5231674 $"
710709if(__version_info__[4] is not None):
711710 __version_date_plusrc__ = __version_date__ + \
712711 "-" + str(__version_date_info__[4])
@@ -1377,36 +1376,17 @@ def _normalize_initial_data(data, isbytes, encoding, errors=None):
13771376 b = _as_bytes_like(data)
13781377 if b is not None:
13791378 return b
1380- if PY2:
1381- # Py2: 'unicode' -> encode; 'str' is already bytes
1382- if isinstance(data, unicode_type):
1383- return data.encode(encoding, errors)
1384- if isinstance(data, str):
1385- return data
1386- else:
1387- # Py3: text -> encode
1388- if isinstance(data, str):
1389- return data.encode(encoding, errors)
1379+ if isinstance(data, str):
1380+ return data.encode(encoding, errors)
13901381 raise TypeError("data must be bytes-like or text for isbytes=True (got %r)" % (type(data),))
13911382 else:
13921383 # text mode
1393- if PY2:
1394- if isinstance(data, unicode_type):
1395- return data
1396- b = _as_bytes_like(data)
1397- if b is not None:
1398- return b.decode(encoding, errors)
1399- if isinstance(data, str):
1400- # Py2 str -> decode
1401- return data.decode(encoding, errors)
1402- raise TypeError("data must be unicode or bytes-like for text mode (got %r)" % (type(data),))
1403- else:
1404- if isinstance(data, str):
1405- return data
1406- b = _as_bytes_like(data)
1407- if b is not None:
1408- return b.decode(encoding, errors)
1409- raise TypeError("data must be str or bytes-like for text mode (got %r)" % (type(data),))
1384+ if isinstance(data, str):
1385+ return data
1386+ b = _as_bytes_like(data)
1387+ if b is not None:
1388+ return b.decode(encoding, errors)
1389+ raise TypeError("data must be str or bytes-like for text mode (got %r)" % (type(data),))
14101390
14111391
14121392def MkTempFile(data=None,
@@ -1888,17 +1868,12 @@ def alias_function(*args, **kwargs):
18881868 # Add the new function to the global namespace
18891869 globals()[function_name] = alias_function
18901870
1891- if PY2:
1892- binary_types = (str, bytearray, buffer) # noqa: F821 (buffer in Py2)
1893- else:
1894- binary_types = (bytes, bytearray, memoryview)
1871+ binary_types = (bytes, bytearray, memoryview)
18951872
18961873
18971874# ---------- Helpers (same semantics as your snippet) ----------
18981875def _byte_at(b, i):
18991876 """Return int value of byte at index i for both Py2 and Py3."""
1900- if PY2:
1901- return ord(b[i:i+1])
19021877 return b[i]
19031878
19041879def _is_valid_zlib_header(cmf, flg):
@@ -2071,23 +2046,20 @@ def read(self, size=-1):
20712046 size = 0
20722047
20732048 if size == 0:
2074- return b'' if not PY2 else ''
2049+ return b''
20752050
20762051 start, end_abs = self._region_bounds()
20772052 available = end_abs - (self._base_offset + self._pos)
20782053 if available <= 0:
2079- return b'' if not PY2 else ''
2054+ return b''
20802055
20812056 size = min(size, available)
20822057
20832058 abs_start = self._base_offset + self._pos
20842059 abs_end = abs_start + size
20852060
20862061 chunk = self._buf[abs_start:abs_end]
2087- if PY2:
2088- data = bytes(chunk) # bytes() -> str in py2
2089- else:
2090- data = bytes(chunk)
2062+ data = bytes(chunk)
20912063
20922064 self._pos += len(data)
20932065 return data
@@ -2105,7 +2077,7 @@ def readline(self, size=-1):
21052077 start, end_abs = self._region_bounds()
21062078 remaining = end_abs - (self._base_offset + self._pos)
21072079 if remaining <= 0:
2108- return b'' if not PY2 else ''
2080+ return b''
21092081
21102082 if size is not None and size >= 0:
21112083 size = int(size)
@@ -2117,10 +2089,7 @@ def readline(self, size=-1):
21172089 abs_max = abs_start + max_len
21182090
21192091 # Work on a local bytes slice for easy .find()
2120- if PY2:
2121- buf_bytes = bytes(self._buf[abs_start:abs_max])
2122- else:
2123- buf_bytes = bytes(self._buf[abs_start:abs_max])
2092+ buf_bytes = bytes(self._buf[abs_start:abs_max])
21242093
21252094 idx = buf_bytes.find(b'\n')
21262095 if idx == -1:
@@ -2131,8 +2100,6 @@ def readline(self, size=-1):
21312100
21322101 self._pos += len(line_bytes)
21332102
2134- if PY2:
2135- return line_bytes # already str
21362103 return line_bytes
21372104
21382105 def readinto(self, b):
@@ -2279,9 +2246,6 @@ def __next__(self):
22792246 raise StopIteration
22802247 return line
22812248
2282- if PY2:
2283- next = __next__
2284-
22852249 # ---------- misc helpers ----------
22862250
22872251 def fileno(self):
@@ -2325,8 +2289,6 @@ def __init__(self, file_path=None, fileobj=None, mode='rb', level=6, wbits=15,
23252289
23262290 if 'b' not in mode and 't' not in mode:
23272291 mode += 'b' # default to binary
2328- if 'x' in mode and PY2:
2329- raise ValueError("Exclusive creation mode 'x' is not supported on Python 2")
23302292
23312293 self.file_path = file_path
23322294 self.file = None
@@ -2545,9 +2507,6 @@ def __next__(self):
25452507 raise StopIteration
25462508 return line
25472509
2548- if PY2:
2549- next = __next__
2550-
25512510 def seek(self, offset, whence=0):
25522511 if self.closed:
25532512 raise ValueError("I/O operation on closed file")
@@ -2585,11 +2544,9 @@ def write(self, data):
25852544 if not isinstance(data, binary_types):
25862545 raise TypeError("write() expects bytes-like in binary mode")
25872546
2588- # Normalize to bytes for Py2/3 edge cases
2589- if (not PY2) and isinstance(data, memoryview):
2547+ # Normalize to bytes
2548+ if isinstance(data, memoryview):
25902549 data = data.tobytes()
2591- elif PY2 and isinstance(data, bytearray):
2592- data = bytes(data)
25932550
25942551 # Buffer and compress in chunks to limit memory
25952552 self._write_buf += data
@@ -2812,8 +2769,6 @@ def __init__(self, file_path=None, fileobj=None, mode='rb',
28122769
28132770 if 'b' not in mode and 't' not in mode:
28142771 mode += 'b'
2815- if 'x' in mode and PY2:
2816- raise ValueError("Exclusive creation mode 'x' not supported on Python 2")
28172772
28182773 self.file_path = file_path
28192774 self.file = fileobj
@@ -3013,9 +2968,6 @@ def __next__(self):
30132968 raise StopIteration
30142969 return line
30152970
3016- if PY2:
3017- next = __next__
3018-
30192971 def seek(self, offset, whence=0):
30202972 if self.closed:
30212973 raise ValueError("I/O operation on closed file")
@@ -3053,11 +3005,9 @@ def write(self, data):
30533005 if not isinstance(data, binary_types):
30543006 raise TypeError("write() expects bytes-like in binary mode")
30553007
3056- # Normalize Py3 memoryview and Py2 bytearray
3057- if (not PY2) and isinstance(data, memoryview):
3008+ # Normalize Py3 memoryview
3009+ if isinstance(data, memoryview):
30583010 data = data.tobytes()
3059- elif PY2 and isinstance(data, bytearray):
3060- data = bytes(data)
30613011
30623012 # Stage and compress in chunks
30633013 self._write_buf += data
@@ -8945,7 +8895,7 @@ def read(self, n=-1):
89458895 n = len(self._mm) - self._pos
89468896 end = min(self._pos + n, len(self._mm))
89478897 if end <= self._pos:
8948- return b"" if not PY2 else bytes_type()
8898+ return b""
89498899 out = bytes(self._mm[self._pos:end])
89508900 self._pos = end
89518901 return out
@@ -9008,9 +8958,6 @@ def __next__(self):
90088958 raise StopIteration
90098959 return line
90108960
9011- if PY2:
9012- next = __next__
9013-
90148961 # ---- writes ----
90158962 def write(self, b):
90168963 if not self._writable:
@@ -9440,7 +9387,7 @@ def CompressOpenFile(outfile, compressionenable=True, compressionlevel=None,
94409387
94419388 fbasename, fextname = os.path.splitext(outfile)
94429389 compressionlevel = 9 if compressionlevel is None else int(compressionlevel)
9443- mode = "w" if PY2 else " wb"
9390+ mode = "wb"
94449391
94459392 try:
94469393 # Uncompressed branch
@@ -9457,10 +9404,7 @@ def CompressOpenFile(outfile, compressionenable=True, compressionlevel=None,
94579404
94589405 # Compressed branches (unchanged openers; all wrapped)
94599406 elif (fextname == ".gz" and "gzip" in compressionsupport):
9460- if PY2:
9461- outfp = FileLikeAdapter(GzipFile(outfile, mode=mode, level=compressionlevel), mode="wb")
9462- else:
9463- outfp = FileLikeAdapter(gzip.open(outfile, mode, compressionlevel), mode="wb")
9407+ outfp = FileLikeAdapter(gzip.open(outfile, mode, compressionlevel), mode="wb")
94649408
94659409 elif (fextname == ".bz2" and "bzip2" in compressionsupport):
94669410 outfp = FileLikeAdapter(bz2.open(outfile, mode, compressionlevel), mode="wb")
0 commit comments