A malformed config file should be reported, not raised as a configparser traceback by qianeric-backup · Pull Request #3473 · python-gitlab/python-gitlab
Repository navigation
A malformed config file should be reported, not raised as a configparser traceback - #3473
Open
qianeric-backup wants to merge 1 commit into python-gitlab:main from qianeric-backup:fix/broken-config-file-report
Open
A malformed config file should be reported, not raised as a configparser traceback #3473 qianeric-backup wants to merge 1 commit into python-gitlab:main from qianeric-backup:fix/broken-config-file-report
Conversation
qianeric-backup commented Nov 25, 2026
Copy link Copy Markdown
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():
_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:
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.
A malformed config file should be reported, not raised as a configpar...
55e723b
...ser 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`.
codecov
Bot
commented
Nov 25, 2026
·
edited
Loading
Copy link Copy Markdown
Codecov Report✅ All modified and coverable lines are covered by tests. @@ Coverage Diff @@ ## main #3473 +/- ## ======================================= Coverage 95.78% 95.78% ======================================= Files 100 100 Lines 6172 6175 +3 ======================================= + Hits 5912 5915 +3 Misses 260 260
Flags with carried forward coverage won't be shown. Click here to find out more.
|
This branch has not been deployed
No deployments
Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment