This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients vstinner
Date 2021-03-31.12:59:21
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1617195561.83.0.0407283597649.issue43680@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2021-03-31 12:59:21vstinnersetrecipients: + vstinner
2021-03-31 12:59:21vstinnersetmessageid: <1617195561.83.0.0407283597649.issue43680@roundup.psfhosted.org>
2021-03-31 12:59:21vstinnerlinkissue43680 messages
2021-03-31 12:59:21vstinnercreate