From 25c1b27813eda67e9c48a622bbca99eaa24cd5d4 Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Thu, 4 Jan 2024 12:13:39 +0000 Subject: [PATCH 1/3] Add regression test for pkg_resources._mac_vers --- pkg_resources/tests/test_pkg_resources.py | 28 +++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/pkg_resources/tests/test_pkg_resources.py b/pkg_resources/tests/test_pkg_resources.py index a05aeb2603..0dd9c3c105 100644 --- a/pkg_resources/tests/test_pkg_resources.py +++ b/pkg_resources/tests/test_pkg_resources.py @@ -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 @@ -323,6 +325,32 @@ def test_dist_info_is_not_dir(tmp_path, only): assert not pkg_resources.dist_factory(str(tmp_path), str(dist_info), only) +@pytest.mark.skipif(sys.version_info >= (3, 9), reason="requires Python < 3.9") +@pytest.mark.filterwarnings("ignore::DeprecationWarning") +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): From b3935708f1b8857c45bb63abac390b013c450694 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Wed, 3 Jan 2024 22:29:11 +0100 Subject: [PATCH 2/3] =?UTF-8?q?`plistlib.readPlist()`=20is=20missing=20fro?= =?UTF-8?q?m=20Python=20=E2=89=A5=203.9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This function has been deprecated since Python 3.4, replaced by `plistlib.load()`. --- pkg_resources/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index ab6afe955d..584ce5bc4a 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -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] From 38fe69e54f30a7b67713ec19a0f856f433560147 Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Thu, 4 Jan 2024 12:25:14 +0000 Subject: [PATCH 3/3] Remove pytest marks no longer necessary in regression test --- pkg_resources/tests/test_pkg_resources.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg_resources/tests/test_pkg_resources.py b/pkg_resources/tests/test_pkg_resources.py index 0dd9c3c105..77d650a7d0 100644 --- a/pkg_resources/tests/test_pkg_resources.py +++ b/pkg_resources/tests/test_pkg_resources.py @@ -325,8 +325,6 @@ def test_dist_info_is_not_dir(tmp_path, only): assert not pkg_resources.dist_factory(str(tmp_path), str(dist_info), only) -@pytest.mark.skipif(sys.version_info >= (3, 9), reason="requires Python < 3.9") -@pytest.mark.filterwarnings("ignore::DeprecationWarning") def test_macos_vers_fallback(monkeypatch, tmp_path): """Regression test for pkg_resources._macos_vers""" orig_open = builtins.open