Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ConfigParser AttributeError: 'NoneType' object has no attribute 'append' #107625

Closed
Labels
stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
@mpf82

Description

@mpf82

Bug report

Checklist

  • I am confident this is a bug in CPython, not a bug in a third-party project
  • I have searched the CPython issue tracker, and am confident this bug has not been reported before

A clear and concise description of the bug

When trying to parse an INI with an indented key that follows a no-value key, an AttributeError is raised

importconfigparserlines= ['[SECT]\n','KEY1\n',' KEY2 = VAL2\n',# note the Space before the key!]conf=configparser.ConfigParser(comment_prefixes="",allow_no_value=True,strict=False,delimiters=('=', ),interpolation=None,)conf.read_file(lines,"test" )print(conf.__dict__['_sections' ] )
File "C:\Python311\Lib\configparser.py", line 1076, in _read          cursect[optname].append(value)    ^^^^^^^^^^^^^^^^^^^^^^^AttributeError: 'NoneType' object has no attribute 'append'

The reason is that ConfigParser assumes the second key is a continuation line and therefore further assumes thatcursect[optname] is initialized in the following check:

if (cursectisnotNoneandoptnameandcur_indent_level>indent_level):cursect[optname].append(value)

Suggested fix: add a check forcursect[optname] is not None:

if (cursectisnotNoneandoptnameandcursect[optname]isnotNoneandcur_indent_level>indent_level):cursect[optname].append(value)

With this check added, the print will output:

{'SECT': {'key1':None,'key2':'VAL2'}}

If the suggested fix is not acceptable, please consider to add a check and maybe raise aParsingError, but making an assumption aboutcursect[optname] and let it raise an AttributeError is just not a good thing, IMHO.

Your environment

  • CPython versions tested on: 3.7, 3.9, 3.11.2 (Python 3.11.2 (tags/v3.11.2:878ead1, Feb 7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] on win32)
  • Operating system and architecture: Windows 10 Pro, 64 bit

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp