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')
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
^^^^^^^^^^^^^^^^
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
=============
# 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):
--- /dev/null
+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.