From 55e723b96f740619c747e63781212751f7690e82 Mon Sep 17 00:00:00 2001 From: QianEric Date: Wed, 25 Nov 2026 16:50:29 +0800 Subject: [PATCH] A malformed config file should be reported, not raised as a configparser traceback Pointing `--config-file` at a file that exists but is not a valid INI file escapes as a raw exception: ``` $ gitlab --config-file broken.cfg project list Traceback (most recent call last): File ".../gitlab/config.py", line 123, in _parse_config _config.read(self._files, encoding="utf-8") File ".../configparser.py", line 696, in read self._read(fp, filename) configparser.MissingSectionHeaderError: File contains no section headers. file: 'broken.cfg', line: 1 ``` Every other problem with the config file the user names is reported as one line, through `GitlabConfigMissingError` in `_get_config_files()`: ``` $ gitlab --config-file /nope.cfg project list Cannot read config from file: [Errno 2] No such file or directory: '/nope.cfg' ``` `configparser.read()` deliberately swallows `OSError`, which is why the missing-file case is already handled, but it lets `configparser.Error` (a syntax error, a duplicate option, ...) propagate. ## Change `gitlab/config.py`, `GitlabConfigParser._parse_config()`: ```python _config = configparser.ConfigParser() - _config.read(self._files, encoding="utf-8") + try: + _config.read(self._files, encoding="utf-8") + except configparser.Error as e: + raise GitlabConfigMissingError( + f"Cannot read config from file: {e}" + ) from e ``` | invocation | before | after | |---|---|---| | `--config-file broken.cfg` (no section header) | `MissingSectionHeaderError` traceback, exit 1 | `Cannot read config from file: File contains no section headers. file: 'broken.cfg', line: 1`, exit 1 | | `--config-file dup.cfg` (duplicate option) | `DuplicateOptionError` traceback | same one-line report | | `--config-file /nope.cfg` | `Cannot read config from file: [Errno 2] ...` | unchanged | | `--config-file good.cfg` | works | unchanged | The error class and the message prefix are the ones the CLI already prints for an unreadable config file (`tests/functional/cli/test_cli.py::test_arg_config_missing_file_raises` asserts exactly that prefix), so both ways of getting the file wrong now look the same. ## Test `tests/unit/test_config.py`: ```python def test_broken_config_file_raises(tmp_path): config_file = tmp_path / "config" config_file.write_text("this is not an ini file\n") with pytest.raises(config.GitlabConfigMissingError, match="Cannot read config from file"): config.GitlabConfigParser(config_files=[str(config_file)]) ``` `pytest tests/unit/test_config.py`: 19 passed. The new test fails on an unmodified checkout with `configparser.MissingSectionHeaderError`. --- gitlab/config.py | 7 ++++++- tests/unit/test_config.py | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/gitlab/config.py b/gitlab/config.py index 46be3e26d..e030106ff 100644 --- a/gitlab/config.py +++ b/gitlab/config.py @@ -120,7 +120,12 @@ def __init__( def _parse_config(self) -> None: _config = configparser.ConfigParser() - _config.read(self._files, encoding="utf-8") + try: + _config.read(self._files, encoding="utf-8") + except configparser.Error as e: + raise GitlabConfigMissingError( + f"Cannot read config from file: {e}" + ) from e if self.gitlab_id and not _config.has_section(self.gitlab_id): raise GitlabDataError( diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index b8470f22b..823687cfd 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -124,6 +124,14 @@ def test_env_config_missing_file_raises(monkeypatch): config._get_config_files() +def test_broken_config_file_raises(tmp_path): + config_file = tmp_path / "config" + config_file.write_text("this is not an ini file\n") + + with pytest.raises(config.GitlabConfigMissingError, match="Cannot read config from file"): + config.GitlabConfigParser(config_files=[str(config_file)]) + + def test_env_config_not_defined_does_not_raise(monkeypatch): with monkeypatch.context() as m: m.setattr(config, "_DEFAULT_FILES", [])