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

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

Closed
The-Compiler opened this issue May 23, 2022 · 0 comments · Fixed by #93136
Closed

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

The-Compiler opened this issue May 23, 2022 · 0 comments · Fixed by #93136
Labels
3.11 only security fixes 3.12 bugs and security fixes topic-IO type-bug An unexpected behavior, bug, or error

Comments

@The-Compiler
Copy link
Contributor

The-Compiler commented May 23, 2022

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
@The-Compiler The-Compiler added the type-bug An unexpected behavior, bug, or error label May 23, 2022
@corona10 corona10 added 3.11 only security fixes 3.12 bugs and security fixes labels May 23, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 24, 2022
miss-islington added a commit that referenced this issue May 24, 2022
(cherry picked from commit f7fabae)

Co-authored-by: Dong-hee Na <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 bugs and security fixes topic-IO type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants