Skip to content

bpo-43680: Remove undocumented io.OpenWrapper #25114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
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
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,11 @@ Removed
(Contributed by Yurii Karabas, Andrew Svetlov, Yury Selivanov and Kyle Stanley
in :issue:`42392`.)

* Remove the undocumented ``io.OpenWrapper`` and ``_pyio.OpenWrapper``
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OpenWrapper itself is not purposed to be used in user code. The main effect is that it changes builtin open if Python implementation is used. It can affect PyPy or other implementations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_pyio is supposed to be a drop-in replacement for the io module, whereas _pyio.open and io.open behave differently.

See https://bugs.python.org/issue43682 which would make it possible to use _pyio.open as a method, rather than having to go through OpenWrapper.

functions which could be used directly to define a method:
``staticmethod(open)`` should now be used instead.
(Contributed by Victor Stinner in :issue:`43680`.)


Porting to Python 3.10
======================
Expand Down
13 changes: 0 additions & 13 deletions Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,19 +313,6 @@ def __get__(self, obj, typ=None):
"errors=None, newline=None, closefd=True)\n\n" +
open.__doc__)

class OpenWrapper:
"""Wrapper for builtins.open

Trick so that open won't become a bound method when stored
as a class variable (as dbm.dumb does).

See initstdio() in Python/pylifecycle.c.
"""
__doc__ = DocDescriptor()

def __new__(cls, *args, **kwargs):
return open(*args, **kwargs)


# In normal operation, both `UnsupportedOperation`s should be bound to the
# same object.
Expand Down
2 changes: 0 additions & 2 deletions Lib/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@
BufferedWriter, BufferedRWPair, BufferedRandom,
IncrementalNewlineDecoder, text_encoding, TextIOWrapper)

OpenWrapper = _io.open # for compatibility with _pyio

# Pretend this exception was created here.
UnsupportedOperation.__module__ = "io"

Expand Down
9 changes: 8 additions & 1 deletion Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4574,6 +4574,13 @@ class PySignalsTest(SignalsTest):
test_reentrant_write_text = None


class PyioOpenWrapper:
"""Wrapper for pyio.open Trick so that open won't become a bound method
when stored as a class variable."""
def __new__(cls, *args, **kwargs):
return pyio.open(*args, **kwargs)


def load_tests(*args):
tests = (CIOTest, PyIOTest, APIMismatchTest,
CBufferedReaderTest, PyBufferedReaderTest,
Expand All @@ -4599,7 +4606,7 @@ def load_tests(*args):
c_io_ns.update((x.__name__, globs["C" + x.__name__]) for x in mocks)
py_io_ns.update((x.__name__, globs["Py" + x.__name__]) for x in mocks)
# Avoid turning open into a bound method.
py_io_ns["open"] = pyio.OpenWrapper
py_io_ns["open"] = PyioOpenWrapper
for test in tests:
if test.__name__.startswith("C"):
for name, obj in c_io_ns.items():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Remove the undocumented ``io.OpenWrapper`` and ``_pyio.OpenWrapper``
functions which could be used directly to define a method:
``staticmethod(open)`` should now be used instead.
12 changes: 6 additions & 6 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -2213,11 +2213,11 @@ create_stdio(const PyConfig *config, PyObject* io,
return NULL;
}

/* Set builtins.open to io.OpenWrapper */
/* Set builtins.open to io.open */
static PyStatus
init_set_builtins_open(void)
{
PyObject *iomod = NULL, *wrapper;
PyObject *iomod = NULL, *open_func;
PyObject *bimod = NULL;
PyStatus res = _PyStatus_OK();

Expand All @@ -2229,16 +2229,16 @@ init_set_builtins_open(void)
goto error;
}

if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
if (!(open_func = PyObject_GetAttrString(iomod, "open"))) {
goto error;
}

/* Set builtins.open */
if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
Py_DECREF(wrapper);
if (PyObject_SetAttrString(bimod, "open", open_func) == -1) {
Py_DECREF(open_func);
goto error;
}
Py_DECREF(wrapper);
Py_DECREF(open_func);
goto done;

error:
Expand Down