X Tutup
The Wayback Machine - https://web.archive.org/web/20250616140931/https://github.com/python/cpython/issues/104505
Skip to content

Values CANNOT be indented despite contrary statement in the docs #104505

Closed as not planned
@ullix

Description

@ullix

The docs state:

[You can use comments]
    [Sections Can Be Indented]
        can_values_be_as_well = True

but while sections can indeed be indented, indenting values results in an Exception.

Simple code:

#! /usr/bin/env python3
# -*- coding: UTF-8 -*-

import configparser

config = configparser.ConfigParser()
with open("testconfigparser.cfg") as f:
    config.read_file(f)

print(config.get("alpha", "alpha1"))
print(config.get("alpha", "alpha2"))
print(config.get("alpha", "alpha3"))
print(config.get("alpha", "alpha4"))

Simple config file:

## configparsertest.cfg

    [alpha]
alpha1    = 100
alpha2    = 200
 alpha3    = 300
alpha4    = 400

Result of having alpha3 (inadvertently) indented by a single space is:

$ python testconfigparser.py 
100
200
alpha3    = 300
Traceback (most recent call last):
  File "/usr/lib/python3.9/configparser.py", line 789, in get
    value = d[option]
  File "/usr/lib/python3.9/collections/__init__.py", line 941, in __getitem__
    return self.__missing__(key)            # support subclasses that define __missing__
  File "/usr/lib/python3.9/collections/__init__.py", line 933, in __missing__
    raise KeyError(key)
KeyError: 'alpha3'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ullix/geigerlog/geigerlog/misc/Bugs/testconfigparser.py", line 12, in <module>
    print(config.get("alpha", "alpha3"))
  File "/usr/lib/python3.9/configparser.py", line 792, in get
    raise NoOptionError(option, section)
configparser.NoOptionError: No option 'alpha3' in section: 'alpha'

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup