|
|
Created:
6 years, 6 months ago by szager1 Modified:
6 years, 6 months ago CC:
chromium-reviews, cmp-cc_chromium.org, iannucci+depot_tools_chromium.org, ilevy-cc_chromium.org Visibility:
Public. |
DescriptionExplicitly print diff stats to sys.stdout, if possible.
This makes it possible to effectively re-route the stdout by
re-assigning sys.stdout.
BUG=
Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=273595
Patch Set 1 #
Total comments: 3
Messages
Total messages: 12 (0 generated)
https://codereview.chromium.org/303223005/diff/1/git_cl.py File git_cl.py (right): https://codereview.chromium.org/303223005/diff/1/git_cl.py#newcode263 git_cl.py:263: stdout = sys.stdout.fileno() drive-by nit: sys.stdout works fine, you don't need the fileno() ...
https://codereview.chromium.org/303223005/diff/1/git_cl.py File git_cl.py (right): https://codereview.chromium.org/303223005/diff/1/git_cl.py#newcode263 git_cl.py:263: stdout = sys.stdout.fileno() On 2014/05/29 20:31:36, Dirk Pranke wrote: > drive-by nit: sys.stdout works fine, you don't need the fileno() ... There is some existing code that replaces sys.stdout with a StringIO, and that won't work, because there's no file descriptor associated with a StringIO. So, to do what you say, I'd have to: try: sys.stdout.fileno() stdout = sys.stdout except AttributeError: stdout = None ... which seems silly to me.
lgtm
Where would sys.stdout be redirected?
On 2014/05/29 20:49:44, M-A Ruel wrote: > Where would sys.stdout be redirected? It's for test code; I'm using this to wrap main() in git_cl: import git_cl def capture_terminal(f, *args, **kwargs): old_std = (sys.stdout, sys.stderr) bufs = (StringIO(), StringIO()) pipes = (os.pipe(), os.pipe()) def _thread_main(fd, buf): with os.fdopen(fd) as fh: buf.write(fh.read()) threads = ( threading.Thread(target=_thread_main, args=(pipes[0][0], bufs[0])), threading.Thread(target=_thread_main, args=(pipes[1][0], bufs[1]))) threads[0].start() threads[1].start() sys.stdout = os.fdopen(pipes[0][1], 'w') sys.stderr = os.fdopen(pipes[1][1], 'w') try: result = f(*args, **kwargs) except Exception as e: sys.excepthook(*sys.exc_info()) result = e sys.stdout.close() sys.stderr.close() threads[0].join() threads[1].join() (sys.stdout, sys.stderr) = old_std return result, bufs[0], bufs[1] args = [...] capture_terminal(git_cl.main, args) The nice thing about this is that I can debug a test failure in pdb, without having to deal with a subprocess call to git-cl.
The CQ bit was checked by szager@chromium.org
CQ is trying da patch. Follow status at https://chromium-status.appspot.com/cq/szager@chromium.org/303223005/1
Message was sent while issue was closed.
Change committed as 273595
Message was sent while issue was closed.
https://codereview.chromium.org/303223005/diff/1/git_cl.py File git_cl.py (right): https://codereview.chromium.org/303223005/diff/1/git_cl.py#newcode263 git_cl.py:263: stdout = sys.stdout.fileno() On 2014/05/29 20:48:04, szager1 wrote: > On 2014/05/29 20:31:36, Dirk Pranke wrote: > > drive-by nit: sys.stdout works fine, you don't need the fileno() ... > > There is some existing code that replaces sys.stdout with a StringIO, and that > won't work, because there's no file descriptor associated with a StringIO. So, > to do what you say, I'd have to: > > try: > sys.stdout.fileno() > stdout = sys.stdout > except AttributeError: > stdout = None > > ... which seems silly to me. You lost me, or maybe I'm not understanding something. subprocess2.call() should be just fine w/ receiving a file-like object for the stdout= keyword parameter, and so passing a StringIO should be fine as well.
Message was sent while issue was closed.
On 2014/05/29 21:10:47, Dirk Pranke wrote: > https://codereview.chromium.org/303223005/diff/1/git_cl.py > File git_cl.py (right): > > https://codereview.chromium.org/303223005/diff/1/git_cl.py#newcode263 > git_cl.py:263: stdout = sys.stdout.fileno() > On 2014/05/29 20:48:04, szager1 wrote: > > On 2014/05/29 20:31:36, Dirk Pranke wrote: > > > drive-by nit: sys.stdout works fine, you don't need the fileno() ... > > > > There is some existing code that replaces sys.stdout with a StringIO, and that > > won't work, because there's no file descriptor associated with a StringIO. > So, > > to do what you say, I'd have to: > > > > try: > > sys.stdout.fileno() > > stdout = sys.stdout > > except AttributeError: > > stdout = None > > > > ... which seems silly to me. > > You lost me, or maybe I'm not understanding something. subprocess2.call() should > be just fine w/ receiving a file-like object for the stdout= keyword parameter, > and so passing a StringIO should be fine as well. 'File-like' in this context means 'associated with a file descriptor that can be inherited by the subprocess.' But why explain with words when I can explain with code: $ python Python 2.7.3 (default, Feb 27 2014, 19:58:35) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import cStringIO, subprocess2 >>> subprocess2.call(['ls'], stdout=cStringIO.StringIO()) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "subprocess2.py", line 467, in call return communicate(args, **kwargs)[1] File "subprocess2.py", line 449, in communicate proc = Popen(args, **kwargs) File "subprocess2.py", line 237, in __init__ super(Popen, self).__init__(args, **kwargs) File "/usr/lib/python2.7/subprocess.py", line 672, in __init__ errread, errwrite) = self._get_handles(stdin, stdout, stderr) File "/usr/lib/python2.7/subprocess.py", line 1053, in _get_handles c2pwrite = stdout.fileno() AttributeError: 'cStringIO.StringO' object has no attribute 'fileno'
Message was sent while issue was closed.
Hm. I guess I'm wrong :). Sorry! I could've sworn I've seen this work before. |