8000 Attempt to avoid error with `_read_utf8_with_fallback` by moving code… · pypa/setuptools@8cc50d4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8cc50d4

Browse files
authored
Attempt to avoid error with _read_utf8_with_fallback by moving code in pkg_resources (#4422)
2 parents 0f8c58d + a4b15f3 commit 8cc50d4

File tree

3 files changed

+87
-32
lines changed

3 files changed

+87
-32
lines changed

newsfragments/4422.misc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Reorder code in ``pkg_resources/__init__.py`` to avoid definitions after ``@_call_aside``.

pkg_resources/__init__.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3426,6 +3426,38 @@ class PkgResourcesDeprecationWarning(Warning):
34263426
"""
34273427

34283428

3429+
# Ported from ``setuptools`` to avoid introducing an import inter-dependency:
3430+
_LOCALE_ENCODING = "locale" if sys.version_info >= (3, 10) else None
3431+
3432+
3433+
def _read_utf8_with_fallback(file: str, fallback_encoding=_LOCALE_ENCODING) -> str:
3434+
"""See setuptools.unicode_utils._read_utf8_with_fallback"""
3435+
try:
3436+
with open(file, "r", encoding="utf-8") as f:
3437+
return f.read()
3438+
except UnicodeDecodeError: # pragma: no cover
3439+
msg = f"""\
3440+
********************************************************************************
3441+
`encoding="utf-8"` fails with {file!r}, trying `encoding={fallback_encoding!r}`.
3442+
3443+
This fallback behaviour is considered **deprecated** and future versions of
3444+
`setuptools/pkg_resources` may not implement it.
3445+
3446+
Please encode {file!r} with "utf-8" to ensure future builds will succeed.
3447+
3448+
If this file was produced by `setuptools` itself, cleaning up the cached files
3449+
and re-building/re-installing the package with a newer version of `setuptools`
3450+
(e.g. by updating `build-system.requires` in its `pyproject.toml`)
3451+
might solve the problem.
3452+
********************************************************************************
3453+
"""
3454+
# TODO: Add a deadline?
3455+
# See comment in setuptools.unicode_utils._Utf8EncodingNeeded
3456+
warnings.warn(msg, PkgResourcesDeprecationWarning, stacklevel=2)
3457+
with open(file, "r", encoding=fallback_encoding) as f:
3458+
return f.read()
3459+
3460+
34293461
# from jaraco.functools 1.3
34303462
def _call_aside(f, *args, **kwargs):
34313463
f(*args, **kwargs)
@@ -3498,35 +3530,3 @@ def _initialize_master_working_set():
34983530
add_activation_listener = working_set.subscribe
34993531
run_script = working_set.run_script
35003532
run_main = run_script
3501-
3502-
3503-
# ---- Ported from ``setuptools`` to avoid introducing an import inter-dependency ----
3504-
LOCALE_ENCODING = "locale" if sys.version_info >= (3, 10) else None
3505-
3506-
3507-
def _read_utf8_with_fallback(file: str, fallback_encoding=LOCALE_ENCODING) -> str:
3508-
"""See setuptools.unicode_utils._read_utf8_with_fallback"""
3509-
try:
3510-
with open(file, "r", encoding="utf-8") as f:
3511-
return f.read()
3512-
except UnicodeDecodeError: # pragma: no cover
3513-
msg = f"""\
3514-
********************************************************************************
3515-
`encoding="utf-8"` fails with {file!r}, trying `encoding={fallback_encoding!r}`.
3516-
3517-
This fallback behaviour is considered **deprecated** and future versions of
3518-
`setuptools/pkg_resources` may not implement it.
3519-
3520-
Please encode {file!r} with "utf-8" to ensure future builds will succeed.
3521-
3522-
If this file was produced by `setuptools` itself, cleaning up the cached files
3523-
and re-building/re-installing the package with a newer version of `setuptools`
3524-
(e.g. by updating `build-system.requires` in its `pyproject.toml`)
3525-
might solve the problem.
3526-
********************************************************************************
3527-
"""
3528-
# TODO: Add a deadline?
3529-
# See comment in setuptools.unicode_utils._Utf8EncodingNeeded
3530-
warnings.warn(msg, PkgResourcesDeprecationWarning, stacklevel=2)
3531-
with open(file, "r", encoding=fallback_encoding) as f:
3532-
return f.read()
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import platform
2+
from inspect import cleandoc
3+
4+
import jaraco.path
5+
import pytest
6+
7+
pytestmark = pytest.mark.integration
8+
9+
10+
# For the sake of simplicity this test uses fixtures defined in
11+
# `setuptools.test.fixtures`,
12+
# and it also exercise conditions considered deprecated...
13+
# So if needed this test can be deleted.
14+
@pytest.mark.skipif(
15+
platform.system() != "Linux",
16+
reason="only demonstrated to fail on Linux in #4399",
17+
)
18+
def test_interop_pkg_resources_iter_entry_points(tmp_path, venv):
19+
"""
20+
Importing pkg_resources.iter_entry_points on console_scripts
21+
seems to cause trouble with zope-interface, when deprecates installation method
22+
is used. See #4399.
23+
"""
24+
project = {
25+
"pkg": {
26+
"foo.py": cleandoc(
27+
"""
28+
from pkg_resources import iter_entry_points
29+
30+
def bar():
31+
print("Print me if you can")
32+
"""
33+
),
34+
"setup.py": cleandoc(
35+
"""
36+
from setuptools import setup, find_packages
37+
38+
setup(
39+
install_requires=["zope-interface==6.4.post2"],
40+
entry_points={
41+
"console_scripts": [
42+
"foo=foo:bar",
43+
],
44+
},
45+
)
46+
"""
47+
),
48+
}
49+
}
50+
jaraco.path.build(project, prefix=tmp_path)
51+
cmd = ["pip", "install", "-e", ".", "--no-use-pep517"]
52+
venv.run(cmd, cwd=tmp_path / "pkg") # Needs this version of pkg_resources installed
53+
out = venv.run(["foo"])
54+
assert "Print me if you can" in out

0 commit comments

Comments
 (0)
0