Skip to content

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

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

Closed
tomasr8 opened this issue Apr 8, 2023 · 0 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@tomasr8
Copy link
Member

tomasr8 commented Apr 8, 2023

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

@tomasr8 tomasr8 added the type-bug An unexpected behavior, bug, or error label Apr 8, 2023
@AlexWaygood AlexWaygood added the stdlib Python modules in the Lib dir label Apr 8, 2023
dhuadaar added a commit to dhuadaar/cpython that referenced this issue Apr 9, 2023
dhuadaar added a commit to dhuadaar/cpython that referenced this issue Apr 9, 2023
dhuadaar added a commit to dhuadaar/cpython that referenced this issue Apr 9, 2023
dhuadaar added a commit to dhuadaar/cpython that referenced this issue Apr 13, 2023
dhuadaar added a commit to dhuadaar/cpython that referenced this issue Apr 16, 2023
AlexWaygood added a commit to dhuadaar/cpython that referenced this issue May 2, 2023
erlend-aasland added a commit to dhuadaar/cpython that referenced this issue Aug 4, 2023
dhuadaar added a commit to dhuadaar/cpython that referenced this issue Aug 25, 2023
vsajip added a commit that referenced this issue Aug 25, 2023
…RN` to allow spaces and non-alphanumeric characters in keys. (GH-103391)

Co-authored-by: Vinay Sajip <[email protected]>
Co-authored-by: Alex Waygood <[email protected]>
Co-authored-by: Erlend E. Aasland <[email protected]>
@vsajip vsajip closed this as completed Aug 25, 2023
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
Development

No branches or pull requests

3 participants