Author: Armin Rigo <[email protected]>
Branch: remove-py-log
Changeset: r82979:2fd6f2c58dda
Date: 2016-03-11 17:00 +0100
http://bitbucket.org/pypy/pypy/changeset/2fd6f2c58dda/
Log: log.dot()
diff --git a/rpython/tool/ansi_print.py b/rpython/tool/ansi_print.py
--- a/rpython/tool/ansi_print.py
+++ b/rpython/tool/ansi_print.py
@@ -9,6 +9,7 @@
isatty = getattr(sys.stderr, 'isatty', lambda: False)
mandelbrot_driver = Driver()
+wrote_dot = False # global shared state
class Logger(object):
@@ -19,12 +20,16 @@
def _make_method(subname, colors):
#
def logger_method(self, text):
+ global wrote_dot
text = "[%s%s] %s" % (self.name, subname, text)
if isatty():
col = colors
else:
col = ()
+ if wrote_dot:
+ text = '\n' + text
ansi_print(text, col)
+ wrote_dot = False
#
return logger_method
@@ -38,68 +43,9 @@
stub = _make_method(':stub', (34,))
__call__ = _make_method('', ())
-
-class AnsiLog:
- wrote_dot = False # XXX sharing state with all instances
-
- KW_TO_COLOR = {
- # color supress
- 'red': ((31,), True),
- 'bold': ((1,), True),
- 'WARNING': ((31,), False),
- 'event': ((1,), True),
- 'ERROR': ((1, 31), False),
- 'Error': ((1, 31), False),
- 'info': ((35,), False),
- 'stub': ((34,), False),
- }
-
- def __init__(self, kw_to_color={}, file=None):
- self.kw_to_color = self.KW_TO_COLOR.copy()
- self.kw_to_color.update(kw_to_color)
- self.file = file
- self.fancy = True
- self.isatty = getattr(sys.stderr, 'isatty', lambda: False)
- if self.fancy and self.isatty():
- self.mandelbrot_driver = Driver()
- else:
- self.mandelbrot_driver = None
-
- def __call__(self, msg):
- tty = self.isatty()
- flush = False
- newline = True
- keywords = []
- esc = []
- for kw in msg.keywords:
- color, supress = self.kw_to_color.get(kw, (None, False))
- if color:
- esc.extend(color)
- if not supress:
- keywords.append(kw)
- if 'start' in keywords:
- if tty:
- newline = False
- flush = True
- keywords.remove('start')
- elif 'done' in keywords:
- if tty:
- print >> sys.stderr
- return
- elif 'dot' in keywords:
- if tty:
- if self.fancy:
- if not AnsiLog.wrote_dot:
- self.mandelbrot_driver.reset()
- self.mandelbrot_driver.dot()
- else:
- ansi_print(".", tuple(esc), file=self.file, newline=False,
flush=flush)
- AnsiLog.wrote_dot = True
- return
- if AnsiLog.wrote_dot:
- AnsiLog.wrote_dot = False
- sys.stderr.write("\n")
- esc = tuple(esc)
- for line in msg.content().splitlines():
- ansi_print("[%s] %s" %(":".join(keywords), line), esc,
- file=self.file, newline=newline, flush=flush)
+ def dot(self):
+ global wrote_dot
+ if not wrote_dot:
+ mandelbrot_driver.reset()
+ wrote_dot = True
+ mandelbrot_driver.dot()
diff --git a/rpython/tool/test/test_ansi_print.py
b/rpython/tool/test/test_ansi_print.py
--- a/rpython/tool/test/test_ansi_print.py
+++ b/rpython/tool/test/test_ansi_print.py
@@ -1,5 +1,5 @@
from _pytest.monkeypatch import monkeypatch
-from rpython.tool import ansi_print
+from rpython.tool import ansi_print, ansi_mandelbrot
class FakeOutput(object):
@@ -10,11 +10,14 @@
def __enter__(self, *args):
self.monkey.setattr(ansi_print, 'ansi_print', self._print)
self.monkey.setattr(ansi_print, 'isatty', self._isatty)
+ self.monkey.setattr(ansi_mandelbrot, 'ansi_print', self._print)
return self.output
def __exit__(self, *args):
self.monkey.undo()
- def _print(self, text, colors):
+ def _print(self, text, colors, newline=True, flush=True):
+ if newline:
+ text += '\n'
self.output.append((text, colors))
def _isatty(self):
return self.tty
@@ -24,16 +27,39 @@
log = ansi_print.Logger('test')
with FakeOutput() as output:
log('Hello')
- assert output == [('[test] Hello', ())]
+ assert output == [('[test] Hello\n', ())]
def test_bold():
log = ansi_print.Logger('test')
with FakeOutput() as output:
log.bold('Hello')
- assert output == [('[test] Hello', (1,))]
+ assert output == [('[test] Hello\n', (1,))]
def test_not_a_tty():
log = ansi_print.Logger('test')
with FakeOutput(tty=False) as output:
log.bold('Hello')
- assert output == [('[test] Hello', ())]
+ assert output == [('[test] Hello\n', ())]
+
+def test_dot_1():
+ log = ansi_print.Logger('test')
+ with FakeOutput() as output:
+ log.dot()
+ assert len(output) == 1
+ assert len(output[0][0]) == 1 # single character
+ # output[0][1] is some ansi color code from mandelbort_driver
+
+def test_dot_mixing_with_regular_lines():
+ log = ansi_print.Logger('test')
+ with FakeOutput() as output:
+ log.dot()
+ log.dot()
+ log.WARNING('oops')
+ log.WARNING('maybe?')
+ log.dot()
+ assert len(output) == 5
+ assert len(output[0][0]) == 1 # single character
+ assert len(output[1][0]) == 1 # single character
+ assert output[2] == ('\n[test:WARNING] oops\n', (31,))
+ assert output[3] == ('[test:WARNING] maybe?\n', (31,))
+ assert len(output[4][0]) == 1 # single character
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit