New GitHub issue #96239 from jseakle:<br>

<hr>

<pre>
# 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'
```
# Your environment

- CPython versions tested on: 3.10.4, 3.9.5
- Operating system and architecture: MacOS, but replicated in online REPLs as well
</pre>

<hr>

<a href="https://github.com/python/cpython/issues/96239">View on GitHub</a>
<p>Labels: type-bug</p>
<p>Assignee: </p>