New submission from STINNER Victor <[email protected]>:
The OpenWrapper function of io and _pyio is an undocumented hack allowing to
use the builtin open() function as a method:
class MyClass:
method = open
MyClass.method(...) # class method
MyClass().method(...) # instance method
It is only needed by the _pyio module: the pure Python implementation of the io
module:
---
class DocDescriptor:
"""Helper for builtins.open.__doc__
"""
def __get__(self, obj, typ=None):
return (
"open(file, mode='r', buffering=-1, encoding=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)
---
The io module simply uses an alias to open:
---
OpenWrapper = _io.open # for compatibility with _pyio
---
No wrapper is needed since built-in functions can be used directly as methods.
Example:
---
class MyClass:
method = len # built-in function
print(MyClass.method("abc"))
print(MyClass().method("abc"))
---
This example works as expected, it displays "3" two times.
I propose to simply remove io.OpenWrapper and force developers to explicitly
use staticmethod:
class MyClass:
method = staticmethod(open)
io.OpenWrapper is not documented.
I don't understand the remark about dbm.dumb: I fail to see where the built-in
open() function is used as a method.
----------
components: Library (Lib)
messages: 389896
nosy: vstinner
priority: normal
severity: normal
status: open
title: Remove undocumented io.OpenWrapper and _pyio.OpenWrapper
versions: Python 3.10
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue43680>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com