Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plistlib.readPlist() is missing from Python ≥ 3.9 #4171

Merged
merged 3 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkg_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,10 @@ def _macos_vers(_cache=[]):
if version == '':
plist = '/System/Library/CoreServices/SystemVersion.plist'
if os.path.exists(plist):
if hasattr(plistlib, 'readPlist'):
plist_content = plistlib.readPlist(plist)
if 'ProductVersion' in plist_content:
version = plist_content['ProductVersion']
with open(plist, 'rb') as fh:
plist_content = plistlib.load(fh)
if 'ProductVersion' in plist_content:
version = plist_content['ProductVersion']

_cache.append(version.split('.'))
return _cache[0]
Expand Down
26 changes: 26 additions & 0 deletions pkg_resources/tests/test_pkg_resources.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import builtins
import sys
import tempfile
import os
import zipfile
import datetime
import time
import plistlib
import subprocess
import stat
import distutils.dist
Expand Down Expand Up @@ -323,6 +325,30 @@ def test_dist_info_is_not_dir(tmp_path, only):
assert not pkg_resources.dist_factory(str(tmp_path), str(dist_info), only)


def test_macos_vers_fallback(monkeypatch, tmp_path):
"""Regression test for pkg_resources._macos_vers"""
orig_open = builtins.open

# Pretend we need to use the plist file
monkeypatch.setattr('platform.mac_ver', mock.Mock(return_value=('', (), '')))

# Create fake content for the fake plist file
with open(tmp_path / 'fake.plist', 'wb') as fake_file:
plistlib.dump({"ProductVersion": "11.4"}, fake_file)

# Pretend the fake file exists
monkeypatch.setattr('os.path.exists', mock.Mock(return_value=True))

def fake_open(file, *args, **kwargs):
return orig_open(tmp_path / 'fake.plist', *args, **kwargs)

# Ensure that the _macos_vers works correctly
with mock.patch('builtins.open', mock.Mock(side_effect=fake_open)) as m:
assert pkg_resources._macos_vers([]) == ["11", "4"]

m.assert_called()


class TestDeepVersionLookupDistutils:
@pytest.fixture
def env(self, tmpdir):
Expand Down
Loading