From 86b8152c6eb6f854be553f6aa36b51d240be816f Mon Sep 17 00:00:00 2001 From: Fangchen Li Date: Sat, 30 Jul 2022 23:06:42 -0700 Subject: [PATCH 1/6] Update pyupgrade to py37-plus --- .pre-commit-config.yaml | 2 +- packaging/_musllinux.py | 2 +- packaging/tags.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f0b033f3..db1483e0 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -19,7 +19,7 @@ repos: rev: v2.32.0 hooks: - id: pyupgrade - args: [--py36-plus] + args: [--py37-plus] - repo: https://github.com/psf/black rev: 22.3.0 diff --git a/packaging/_musllinux.py b/packaging/_musllinux.py index d5d3e044..ac2236a5 100644 --- a/packaging/_musllinux.py +++ b/packaging/_musllinux.py @@ -105,7 +105,7 @@ def _get_musl_version(executable: str) -> Optional[_MuslVersion]: ld = _parse_ld_musl_from_elf(f) if not ld: return None - proc = subprocess.run([ld], stderr=subprocess.PIPE, universal_newlines=True) + proc = subprocess.run([ld], stderr=subprocess.PIPE, text=True) return _parse_musl_version(proc.stderr) diff --git a/packaging/tags.py b/packaging/tags.py index 5b6c5ffd..976f3ef5 100644 --- a/packaging/tags.py +++ b/packaging/tags.py @@ -370,7 +370,7 @@ def mac_platforms( check=True, env={"SYSTEM_VERSION_COMPAT": "0"}, stdout=subprocess.PIPE, - universal_newlines=True, + text=True, ).stdout version = cast("MacVersion", tuple(map(int, version_str.split(".")[:2]))) else: From bdf3e08afca9eff18e17414f2cac5a70397c37b9 Mon Sep 17 00:00:00 2001 From: Fangchen Li Date: Fri, 12 Aug 2022 10:36:13 -0700 Subject: [PATCH 2/6] update test --- tests/test_musllinux.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_musllinux.py b/tests/test_musllinux.py index 2623bdbc..3968685b 100644 --- a/tests/test_musllinux.py +++ b/tests/test_musllinux.py @@ -139,7 +139,7 @@ def mock_run(*args, **kwargs): pretend.call( [ld_musl], stderr=subprocess.PIPE, - universal_newlines=True, + text=True, ) ] else: From 3b597ad4e10afe5a4a7738a0be0ba4b9795d812c Mon Sep 17 00:00:00 2001 From: Fangchen Li Date: Thu, 10 Aug 2023 13:48:30 -0700 Subject: [PATCH 3/6] rollback --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index db1483e0..f0b033f3 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -19,7 +19,7 @@ repos: rev: v2.32.0 hooks: - id: pyupgrade - args: [--py37-plus] + args: [--py36-plus] - repo: https://github.com/psf/black rev: 22.3.0 From b797a3e6a161d2f22f04c4f2ce64493ce1c24adc Mon Sep 17 00:00:00 2001 From: Fangchen Li Date: Thu, 10 Aug 2023 13:50:13 -0700 Subject: [PATCH 4/6] rollback --- tests/test_musllinux.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_musllinux.py b/tests/test_musllinux.py index 3968685b..2623bdbc 100644 --- a/tests/test_musllinux.py +++ b/tests/test_musllinux.py @@ -139,7 +139,7 @@ def mock_run(*args, **kwargs): pretend.call( [ld_musl], stderr=subprocess.PIPE, - text=True, + universal_newlines=True, ) ] else: From 27eff9f1488c6fde9500eae76313db360b24374a Mon Sep 17 00:00:00 2001 From: Fangchen Li Date: Thu, 10 Aug 2023 15:37:01 -0700 Subject: [PATCH 5/6] rm typo --- packaging/_musllinux.py | 138 ---------------------------------------- 1 file changed, 138 deletions(-) delete mode 100644 packaging/_musllinux.py diff --git a/packaging/_musllinux.py b/packaging/_musllinux.py deleted file mode 100644 index ac2236a5..00000000 --- a/packaging/_musllinux.py +++ /dev/null @@ -1,138 +0,0 @@ -"""PEP 656 support. - -This module implements logic to detect if the currently running Python is -linked against musl, and what musl version is used. -""" - -import contextlib -import functools -import operator -import os -import re -import struct -import subprocess -import sys -from typing import IO, Iterator, NamedTuple, Optional, Tuple - - -def _read_unpacked(f: IO[bytes], fmt: str) -> Tuple[int, ...]: - return struct.unpack(fmt, f.read(struct.calcsize(fmt))) - - -def _parse_ld_musl_from_elf(f: IO[bytes]) -> Optional[str]: - """Detect musl libc location by parsing the Python executable. - - Based on: https://gist.github.com/lyssdod/f51579ae8d93c8657a5564aefc2ffbca - ELF header: https://refspecs.linuxfoundation.org/elf/gabi4+/ch4.eheader.html - """ - f.seek(0) - try: - ident = _read_unpacked(f, "16B") - except struct.error: - return None - if ident[:4] != tuple(b"\x7fELF"): # Invalid magic, not ELF. - return None - f.seek(struct.calcsize("HHI"), 1) # Skip file type, machine, and version. - - try: - # e_fmt: Format for program header. - # p_fmt: Format for section header. - # p_idx: Indexes to find p_type, p_offset, and p_filesz. - e_fmt, p_fmt, p_idx = { - (1, 1): ("IIIIHHH", ">IIIIIIII", (0, 1, 4)), # 32-bit MSB. - (2, 1): ("QQQIHHH", ">IIQQQQQQ", (0, 2, 5)), # 64-bit MSB. - }[(ident[4], ident[5])] - except KeyError: - return None - else: - p_get = operator.itemgetter(*p_idx) - - # Find the interpreter section and return its content. - try: - _, e_phoff, _, _, _, e_phentsize, e_phnum = _read_unpacked(f, e_fmt) - except struct.error: - return None - for i in range(e_phnum + 1): - f.seek(e_phoff + e_phentsize * i) - try: - p_type, p_offset, p_filesz = p_get(_read_unpacked(f, p_fmt)) - except struct.error: - return None - if p_type != 3: # Not PT_INTERP. - continue - f.seek(p_offset) - interpreter = os.fsdecode(f.read(p_filesz)).strip("\0") - if "musl" not in interpreter: - return None - return interpreter - return None - - -class _MuslVersion(NamedTuple): - major: int - minor: int - - -def _parse_musl_version(output: str) -> Optional[_MuslVersion]: - lines = [n for n in (n.strip() for n in output.splitlines()) if n] - if len(lines) < 2 or lines[0][:4] != "musl": - return None - m = re.match(r"Version (\d+)\.(\d+)", lines[1]) - if not m: - return None - return _MuslVersion(major=int(m.group(1)), minor=int(m.group(2))) - - -@functools.lru_cache() -def _get_musl_version(executable: str) -> Optional[_MuslVersion]: - """Detect currently-running musl runtime version. - - This is done by checking the specified executable's dynamic linking - information, and invoking the loader to parse its output for a version - string. If the loader is musl, the output would be something like:: - - musl libc (x86_64) - Version 1.2.2 - Dynamic Program Loader - """ - with contextlib.ExitStack() as stack: - try: - f = stack.enter_context(open(executable, "rb")) - except OSError: - return None - ld = _parse_ld_musl_from_elf(f) - if not ld: - return None - proc = subprocess.run([ld], stderr=subprocess.PIPE, text=True) - return _parse_musl_version(proc.stderr) - - -def platform_tags(arch: str) -> Iterator[str]: - """Generate musllinux tags compatible to the current platform. - - :param arch: Should be the part of platform tag after the ``linux_`` - prefix, e.g. ``x86_64``. The ``linux_`` prefix is assumed as a - prerequisite for the current platform to be musllinux-compatible. - - :returns: An iterator of compatible musllinux tags. - """ - sys_musl = _get_musl_version(sys.executable) - if sys_musl is None: # Python not dynamically linked against musl. - return - for minor in range(sys_musl.minor, -1, -1): - yield f"musllinux_{sys_musl.major}_{minor}_{arch}" - - -if __name__ == "__main__": # pragma: no cover - import sysconfig - - plat = sysconfig.get_platform() - assert plat.startswith("linux-"), "not linux" - - print("plat:", plat) - print("musl:", _get_musl_version(sys.executable)) - print("tags:", end=" ") - for t in platform_tags(re.sub(r"[.-]", "_", plat.split("-", 1)[-1])): - print(t, end="\n ") From 3d9b9765c9ced2d774b6b723530230e32df1b35d Mon Sep 17 00:00:00 2001 From: Fangchen Li Date: Thu, 10 Aug 2023 15:38:34 -0700 Subject: [PATCH 6/6] update py37 setting --- .pre-commit-config.yaml | 2 +- src/packaging/_musllinux.py | 2 +- tests/test_musllinux.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8f449ffc..c03c4d43 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -19,7 +19,7 @@ repos: rev: v3.3.1 hooks: - id: pyupgrade - args: [--py36-plus] + args: [--py37-plus] - repo: https://github.com/psf/black rev: 22.12.0 diff --git a/src/packaging/_musllinux.py b/src/packaging/_musllinux.py index a71f85ee..86419df9 100644 --- a/src/packaging/_musllinux.py +++ b/src/packaging/_musllinux.py @@ -47,7 +47,7 @@ def _get_musl_version(executable: str) -> Optional[_MuslVersion]: return None if ld is None or "musl" not in ld: return None - proc = subprocess.run([ld], stderr=subprocess.PIPE, universal_newlines=True) + proc = subprocess.run([ld], stderr=subprocess.PIPE, text=True) return _parse_musl_version(proc.stderr) diff --git a/tests/test_musllinux.py b/tests/test_musllinux.py index c2ab8601..1a6a9a26 100644 --- a/tests/test_musllinux.py +++ b/tests/test_musllinux.py @@ -72,7 +72,7 @@ def mock_run(*args, **kwargs): pretend.call( [ld_musl], stderr=subprocess.PIPE, - universal_newlines=True, + text=True, ) ] else: