Skip to content

Commit 095fd85

Browse files
committed
Isolate EXTERNALLY-MANAGED parsing logic
This makes the parser easier to test.
1 parent e27a819 commit 095fd85

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/pip/_internal/exceptions.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import configparser
99
import contextlib
1010
import locale
11+
import logging
1112
import re
1213
import sys
1314
from itertools import chain, groupby, repeat
@@ -25,6 +26,8 @@
2526
from pip._internal.metadata import BaseDistribution
2627
from pip._internal.req.req_install import InstallRequirement
2728

29+
logger = logging.getLogger(__name__)
30+
2831

2932
#
3033
# Scaffolding
@@ -708,10 +711,15 @@ def _iter_externally_managed_error_keys() -> Iterator[str]:
708711
yield "Error"
709712

710713
@classmethod
711-
def from_config(
712-
cls,
713-
parser: configparser.ConfigParser,
714-
) -> "ExternallyManagedEnvironment":
714+
def from_config(cls, config: str) -> "ExternallyManagedEnvironment":
715+
parser = configparser.ConfigParser(interpolation=None)
716+
try:
717+
parser.read(config, encoding="utf-8")
718+
except (OSError, UnicodeDecodeError):
719+
from pip._internal.utils._log import VERBOSE
720+
721+
exc_info = logger.isEnabledFor(VERBOSE)
722+
logger.warning("Failed to read %s", config, exc_info=exc_info)
715723
try:
716724
section = parser["externally-managed"]
717725
except KeyError:

src/pip/_internal/utils/misc.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# The following comment should be removed at some point in the future.
22
# mypy: strict-optional=False
33

4-
import configparser
54
import contextlib
65
import errno
76
import getpass
87
import hashlib
98
import io
9+
import logging
1010
import os
1111
import posixpath
1212
import shutil
@@ -41,7 +41,6 @@
4141
from pip import __version__
4242
from pip._internal.exceptions import CommandError, ExternallyManagedEnvironment
4343
from pip._internal.locations import get_major_minor_version
44-
from pip._internal.utils._log import VERBOSE, getLogger
4544
from pip._internal.utils.compat import WINDOWS
4645
from pip._internal.utils.virtualenv import running_under_virtualenv
4746

@@ -63,8 +62,7 @@
6362
"ConfiguredBuildBackendHookCaller",
6463
]
6564

66-
67-
logger = getLogger(__name__)
65+
logger = logging.getLogger(__name__)
6866

6967
T = TypeVar("T")
7068
ExcInfo = Tuple[Type[BaseException], BaseException, TracebackType]
@@ -592,17 +590,11 @@ def check_externally_managed() -> None:
592590
raised.
593591
"""
594592
if running_under_virtualenv():
595-
return None
593+
return
596594
marker = os.path.join(sysconfig.get_path("stdlib"), "EXTERNALLY-MANAGED")
597595
if not os.path.isfile(marker):
598596
return
599-
parser = configparser.ConfigParser(interpolation=None)
600-
try:
601-
parser.read(marker, encoding="utf-8")
602-
except (OSError, UnicodeDecodeError):
603-
exc_info = logger.isEnabledFor(VERBOSE)
604-
logger.warning("Failed to read %s", marker, exc_info=exc_info)
605-
raise ExternallyManagedEnvironment.from_config(parser)
597+
raise ExternallyManagedEnvironment.from_config(marker)
606598

607599

608600
def is_console_interactive() -> bool:

0 commit comments

Comments
 (0)