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

TextIOWrapper should fall back on read() if read1() doesn't exist #56800

Closed
anacrolix mannequin opened this issue Jul 20, 2011 · 15 comments
Closed

TextIOWrapper should fall back on read() if read1() doesn't exist #56800

anacrolix mannequin opened this issue Jul 20, 2011 · 15 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) stdlib Python modules in the Lib dir topic-IO type-bug An unexpected behavior, bug, or error

Comments

@anacrolix
Copy link
Mannequin

anacrolix mannequin commented Jul 20, 2011

BPO 12591
Nosy @gpshead, @amauryfa, @pitrou, @vstinner, @merwok, @bitdancer, @anacrolix, @ambv
Files
  • textio_rawio.patch
  • spnewlines.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 2011-07-23.20:12:23.262>
    created_at = <Date 2011-07-20.05:24:36.750>
    labels = ['interpreter-core', 'type-bug', 'library', 'expert-IO']
    title = "TextIOWrapper should fall back on read() if read1() doesn't exist"
    updated_at = <Date 2011-07-23.20:12:23.260>
    user = 'https://github.com/anacrolix'

    bugs.python.org fields:

    activity = <Date 2011-07-23.20:12:23.260>
    actor = 'pitrou'
    assignee = 'none'
    closed = True
    closed_date = <Date 2011-07-23.20:12:23.262>
    closer = 'pitrou'
    components = ['Interpreter Core', 'Library (Lib)', 'IO']
    creation = <Date 2011-07-20.05:24:36.750>
    creator = 'anacrolix'
    dependencies = []
    files = ['22731', '22732']
    hgrepos = []
    issue_num = 12591
    keywords = ['patch']
    message_count = 15.0
    messages = ['140720', '140726', '140849', '140856', '140998', '141000', '141002', '141003', '141005', '141006', '141008', '141009', '141011', '141013', '141015']
    nosy_count = 9.0
    nosy_names = ['gregory.p.smith', 'amaury.forgeotdarc', 'pitrou', 'vstinner', 'eric.araujo', 'r.david.murray', 'anacrolix', 'lukasz.langa', 'python-dev']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue12591'
    versions = ['Python 3.2', 'Python 3.3']

    @anacrolix
    Copy link
    Mannequin Author

    anacrolix mannequin commented Jul 20, 2011

    >>> a = subprocess.Popen(['cat', '/path/to/text.ini'], stdout=subprocess.PIPE, universal_newlines=True)
    >>> b = configparser.ConfigParser()
    >>> b.read_file(a.stdout)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/hostname/sig/local/lib/python3.2/configparser.py", line 708, in read_file
        self._read(f, source)
      File "/hostname/sig/local/lib/python3.2/configparser.py", line 994, in _read
        for lineno, line in enumerate(fp, start=1):
    AttributeError: '_io.FileIO' object has no attribute 'read1'

    Also this fails without universal_readlines, which is not so bad except that the name 'read_file' doesn't exactly indicate this.

    I found one mildly related bug: http://bugs.python.org/issue11670

    @anacrolix anacrolix mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) stdlib Python modules in the Lib dir topic-IO labels Jul 20, 2011
    @bitdancer
    Copy link
    Member

    This isn't a problem with ConfigParser, it's a problem with the text file object returned by Subprocess. You can reproduce the bug by trying to iterate over the TextIOWrapper returned by subprocess.

    (It is true, however, that the ConfigParser docs should be specific that it isn't "any file", but "any text file". If you would like to open a separate issue about that, that would be great).

    @bitdancer bitdancer changed the title configparser can't read_file the output of subprocess.Popen text files returned by subprocess.Popen with universal_newlines=True are not iterable Jul 20, 2011
    @bitdancer bitdancer added the type-bug An unexpected behavior, bug, or error label Jul 20, 2011
    @pitrou
    Copy link
    Member

    pitrou commented Jul 22, 2011

    The "universal_newlines" feature is rather poorly named in Python 3.x, since it does much more than that (the resulting files yield and expect unicode strings rather than bytes objects).

    The problem is that io.TextIOWrapper needs a buffered I/O object, but bufsize is 0 by default in subprocess.Popen(). In the meantime, you can workaround it using a non-zero bufsize in the Popen() constructor. Of course, this has the side-effect of forcing you to call flush() on the stdin stream (if you are using it).

    @bitdancer
    Copy link
    Member

    So this is a doc bug in subprocess? Explaining this clearly doesn't sound like much fun...

    @pitrou pitrou changed the title text files returned by subprocess.Popen with universal_newlines=True are not iterable TextIOWrapper should fall back on read() if read1() doesn't exist Jul 23, 2011
    @pitrou
    Copy link
    Member

    pitrou commented Jul 23, 2011

    So, as the title indicates, I think we should make TextIOWrapper work with raw IO objects. The reason is so that write() can behave in a totally unbuffered way, which is necessary for Popen to behave appropriately.

    @pitrou
    Copy link
    Member

    pitrou commented Jul 23, 2011

    Hmm, one problem is that the C TextIOWrapper buffers writes. We would need an additional constructor parameter to prevent that :/

    @pitrou
    Copy link
    Member

    pitrou commented Jul 23, 2011

    Another possibility is to provide read1() on RawIO, as a synonym of read().

    @pitrou
    Copy link
    Member

    pitrou commented Jul 23, 2011

    Here is a first patch allowing TextIOWrapper to work with raw IO objects, and adding a "write_through" flag.

    @amauryfa
    Copy link
    Member

    "write_through" is not used in _pyio.py, is it expected?

    @pitrou
    Copy link
    Member

    pitrou commented Jul 23, 2011

    "write_through" is not used in _pyio.py, is it expected?

    Yup, because it is actually always write-through (writes are not
    cached). The argument is only there so that the signature is the same as
    the C version.

    @amauryfa
    Copy link
    Member

    Looks good, then.

    @pitrou
    Copy link
    Member

    pitrou commented Jul 23, 2011

    This additional patch improves universal_newlines support in subprocess (still broken with the select- and poll-based loops in communicate()).

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jul 23, 2011

    New changeset 9144014028f3 by Antoine Pitrou in branch '3.2':
    Issue bpo-12591: Allow io.TextIOWrapper to work with raw IO objects (without
    http://hg.python.org/cpython/rev/9144014028f3

    New changeset c3b47cdea0d1 by Antoine Pitrou in branch 'default':
    Issue bpo-12591: Allow io.TextIOWrapper to work with raw IO objects (without
    http://hg.python.org/cpython/rev/c3b47cdea0d1

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jul 23, 2011

    New changeset 5cc536fbd7c1 by Antoine Pitrou in branch '3.2':
    Issue bpo-12591: Improve support of "universal newlines" in the subprocess
    http://hg.python.org/cpython/rev/5cc536fbd7c1

    New changeset b616396fa170 by Antoine Pitrou in branch 'default':
    Issue bpo-12591: Improve support of "universal newlines" in the subprocess
    http://hg.python.org/cpython/rev/b616396fa170

    @pitrou
    Copy link
    Member

    pitrou commented Jul 23, 2011

    Ok, this should be fixed now. universal newlines support is still broken with communicate(), I've opened bpo-12623 for that.

    @pitrou pitrou closed this as completed Jul 23, 2011
    @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
    interpreter-core (Objects, Python, Grammar, and Parser dirs) stdlib Python modules in the Lib dir topic-IO type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants