Error out on encoding issues while loading configuration (#4976)

This commit is contained in:
Pradyun Gedam 2018-01-23 21:56:20 +05:30 committed by GitHub
parent 4f8541972f
commit 3acb90fb8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 6 deletions

1
news/4976.bugfix Normal file
View File

@ -0,0 +1 @@
Abort if reading configuration causes encoding errors.

View File

@ -9,7 +9,7 @@ from distutils.util import strtobool
from pip._vendor.six import string_types
from pip._internal.configuration import Configuration
from pip._internal.configuration import Configuration, ConfigurationError
from pip._internal.utils.misc import get_terminal_size
logger = logging.getLogger(__name__)
@ -177,9 +177,6 @@ class ConfigOptionParser(CustomOptionParser):
the environ. Does a little special handling for certain types of
options (lists)."""
# Load the configuration
self.config.load()
# Accumulate complex default state.
self.values = optparse.Values(self.defaults)
late_eval = set()
@ -224,6 +221,12 @@ class ConfigOptionParser(CustomOptionParser):
# Old, pre-Optik 1.5 behaviour.
return optparse.Values(self.defaults)
# Load the configuration, or error out in case of an error
try:
self.config.load()
except ConfigurationError as err:
self.exit(2, err.args[0])
defaults = self._update_defaults(self.defaults.copy()) # ours
for option in self._get_all_options():
default = defaults.get(option.dest)

View File

@ -11,6 +11,7 @@ Some terminology:
A single word describing where the configuration key-value pair came from
"""
import locale
import logging
import os
@ -283,8 +284,14 @@ class Configuration(object):
# Doing this is useful when modifying and saving files, where we don't
# need to construct a parser.
if os.path.exists(fname):
parser.read(fname)
try:
parser.read(fname)
except UnicodeDecodeError:
raise ConfigurationError((
"ERROR: "
"Configuration file contains invalid %s characters.\n"
"Please fix your configuration, located at %s\n"
) % (locale.getpreferredencoding(False), fname))
return parser
def _load_environment_vars(self):