Skip to content

Commit 52d36ef

Browse files
rahuldevikarryukofcpre-commit-ci[bot]
authored
export normalize_isa and deprecate KNOWN_ARCHITECTURES (#62)
- Add `normalize_isa()` to the public API, which normalizes ISA strings to canonical form (e.g. `amd64` → `x86_64`, `aarch64` → `arm64`) and passes unrecognized values through lowercased — making it safe for any platform without requiring an allowlist - Deprecate `KNOWN_ARCHITECTURES` — the constant remains exported for backwards compatibility but consumers should migrate to `normalize_isa()` - Relax `test_py_info_machine_property` to assert the machine value is lowercase instead of requiring membership in `KNOWN_ARCHITECTURES`, fixing test failures on platforms like `alpha` or `armv8l` Fixes: #59 --------- Co-authored-by: Rahul Devikar <radevika@microsoft.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 993fced commit 52d36ef

File tree

6 files changed

+47
-7
lines changed

6 files changed

+47
-7
lines changed

docs/changelog/59.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export normalize_isa and deprecate KNOWN_ARCHITECTURES - by :user:`rahuldevikar`.

docs/conf.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,19 @@
2121
"sphinxcontrib.mermaid",
2222
]
2323

24+
extlinks = {
25+
"issue": ("https://github.com/tox-dev/python-discovery/issues/%s", "#%s"),
26+
"pull": ("https://github.com/tox-dev/python-discovery/pull/%s", "PR #%s"),
27+
"user": ("https://github.com/%s", "@%s"),
28+
}
29+
2430
intersphinx_mapping = {
2531
"python": ("https://docs.python.org/3", None),
2632
}
2733

2834
templates_path = []
2935
source_suffix = ".rst"
30-
exclude_patterns = ["_build"]
36+
exclude_patterns = ["_build", "changelog/*.rst"]
3137

3238
main_doc = "index"
3339
pygments_style = "default"

src/python_discovery/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from ._cache import ContentStore, DiskCache, PyInfoCache
88
from ._discovery import get_interpreter
9-
from ._py_info import KNOWN_ARCHITECTURES, PythonInfo
9+
from ._py_info import KNOWN_ARCHITECTURES, PythonInfo, normalize_isa
1010
from ._py_spec import PythonSpec
1111
from ._specifier import SimpleSpecifier, SimpleSpecifierSet, SimpleVersion
1212

@@ -24,4 +24,5 @@
2424
"SimpleVersion",
2525
"__version__",
2626
"get_interpreter",
27+
"normalize_isa",
2728
]

src/python_discovery/_py_info.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -770,21 +770,43 @@ def _possible_base(self) -> Generator[str, None, None]:
770770

771771
KNOWN_ARCHITECTURES: frozenset[str] = frozenset({
772772
"arm64",
773-
"i686",
774773
"loongarch64",
774+
"ppc",
775775
"ppc64",
776776
"ppc64le",
777777
"riscv64",
778778
"s390x",
779+
"sparc64",
779780
"x86",
780781
"x86_64",
781782
})
782-
"""Known CPU architecture (ISA) values after normalization."""
783+
"""Known CPU architecture (ISA) values after normalization.
784+
785+
.. deprecated::
786+
Use :func:`normalize_isa` instead, which handles both known and unknown architectures.
787+
"""
783788

784789

785790
def normalize_isa(isa: str) -> str:
791+
"""
792+
Normalize an ISA (instruction set architecture) string to a canonical form.
793+
794+
Known aliases are mapped (e.g. ``amd64`` → ``x86_64``, ``aarch64`` → ``arm64``).
795+
Unrecognized values are lowercased and returned as-is.
796+
"""
786797
low = isa.lower()
787-
return {"amd64": "x86_64", "aarch64": "arm64", "i386": "x86", "i586": "x86"}.get(low, low)
798+
return {
799+
"amd64": "x86_64",
800+
"aarch64": "arm64",
801+
"i386": "x86",
802+
"i486": "x86",
803+
"i586": "x86",
804+
"i686": "x86",
805+
"powerpc": "ppc",
806+
"powerpc64": "ppc64",
807+
"powerpc64le": "ppc64le",
808+
"sparcv9": "sparc64",
809+
}.get(low, low)
788810

789811

790812
def _main() -> None: # pragma: no cover

tests/py_info/test_py_info.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import pytest
1515
from setuptools.dist import Distribution
1616

17-
from python_discovery import KNOWN_ARCHITECTURES, DiskCache, PythonInfo, PythonSpec
17+
from python_discovery import DiskCache, PythonInfo, PythonSpec
1818
from python_discovery import _cached_py_info as cached_py_info
1919
from python_discovery._py_info import VersionInfo
2020

@@ -338,7 +338,7 @@ def test_py_info_machine_property() -> None:
338338
assert machine is not None
339339
assert isinstance(machine, str)
340340
assert len(machine) > 0
341-
assert machine in KNOWN_ARCHITECTURES, f"unexpected machine value: {machine}"
341+
assert machine == machine.lower(), f"machine value should be lowercase: {machine}"
342342

343343

344344
def test_py_info_machine_in_spec() -> None:

tests/test_py_spec.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,19 @@ def test_spec_satisfies_machine(left: str, right: str, expected: bool) -> None:
217217
pytest.param("x86_64", "x86_64", id="x86_64"),
218218
pytest.param("arm64", "arm64", id="arm64"),
219219
pytest.param("x86", "x86", id="x86"),
220+
pytest.param("i386", "x86", id="i386"),
221+
pytest.param("i486", "x86", id="i486"),
222+
pytest.param("i586", "x86", id="i586"),
223+
pytest.param("i686", "x86", id="i686"),
220224
pytest.param("ppc64le", "ppc64le", id="ppc64le"),
225+
pytest.param("powerpc", "ppc", id="powerpc"),
226+
pytest.param("powerpc64", "ppc64", id="powerpc64"),
227+
pytest.param("powerpc64le", "ppc64le", id="powerpc64le"),
221228
pytest.param("riscv64", "riscv64", id="riscv64"),
222229
pytest.param("s390x", "s390x", id="s390x"),
230+
pytest.param("sparcv9", "sparc64", id="sparcv9"),
231+
pytest.param("sparc64", "sparc64", id="sparc64"),
232+
pytest.param("alpha", "alpha", id="alpha-passthrough"),
223233
],
224234
)
225235
def test_normalize_isa(isa: str, normalized: str) -> None:

0 commit comments

Comments
 (0)