◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
11 changes: 8 additions & 3 deletions Doc/library/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,13 @@ High-level Module Interface
This is a helper function for callables that use :func:`open` or
:class:`TextIOWrapper` and have an ``encoding=None`` parameter.

This function returns *encoding* if it is not ``None`` and ``"locale"`` if
*encoding* is ``None``.

This function emits an :class:`EncodingWarning` if
:data:`sys.flags.warn_default_encoding <sys.flags>` is true and *encoding*
is None. *stacklevel* specifies where the warning is emitted.
For example::

def read_text(path, encoding=None):
Expand All @@ -218,6 +219,10 @@ High-level Module Interface

.. versionadded:: 3.10


.. exception:: BlockingIOError

Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct _Py_global_strings {
STRUCT_FOR_STR(newline, "\n")
STRUCT_FOR_STR(open_br, "{")
STRUCT_FOR_STR(percent, "%")
} literals;

struct {
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_runtime_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ extern "C" {
INIT_STR(newline, "\n"), \
INIT_STR(open_br, "{"), \
INIT_STR(percent, "%"), \
}, \
.identifiers = { \
INIT_ID(False), \
Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_utf8_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def test_io(self):
filename = __file__

out = self.get_output('-c', code, filename, PYTHONUTF8='1')
self.assertEqual(out, 'UTF-8/strict')

def _check_io_encoding(self, module, encoding=None, errors=None):
filename = __file__
Expand All @@ -183,10 +183,10 @@ def _check_io_encoding(self, module, encoding=None, errors=None):
PYTHONUTF8='1')

if not encoding:
encoding = 'UTF-8'
if not errors:
errors = 'strict'
self.assertEqual(out, f'{encoding}/{errors}')

def check_io_encoding(self, module):
self._check_io_encoding(module, encoding="latin1")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16 changes: 12 additions & 4 deletions Modules/_io/_iomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,9 @@ _io.text_encoding

A helper function to choose the text encoding.

When encoding is not None, just return it.
Otherwise, return the default text encoding (i.e. "locale").

This function emits an EncodingWarning if encoding is None and
sys.flags.warn_default_encoding is true.
Expand All @@ -469,7 +470,7 @@ However, please consider using encoding="utf-8" for new APIs.

static PyObject *
_io_text_encoding_impl(PyObject *module, PyObject *encoding, int stacklevel)
/*[clinic end generated code: output=91b2cfea6934cc0c input=bf70231213e2a7b4]*/
{
if (encoding == NULL || encoding == Py_None) {
PyInterpreterState *interp = _PyInterpreterState_GET();
Expand All @@ -479,7 +480,14 @@ _io_text_encoding_impl(PyObject *module, PyObject *encoding, int stacklevel)
return NULL;
}
}
return &_Py_ID(locale);
}
Py_INCREF(encoding);
return encoding;
7 changes: 4 additions & 3 deletions Modules/_io/clinic/_iomodule.c.h
5 changes: 4 additions & 1 deletion Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,10 @@ static PyObject *
sys_getdefaultencoding_impl(PyObject *module)
/*[clinic end generated code: output=256d19dfcc0711e6 input=d416856ddbef6909]*/
{
return PyUnicode_FromString(PyUnicode_GetDefaultEncoding());
}

/*[clinic input]
Expand Down
Toggle all file notes Toggle all file annotations