# HG changeset patch # User Vadim Gelfer # Date 1136244333 28800 # Node ID c50bddfbc812f541b17fae79cf6501e492efe5c3 # Parent 722fd16f6f8cd86436e2ece26ee1feea1d91895a eliminate backtrace when piping output on windows. this fixes issue 54. diff --git a/mercurial/ui.py b/mercurial/ui.py --- a/mercurial/ui.py +++ b/mercurial/ui.py @@ -110,7 +110,7 @@ class ui(object): sys.stdout.write(str(a)) def write_err(self, *args): - sys.stdout.flush() + if not sys.stdout.closed: sys.stdout.flush() for a in args: sys.stderr.write(str(a)) diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -13,7 +13,8 @@ platform-specific details from the core. import os, errno from i18n import gettext as _ from demandload import * -demandload(globals(), "re cStringIO shutil popen2 sys tempfile threading time") +demandload(globals(), "cStringIO errno popen2 re shutil sys tempfile") +demandload(globals(), "threading time") def pipefilter(s, cmd): '''filter string S through command CMD, returning its output''' @@ -442,12 +443,36 @@ else: if os.name == 'nt': demandload(globals(), "msvcrt") nulldev = 'NUL:' - + + class winstdout: + '''stdout on windows misbehaves if sent through a pipe''' + + def __init__(self, fp): + self.fp = fp + + def __getattr__(self, key): + return getattr(self.fp, key) + + def close(self): + try: + self.fp.close() + except: pass + + def write(self, s): + try: + return self.fp.write(s) + except IOError, inst: + if inst.errno != 0: raise + self.close() + raise IOError(errno.EPIPE, 'Broken pipe') + + sys.stdout = winstdout(sys.stdout) + try: import win32api, win32process filename = win32process.GetModuleFileNameEx(win32api.GetCurrentProcess(), 0) systemrc = os.path.join(os.path.dirname(filename), 'mercurial.ini') - + except ImportError: systemrc = r'c:\mercurial\mercurial.ini' pass