Skip to content

Commit

Permalink
Merge tag 'v4.6.2' into cpython
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Jul 31, 2021
2 parents d078f9d + 5583567 commit 29c7a59
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
15 changes: 13 additions & 2 deletions Lib/importlib/metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pathlib
import zipfile
import operator
import platform
import textwrap
import warnings
import functools
Expand Down Expand Up @@ -44,6 +45,16 @@
]


def _pypy_partial(val):
"""
Adjust for variable stacklevel on partial under PyPy.
Workaround for #327.
"""
is_pypy = platform.python_implementation() == 'PyPy'
return val + is_pypy


class PackageNotFoundError(ModuleNotFoundError):
"""The package was not found."""

Expand Down Expand Up @@ -240,7 +251,7 @@ class DeprecatedList(list):
warnings.warn,
"EntryPoints list interface is deprecated. Cast to list if needed.",
DeprecationWarning,
stacklevel=2,
stacklevel=_pypy_partial(2),
)

def __setitem__(self, *args, **kwargs):
Expand Down Expand Up @@ -389,7 +400,7 @@ class Deprecated:
warnings.warn,
"SelectableGroups dict interface is deprecated. Use select.",
DeprecationWarning,
stacklevel=2,
stacklevel=_pypy_partial(2),
)

def __getitem__(self, name):
Expand Down
18 changes: 12 additions & 6 deletions Lib/test/test_importlib/test_metadata_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import unittest
import warnings
import importlib
import contextlib

from . import fixtures
from importlib.metadata import (
Expand All @@ -17,6 +18,13 @@
)


@contextlib.contextmanager
def suppress_known_deprecation():
with warnings.catch_warnings(record=True) as ctx:
warnings.simplefilter('default')
yield ctx


class APITests(
fixtures.EggInfoPkg,
fixtures.DistInfoPkg,
Expand Down Expand Up @@ -118,8 +126,7 @@ def test_entry_points_dict_construction(self):
# Prior versions of entry_points() returned simple lists and
# allowed casting those lists into maps by name using ``dict()``.
# Capture this now deprecated use-case.
with warnings.catch_warnings(record=True) as caught:
warnings.filterwarnings("default", category=DeprecationWarning)
with suppress_known_deprecation() as caught:
eps = dict(entry_points(group='entries'))

assert 'main' in eps
Expand All @@ -138,8 +145,7 @@ def test_entry_points_by_index(self):
See python/importlib_metadata#300 and bpo-44246.
"""
eps = distribution('distinfo-pkg').entry_points
with warnings.catch_warnings(record=True) as caught:
warnings.filterwarnings("default", category=DeprecationWarning)
with suppress_known_deprecation() as caught:
eps[0]

# check warning
Expand All @@ -151,7 +157,7 @@ def test_entry_points_groups_getitem(self):
# Prior versions of entry_points() returned a dict. Ensure
# that callers using '.__getitem__()' are supported but warned to
# migrate.
with warnings.catch_warnings(record=True):
with suppress_known_deprecation():
entry_points()['entries'] == entry_points(group='entries')

with self.assertRaises(KeyError):
Expand All @@ -161,7 +167,7 @@ def test_entry_points_groups_get(self):
# Prior versions of entry_points() returned a dict. Ensure
# that callers using '.get()' are supported but warned to
# migrate.
with warnings.catch_warnings(record=True):
with suppress_known_deprecation():
entry_points().get('missing', 'default') == 'default'
entry_points().get('entries', 'default') == entry_points()['entries']
entry_points().get('missing', ()) == ()
Expand Down

0 comments on commit 29c7a59

Please sign in to comment.