Skip to content

Commit 0efbf03

Browse files
authored
Add files via upload
1 parent 6fe484d commit 0efbf03

1 file changed

Lines changed: 22 additions & 78 deletions

File tree

pycatfile_py3.py

Lines changed: 22 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ def randbits(k):
138138
unicode = str
139139
long = int
140140
file = IOBase
141-
PY2 = False
142141

143142
text_type = str
144143
bytes_type = bytes
@@ -705,7 +704,7 @@ def _get(section_dict, key, default=None):
705704
__version_date__ = str(__version_date_info__[0]) + "." + str(
706705
__version_date_info__[1]).zfill(2) + "." + str(__version_date_info__[2]).zfill(2)
707706
__revision__ = __version_info__[3]
708-
__revision_id__ = "$Id$"
707+
__revision_id__ = "$Id: bb61609918b298a313c64211b6e31f22719f07bc $"
709708
if(__version_info__[4] is not None):
710709
__version_date_plusrc__ = __version_date__ + \
711710
"-" + str(__version_date_info__[4])
@@ -1376,36 +1375,17 @@ def _normalize_initial_data(data, isbytes, encoding, errors=None):
13761375
b = _as_bytes_like(data)
13771376
if b is not None:
13781377
return b
1379-
if PY2:
1380-
# Py2: 'unicode' -> encode; 'str' is already bytes
1381-
if isinstance(data, unicode_type):
1382-
return data.encode(encoding, errors)
1383-
if isinstance(data, str):
1384-
return data
1385-
else:
1386-
# Py3: text -> encode
1387-
if isinstance(data, str):
1388-
return data.encode(encoding, errors)
1378+
if isinstance(data, str):
1379+
return data.encode(encoding, errors)
13891380
raise TypeError("data must be bytes-like or text for isbytes=True (got %r)" % (type(data),))
13901381
else:
13911382
# text mode
1392-
if PY2:
1393-
if isinstance(data, unicode_type):
1394-
return data
1395-
b = _as_bytes_like(data)
1396-
if b is not None:
1397-
return b.decode(encoding, errors)
1398-
if isinstance(data, str):
1399-
# Py2 str -> decode
1400-
return data.decode(encoding, errors)
1401-
raise TypeError("data must be unicode or bytes-like for text mode (got %r)" % (type(data),))
1402-
else:
1403-
if isinstance(data, str):
1404-
return data
1405-
b = _as_bytes_like(data)
1406-
if b is not None:
1407-
return b.decode(encoding, errors)
1408-
raise TypeError("data must be str or bytes-like for text mode (got %r)" % (type(data),))
1383+
if isinstance(data, str):
1384+
return data
1385+
b = _as_bytes_like(data)
1386+
if b is not None:
1387+
return b.decode(encoding, errors)
1388+
raise TypeError("data must be str or bytes-like for text mode (got %r)" % (type(data),))
14091389

14101390

14111391
def MkTempFile(data=None,
@@ -1887,17 +1867,12 @@ def alias_function(*args, **kwargs):
18871867
# Add the new function to the global namespace
18881868
globals()[function_name] = alias_function
18891869

1890-
if PY2:
1891-
binary_types = (str, bytearray, buffer) # noqa: F821 (buffer in Py2)
1892-
else:
1893-
binary_types = (bytes, bytearray, memoryview)
1870+
binary_types = (bytes, bytearray, memoryview)
18941871

18951872

18961873
# ---------- Helpers (same semantics as your snippet) ----------
18971874
def _byte_at(b, i):
18981875
"""Return int value of byte at index i for both Py2 and Py3."""
1899-
if PY2:
1900-
return ord(b[i:i+1])
19011876
return b[i]
19021877

19031878
def _is_valid_zlib_header(cmf, flg):
@@ -2070,23 +2045,20 @@ def read(self, size=-1):
20702045
size = 0
20712046

20722047
if size == 0:
2073-
return b'' if not PY2 else ''
2048+
return b''
20742049

20752050
start, end_abs = self._region_bounds()
20762051
available = end_abs - (self._base_offset + self._pos)
20772052
if available <= 0:
2078-
return b'' if not PY2 else ''
2053+
return b''
20792054

20802055
size = min(size, available)
20812056

20822057
abs_start = self._base_offset + self._pos
20832058
abs_end = abs_start + size
20842059

20852060
chunk = self._buf[abs_start:abs_end]
2086-
if PY2:
2087-
data = bytes(chunk) # bytes() -> str in py2
2088-
else:
2089-
data = bytes(chunk)
2061+
data = bytes(chunk)
20902062

20912063
self._pos += len(data)
20922064
return data
@@ -2104,7 +2076,7 @@ def readline(self, size=-1):
21042076
start, end_abs = self._region_bounds()
21052077
remaining = end_abs - (self._base_offset + self._pos)
21062078
if remaining <= 0:
2107-
return b'' if not PY2 else ''
2079+
return b''
21082080

21092081
if size is not None and size >= 0:
21102082
size = int(size)
@@ -2116,10 +2088,7 @@ def readline(self, size=-1):
21162088
abs_max = abs_start + max_len
21172089

21182090
# Work on a local bytes slice for easy .find()
2119-
if PY2:
2120-
buf_bytes = bytes(self._buf[abs_start:abs_max])
2121-
else:
2122-
buf_bytes = bytes(self._buf[abs_start:abs_max])
2091+
buf_bytes = bytes(self._buf[abs_start:abs_max])
21232092

21242093
idx = buf_bytes.find(b'\n')
21252094
if idx == -1:
@@ -2130,8 +2099,6 @@ def readline(self, size=-1):
21302099

21312100
self._pos += len(line_bytes)
21322101

2133-
if PY2:
2134-
return line_bytes # already str
21352102
return line_bytes
21362103

21372104
def readinto(self, b):
@@ -2278,9 +2245,6 @@ def __next__(self):
22782245
raise StopIteration
22792246
return line
22802247

2281-
if PY2:
2282-
next = __next__
2283-
22842248
# ---------- misc helpers ----------
22852249

22862250
def fileno(self):
@@ -2324,8 +2288,6 @@ def __init__(self, file_path=None, fileobj=None, mode='rb', level=6, wbits=15,
23242288

23252289
if 'b' not in mode and 't' not in mode:
23262290
mode += 'b' # default to binary
2327-
if 'x' in mode and PY2:
2328-
raise ValueError("Exclusive creation mode 'x' is not supported on Python 2")
23292291

23302292
self.file_path = file_path
23312293
self.file = None
@@ -2544,9 +2506,6 @@ def __next__(self):
25442506
raise StopIteration
25452507
return line
25462508

2547-
if PY2:
2548-
next = __next__
2549-
25502509
def seek(self, offset, whence=0):
25512510
if self.closed:
25522511
raise ValueError("I/O operation on closed file")
@@ -2584,11 +2543,9 @@ def write(self, data):
25842543
if not isinstance(data, binary_types):
25852544
raise TypeError("write() expects bytes-like in binary mode")
25862545

2587-
# Normalize to bytes for Py2/3 edge cases
2588-
if (not PY2) and isinstance(data, memoryview):
2546+
# Normalize to bytes
2547+
if isinstance(data, memoryview):
25892548
data = data.tobytes()
2590-
elif PY2 and isinstance(data, bytearray):
2591-
data = bytes(data)
25922549

25932550
# Buffer and compress in chunks to limit memory
25942551
self._write_buf += data
@@ -2811,8 +2768,6 @@ def __init__(self, file_path=None, fileobj=None, mode='rb',
28112768

28122769
if 'b' not in mode and 't' not in mode:
28132770
mode += 'b'
2814-
if 'x' in mode and PY2:
2815-
raise ValueError("Exclusive creation mode 'x' not supported on Python 2")
28162771

28172772
self.file_path = file_path
28182773
self.file = fileobj
@@ -3012,9 +2967,6 @@ def __next__(self):
30122967
raise StopIteration
30132968
return line
30142969

3015-
if PY2:
3016-
next = __next__
3017-
30182970
def seek(self, offset, whence=0):
30192971
if self.closed:
30202972
raise ValueError("I/O operation on closed file")
@@ -3052,11 +3004,9 @@ def write(self, data):
30523004
if not isinstance(data, binary_types):
30533005
raise TypeError("write() expects bytes-like in binary mode")
30543006

3055-
# Normalize Py3 memoryview and Py2 bytearray
3056-
if (not PY2) and isinstance(data, memoryview):
3007+
# Normalize Py3 memoryview
3008+
if isinstance(data, memoryview):
30573009
data = data.tobytes()
3058-
elif PY2 and isinstance(data, bytearray):
3059-
data = bytes(data)
30603010

30613011
# Stage and compress in chunks
30623012
self._write_buf += data
@@ -8944,7 +8894,7 @@ def read(self, n=-1):
89448894
n = len(self._mm) - self._pos
89458895
end = min(self._pos + n, len(self._mm))
89468896
if end <= self._pos:
8947-
return b"" if not PY2 else bytes_type()
8897+
return b""
89488898
out = bytes(self._mm[self._pos:end])
89498899
self._pos = end
89508900
return out
@@ -9007,9 +8957,6 @@ def __next__(self):
90078957
raise StopIteration
90088958
return line
90098959

9010-
if PY2:
9011-
next = __next__
9012-
90138960
# ---- writes ----
90148961
def write(self, b):
90158962
if not self._writable:
@@ -9439,7 +9386,7 @@ def CompressOpenFile(outfile, compressionenable=True, compressionlevel=None,
94399386

94409387
fbasename, fextname = os.path.splitext(outfile)
94419388
compressionlevel = 9 if compressionlevel is None else int(compressionlevel)
9442-
mode = "w" if PY2 else "wb"
9389+
mode = "wb"
94439390

94449391
try:
94459392
# Uncompressed branch
@@ -9456,10 +9403,7 @@ def CompressOpenFile(outfile, compressionenable=True, compressionlevel=None,
94569403

94579404
# Compressed branches (unchanged openers; all wrapped)
94589405
elif (fextname == ".gz" and "gzip" in compressionsupport):
9459-
if PY2:
9460-
outfp = FileLikeAdapter(GzipFile(outfile, mode=mode, level=compressionlevel), mode="wb")
9461-
else:
9462-
outfp = FileLikeAdapter(gzip.open(outfile, mode, compressionlevel), mode="wb")
9406+
outfp = FileLikeAdapter(gzip.open(outfile, mode, compressionlevel), mode="wb")
94639407

94649408
elif (fextname == ".bz2" and "bzip2" in compressionsupport):
94659409
outfp = FileLikeAdapter(bz2.open(outfile, mode, compressionlevel), mode="wb")

0 commit comments

Comments
 (0)