Skip to content

Commit e50a9f2

Browse files
authored
plistlib.readPlist() is missing from Python ≥ 3.9 (#4171)
2 parents 82cc531 + 38fe69e commit e50a9f2

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

pkg_resources/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,10 @@ def _macos_vers(_cache=[]):
414414
if version == '':
415415
plist = '/System/Library/CoreServices/SystemVersion.plist'
416416
if os.path.exists(plist):
417-
if hasattr(plistlib, 'readPlist'):
418-
plist_content = plistlib.readPlist(plist)
419-
if 'ProductVersion' in plist_content:
420-
version = plist_content['ProductVersion']
417+
with open(plist, 'rb') as fh:
418+
plist_content = plistlib.load(fh)
419+
if 'ProductVersion' in plist_content:
420+
version = plist_content['ProductVersion']
421421

422422
_cache.append(version.split('.'))
423423
return _cache[0]

pkg_resources/tests/test_pkg_resources.py

+26
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import builtins
12
import sys
23
import tempfile
34
import os
45
import zipfile
56
import datetime
67
import time
8+
import plistlib
79
import subprocess
810
import stat
911
import distutils.dist
@@ -323,6 +325,30 @@ def test_dist_info_is_not_dir(tmp_path, only):
323325
assert not pkg_resources.dist_factory(str(tmp_path), str(dist_info), only)
324326

325327

328+
def test_macos_vers_fallback(monkeypatch, tmp_path):
329+
"""Regression test for pkg_resources._macos_vers"""
330+
orig_open = builtins.open
331+
332+
# Pretend we need to use the plist file
333+
monkeypatch.setattr('platform.mac_ver', mock.Mock(return_value=('', (), '')))
334+
335+
# Create fake content for the fake plist file
336+
with open(tmp_path / 'fake.plist', 'wb') as fake_file:
337+
plistlib.dump({"ProductVersion": "11.4"}, fake_file)
338+
339+
# Pretend the fake file exists
340+
monkeypatch.setattr('os.path.exists', mock.Mock(return_value=True))
341+
342+
def fake_open(file, *args, **kwargs):
343+
return orig_open(tmp_path / 'fake.plist', *args, **kwargs)
344+
345+
# Ensure that the _macos_vers works correctly
346+
with mock.patch('builtins.open', mock.Mock(side_effect=fake_open)) as m:
347+
assert pkg_resources._macos_vers([]) == ["11", "4"]
348+
349+
m.assert_called()
350+
351+
326352
class TestDeepVersionLookupDistutils:
327353
@pytest.fixture
328354
def env(self, tmpdir):

0 commit comments

Comments
 (0)