Skip to content

Commit cba4e03

Browse files
committed
remove codecs.open from error message
1 parent ff0d25d commit cba4e03

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

Lib/_pyio.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,8 +2056,7 @@ def __init__(self, buffer, encoding=None, errors=None, newline=None,
20562056
raise ValueError("invalid encoding: %r" % encoding)
20572057

20582058
if not codecs.lookup(encoding)._is_text_encoding:
2059-
msg = ("%r is not a text encoding; "
2060-
"use codecs.open() to handle arbitrary codecs")
2059+
msg = "%r is not a text encoding"
20612060
raise LookupError(msg % encoding)
20622061

20632062
if errors is None:

Modules/_io/textio.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
11851185
}
11861186

11871187
/* Check we have been asked for a real text encoding */
1188-
codec_info = _PyCodec_LookupTextEncoding(encoding, "codecs.open()");
1188+
codec_info = _PyCodec_LookupTextEncoding(encoding, NULL);
11891189
if (codec_info == NULL) {
11901190
Py_CLEAR(self->encoding);
11911191
goto error;
@@ -1324,8 +1324,7 @@ textiowrapper_change_encoding(textio *self, PyObject *encoding,
13241324
}
13251325

13261326
// Create new encoder & decoder
1327-
PyObject *codec_info = _PyCodec_LookupTextEncoding(
1328-
c_encoding, "codecs.open()");
1327+
PyObject *codec_info = _PyCodec_LookupTextEncoding(c_encoding, NULL);
13291328
if (codec_info == NULL) {
13301329
Py_DECREF(encoding);
13311330
Py_DECREF(errors);

Python/codecs.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -540,11 +540,19 @@ PyObject * _PyCodec_LookupTextEncoding(const char *encoding,
540540
Py_DECREF(attr);
541541
if (is_text_codec <= 0) {
542542
Py_DECREF(codec);
543-
if (!is_text_codec)
544-
PyErr_Format(PyExc_LookupError,
545-
"'%.400s' is not a text encoding; "
546-
"use %s to handle arbitrary codecs",
547-
encoding, alternate_command);
543+
if (!is_text_codec) {
544+
if (alternate_command != NULL) {
545+
PyErr_Format(PyExc_LookupError,
546+
"'%.400s' is not a text encoding; "
547+
"use %s to handle arbitrary codecs",
548+
encoding, alternate_command);
549+
}
550+
else {
551+
PyErr_Format(PyExc_LookupError,
552+
"'%.400s' is not a text encoding",
553+
encoding);
554+
}
555+
}
548556
return NULL;
549557
}
550558
}

0 commit comments

Comments
 (0)