| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
|
Thanks @vruyr , could you add a unit test? |
Sorry, something went wrong.
|
Done |
Sorry, something went wrong.
The `get_multivar` method accepts any key name — there is no multivar marker in the git config format. A valueless key can therefore appear among multivar results, so `__next__` on `ConfigMultivarIterator` returns `str | None`. Valueless keys are also returned as `None` by `__getitem__`.
See libgit2/pygit2#1457. Now, pygit2 returns None for valueless boolean entries, instead of failing. Dulwich returns "true" in this case, so do the same here to avoid discrepancies between the two backends. ```ini [some-section "identifier"] booleanflag ```
| Back | FazBrowse Home | New Git URL |
Fixes #1456
Description
Iterating over repo.config raises a RuntimeError if the configuration (including any include.path-sourced files) contains a valueless key — a boolean key written without =, e.g.:
[some-section "identifier"] booleanflagTraceback
Traceback (most recent call last): File "<stdin>", line 3, in <module> File ".../pygit2/config.py", line 65, in __next__ return self._next_entry() ~~~~~~~~~~~~~~~~^^ File ".../pygit2/config.py", line 72, in _next_entry return ConfigEntry._from_c(centry[0], self) ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^ File ".../pygit2/config.py", line 349, in _from_c entry.raw_value = entry.raw_value ^^^^^^^^^^^^^^^ File ".../functools.py", line 1126, in __get__ val = self.func(instance) File ".../pygit2/config.py", line 369, in raw_value return ffi.string(self.c_value) ~~~~~~~~~~^^^^^^^^^^^^^^ RuntimeError: cannot use string() on <cdata 'char *' NULL>Root Cause
libgit2 deliberately sets git_config_entry.value = NULL for valueless keys. The entry struct is zero-initialized via git__calloc, and the value field is only populated when a value is actually present (src/libgit2/config_file.c):
libgit2's own code handles the NULL case explicitly (same file):
So NULL in git_config_entry.value is intentional and documented behavior.
pygit2's _from_c eagerly caches raw_value during iteration (the workaround introduced for #970):
And raw_value has no NULL guard:
Expected Behavior
Valueless keys should be represented with value = None (or raw_value = None) rather than raising an exception.
Reproduction
$ mkdir /tmp/testrepo && cd /tmp/testrepo && git init Initialized empty Git repository in /tmp/testrepo/.git/ $ printf '[mysection]\n\tbooleanflag\n' >> .git/config $ cat .git/config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [mysection] booleanflag $ uv run --with pygit2==1.19.1 python - <<EOF import pygit2 repo = pygit2.Repository('/tmp/testrepo') for entry in repo.config: print(entry.name, entry.value) EOF Traceback (most recent call last): File "<stdin>", line 3, in <module> File ".../pygit2/config.py", line 65, in __next__ return self._next_entry() ~~~~~~~~~~~~~~~~^^ File ".../pygit2/config.py", line 72, in _next_entry return ConfigEntry._from_c(centry[0], self) ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^ File ".../pygit2/config.py", line 349, in _from_c entry.raw_value = entry.raw_value ^^^^^^^^^^^^^^^ File ".../functools.py", line 1126, in __get__ val = self.func(instance) File ".../pygit2/config.py", line 369, in raw_value return ffi.string(self.c_value) ~~~~~~~~~~^^^^^^^^^^^^^^ RuntimeError: cannot use string() on <cdata 'char *' NULL>