Remove has_flags conditions from src._pytest.assertion.rewrite._read_pyc

This commit is contained in:
Hasan Ramezani 2021-12-26 20:59:03 +01:00
parent 77042f77cc
commit b72ad0fa8e
1 changed files with 5 additions and 7 deletions

View File

@ -375,31 +375,29 @@ def _read_pyc(
except OSError: except OSError:
return None return None
with fp: with fp:
# https://www.python.org/dev/peps/pep-0552/
has_flags = sys.version_info >= (3, 7)
try: try:
stat_result = os.stat(source) stat_result = os.stat(source)
mtime = int(stat_result.st_mtime) mtime = int(stat_result.st_mtime)
size = stat_result.st_size size = stat_result.st_size
data = fp.read(16 if has_flags else 12) data = fp.read(16)
except OSError as e: except OSError as e:
trace(f"_read_pyc({source}): OSError {e}") trace(f"_read_pyc({source}): OSError {e}")
return None return None
# Check for invalid or out of date pyc file. # Check for invalid or out of date pyc file.
if len(data) != (16 if has_flags else 12): if len(data) != (16):
trace("_read_pyc(%s): invalid pyc (too short)" % source) trace("_read_pyc(%s): invalid pyc (too short)" % source)
return None return None
if data[:4] != importlib.util.MAGIC_NUMBER: if data[:4] != importlib.util.MAGIC_NUMBER:
trace("_read_pyc(%s): invalid pyc (bad magic number)" % source) trace("_read_pyc(%s): invalid pyc (bad magic number)" % source)
return None return None
if has_flags and data[4:8] != b"\x00\x00\x00\x00": if data[4:8] != b"\x00\x00\x00\x00":
trace("_read_pyc(%s): invalid pyc (unsupported flags)" % source) trace("_read_pyc(%s): invalid pyc (unsupported flags)" % source)
return None return None
mtime_data = data[8 if has_flags else 4 : 12 if has_flags else 8] mtime_data = data[8:12]
if int.from_bytes(mtime_data, "little") != mtime & 0xFFFFFFFF: if int.from_bytes(mtime_data, "little") != mtime & 0xFFFFFFFF:
trace("_read_pyc(%s): out of date" % source) trace("_read_pyc(%s): out of date" % source)
return None return None
size_data = data[12 if has_flags else 8 : 16 if has_flags else 12] size_data = data[12:16]
if int.from_bytes(size_data, "little") != size & 0xFFFFFFFF: if int.from_bytes(size_data, "little") != size & 0xFFFFFFFF:
trace("_read_pyc(%s): invalid pyc (incorrect size)" % source) trace("_read_pyc(%s): invalid pyc (incorrect size)" % source)
return None return None