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

"for line in file" is *still* broken in Python 2.7 on pipes #59737

Closed
AndyLutomirski mannequin opened this issue Aug 1, 2012 · 2 comments
Closed

"for line in file" is *still* broken in Python 2.7 on pipes #59737

AndyLutomirski mannequin opened this issue Aug 1, 2012 · 2 comments
Labels
topic-IO type-bug An unexpected behavior, bug, or error

Comments

@AndyLutomirski
Copy link
Mannequin

AndyLutomirski mannequin commented Aug 1, 2012

BPO 15532
Nosy @ned-deily

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 2012-08-02.05:04:28.299>
created_at = <Date 2012-08-01.21:56:37.731>
labels = ['type-bug', 'expert-IO']
title = '"for line in file" is *still* broken in Python 2.7 on pipes'
updated_at = <Date 2012-08-02.05:04:28.278>
user = 'https://bugs.python.org/AndyLutomirski'

bugs.python.org fields:

activity = <Date 2012-08-02.05:04:28.278>
actor = 'ned.deily'
assignee = 'none'
closed = True
closed_date = <Date 2012-08-02.05:04:28.299>
closer = 'ned.deily'
components = ['IO']
creation = <Date 2012-08-01.21:56:37.731>
creator = 'Andy.Lutomirski'
dependencies = []
files = []
hgrepos = []
issue_num = 15532
keywords = []
message_count = 2.0
messages = ['167170', '167189']
nosy_count = 2.0
nosy_names = ['ned.deily', 'Andy.Lutomirski']
pr_nums = []
priority = 'normal'
resolution = 'works for me'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue15532'
versions = ['Python 2.7']

@AndyLutomirski
Copy link
Mannequin Author

AndyLutomirski mannequin commented Aug 1, 2012

This program:

import subprocess, sys
p = subprocess.Popen(['bash', '-c', 'while true; do echo x; sleep 1; done'], bufsize=0, stdout=subprocess.PIPE)

for line in p.stdout:
    sys.stdout.buffer.write(line)
    sys.stdout.flush()

sits around and does nothing on Python 2.7.3. It works (i.e. prints 'x' once per second) on Python 3.

This was http://bugs.python.org/issue3907 and is supposedly fixed, but it's not.

@AndyLutomirski AndyLutomirski mannequin added topic-IO type-bug An unexpected behavior, bug, or error labels Aug 1, 2012
@AndyLutomirski AndyLutomirski mannequin changed the title "for line in file" is *still* broken in Python 2.7 "for line in file" is *still* broken in Python 2.7 on pipes Aug 1, 2012
@ned-deily
Copy link
Member

Notice in the reply to bpo-3907, "with 2.6, you have to use io.open() explicitly". This is still true in Python 2.7, i.e. the new 3.x-compatible io library is not used by default in Python 2. If you want to use it with subprocess.Popen, one way is to supply a pipe with an io wrapper, perhaps something like:

import io, os
i, o = os.pipe()
piperead = io.open(i,'rb',buffering=0)
p = subprocess.Popen(..., stdout=o)

See Chapter 15 of the Python 2.7 Standard Library doc for more information on the io module: http://docs.python.org/library/io.html

@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

1 participant