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

[doc] Explain ConfigParser 'valid section name' and .SECTCRE #65122

Closed
miloskomarcevic mannequin opened this issue Mar 14, 2014 · 23 comments
Closed

[doc] Explain ConfigParser 'valid section name' and .SECTCRE #65122

miloskomarcevic mannequin opened this issue Mar 14, 2014 · 23 comments
Assignees
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes docs Documentation in the Doc dir easy type-feature A feature request or enhancement

Comments

@miloskomarcevic
Copy link
Mannequin

miloskomarcevic mannequin commented Mar 14, 2014

BPO 20923
Nosy @terryjreedy, @vstinner, @bitdancer, @ambv, @xflr6, @spaceone, @miss-islington, @iritkatriel, @Vidhyavinu
PRs
  • bpo-20923 : [doc] Explain ConfigParser 'valid section name' #31359
  • bpo-20923 : [doc] Explain ConfigParser 'valid section name' and .SECTCRE #31413
  • [3.10] bpo-20923 : [doc] Explain ConfigParser 'valid section name' and .SECTCRE (GH-31413) #31506
  • [3.9] bpo-20923 : [doc] Explain ConfigParser 'valid section name' and .SECTCRE (GH-31413) #31507
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/terryjreedy'
    closed_at = <Date 2022-02-22.18:46:35.431>
    created_at = <Date 2014-03-14.14:21:45.867>
    labels = ['easy', '3.9', '3.10', '3.11', 'type-feature', 'docs']
    title = "[doc] Explain ConfigParser 'valid section name' and .SECTCRE"
    updated_at = <Date 2022-02-22.18:46:35.431>
    user = 'https://bugs.python.org/miloskomarcevic'

    bugs.python.org fields:

    activity = <Date 2022-02-22.18:46:35.431>
    actor = 'iritkatriel'
    assignee = 'terry.reedy'
    closed = True
    closed_date = <Date 2022-02-22.18:46:35.431>
    closer = 'iritkatriel'
    components = ['Documentation']
    creation = <Date 2014-03-14.14:21:45.867>
    creator = 'miloskomarcevic'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 20923
    keywords = ['patch', 'easy']
    message_count = 23.0
    messages = ['213554', '213609', '213678', '213687', '234954', '255263', '255269', '255277', '255278', '255279', '255281', '255304', '255325', '255368', '255369', '412197', '413270', '413271', '413292', '413727', '413734', '413738', '413739']
    nosy_count = 12.0
    nosy_names = ['terry.reedy', 'vstinner', 'r.david.murray', 'docs@python', 'lukasz.langa', 'python-dev', 'miloskomarcevic', 'xflr6', 'spaceone', 'miss-islington', 'iritkatriel', 'vidhya']
    pr_nums = ['31359', '31413', '31506', '31507']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue20923'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @miloskomarcevic
    Copy link
    Mannequin Author

    miloskomarcevic mannequin commented Mar 14, 2014

    It would be good if ConfigParser supported angled brackets in section names by being greedy when parsing.

    For example, section:

    [Test[2]_foo]

    gets parsed as:

    Test[2

    @miloskomarcevic miloskomarcevic mannequin added the type-bug An unexpected behavior, bug, or error label Mar 14, 2014
    @terryjreedy
    Copy link
    Member

    This is invalid as a bug report, unless one considers that the presence of '_foo]' on the same line should have raised an exception. As an enhancement request, I think it should be rejected.

    ConfigParser configuration language is based on msdos/windows .ini files. Similar files are used on windows. "A configuration file consists of sections, each led by a [section] header," That and "The section name appears on a line by itself, in square brackets ([ and ])." from
    https://en.wikipedia.org/wiki/.ini#Sections
    mark [] as delimiters. I am rather sure that no other language/system allows square brackets in the section name. If you know differently, please present evidence. Unescaped delimiters are generally not allowed between delimiters unless there is a semantic reason to have nesting, and there is not one here. Parentheses, angle brackets, and curly brackets (and more from the rest of Unicode) are available to use. The request here is similar to asking that
    'abc'cd'
    be parsed as one string. Parsing nested constructs is much more complicated than parsing flat constructs.

    @terryjreedy terryjreedy changed the title ConfigParser should be greedy when parsing section name ConfigParser should nested [] in section names. Mar 15, 2014
    @miloskomarcevic
    Copy link
    Mannequin Author

    miloskomarcevic mannequin commented Mar 15, 2014

    Thanks for the exhaustive explanation.

    I did however come across a proprietary application that stores it's configuration in an INI like file that exhibits this corner case behaviour with section names, hence the suggestion for enhancement.

    @terryjreedy
    Copy link
    Member

    I see. Perhaps it uses a proprietary .ini reader ;-).

    @xflr6
    Copy link
    Mannequin

    xflr6 mannequin commented Jan 29, 2015

    If this is the intended behaviuour, I guess ConfigParser should warn the user if he supplies a section name with a ']' (prevent failed roundtrips).

    See http://bugs.python.org/issue23301

    The current behaviour looks like the opposite of Postel's law.

    @spaceone
    Copy link
    Mannequin

    spaceone mannequin commented Nov 24, 2015

    IMHO your rejection is stupid. User input should always be validated.

    At least a ValueError should be raised if add_section() is called with a string containing ']\x00\n['. As this will always create a broken configuration.

    Otherwise ConfigParser cannot be used to write new config files without having deeper knowledge about the implementation.

    @bitdancer
    Copy link
    Member

    The enhancement request was rejected. At this point I think it would be better to open a new bug requesting that an error be raised if the supplied section name contains a ']'. The question there is if there are backward compatibility issues. That can be discussed on the new issue you open.

    @terryjreedy
    Copy link
    Member

    Sebastian: you have it backwards. A paraphrase of Postel's recommendation (calling it a Law is wrong) is 'accept dirty data, emit clean data'. This is the current behavior. See https://en.wikipedia.org/wiki/Robustness_principle. This article also explains the opposite viewpoint, that dirty data should be rejected, as SpaceOne is suggesting.

    SpaceOne: unless it is your intention to discourage people from volunteering their time to respond to issues raised on the tracker, you should read what they write more carefully and think more carefully about how you express your opinions. If you really want a ValueError here, open an new enhancement issue for 3.6.

    @terryjreedy terryjreedy added type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error labels Nov 24, 2015
    @spaceone
    Copy link
    Mannequin

    spaceone mannequin commented Nov 24, 2015

    Sorry about that!
    I created http://bugs.python.org/issue25723.

    @xflr6
    Copy link
    Mannequin

    xflr6 mannequin commented Nov 24, 2015

    Terry: I am not so sure about that interpretation. Do we agree that the INI-files are the data/message? ConfigParser refuses to accept dirty INI-Files (with ']' in section names) but will produce this kind of files.
    I we see the arguments given to ConfigParser as data/message, it does indeed accept dirty data as you say, but still it does not emit clean one in that case, right?

    @BreamoreBoy
    Copy link
    Mannequin

    BreamoreBoy mannequin commented Nov 24, 2015

    Why the debate on an issue that was closed over 18 months ago?

    @terryjreedy
    Copy link
    Member

    Discussion continues because my close message was, I now realize, incomplete and therefore unsatisfying. Ditto for the doc. So I complete my close message here and reopen issue to augment the doc.

    The discussion has so far has glossed over the key question: "What is a legal section name?" Pulling the answer from the doc was a challenge. It uses 'legal section name', once, as if one should already know. Reading further, I found the answer: there is no (fixed) answer!

    The legal section name for a particular parser is determined by its .SECTCRE class attribute.
    '''configparser.SECTCRE
    A compiled regular expression used to parse section headers. The default matches [section] to the name "section".''' (This neglects to say whether the closing ']' is the first or last ']' on the line after the opening '['.) A non-verbose version of the default is
    re.compile(r"\[(?P<header>[^]]+)\]").

    I propose adding near the top of the doc:
    "By default, a legal section name can be any string that does not contain '\n' or ']'. To change this, see configparser.SECTCRE."

    So my response to Miloš should have been to set SECTCRE to something like p below.

    >>> p = re.compile(r"\[(?P<header>.*)\]")
    >>> m = p.search("[Test[2]_foo]")
    >>> m.group('header')
    'Test[2]_foo'

    Additional note: Postel's principle was formulated for internet protocols, which .ini files are not. In any case, it is not a Python design principle. Neither is "always check user input", which amounts to 'look before you leap'. So I will not debate these. However, "Errors should never pass silently." is #10 on the Zen of Python ('import this') and that I do attend to.

    @terryjreedy terryjreedy added the docs Documentation in the Doc dir label Nov 24, 2015
    @terryjreedy terryjreedy reopened this Nov 24, 2015
    @vstinner
    Copy link
    Member

    It would be good if ConfigParser supported angled brackets in section names by being greedy when parsing.

    I agree with Terry, it's the opposite: we must explicitly reject them to be compatible with other applications. Please move the discussion to issue bpo-25723.

    @terryjreedy
    Copy link
    Member

    Victor, I reopened this a a doc issue to add the sentence that would have cut short the discussion. Please leave it.

    @terryjreedy terryjreedy reopened this Nov 25, 2015
    @terryjreedy terryjreedy changed the title ConfigParser should nested [] in section names. Explain ConfigParser 'valid section name' and .SECTCRE Nov 25, 2015
    @bitdancer
    Copy link
    Member

    Yes, the doc issue is separate from the other bug, since that one will apply only to 3.6, and the doc changes apply to all maintenance releases.

    @iritkatriel
    Copy link
    Member

    The comment Terry suggests to add (see https://bugs.python.org/issue20923#msg255304) could be placed in this paragraph:

    https://docs.python.org/3/library/configparser.html#supported-ini-file-structure

    @iritkatriel iritkatriel added 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes labels Jan 31, 2022
    @iritkatriel iritkatriel changed the title Explain ConfigParser 'valid section name' and .SECTCRE [doc] Explain ConfigParser 'valid section name' and .SECTCRE Jan 31, 2022
    @Vidhyavinu
    Copy link
    Mannequin

    Vidhyavinu mannequin commented Feb 14, 2022

    I am newbie to Python developer group and have the document source build and compiled successfully.

    Is this issue updated in the document.
    I would like to work on this if this is not closed.
    Please let me know.

    Thanks

    @iritkatriel
    Copy link
    Member

    It’s still open. Go for it.

    @Vidhyavinu
    Copy link
    Mannequin

    Vidhyavinu mannequin commented Feb 15, 2022

    Thanks.
    A pull request is created at #31359.

    @iritkatriel
    Copy link
    Member

    New changeset bba8008 by vidhya in branch 'main':
    bpo-20923 : [doc] Explain ConfigParser 'valid section name' and .SECTCRE (GH-31413)
    bba8008

    @miss-islington
    Copy link
    Contributor

    New changeset a7af34d by Miss Islington (bot) in branch '3.10':
    [3.10] bpo-20923 : [doc] Explain ConfigParser 'valid section name' and .SECTCRE (GH-31413) (GH-31506)
    a7af34d

    @miss-islington
    Copy link
    Contributor

    New changeset 2387aea by Miss Islington (bot) in branch '3.9':
    [3.9] bpo-20923 : [doc] Explain ConfigParser 'valid section name' and .SECTCRE (GH-31413) (GH-31507)
    2387aea

    @iritkatriel
    Copy link
    Member

    Thank you @Vidhya!

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 only security fixes 3.11 only security fixes docs Documentation in the Doc dir easy type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants