]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43680: _pyio.open() becomes a static method (GH-25354)
authorVictor Stinner <vstinner@python.org>
Mon, 12 Apr 2021 08:44:53 +0000 (10:44 +0200)
committerGitHub <noreply@github.com>
Mon, 12 Apr 2021 08:44:53 +0000 (10:44 +0200)
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
Misc/NEWS.d/next/Library/2021-04-12-09-57-37.bpo-43680.o1zEk_.rst [new file with mode: 0644]
Python/pylifecycle.c

index 0f182d42402063e9dac2d8c45007300c98fdc101..cb5a619f02a48ca88ee15dc83ea7a8315ce1790a 100644 (file)
@@ -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 @@ class DocDescriptor:
                  "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 (file)
index 0000000..cb561ae
--- /dev/null
@@ -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.
index 0ad1796e3cbc6e7278a2c75a8e22e167b848a20f..2dbebe45fd4b0be16b6cd6b4be83bb8457a804cb 100644 (file)
@@ -2241,7 +2241,7 @@ error:
     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 @@ done:
 }
 
 
-/* Initialize sys.stdin, stdout, stderr and builtins.open */
+/* Create sys.stdin, sys.stdout and sys.stderr */
 static PyStatus
 init_sys_streams(PyThreadState *tstate)
 {