]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-85308: argparse: Use filesystem encoding for arguments file (GH-93277)
authorInada Naoki <songofacandy@gmail.com>
Thu, 23 Jun 2022 03:09:57 +0000 (12:09 +0900)
committerGitHub <noreply@github.com>
Thu, 23 Jun 2022 03:09:57 +0000 (12:09 +0900)
Doc/library/argparse.rst
Doc/whatsnew/3.12.rst
Lib/argparse.py
Misc/NEWS.d/next/Library/2022-05-27-10-52-06.gh-issue-85308.K6r-tJ.rst [new file with mode: 0644]

index 0e62e99d706d4ca3926c343b10f8baf9fd9f042f..b2fa0b3c23c3a129350aca2b04ff6afbec7f807a 100644 (file)
@@ -562,7 +562,7 @@ at the command line.  If the ``fromfile_prefix_chars=`` argument is given to the
 specified characters will be treated as files, and will be replaced by the
 arguments they contain.  For example::
 
-   >>> with open('args.txt', 'w') as fp:
+   >>> with open('args.txt', 'w', encoding=sys.getfilesystemencoding()) as fp:
    ...     fp.write('-f\nbar')
    >>> parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
    >>> parser.add_argument('-f')
@@ -575,9 +575,18 @@ were in the same place as the original file referencing argument on the command
 line.  So in the example above, the expression ``['-f', 'foo', '@args.txt']``
 is considered equivalent to the expression ``['-f', 'foo', '-f', 'bar']``.
 
+:class:`ArgumentParser` uses :term:`filesystem encoding and error handler`
+to read the file containing arguments.
+
 The ``fromfile_prefix_chars=`` argument defaults to ``None``, meaning that
 arguments will never be treated as file references.
 
+.. versionchanged:: 3.12
+   :class:`ArgumentParser` changed encoding and errors to read arguments files
+   from default (e.g. :func:`locale.getpreferredencoding(False)` and
+   ``"strict"``) to :term:`filesystem encoding and error handler`.
+   Arguments file should be encoded in UTF-8 instead of ANSI Codepage on Windows.
+
 
 argument_default
 ^^^^^^^^^^^^^^^^
index 2439479b458d377b9185849040e4fb1dc59679ae..8dde1350a7b166c914c19dac04da26d26829945d 100644 (file)
@@ -233,6 +233,12 @@ Changes in the Python API
   select from a larger range than ``randrange(10**25)``.
   (Originally suggested by Serhiy Storchaka gh-86388.)
 
+* :class:`argparse.ArgumentParser` changed encoding and error handler
+  for reading arguments from file (e.g. ``fromfile_prefix_chars`` option)
+  from default text encoding (e.g. :func:`locale.getpreferredencoding(False) <locale.getpreferredencoding>`)
+  to :term:`filesystem encoding and error handler`.
+  Argument files should be encoded in UTF-8 instead of ANSI Codepage on Windows.
+
 
 Build Changes
 =============
index 1c5520c4b41bd19623a10553b368dc0d13df581e..02e98bbf920cf1dcc0d05c57cce1336efb17f465 100644 (file)
@@ -2161,7 +2161,9 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
             # replace arguments referencing files with the file content
             else:
                 try:
-                    with open(arg_string[1:]) as args_file:
+                    with open(arg_string[1:],
+                              encoding=_sys.getfilesystemencoding(),
+                              errors=_sys.getfilesystemencodeerrors()) as args_file:
                         arg_strings = []
                         for arg_line in args_file.read().splitlines():
                             for arg in self.convert_arg_line_to_args(arg_line):
diff --git a/Misc/NEWS.d/next/Library/2022-05-27-10-52-06.gh-issue-85308.K6r-tJ.rst b/Misc/NEWS.d/next/Library/2022-05-27-10-52-06.gh-issue-85308.K6r-tJ.rst
new file mode 100644 (file)
index 0000000..4574264
--- /dev/null
@@ -0,0 +1,4 @@
+Changed :class:`argparse.ArgumentParser` to use :term:`filesystem encoding
+and error handler` instead of default text encoding to read arguments from
+file (e.g. ``fromfile_prefix_chars`` option). This change affects Windows;
+argument file should be encoded with UTF-8 instead of ANSI Codepage.