Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove codecs.open from error message
  • Loading branch information
methane committed Apr 28, 2025
commit cba4e03829d96bd1f91b66e148c96a22c170a7f5
3 changes: 1 addition & 2 deletions Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2056,8 +2056,7 @@ def __init__(self, buffer, encoding=None, errors=None, newline=None,
raise ValueError("invalid encoding: %r" % encoding)

if not codecs.lookup(encoding)._is_text_encoding:
msg = ("%r is not a text encoding; "
"use codecs.open() to handle arbitrary codecs")
msg = "%r is not a text encoding"
raise LookupError(msg % encoding)

if errors is None:
Expand Down
5 changes: 2 additions & 3 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
}

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

// Create new encoder & decoder
PyObject *codec_info = _PyCodec_LookupTextEncoding(
c_encoding, "codecs.open()");
PyObject *codec_info = _PyCodec_LookupTextEncoding(c_encoding, NULL);
if (codec_info == NULL) {
Py_DECREF(encoding);
Py_DECREF(errors);
Expand Down
18 changes: 13 additions & 5 deletions Python/codecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,11 +540,19 @@ PyObject * _PyCodec_LookupTextEncoding(const char *encoding,
Py_DECREF(attr);
if (is_text_codec <= 0) {
Py_DECREF(codec);
if (!is_text_codec)
PyErr_Format(PyExc_LookupError,
"'%.400s' is not a text encoding; "
"use %s to handle arbitrary codecs",
encoding, alternate_command);
if (!is_text_codec) {
if (alternate_command != NULL) {
PyErr_Format(PyExc_LookupError,
"'%.400s' is not a text encoding; "
"use %s to handle arbitrary codecs",
encoding, alternate_command);
}
else {
PyErr_Format(PyExc_LookupError,
"'%.400s' is not a text encoding",
encoding);
}
}
return NULL;
}
}
Expand Down
Loading