Skip to content

Commit

Permalink
Specify "locale" encoding when reading configuration file
Browse files Browse the repository at this point in the history
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: #1274
Pull-request-sha: d0abd33

Change-Id: I75c1237ba8bd2e38890d0e5e4a578a6ca0b24883
  • Loading branch information
Kevin Kirsche authored and zzzeek committed Aug 16, 2023
1 parent 23f2b36 commit 63be916
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion alembic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions alembic/util/compat.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
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

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)

Expand Down Expand Up @@ -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)
11 changes: 11 additions & 0 deletions docs/build/unreleased/1273.rst
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 63be916

Please sign in to comment.