FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-43680: _pyio.open() becomes a static method by vstinner · Pull Request #25354 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
20 changes: 9 additions & 11 deletions Lib/_pyio.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ def text_encoding(encoding, stacklevel=2):
return encoding


# 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 init_set_builtins_open() in Python/pylifecycle.c.
@staticmethod
def open(file, mode="r", buffering=-1, encoding=None, errors=None,
newline=None, closefd=True, opener=None):

Expand Down Expand Up @@ -313,18 +320,9 @@ 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)
# bpo-43680: Alias to open() kept for backward compatibility
OpenWrapper = open


# In normal operation, both `UnsupportedOperation`s should be bound to the
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
The Python :func:`_pyio.open` function becomes a static method to behave as
:func:`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, :func:`_pyio.OpenWrapper` becomes a simple alias to
:func:`_pyio.open`.
Patch by Victor Stinner.
6 changes: 3 additions & 3 deletions Python/pylifecycle.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2241,7 +2241,7 @@ 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)
{
Expand All @@ -2257,7 +2257,7 @@ init_set_builtins_open(void)
goto error;
}

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

Expand All @@ -2279,7 +2279,7 @@ init_set_builtins_open(void)
}


/* Initialize sys.stdin, stdout, stderr and builtins.open */
/* Create sys.stdin, sys.stdout and sys.stderr */
static PyStatus
init_sys_streams(PyThreadState *tstate)
{
Expand Down

Back | FazBrowse Home | New Git URL