Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 tasks done
mpf82 opened this issue Aug 4, 2023 · 3 comments
Closed
2 tasks done

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

mpf82 opened this issue Aug 4, 2023 · 3 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@mpf82
Copy link

mpf82 commented Aug 4, 2023

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

import configparser
lines = [
  '[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 that cursect[optname] is initialized in the following check:

            if (cursect is not None and optname and
                cur_indent_level > indent_level):
                cursect[optname].append(value)

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

            if (cursect is not None and optname and
                cursect[optname] is not None and
                cur_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 a ParsingError, but making an assumption about cursect[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

@mpf82 mpf82 added the type-bug An unexpected behavior, bug, or error label Aug 4, 2023
@terryjreedy
Copy link
Member

Behavior verified with 3.12.0b4 on Win10. I agree that configparser has a bug. I also think that a continuation line for a no-value option is a user bug. So I would a line to the structure section saying so and raise a ParsingError with a message something like "No-value option {opname} cannot be continued".

Line 1084, cursect[optname] = None executes when optval is not None (line 1079) is false. Since this appears to be the only such setting, I think cursect[optname].append(value) can be replaced with if...else raise....

Adding = to the KEY1 line results in KEY1 initially getting a blank value "" that is continued, resulting in value '\nKEY2 = VAL2'

@terryjreedy terryjreedy added the stdlib Python modules in the Lib dir label Aug 4, 2023
Agent-Hellboy added a commit to Agent-Hellboy/cpython that referenced this issue Aug 5, 2023
- raise ParsingError instaed of AttributeError if a key
  contains empty value in a non recommended scenario where
  extra space and blankline is present within the section
@Agent-Hellboy
Copy link
Contributor

I have added a PR as you suggested @terryjreedy

encukou added a commit that referenced this issue Mar 6, 2024
…H-107651)


Co-authored-by: Éric <merwok@netwok.org>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
@encukou
Copy link
Member

encukou commented Mar 7, 2024

Thank you for the fix!

Since this only changes the error type (and message), I'll not backport. Let me know if you think I should.

@encukou encukou closed this as completed Mar 7, 2024
adorilson pushed a commit to adorilson/cpython that referenced this issue Mar 25, 2024
…inued (pythonGH-107651)


Co-authored-by: Éric <merwok@netwok.org>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
…inued (pythonGH-107651)


Co-authored-by: Éric <merwok@netwok.org>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants