diff options
| author | Alex Grönholm <alex.gronholm@nextday.fi> | 2021-12-24 22:56:58 +0200 |
|---|---|---|
| committer | Alex Grönholm <alex.gronholm@nextday.fi> | 2021-12-24 23:00:52 +0200 |
| commit | 0c061c3b3478ae2ebefa178fbf7624860eb231e9 (patch) | |
| tree | a2c40cc7d876524bb9b784d2dd29592c9966a945 | |
| parent | fdb6eca68c3c1e7258f9be5236f511827289947f (diff) | |
| download | wheel-git-0c061c3b3478ae2ebefa178fbf7624860eb231e9.tar.gz | |
Eliminated more py2 compatibility code
- Got rid of as_bytes(), as_unicode() and native()
- Eliminated the py2 code path in _update_crc()
| -rw-r--r-- | src/wheel/util.py | 20 | ||||
| -rw-r--r-- | src/wheel/wheelfile.py | 44 | ||||
| -rw-r--r-- | tests/test_wheelfile.py | 45 |
3 files changed, 37 insertions, 72 deletions
diff --git a/src/wheel/util.py b/src/wheel/util.py index 09f5b29..f17cafd 100644 --- a/src/wheel/util.py +++ b/src/wheel/util.py @@ -1,13 +1,6 @@ import base64 -def native(s, encoding="utf-8"): - if isinstance(s, bytes): - return s.decode(encoding) - else: - return s - - def urlsafe_b64encode(data): """urlsafe_b64encode without padding""" return base64.urlsafe_b64encode(data).rstrip(b"=") @@ -17,16 +10,3 @@ def urlsafe_b64decode(data): """urlsafe_b64decode without padding""" pad = b"=" * (4 - (len(data) & 3)) return base64.urlsafe_b64decode(data + pad) - - -def as_unicode(s): - if isinstance(s, bytes): - return s.decode("utf-8") - return s - - -def as_bytes(s): - if isinstance(s, str): - return s.encode("utf-8") - else: - return s diff --git a/src/wheel/wheelfile.py b/src/wheel/wheelfile.py index 568322f..e97ddd1 100644 --- a/src/wheel/wheelfile.py +++ b/src/wheel/wheelfile.py @@ -10,13 +10,7 @@ from io import StringIO, TextIOWrapper from zipfile import ZIP_DEFLATED, ZipFile, ZipInfo from wheel.cli import WheelError -from wheel.util import ( - as_bytes, - as_unicode, - native, - urlsafe_b64decode, - urlsafe_b64encode, -) +from wheel.util import urlsafe_b64decode, urlsafe_b64encode # Non-greedy matching of an optional build number may be too clever (more # invalid wheel filenames will match). Separate regex for .dist-info? @@ -93,18 +87,14 @@ class WheelFile(ZipFile): ) def open(self, name_or_info, mode="r", pwd=None): - def _update_crc(newdata, eof=None): - if eof is None: - eof = ef._eof - update_crc_orig(newdata) - else: # Python 2 - update_crc_orig(newdata, eof) - + def _update_crc(newdata): + eof = ef._eof + update_crc_orig(newdata) running_hash.update(newdata) if eof and running_hash.digest() != expected_hash: - raise WheelError(f"Hash mismatch for file '{native(ef_name)}'") + raise WheelError(f"Hash mismatch for file '{ef_name}'") - ef_name = as_unicode( + ef_name = ( name_or_info.filename if isinstance(name_or_info, ZipInfo) else name_or_info ) if ( @@ -112,7 +102,7 @@ class WheelFile(ZipFile): and not ef_name.endswith("/") and ef_name not in self._file_hashes ): - raise WheelError(f"No hash found for file '{native(ef_name)}'") + raise WheelError(f"No hash found for file '{ef_name}'") ef = ZipFile.open(self, name_or_info, mode, pwd) if mode == "r" and not ef_name.endswith("/"): @@ -159,8 +149,11 @@ class WheelFile(ZipFile): zinfo.compress_type = compress_type or self.compression self.writestr(zinfo, data, compress_type) - def writestr(self, zinfo_or_arcname, bytes, compress_type=None): - ZipFile.writestr(self, zinfo_or_arcname, bytes, compress_type) + def writestr(self, zinfo_or_arcname, data, compress_type=None): + if isinstance(data, str): + data = data.encode("utf-8") + + ZipFile.writestr(self, zinfo_or_arcname, data, compress_type) fname = ( zinfo_or_arcname.filename if isinstance(zinfo_or_arcname, ZipInfo) @@ -168,11 +161,12 @@ class WheelFile(ZipFile): ) logger.info("adding '%s'", fname) if fname != self.record_path: - hash_ = self._default_algorithm(bytes) - self._file_hashes[fname] = hash_.name, native( - urlsafe_b64encode(hash_.digest()) + hash_ = self._default_algorithm(data) + self._file_hashes[fname] = ( + hash_.name, + urlsafe_b64encode(hash_.digest()).decode("ascii"), ) - self._file_sizes[fname] = len(bytes) + self._file_sizes[fname] = len(data) def close(self): # Write RECORD @@ -186,9 +180,9 @@ class WheelFile(ZipFile): ) ) writer.writerow((format(self.record_path), "", "")) - zinfo = ZipInfo(native(self.record_path), date_time=get_zipinfo_datetime()) + zinfo = ZipInfo(self.record_path, date_time=get_zipinfo_datetime()) zinfo.compress_type = self.compression zinfo.external_attr = 0o664 << 16 - self.writestr(zinfo, as_bytes(data.getvalue())) + self.writestr(zinfo, data.getvalue()) ZipFile.close(self) diff --git a/tests/test_wheelfile.py b/tests/test_wheelfile.py index 25dc472..b6e4eb2 100644 --- a/tests/test_wheelfile.py +++ b/tests/test_wheelfile.py @@ -4,7 +4,6 @@ from zipfile import ZIP_DEFLATED, ZipFile import pytest from wheel.cli import WheelError -from wheel.util import as_bytes, native from wheel.wheelfile import WheelFile @@ -37,7 +36,7 @@ def test_bad_wheel_filename(filename): def test_missing_record(wheel_path): with ZipFile(wheel_path, "w") as zf: - zf.writestr(native("hello/héllö.py"), as_bytes('print("Héllö, w0rld!")\n')) + zf.writestr("hello/héllö.py", 'print("Héllö, w0rld!")\n') exc = pytest.raises(WheelError, WheelFile, wheel_path) exc.match("^Missing test-1.0.dist-info/RECORD file$") @@ -45,12 +44,10 @@ def test_missing_record(wheel_path): def test_unsupported_hash_algorithm(wheel_path): with ZipFile(wheel_path, "w") as zf: - zf.writestr(native("hello/héllö.py"), as_bytes('print("Héllö, w0rld!")\n')) + zf.writestr("hello/héllö.py", 'print("Héllö, w0rld!")\n') zf.writestr( "test-1.0.dist-info/RECORD", - as_bytes( - "hello/héllö.py,sha000=bv-QV3RciQC2v3zL8Uvhd_arp40J5A9xmyubN34OVwo,25" - ), + "hello/héllö.py,sha000=bv-QV3RciQC2v3zL8Uvhd_arp40J5A9xmyubN34OVwo,25", ) exc = pytest.raises(WheelError, WheelFile, wheel_path) @@ -65,10 +62,8 @@ def test_unsupported_hash_algorithm(wheel_path): def test_weak_hash_algorithm(wheel_path, algorithm, digest): hash_string = f"{algorithm}={digest}" with ZipFile(wheel_path, "w") as zf: - zf.writestr(native("hello/héllö.py"), as_bytes('print("Héllö, w0rld!")\n')) - zf.writestr( - "test-1.0.dist-info/RECORD", as_bytes(f"hello/héllö.py,{hash_string},25") - ) + zf.writestr("hello/héllö.py", 'print("Héllö, w0rld!")\n') + zf.writestr("test-1.0.dist-info/RECORD", f"hello/héllö.py,{hash_string},25") exc = pytest.raises(WheelError, WheelFile, wheel_path) exc.match(fr"^Weak hash algorithm \({algorithm}\) is not permitted by PEP 427$") @@ -90,10 +85,8 @@ def test_weak_hash_algorithm(wheel_path, algorithm, digest): def test_testzip(wheel_path, algorithm, digest): hash_string = f"{algorithm}={digest}" with ZipFile(wheel_path, "w") as zf: - zf.writestr(native("hello/héllö.py"), as_bytes('print("Héllö, world!")\n')) - zf.writestr( - "test-1.0.dist-info/RECORD", as_bytes(f"hello/héllö.py,{hash_string},25") - ) + zf.writestr("hello/héllö.py", 'print("Héllö, world!")\n') + zf.writestr("test-1.0.dist-info/RECORD", f"hello/héllö.py,{hash_string},25") with WheelFile(wheel_path) as wf: wf.testzip() @@ -101,45 +94,43 @@ def test_testzip(wheel_path, algorithm, digest): def test_testzip_missing_hash(wheel_path): with ZipFile(wheel_path, "w") as zf: - zf.writestr(native("hello/héllö.py"), as_bytes('print("Héllö, world!")\n')) + zf.writestr("hello/héllö.py", 'print("Héllö, world!")\n') zf.writestr("test-1.0.dist-info/RECORD", "") with WheelFile(wheel_path) as wf: exc = pytest.raises(WheelError, wf.testzip) - exc.match(native("^No hash found for file 'hello/héllö.py'$")) + exc.match("^No hash found for file 'hello/héllö.py'$") def test_testzip_bad_hash(wheel_path): with ZipFile(wheel_path, "w") as zf: - zf.writestr(native("hello/héllö.py"), as_bytes('print("Héllö, w0rld!")\n')) + zf.writestr("hello/héllö.py", 'print("Héllö, w0rld!")\n') zf.writestr( "test-1.0.dist-info/RECORD", - as_bytes( - "hello/héllö.py,sha256=bv-QV3RciQC2v3zL8Uvhd_arp40J5A9xmyubN34OVwo,25" - ), + "hello/héllö.py,sha256=bv-QV3RciQC2v3zL8Uvhd_arp40J5A9xmyubN34OVwo,25", ) with WheelFile(wheel_path) as wf: exc = pytest.raises(WheelError, wf.testzip) - exc.match(native("^Hash mismatch for file 'hello/héllö.py'$")) + exc.match("^Hash mismatch for file 'hello/héllö.py'$") def test_write_str(wheel_path): with WheelFile(wheel_path, "w") as wf: - wf.writestr(native("hello/héllö.py"), as_bytes('print("Héllö, world!")\n')) - wf.writestr(native("hello/h,ll,.py"), as_bytes('print("Héllö, world!")\n')) + wf.writestr("hello/héllö.py", 'print("Héllö, world!")\n') + wf.writestr("hello/h,ll,.py", 'print("Héllö, world!")\n') with ZipFile(wheel_path, "r") as zf: infolist = zf.infolist() assert len(infolist) == 3 - assert infolist[0].filename == native("hello/héllö.py") + assert infolist[0].filename == "hello/héllö.py" assert infolist[0].file_size == 25 - assert infolist[1].filename == native("hello/h,ll,.py") + assert infolist[1].filename == "hello/h,ll,.py" assert infolist[1].file_size == 25 assert infolist[2].filename == "test-1.0.dist-info/RECORD" record = zf.read("test-1.0.dist-info/RECORD") - assert record == as_bytes( + assert record.decode("utf-8") == ( "hello/héllö.py,sha256=bv-QV3RciQC2v3zL8Uvhd_arp40J5A9xmyubN34OVwo,25\n" '"hello/h,ll,.py",sha256=bv-QV3RciQC2v3zL8Uvhd_arp40J5A9xmyubN34OVwo,25\n' "test-1.0.dist-info/RECORD,,\n" @@ -154,7 +145,7 @@ def test_timestamp(tmpdir_factory, wheel_path, monkeypatch): build_dir.join(filename).write(filename + "\n") # The earliest date representable in TarInfos, 1980-01-01 - monkeypatch.setenv(native("SOURCE_DATE_EPOCH"), native("315576060")) + monkeypatch.setenv("SOURCE_DATE_EPOCH", "315576060") with WheelFile(wheel_path, "w") as wf: wf.write_files(str(build_dir)) |
