From 63be91641dde3fba6db408c2f41824637801d284 Mon Sep 17 00:00:00 2001 From: Kevin Kirsche Date: Fri, 11 Aug 2023 10:06:36 -0400 Subject: [PATCH] Specify `"locale"` encoding when reading configuration file Added ``encoding="locale"`` setting to the use of Python's ``ConfigParser.read()``, so that a warning is not generated when using the recently added Python feature ``PYTHONWARNDEFAULTENCODING`` specified in :pep:`597`. The encoding is passed as the ``"locale"`` string under Python 3.10 and greater, which indicates that the system-level locale should be used, as was the case already here. Pull request courtesy Kevin Kirsche. Fixes: #1273 Closes: #1274 Pull-request: https://github.com/sqlalchemy/alembic/pull/1274 Pull-request-sha: d0abd33845754827b8ce69ba1360989c7c57fdb6 Change-Id: I75c1237ba8bd2e38890d0e5e4a578a6ca0b24883 --- alembic/config.py | 2 +- alembic/util/compat.py | 13 +++++++++++++ docs/build/unreleased/1273.rst | 11 +++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 docs/build/unreleased/1273.rst diff --git a/alembic/config.py b/alembic/config.py index d01173cb..41e941b3 100644 --- a/alembic/config.py +++ b/alembic/config.py @@ -200,7 +200,7 @@ def file_config(self): self.config_args["here"] = here file_config = ConfigParser(self.config_args) if self.config_file_name: - file_config.read([self.config_file_name]) + compat.read_config_parser(file_config, [self.config_file_name]) else: file_config.add_section(self.config_ini_section) return file_config diff --git a/alembic/util/compat.py b/alembic/util/compat.py index a5e0b535..31e0208d 100644 --- a/alembic/util/compat.py +++ b/alembic/util/compat.py @@ -1,10 +1,12 @@ from __future__ import annotations +from configparser import ConfigParser import io import os import sys import typing from typing import Sequence +from typing import Union from sqlalchemy.util import inspect_getfullargspec # noqa from sqlalchemy.util.compat import inspect_formatargspec # noqa @@ -12,6 +14,7 @@ is_posix = os.name == "posix" py311 = sys.version_info >= (3, 11) +py310 = sys.version_info >= (3, 10) py39 = sys.version_info >= (3, 9) py38 = sys.version_info >= (3, 8) @@ -58,3 +61,13 @@ def formatannotation_fwdref(annotation, base_module=None): elif isinstance(annotation, typing.TypeVar): return repr(annotation).replace("~", "") return repr(annotation).replace("~", "") + + +def read_config_parser( + file_config: ConfigParser, + file_argument: Sequence[Union[str, os.PathLike[str]]], +) -> list[str]: + if py310: + return file_config.read(file_argument, encoding="locale") + else: + return file_config.read(file_argument) diff --git a/docs/build/unreleased/1273.rst b/docs/build/unreleased/1273.rst new file mode 100644 index 00000000..1577041b --- /dev/null +++ b/docs/build/unreleased/1273.rst @@ -0,0 +1,11 @@ +.. change:: + :tags: bug, commands + :tickets: 1273 + + Added ``encoding="locale"`` setting to the use of Python's + ``ConfigParser.read()``, so that a warning is not generated when using the + recently added Python feature ``PYTHONWARNDEFAULTENCODING`` specified in + :pep:`597`. The encoding is passed as the ``"locale"`` string under Python + 3.10 and greater, which indicates that the system-level locale should be + used, as was the case already here. Pull request courtesy Kevin Kirsche. +