Skip to content

logging.config cfg protocol rejects non-alphanumeric names, contrary to the documentation #103384

Closed
@tomasr8

Description

@tomasr8

Description

In logging.config, when using the cfg protocol, names with spaces or non-alphanumeric characters (e.g. cfg://nested[prop with spaces]) raise a ValueError contrary to what is stated in the documentation.

The documentation states that one can use the bracket notation:

The latter form only needs to be used if the key contains spaces or non-alphanumeric characters.

This does not work however:

from logging.config import BaseConfigurator

config = {'nested': {'prop': 1, 'prop with spaces': 2}}  # Create a simple logging config
bc = BaseConfigurator(config)
bc.cfg_convert('nested[prop]')  # returns 1 (expected)
bc.cfg_convert('nested[prop with spaces]')  # Raises ValueError

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/tomas/dev/cpython/Lib/logging/config.py", line 433, in cfg_convert
    raise ValueError('Unable to convert '
ValueError: Unable to convert 'nested[prop with spaces]' at '[prop with spaces]'

ValueError is also raised for any non-alphanumeric sequence:

bc.cfg_convert('nested[!?]')  # Raises

The culprit is the regex pattern (BaseConfigurator.INDEX_PATTERN) which is used for matching the bracket contents: ^\[\s*(\w+)\s*\]\s*.
This only matches alphanumeric characters. Simply changing this to ^\[([^\[\]]*)\]\s* would give us the behavior described in the docs.

Your environment

  • CPython versions tested on: 3.12.0a6+
  • Operating system and architecture: Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions