]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Specify `"locale"` encoding when reading configuration file
authorKevin Kirsche <kevin.kirsche@one.verizon.com>
Fri, 11 Aug 2023 14:06:36 +0000 (10:06 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 16 Aug 2023 15:54:36 +0000 (11:54 -0400)
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
alembic/util/compat.py
docs/build/unreleased/1273.rst [new file with mode: 0644]

index d01173cbdcdae16613eb8f62f2f7d1d1acbe9ae2..41e941b38b196a497c0a271329de4f02131ac4ac 100644 (file)
@@ -200,7 +200,7 @@ class Config:
         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
index a5e0b5354ce9c173a90feae4be4eca34ab4a9386..31e0208d764373b52baa4d4e8f1013839f917274 100644 (file)
@@ -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 @@ from sqlalchemy.util.compat import inspect_formatargspec  # noqa
 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 (file)
index 0000000..1577041
--- /dev/null
@@ -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.
+