comparison mercurial/utils/procutil.py @ 45042:2bfbd7d2c204

procutil: define LineBufferedWrapper on all Python versions There?s nothing Python 3-only about LineBufferedWrapper. In the future, we may want to use it on Windows, to work around missing line-buffering support.
author Manuel Jacob <me@manueljacob.de>
date Thu, 02 Jul 2020 04:37:18 +0200
parents 3fadbdc47aed
children fd205a9c358a
comparison
equal deleted inserted replaced
45041:c7d109c400a4 45042:2bfbd7d2c204
47 return fp.isatty() 47 return fp.isatty()
48 except AttributeError: 48 except AttributeError:
49 return False 49 return False
50 50
51 51
52 if pycompat.ispy3: 52 class LineBufferedWrapper(object):
53 53 def __init__(self, orig):
54 class LineBufferedWrapper(object): 54 self.orig = orig
55 def __init__(self, orig): 55
56 self.orig = orig 56 def __getattr__(self, attr):
57 57 return getattr(self.orig, attr)
58 def __getattr__(self, attr): 58
59 return getattr(self.orig, attr) 59 def write(self, s):
60 60 orig = self.orig
61 def write(self, s): 61 res = orig.write(s)
62 orig = self.orig 62 if s.endswith(b'\n'):
63 res = orig.write(s) 63 orig.flush()
64 if s.endswith(b'\n'): 64 return res
65 orig.flush() 65
66 return res 66
67 67 io.BufferedIOBase.register(LineBufferedWrapper)
68 io.BufferedIOBase.register(LineBufferedWrapper)
69 68
70 69
71 # glibc determines buffering on first write to stdout - if we replace a TTY 70 # glibc determines buffering on first write to stdout - if we replace a TTY
72 # destined stdout with a pipe destined stdout (e.g. pager), we want line 71 # destined stdout with a pipe destined stdout (e.g. pager), we want line
73 # buffering (or unbuffered, on Windows) 72 # buffering (or unbuffered, on Windows)