Skip to content

Commit 9877f4c

Browse files
authored
gh-85308: argparse: Use filesystem encoding for arguments file (GH-93277)
1 parent 576dd90 commit 9877f4c

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

Doc/library/argparse.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ at the command line. If the ``fromfile_prefix_chars=`` argument is given to the
562562
specified characters will be treated as files, and will be replaced by the
563563
arguments they contain. For example::
564564

565-
>>> with open('args.txt', 'w') as fp:
565+
>>> with open('args.txt', 'w', encoding=sys.getfilesystemencoding()) as fp:
566566
... fp.write('-f\nbar')
567567
>>> parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
568568
>>> parser.add_argument('-f')
@@ -575,9 +575,18 @@ were in the same place as the original file referencing argument on the command
575575
line. So in the example above, the expression ``['-f', 'foo', '@args.txt']``
576576
is considered equivalent to the expression ``['-f', 'foo', '-f', 'bar']``.
577577

578+
:class:`ArgumentParser` uses :term:`filesystem encoding and error handler`
579+
to read the file containing arguments.
580+
578581
The ``fromfile_prefix_chars=`` argument defaults to ``None``, meaning that
579582
arguments will never be treated as file references.
580583

584+
.. versionchanged:: 3.12
585+
:class:`ArgumentParser` changed encoding and errors to read arguments files
586+
from default (e.g. :func:`locale.getpreferredencoding(False)` and
587+
``"strict"``) to :term:`filesystem encoding and error handler`.
588+
Arguments file should be encoded in UTF-8 instead of ANSI Codepage on Windows.
589+
581590

582591
argument_default
583592
^^^^^^^^^^^^^^^^

Doc/whatsnew/3.12.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ Changes in the Python API
233233
select from a larger range than ``randrange(10**25)``.
234234
(Originally suggested by Serhiy Storchaka gh-86388.)
235235

236+
* :class:`argparse.ArgumentParser` changed encoding and error handler
237+
for reading arguments from file (e.g. ``fromfile_prefix_chars`` option)
238+
from default text encoding (e.g. :func:`locale.getpreferredencoding(False) <locale.getpreferredencoding>`)
239+
to :term:`filesystem encoding and error handler`.
240+
Argument files should be encoded in UTF-8 instead of ANSI Codepage on Windows.
241+
236242

237243
Build Changes
238244
=============

Lib/argparse.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2161,7 +2161,9 @@ def _read_args_from_files(self, arg_strings):
21612161
# replace arguments referencing files with the file content
21622162
else:
21632163
try:
2164-
with open(arg_string[1:]) as args_file:
2164+
with open(arg_string[1:],
2165+
encoding=_sys.getfilesystemencoding(),
2166+
errors=_sys.getfilesystemencodeerrors()) as args_file:
21652167
arg_strings = []
21662168
for arg_line in args_file.read().splitlines():
21672169
for arg in self.convert_arg_line_to_args(arg_line):
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Changed :class:`argparse.ArgumentParser` to use :term:`filesystem encoding
2+
and error handler` instead of default text encoding to read arguments from
3+
file (e.g. ``fromfile_prefix_chars`` option). This change affects Windows;
4+
argument file should be encoded with UTF-8 instead of ANSI Codepage.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy