Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes 4893 - error handling for malformed configurations #5026

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/4893.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Malformed configuration files now show helpful error messages, instead of tracebacks.
14 changes: 14 additions & 0 deletions src/pip/_internal/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,20 @@ def _construct_parser(self, fname):
"Configuration file contains invalid %s characters.\n"
"Please fix your configuration, located at %s\n"
) % (locale.getpreferredencoding(False), fname))
except configparser.MissingSectionHeaderError as e:
raise ConfigurationError((
"ERROR: "
"Configuration file headers cannot be parsed. \n"
"Please fix the section formatting of the "
"configuration located at line %s of file %s\n"
) % (e.lineno, fname))
except configparser.ParsingError as e:
raise ConfigurationError(
"ERROR: "
"Configuration file cannot be parsed. \n"
"%s\n" % str(e)
)

return parser

def _load_environment_vars(self):
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ def test_environment_var_does_not_load_version(self):
with pytest.raises(ConfigurationError):
self.configuration.get_value(":env:.version")

def test_environment_config_errors_if_malformed(self):
contents = """
test]
hello = 4
"""
with self.tmpfile(contents) as config_file:
os.environ["PIP_CONFIG_FILE"] = config_file

with pytest.raises(ConfigurationError) as err:
self.configuration.load()

assert "malformed" in str(err)
assert lineno in str(err)
assert filepath in str(err)


class TestConfigurationPrecedence(ConfigurationMixin):
# Tests for methods to that determine the order of precedence of
Expand Down