Skip to content

Commit 77d668b

Browse files
authored
bpo-43680: _pyio.open() becomes a static method (GH-25354)
The Python _pyio.open() function becomes a static method to behave as io.open() built-in function: don't become a bound method when stored as a class variable. It becomes possible since static methods are now callable in Python 3.10. Moreover, _pyio.OpenWrapper becomes a simple alias to _pyio.open. init_set_builtins_open() now sets builtins.open to io.open, rather than setting it to io.OpenWrapper, since OpenWrapper is now an alias to open in the io and _pyio modules.
1 parent 9825bdf commit 77d668b

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

Lib/_pyio.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ def text_encoding(encoding, stacklevel=2):
6363
return encoding
6464

6565

66+
# Wrapper for builtins.open
67+
#
68+
# Trick so that open() won't become a bound method when stored
69+
# as a class variable (as dbm.dumb does).
70+
#
71+
# See init_set_builtins_open() in Python/pylifecycle.c.
72+
@staticmethod
6673
def open(file, mode="r", buffering=-1, encoding=None, errors=None,
6774
newline=None, closefd=True, opener=None):
6875

@@ -313,18 +320,9 @@ def __get__(self, obj, typ=None):
313320
"errors=None, newline=None, closefd=True)\n\n" +
314321
open.__doc__)
315322

316-
class OpenWrapper:
317-
"""Wrapper for builtins.open
318323

319-
Trick so that open won't become a bound method when stored
320-
as a class variable (as dbm.dumb does).
321-
322-
See initstdio() in Python/pylifecycle.c.
323-
"""
324-
__doc__ = DocDescriptor()
325-
326-
def __new__(cls, *args, **kwargs):
327-
return open(*args, **kwargs)
324+
# bpo-43680: Alias to open() kept for backward compatibility
325+
OpenWrapper = open
328326

329327

330328
# In normal operation, both `UnsupportedOperation`s should be bound to the
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
The Python :func:`_pyio.open` function becomes a static method to behave as
2+
:func:`io.open` built-in function: don't become a bound method when stored as a
3+
class variable. It becomes possible since static methods are now callable in
4+
Python 3.10. Moreover, :func:`_pyio.OpenWrapper` becomes a simple alias to
5+
:func:`_pyio.open`.
6+
Patch by Victor Stinner.

Python/pylifecycle.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,7 +2241,7 @@ create_stdio(const PyConfig *config, PyObject* io,
22412241
return NULL;
22422242
}
22432243

2244-
/* Set builtins.open to io.OpenWrapper */
2244+
/* Set builtins.open to io.open */
22452245
static PyStatus
22462246
init_set_builtins_open(void)
22472247
{
@@ -2257,7 +2257,7 @@ init_set_builtins_open(void)
22572257
goto error;
22582258
}
22592259

2260-
if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
2260+
if (!(wrapper = PyObject_GetAttrString(iomod, "open"))) {
22612261
goto error;
22622262
}
22632263

@@ -2279,7 +2279,7 @@ init_set_builtins_open(void)
22792279
}
22802280

22812281

2282-
/* Initialize sys.stdin, stdout, stderr and builtins.open */
2282+
/* Create sys.stdin, sys.stdout and sys.stderr */
22832283
static PyStatus
22842284
init_sys_streams(PyThreadState *tstate)
22852285
{

0 commit comments

Comments
 (0)