Skip to content

fix(tags): handle empty mac_ver and probe failures in mac_platforms - #1408

Open
CAOShurong wants to merge 2 commits into
pypa:mainfrom
CAOShurong:fix-mac-platforms-empty-version
Open

CAOShurong wants to merge 2 commits into
pypa:mainfrom
CAOShurong:fix-mac-platforms-empty-version

Conversation

@CAOShurong

Copy link
Copy Markdown

When platform.mac_ver()[0] returns an empty string (such as in non-standard macOS environments, minimal containers, or chroots), tags.mac_platforms() attempts int("") in tuple(map(int, version_str.split(".")[:2])), raising ValueError: invalid literal for int() with base 10: ''.

Additionally:

  1. When probing for macOS 10.16 compatibility, subprocess.run was passed env={"SYSTEM_VERSION_COMPAT": "0"} without copying os.environ, stripping all existing environment variables (such as PATH and dynamic linker paths) for the child process.
  2. The probe's stdout was not stripped or guarded against empty output / parsing errors.

This PR:

  • Gracefully handles empty or non-integer mac_ver output by returning early without platforms.
  • Preserves os.environ when probing SYSTEM_VERSION_COMPAT=0.
  • Strips probe stdout and guards against SubprocessError and ValueError.
  • Returns early if version is None.
  • Adds unit tests in tests/test_tags.py and updates CHANGELOG.rst.

When platform.mac_ver()[0] returns an empty string (e.g. in containerized or non-standard macOS environments), parsing the version via int() raises ValueError: invalid literal for int() with base 10: ''.

Additionally, when probing for macOS 10.16 SDK compatibility, subprocess.run replaced os.environ entirely instead of copying it, and unhandled probe failures or empty output could cause further errors.

This change:
- Gracefully handles empty or unparseable mac_ver output by returning an empty platforms iterator.
- Preserves os.environ when invoking the SYSTEM_VERSION_COMPAT subprocess probe.
- Strips probe stdout and guards against SubprocessError and ValueError.
- Adds unit tests covering empty mac_ver and subprocess probe failures.
- Remove unreachable dead code 'if version is None: return' in mac_platforms.
- Add test cases covering SubprocessError and non-integer ValueError during macOS 10.16 version probing.
- Ensure 100% line and branch coverage across src/packaging/tags.py.

@sylvesterkaczmarek sylvesterkaczmarek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a failed 10.16 compatibility probe this now continues with version == (10, 16), but 10.16 is the compatibility value that caused the probe to run, not the host's real release. An actual macOS 11+ interpreter can therefore advertise only macosx_10_16_*/older tags and miss compatible macosx_11_0_* (or newer) wheels. The new test currently locks in that misclassification. Please avoid emitting tags from the unresolved compatibility version, or recover the real OS version another way, and add a regression showing a failed probe cannot downgrade a Big Sur-or-newer host to 10.16.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants