ConfigParser read(write()) not idempotent
@ambv já está trabalhando nisso.
Desde 25/8/2022.
Avaliação
Esta issue ainda não foi avaliada.
Descrição
Bug report
Due to the the way that multiline values are handled and the fact that initial whitespace is preserved in keys added with set(), it is possible for calling parser1.write(foo) followed by parser2.read_file(foo) to leave parser1 and parser2 in radically different configuration states. As illustrated below, if a key with leading spaces is added with set() after a key with fewer leading spaces, that whitespace will be understood as part of the key and written to the file in a way that will be read back as part of the value of the previous key.
I believe that this is a bug in set, which should strip whitespace from its key argument. No ConfigParser read method can produce a key with leading whitespace, so such keys should not be accepted programmatically either.
from io import StringIO
import configparser
p = configparser.ConfigParser()
p.add_section('foo')
p.set('foo', 'a', '5')
p.set('foo', ' b', '6')
w = StringIO()
p.write(w)
w.seek(0)
print(w.read())
p = configparser.ConfigParser()
w.seek(0)
p.read_string(w.read())
print(p.items('foo'))
print(p.get('foo', 'b'))
output:
[foo]
a = 5
b = 6
[('a', '5\nb = 6')]
Traceback (most recent call last):
File "/usr/local/Cellar/python@3.10/3.10.4/Frameworks/Python.framework/Versions/3.10/lib/python3.10/configparser.py", line 790, in get
value = d[option]
File "/usr/local/Cellar/python@3.10/3.10.4/Frameworks/Python.framework/Versions/3.10/lib/python3.10/collections/__init__.py", line 986, in __getitem__
return self.__missing__(key) # support subclasses that define __missing__
File "/usr/local/Cellar/python@3.10/3.10.4/Frameworks/Python.framework/Versions/3.10/lib/python3.10/collections/__init__.py", line 978, in __missing__
raise KeyError(key)
KeyError: 'b'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/jeakle/python/tipr/test.py", line 15, in <module>
print(p.get('foo', 'b'))
File "/usr/local/Cellar/python@3.10/3.10.4/Frameworks/Python.framework/Versions/3.10/lib/python3.10/configparser.py", line 793, in get
raise NoOptionError(option, section)
configparser.NoOptionError: No option 'b' in section: 'foo'
For context, this bit me while trying to subclass ConfigParser to handle a slightly different format. Among other things, we don't allow multiline values and want leading whitespace to be irrelevant. While attempting to override read_string() with a method that does some of our own processing on each line and then calls set() on it, I initially just passed the part of the line to left of the separator to set() as the key, which caused both the problem described above, and the issue that calling set() again later on the "same" key can introduce duplicate keys if they have different amounts of leading whitespace:
In fact a similar example shows that the output of write() can crash when being read() back:
from io import StringIO
import configparser
p = configparser.ConfigParser()
p.add_section('foo')
p.set('foo', ' b', '6')
p.set('foo', 'b', '7')
w = StringIO()
p.write(w)
w.seek(0)
print(w.read())
p = configparser.ConfigParser()
w.seek(0)
p.read_string(w.read())
print(p.items('foo'))
print(p.get('foo', 'b'))
output:
[foo]
b = 6
b = 7
Traceback (most recent call last):
File "/Users/jeakle/python/tipr/test.py", line 13, in <module>
p.read_string(w.read())
File "/usr/local/Cellar/python@3.10/3.10.4/Frameworks/Python.framework/Versions/3.10/lib/python3.10/configparser.py", line 724, in read_string
self.read_file(sfile, source)
File "/usr/local/Cellar/python@3.10/3.10.4/Frameworks/Python.framework/Versions/3.10/lib/python3.10/configparser.py", line 719, in read_file
self._read(f, source)
File "/usr/local/Cellar/python@3.10/3.10.4/Frameworks/Python.framework/Versions/3.10/lib/python3.10/configparser.py", line 1097, in _read
raise DuplicateOptionError(sectname, optname,
configparser.DuplicateOptionError: While reading from '<string>' [line 3]: option 'b' in section 'foo' already exists
Your environment
- CPython versions tested on: 3.10.4, 3.9.5
- Operating system and architecture: MacOS, but replicated in online REPLs as well
Linked PRs
- gh-157729
- Linguagem predominante
- Python
- Estrelas
- 77.2k
- Forks
- 36k
- Merge médio
- 1d 9h
- PRs com merge (30d)
- 558
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de python/cpython
-
docs pending
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
-
stdlib type-feature
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
-
stdlib type-feature
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
-
build type-bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
-
stdlib topic-email type-feature
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 70/100
Todas as issues de python/cpython
Issues semelhantes
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 88/100
OpenHands/extensions#626 · 1 comentário ·
-
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 90/100
CSCfi/sd-search-api#39 ·
-
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 90/100
-
please add to porn list Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 68/100
StevenBlack/hosts#3255 ·