From 3aa6d18e4a2a064bcdfa9a5ece6b9cd240f60608 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 12 Apr 2021 09:54:52 +0200 Subject: [PATCH 1/3] bpo-43680: _pyio.open() becomes a static method 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. --- Lib/_pyio.py | 20 +++++++++---------- .../2021-04-12-09-57-37.bpo-43680.o1zEk_.rst | 5 +++++ Python/pylifecycle.c | 6 +++--- 3 files changed, 17 insertions(+), 14 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2021-04-12-09-57-37.bpo-43680.o1zEk_.rst diff --git a/Lib/_pyio.py b/Lib/_pyio.py index 0f182d42402063..cb5a619f02a48c 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -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): @@ -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 diff --git a/Misc/NEWS.d/next/Library/2021-04-12-09-57-37.bpo-43680.o1zEk_.rst b/Misc/NEWS.d/next/Library/2021-04-12-09-57-37.bpo-43680.o1zEk_.rst new file mode 100644 index 00000000000000..0fdae0d09ab526 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-04-12-09-57-37.bpo-43680.o1zEk_.rst @@ -0,0 +1,5 @@ +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`. diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 0ad1796e3cbc6e..2dbebe45fd4b0b 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -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) { @@ -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; } @@ -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) { From 4f5e199af4a0723c2ae2890ef5f08c215fac03ea Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 12 Apr 2021 10:13:35 +0200 Subject: [PATCH 2/3] Update Misc/NEWS.d/next/Library/2021-04-12-09-57-37.bpo-43680.o1zEk_.rst Co-authored-by: Inada Naoki --- .../next/Library/2021-04-12-09-57-37.bpo-43680.o1zEk_.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2021-04-12-09-57-37.bpo-43680.o1zEk_.rst b/Misc/NEWS.d/next/Library/2021-04-12-09-57-37.bpo-43680.o1zEk_.rst index 0fdae0d09ab526..a7d0b0e392194f 100644 --- a/Misc/NEWS.d/next/Library/2021-04-12-09-57-37.bpo-43680.o1zEk_.rst +++ b/Misc/NEWS.d/next/Library/2021-04-12-09-57-37.bpo-43680.o1zEk_.rst @@ -1,5 +1,5 @@ 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 +: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`. From 67bbfe606064e116c9e5f54dfd19bdc9d0815285 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 12 Apr 2021 10:20:19 +0200 Subject: [PATCH 3/3] Update NEWS entry --- .../NEWS.d/next/Library/2021-04-12-09-57-37.bpo-43680.o1zEk_.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Misc/NEWS.d/next/Library/2021-04-12-09-57-37.bpo-43680.o1zEk_.rst b/Misc/NEWS.d/next/Library/2021-04-12-09-57-37.bpo-43680.o1zEk_.rst index a7d0b0e392194f..cb561ae586a98d 100644 --- a/Misc/NEWS.d/next/Library/2021-04-12-09-57-37.bpo-43680.o1zEk_.rst +++ b/Misc/NEWS.d/next/Library/2021-04-12-09-57-37.bpo-43680.o1zEk_.rst @@ -3,3 +3,4 @@ The Python :func:`_pyio.open` function becomes a static method to behave as 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. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy