Skip to content

_pyio: NameError: name 'locale' is not defined #93099

Closed
@The-Compiler

Description

@The-Compiler

Bug report

In 0729b31 (#91982), @methane implemented encoding="locale" support for TextIOWrapper.reconfigure.

However, the implementation in _pyio.py can't possibly work, as locale is not imported in the scope it's used:

encoding = locale.getencoding()

it's only imported locally in __init__:

cpython/Lib/_pyio.py

Lines 2024 to 2031 in 760ec89

if encoding == "locale":
try:
import locale
except ImportError:
# Importing locale may fail if Python is being built
encoding = "utf-8"
else:
encoding = locale.getencoding()

Thus causing python -c 'import _pyio as io; wrapper = io.TextIOWrapper(io.BytesIO(b"test")); wrapper.reconfigure(encoding="locale")' to fail with:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.11/_pyio.py", line 2165, in reconfigure
    encoding = locale.getencoding()
               ^^^^^^
NameError: name 'locale' is not defined. Did you mean: 'locals'?

(Found by running flake8 over Lib/ out of curiosity, see #93010 (comment))

Your environment

  • CPython versions tested on: Python 3.11.0b1
  • Operating system and architecture: Archlinux x86_64

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only security fixes3.12only security fixestopic-IOtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions