From 3d1d12f5002d73c49e52f76968ae524a50f941b6 Mon Sep 17 00:00:00 2001 From: Cool Guy Date: Tue, 24 Nov 2026 18:48:59 +0800 Subject: [PATCH] [3.15] gh-148603: Update docstrings now UTF-8 is default (GH-162327) (cherry picked from commit 814a3031d3d13bb7eb0b9f597c458dca4b0d4067) Co-authored-by: Cool Guy --- Lib/_pyio.py | 12 ++++++++---- Modules/_io/_iomodule.c | 19 ++++++++----------- Modules/_io/clinic/_iomodule.c.h | 19 ++++++++----------- Modules/_io/clinic/textio.c.h | 7 +++++-- Modules/_io/textio.c | 7 +++++-- 5 files changed, 34 insertions(+), 30 deletions(-) diff --git a/Lib/_pyio.py b/Lib/_pyio.py index a1336f5532e7eb..42ed3433cc82e4 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -2063,10 +2063,14 @@ class TextIOWrapper(TextIOBase): r"""Character and line based layer over a BufferedIOBase object, buffer. encoding gives the name of the encoding that the stream will be - decoded or encoded with. It defaults to locale.getencoding(). - - errors determines the strictness of encoding and decoding (see the - codecs.register) and defaults to "strict". + decoded or encoded with. It defaults to UTF-8; if UTF-8 Mode is + disabled, the encoding returned by locale.getencoding() is used + instead. See the codecs module for the list of supported + encodings. + + errors determines the strictness of encoding and decoding (see + help(codecs.Codec) or the documentation for codecs.register) and + defaults to "strict". newline can be None, '', '\n', '\r', or '\r\n'. It controls the handling of line endings. If it is None, universal newlines is diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c index 03e6fbe08889d4..0d28fe24fa0b3a 100644 --- a/Modules/_io/_iomodule.c +++ b/Modules/_io/_iomodule.c @@ -94,10 +94,7 @@ mode. Other common values are 'w' for writing (truncating the file if it already exists), 'x' for creating and writing to a new file, and 'a' for appending (which on some Unix systems, means that all writes append to the end of the file regardless of the current seek position). -In text mode, if encoding is not specified the encoding used is platform -dependent: locale.getencoding() is called to get the current locale -encoding. (For reading and writing raw bytes use binary mode and leave -encoding unspecified.) The available modes are: +The available modes are: ========= ========================================================== Character Meaning @@ -122,8 +119,7 @@ even when the underlying operating system doesn't. Files opened in binary mode (appending 'b' to the mode argument) return contents as bytes objects without any decoding. In text mode (the default, or when 't' is appended to the mode argument), the contents of the file are -returned as strings, the bytes having been first decoded using a -platform-dependent encoding or using the specified encoding if given. +decoded using the encoding described below and returned as strings. buffering is an optional integer used to set the buffering policy. Pass 0 to switch buffering off (only allowed in binary mode), 1 to @@ -140,10 +136,11 @@ argument is given, the default buffering policy works as follows: use line buffering. Other text files use the policy described above for binary files. -encoding is the name of the encoding used to decode or encode the -file. This should only be used in text mode. The default encoding is -platform dependent, but any encoding supported by Python can be -passed. See the codecs module for the list of supported encodings. +encoding is the name of the encoding used to decode or encode the file. +This should only be used in text mode. The default encoding is UTF-8; +if UTF-8 Mode is disabled, the encoding returned by +locale.getencoding() is used instead. See the codecs module for the +list of supported encodings. errors is an optional string that specifies how encoding errors are to be handled---this argument should not be used in binary mode. Pass @@ -200,7 +197,7 @@ static PyObject * _io_open_impl(PyObject *module, PyObject *file, const char *mode, int buffering, const char *encoding, const char *errors, const char *newline, int closefd, PyObject *opener) -/*[clinic end generated code: output=aefafc4ce2b46dc0 input=b3cefa70bef404b3]*/ +/*[clinic end generated code: output=aefafc4ce2b46dc0 input=5a005681afa02242]*/ { size_t i; diff --git a/Modules/_io/clinic/_iomodule.c.h b/Modules/_io/clinic/_iomodule.c.h index f03638064385e2..e12696beca4bb9 100644 --- a/Modules/_io/clinic/_iomodule.c.h +++ b/Modules/_io/clinic/_iomodule.c.h @@ -27,10 +27,7 @@ PyDoc_STRVAR(_io_open__doc__, "it already exists), \'x\' for creating and writing to a new file, and\n" "\'a\' for appending (which on some Unix systems, means that all writes\n" "append to the end of the file regardless of the current seek position).\n" -"In text mode, if encoding is not specified the encoding used is platform\n" -"dependent: locale.getencoding() is called to get the current locale\n" -"encoding. (For reading and writing raw bytes use binary mode and leave\n" -"encoding unspecified.) The available modes are:\n" +"The available modes are:\n" "\n" "========= ==========================================================\n" "Character Meaning\n" @@ -55,8 +52,7 @@ PyDoc_STRVAR(_io_open__doc__, "binary mode (appending \'b\' to the mode argument) return contents as\n" "bytes objects without any decoding. In text mode (the default, or when\n" "\'t\' is appended to the mode argument), the contents of the file are\n" -"returned as strings, the bytes having been first decoded using a\n" -"platform-dependent encoding or using the specified encoding if given.\n" +"decoded using the encoding described below and returned as strings.\n" "\n" "buffering is an optional integer used to set the buffering policy.\n" "Pass 0 to switch buffering off (only allowed in binary mode), 1 to\n" @@ -73,10 +69,11 @@ PyDoc_STRVAR(_io_open__doc__, " use line buffering. Other text files use the policy described above\n" " for binary files.\n" "\n" -"encoding is the name of the encoding used to decode or encode the\n" -"file. This should only be used in text mode. The default encoding is\n" -"platform dependent, but any encoding supported by Python can be\n" -"passed. See the codecs module for the list of supported encodings.\n" +"encoding is the name of the encoding used to decode or encode the file.\n" +"This should only be used in text mode. The default encoding is UTF-8;\n" +"if UTF-8 Mode is disabled, the encoding returned by\n" +"locale.getencoding() is used instead. See the codecs module for the\n" +"list of supported encodings.\n" "\n" "errors is an optional string that specifies how encoding errors are to\n" "be handled---this argument should not be used in binary mode. Pass\n" @@ -411,4 +408,4 @@ _io_open_code(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObjec exit: return return_value; } -/*[clinic end generated code: output=5190d11f0803bfe8 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=f5f0bb74e12703a8 input=a9049054013a1b77]*/ diff --git a/Modules/_io/clinic/textio.c.h b/Modules/_io/clinic/textio.c.h index 10d0f1390cccbb..298df582fb3a78 100644 --- a/Modules/_io/clinic/textio.c.h +++ b/Modules/_io/clinic/textio.c.h @@ -517,7 +517,10 @@ PyDoc_STRVAR(_io_TextIOWrapper___init____doc__, "Character and line based layer over a BufferedIOBase object, buffer.\n" "\n" "encoding gives the name of the encoding that the stream will be\n" -"decoded or encoded with. It defaults to locale.getencoding().\n" +"decoded or encoded with. It defaults to UTF-8; if UTF-8 Mode is\n" +"disabled, the encoding returned by locale.getencoding() is used\n" +"instead. See the codecs module for the list of supported\n" +"encodings.\n" "\n" "errors determines the strictness of encoding and decoding (see\n" "help(codecs.Codec) or the documentation for codecs.register) and\n" @@ -1362,4 +1365,4 @@ _io_TextIOWrapper_buffer_get(PyObject *self, void *Py_UNUSED(context)) return return_value; } -/*[clinic end generated code: output=e93032a0691ff0e4 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=f5db3431162a948e input=a9049054013a1b77]*/ diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index 5f9e517a13a5e4..b5ee414cabdfe0 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -1148,7 +1148,10 @@ _io.TextIOWrapper.__init__ Character and line based layer over a BufferedIOBase object, buffer. encoding gives the name of the encoding that the stream will be -decoded or encoded with. It defaults to locale.getencoding(). +decoded or encoded with. It defaults to UTF-8; if UTF-8 Mode is +disabled, the encoding returned by locale.getencoding() is used +instead. See the codecs module for the list of supported +encodings. errors determines the strictness of encoding and decoding (see help(codecs.Codec) or the documentation for codecs.register) and @@ -1180,7 +1183,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer, const char *encoding, PyObject *errors, const char *newline, int line_buffering, int write_through) -/*[clinic end generated code: output=72267c0c01032ed2 input=0f077220214c40a4]*/ +/*[clinic end generated code: output=72267c0c01032ed2 input=4f0bee5a0b2302fd]*/ { PyObject *raw, *codec_info = NULL; PyObject *res;