summaryrefslogtreecommitdiffstats
path: root/Modules/_io/clinic
diff options
context:
space:
mode:
authorInada Naoki <songofacandy@gmail.com>2021-03-29 03:28:14 (GMT)
committerGitHub <noreply@github.com>2021-03-29 03:28:14 (GMT)
commit4827483f47906fecee6b5d9097df2a69a293a85c (patch)
treec4d7e34163e9583c06003d5335d020ce27aa4559 /Modules/_io/clinic
parent261a452a1300eeeae1428ffd6e6623329c085e2c (diff)
downloadcpython-4827483f47906fecee6b5d9097df2a69a293a85c.zip
cpython-4827483f47906fecee6b5d9097df2a69a293a85c.tar.gz
cpython-4827483f47906fecee6b5d9097df2a69a293a85c.tar.bz2
bpo-43510: Implement PEP 597 opt-in EncodingWarning. (GH-19481)
See [PEP 597](https://www.python.org/dev/peps/pep-0597/). * Add `-X warn_default_encoding` and `PYTHONWARNDEFAULTENCODING`. * Add EncodingWarning * Add io.text_encoding() * open(), TextIOWrapper() emits EncodingWarning when encoding is omitted and warn_default_encoding is enabled. * _pyio.TextIOWrapper() uses UTF-8 as fallback default encoding used when failed to import locale module. (used during building Python) * bz2, configparser, gzip, lzma, pathlib, tempfile modules use io.text_encoding(). * What's new entry
Diffstat (limited to 'Modules/_io/clinic')
-rw-r--r--Modules/_io/clinic/_iomodule.c.h48
1 files changed, 47 insertions, 1 deletions
diff --git a/Modules/_io/clinic/_iomodule.c.h b/Modules/_io/clinic/_iomodule.c.h
index dc7b5ff..91c55b1 100644
--- a/Modules/_io/clinic/_iomodule.c.h
+++ b/Modules/_io/clinic/_iomodule.c.h
@@ -272,6 +272,52 @@ exit:
return return_value;
}
+PyDoc_STRVAR(_io_text_encoding__doc__,
+"text_encoding($module, encoding, stacklevel=2, /)\n"
+"--\n"
+"\n"
+"A helper function to choose the text encoding.\n"
+"\n"
+"When encoding is not None, just return it.\n"
+"Otherwise, return the default text encoding (i.e. \"locale\").\n"
+"\n"
+"This function emits an EncodingWarning if encoding is None and\n"
+"sys.flags.warn_default_encoding is true.\n"
+"\n"
+"This can be used in APIs with an encoding=None parameter.\n"
+"However, please consider using encoding=\"utf-8\" for new APIs.");
+
+#define _IO_TEXT_ENCODING_METHODDEF \
+ {"text_encoding", (PyCFunction)(void(*)(void))_io_text_encoding, METH_FASTCALL, _io_text_encoding__doc__},
+
+static PyObject *
+_io_text_encoding_impl(PyObject *module, PyObject *encoding, int stacklevel);
+
+static PyObject *
+_io_text_encoding(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
+{
+ PyObject *return_value = NULL;
+ PyObject *encoding;
+ int stacklevel = 2;
+
+ if (!_PyArg_CheckPositional("text_encoding", nargs, 1, 2)) {
+ goto exit;
+ }
+ encoding = args[0];
+ if (nargs < 2) {
+ goto skip_optional;
+ }
+ stacklevel = _PyLong_AsInt(args[1]);
+ if (stacklevel == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+skip_optional:
+ return_value = _io_text_encoding_impl(module, encoding, stacklevel);
+
+exit:
+ return return_value;
+}
+
PyDoc_STRVAR(_io_open_code__doc__,
"open_code($module, /, path)\n"
"--\n"
@@ -313,4 +359,4 @@ _io_open_code(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObjec
exit:
return return_value;
}
-/*[clinic end generated code: output=5c0dd7a262c30ebc input=a9049054013a1b77]*/
+/*[clinic end generated code: output=06e055d1d80b835d input=a9049054013a1b77]*/