Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repr() on detached stream objects fails #67282

Closed
vadmium opened this issue Dec 20, 2014 · 6 comments
Closed

repr() on detached stream objects fails #67282

vadmium opened this issue Dec 20, 2014 · 6 comments
Labels
topic-IO type-bug An unexpected behavior, bug, or error

Comments

@vadmium
Copy link
Member

vadmium commented Dec 20, 2014

BPO 23093
Nosy @pitrou, @benjaminp, @hynek, @vadmium, @serhiy-storchaka
Files
  • detach-indep.patch
  • detach-indep.v2.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2014-12-22.03:01:25.375>
    created_at = <Date 2014-12-20.14:19:49.437>
    labels = ['type-bug', 'expert-IO']
    title = 'repr() on detached stream objects fails'
    updated_at = <Date 2014-12-22.03:01:25.272>
    user = 'https://github.com/vadmium'

    bugs.python.org fields:

    activity = <Date 2014-12-22.03:01:25.272>
    actor = 'python-dev'
    assignee = 'none'
    closed = True
    closed_date = <Date 2014-12-22.03:01:25.375>
    closer = 'python-dev'
    components = ['IO']
    creation = <Date 2014-12-20.14:19:49.437>
    creator = 'martin.panter'
    dependencies = []
    files = ['37516', '37523']
    hgrepos = []
    issue_num = 23093
    keywords = ['patch']
    message_count = 6.0
    messages = ['232971', '232973', '232983', '232997', '232998', '233006']
    nosy_count = 7.0
    nosy_names = ['pitrou', 'benjamin.peterson', 'stutzbach', 'python-dev', 'hynek', 'martin.panter', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue23093'
    versions = ['Python 2.7', 'Python 3.4', 'Python 3.5']

    @vadmium
    Copy link
    Member Author

    vadmium commented Dec 20, 2014

    Patch to fix the underlying issue I mentioned in msg230955. After calling detach() on one of the BufferedIOBase wrappers or a TextIOWrapper, most operations will raise an exception. My patch ensures the following operations are still usable, because they are documented and it doesn’t make sense to disable them:

    repr(stream)
    stream.encoding
    stream.errors
    stream.line_buffering

    @vadmium vadmium added topic-IO type-bug An unexpected behavior, bug, or error labels Dec 20, 2014
    @serhiy-storchaka
    Copy link
    Member

    The issue is still here.

    >>> f = open('/dev/null')
    >>> f
    <_io.TextIOWrapper name='/dev/null' mode='r' encoding='UTF-8'>
    >>> f.buffer.detach()
    <_io.FileIO name='/dev/null' mode='rb' closefd=True>
    >>> f
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: raw stream has been detached

    Python implementation works.

    >>> import _pyio
    >>> f = _pyio.open('/dev/null')
    >>> f
    <_pyio.TextIOWrapper name='/dev/null' mode='r' encoding='UTF-8'>
    >>> f.buffer.detach()
    <_io.FileIO name='/dev/null' mode='rb' closefd=True>
    >>> f
    <_pyio.TextIOWrapper mode='r' encoding='UTF-8'>
    >>> f = _pyio.open('/dev/null')
    >>> f.detach()
    <_pyio.BufferedReader name='/dev/null'>
    >>> f
    <_pyio.TextIOWrapper mode='r' encoding='UTF-8'>
    >>> f = _pyio.open('/dev/null', 'rb')
    >>> f
    <_pyio.BufferedReader name='/dev/null'>
    >>> f.detach()
    <_io.FileIO name='/dev/null' mode='rb' closefd=True>
    >>> f
    <_pyio.BufferedReader>

    I would be good to make Python and C implementation match.

    @vadmium
    Copy link
    Member Author

    vadmium commented Dec 20, 2014

    Damn, detaching the intermediate buffered stream is a bit more awkward. The difference between the “io” and “_pyio” implementations boils down to:

    • io.BufferedReader/Writer/RWPair.name properties raise a ValueError if the stream is detached
    • _pyio._BufferedIOMixin.name property returns “self.raw.name”. When detached, “self.raw” is None, so this causes an AttributeError.

    This is significant because io.TextIOWrapper.__repr__() only handles AttributeError when accessing “self.buffer.name”. The best option that I can think of to fix this is to make all the repr() implementations handle this ValueError exception.

    @vadmium
    Copy link
    Member Author

    vadmium commented Dec 21, 2014

    Here is patch v2, which ignores any exception derived from the Exception base class when reading the self.name etc properties. I’m interested what people think of this approach.

    @serhiy-storchaka
    Copy link
    Member

    It looks reasonable to me.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Dec 22, 2014

    New changeset f3ff3e424b6f by Benjamin Peterson in branch '3.4':
    allow more operations to work on detached streams (closes bpo-23093)
    https://hg.python.org/cpython/rev/f3ff3e424b6f

    New changeset afa8d8ab0937 by Benjamin Peterson in branch '2.7':
    allow more operations to work on detached streams (closes bpo-23093)
    https://hg.python.org/cpython/rev/afa8d8ab0937

    New changeset f2cfa8a348dd by Benjamin Peterson in branch 'default':
    merge 3.4 (bpo-23093)
    https://hg.python.org/cpython/rev/f2cfa8a348dd

    @python-dev python-dev mannequin closed this as completed Dec 22, 2014
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    topic-IO type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants