Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Closed
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions 8 Doc/library/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,14 @@ The following exceptions are used as warning categories; see the
Base class for warnings generated by user code.


.. exception:: EncodingWarning

Base class for warnings about encodings when those warnings are intended for
other Python developers.

.. versionadded:: 3.9


.. exception:: DeprecationWarning

Base class for warnings about deprecated features when those warnings are
Expand Down
1 change: 1 addition & 0 deletions 1 Include/pyerrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ PyAPI_DATA(PyObject *) PyExc_WindowsError;
/* Predefined warning categories */
PyAPI_DATA(PyObject *) PyExc_Warning;
PyAPI_DATA(PyObject *) PyExc_UserWarning;
PyAPI_DATA(PyObject *) PyExc_EncodingWarning;
PyAPI_DATA(PyObject *) PyExc_DeprecationWarning;
PyAPI_DATA(PyObject *) PyExc_PendingDeprecationWarning;
PyAPI_DATA(PyObject *) PyExc_SyntaxWarning;
Expand Down
6 changes: 6 additions & 0 deletions 6 Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1986,6 +1986,12 @@ def __init__(self, buffer, encoding=None, errors=None, newline=None,
line_buffering=False, write_through=False):
self._check_newline(newline)
if encoding is None:
import warnings
stack_level = 2
if sys._getframe(1).f_code.co_name == "open":
# open(encoding=None): report the caller frame
stack_level += 1
warnings.warn("encoding=None", EncodingWarning, stack_level)
try:
encoding = os.device_encoding(buffer.fileno())
except (AttributeError, UnsupportedOperation):
Expand Down
5 changes: 5 additions & 0 deletions 5 Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -3642,6 +3642,11 @@ def test_issue25862(self):
t.write('x')
t.tell()

def test_encoding_warning(self):
with support.check_warnings(('encoding=None', EncodingWarning)):
t = self.TextIOWrapper(self.BytesIO(b'test'), encoding=None)
t.close()


class MemviewBytesIO(io.BytesIO):
'''A BytesIO object whose read method returns memoryviews
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add a new :exc:`EncodingWarning` warning category. :func:`open`
(:func:`io.open`) now emits an :exc:`EncodingWarning` if called with
``encoding=None``.
4 changes: 4 additions & 0 deletions 4 Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,10 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
}
}
if (encoding == NULL && self->encoding == NULL) {
if (PyErr_WarnEx(PyExc_EncodingWarning, "encoding=None", 0)) {
goto error;
}

PyObject *locale_module = _PyIO_get_locale_module(state);
if (locale_module == NULL)
goto catch_ImportError;
Expand Down
9 changes: 9 additions & 0 deletions 9 Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -2375,6 +2375,13 @@ SimpleExtendsException(PyExc_Warning, UserWarning,
"Base class for warnings generated by user code.");


/*
* EncodingWarning extends Warning
*/
SimpleExtendsException(PyExc_Warning, EncodingWarning,
"Base class for warnings about encodings.");


/*
* DeprecationWarning extends Warning
*/
Expand Down Expand Up @@ -2560,6 +2567,7 @@ _PyExc_Init(void)
PRE_INIT(BufferError);
PRE_INIT(Warning);
PRE_INIT(UserWarning);
PRE_INIT(EncodingWarning);
PRE_INIT(DeprecationWarning);
PRE_INIT(PendingDeprecationWarning);
PRE_INIT(SyntaxWarning);
Expand Down Expand Up @@ -2701,6 +2709,7 @@ _PyBuiltins_AddExceptions(PyObject *bltinmod)
POST_INIT(BufferError);
POST_INIT(Warning);
POST_INIT(UserWarning);
POST_INIT(EncodingWarning);
POST_INIT(DeprecationWarning);
POST_INIT(PendingDeprecationWarning);
POST_INIT(SyntaxWarning);
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.