Use sys.version_info in _python_version - #2875
Open
SammyTourani wants to merge 1 commit into
Open
SammyTourani wants to merge 1 commit into
SammyTourani wants to merge 1 commit into
Conversation
_python_version parsed sys.version and converted each dotted component to int, which raises ValueError on pre-release and development builds such as 3.15.0rc2 or 3.15.0a1+. Read the numbers from sys.version_info instead. Fixes apple#2874
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2874
coremltools/models/utils.py:_python_version()now returnstuple(sys.version_info[:3]).sys.version_infogives major, minor and micro as ints on every build, so there is nothing to parse. On final releases the return value is unchanged. The existing callers only compare major and minor (_python_version() < (3, 6),_python_version()[:2] <= (3, 8)).coremltools/test/ml_program/test_utils.py: newTestPythonVersiontest. It setssys.versionandsys.version_infoto a final release, a release candidate and a development build, then checks the returned tuple. Without the fix, the release-candidate and development-build cases fail with theValueErrorfrom the report.coremltools/test/optimize/torch/utils.pyhas an identical copy of_python_version, but nothing imports it. I left it unchanged so this change stays on the reported code path.Verification
Tested on macOS 26 (arm64) with Python 3.13.15, torch 2.8.0, numpy 2.1.0 and pytest 9.1.1. The native extensions came from the 9.0 wheel; their sources on
mainare unchanged since 9.0. Each command was run from the repo root, first with the parent commit'sutils.pyand then with the fix.Command:
python -m pytest coremltools/test/ml_program/test_utils.py::TestPythonVersionResult: before:
2 failed, 1 passed in 1.36s(ValueError: invalid literal for int() with base 10: '0rc2'and'0a1+'). After:3 passed in 0.93s.Command:
python -m pytest coremltools/test/ml_program/test_utils.pyResult: before:
2 failed, 34 passed, 7 skipped, 8 xfailed, 142 warnings in 13.57s. After:36 passed, 7 skipped, 8 xfailed, 142 warnings in 13.63s. The skips and xfails come from the file's existing markers (rdar://157488825, rdar://138957606).Command:
python -m pytest coremltools/converters/mil/frontend/torch/test/test_torch_ops.py::TestSlice coremltools/converters/mil/frontend/torch/test/test_torch_conversion_api.py::TestTorchInputs::test_int8_tenors coremltools/test/ml_program/experimental/test_perf_utils.py::TestTorchMLModelBenchmarkerResult:
178 passed, 6 xfailed, 4 xpassed, 199 warningsboth before and after. These are the existing tests that call_python_version. All 188 ran and none were skipped. The xfail/xpass results come from existing markers (#2189, rdar://155895085).Also checked:
from coremltools.models.utils import _python_version; _python_version()raisedValueError: invalid literal for int() with base 10: '0b3'before the fix and returns(3, 15, 0)after.sys.versionset to the report's3.15.0rc2string,importlib.import_module("coremltools.converters.mil.frontend.milproto.test_load")reproduces the traceback from the issue before the fix (TestSlice→_python_version→ValueError: ... '0rc2') and imports cleanly after.