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.DuplicateOptionError leaves ConfigParser instance in bad state #116957

Closed
Labels
type-bugAn unexpected behavior, bug, or error
@drothlis

Description

@drothlis

Bug report

Bug description:

If you catchconfigparser.Error when reading a config file (the intention is to skip invalid config files) and then attempt to use the ConfigParser instance, you can get really weird errors. In the following example,read raises DuplicateOptionError from the 2nd section, but attempting toget a value from the first section fails in the interpolation code, because somehow the value has been stored as a list instead of a string!

>>>importconfigparser>>>cp=configparser.ConfigParser()>>>cp.read_string('[a]\nx=1\n[b]\ny=1\ny=2')Traceback (mostrecentcalllast):File"<stdin>",line1,in<module>File"/usr/lib/python3.12/configparser.py",line710,inread_stringself.read_file(sfile,source)File"/usr/lib/python3.12/configparser.py",line705,inread_fileself._read(f,source)File"/usr/lib/python3.12/configparser.py",line1074,in_readraiseDuplicateOptionError(sectname,optname,configparser.DuplicateOptionError:Whilereadingfrom'<string>' [line5]:option'y'insection'b'alreadyexists>>>cp.get('a','x')Traceback (mostrecentcalllast):File"<stdin>",line1,in<module>File"/usr/lib/python3.12/configparser.py",line777,ingetreturnself._interpolation.before_get(self,section,option,value,^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"/usr/lib/python3.12/configparser.py",line367,inbefore_getself._interpolate_some(parser,option,L,value,section,defaults,1)File"/usr/lib/python3.12/configparser.py",line384,in_interpolate_somep=rest.find("%")^^^^^^^^^AttributeError:'list'objecthasnoattribute'find'

Disabling interpolation can read the value from the 1st section but it's a list, not a str!

>>> cp.get('a', 'x', raw=True)['1']

CPython versions tested on:

3.10, 3.12, CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-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