Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

tools: flamechart.py names a PNG it cannot write

Mert Kaya6b7c6ff7001b
diff --git a/tests/test_flamechart.py b/tests/test_flamechart.py
index d01723f24..f0f6e5957 100644
--- a/tests/test_flamechart.py
+++ b/tests/test_flamechart.py
@@ -61,6 +61,16 @@ def fake_trace(extra_threads=()):
return lambda sql: [dict(r) for r in db.execute(sql)]
+def fake_pillow():
+ """Enough Pillow for draw() to reach the final image save without the optional dependency."""
+ pil = mock.MagicMock()
+ image = pil.Image.new.return_value
+ image.size = (1600, 100)
+ pil.ImageDraw.Draw.return_value.textlength.return_value = 0
+ pil.ImageFont.load_default.return_value = object()
+ return pil, image
+
+
class HighlightTest(unittest.TestCase):
def test_nested_work_is_counted_once_and_drawn_magenta(self):
q = fake_trace()
@@ -215,6 +225,55 @@ class OutputArgumentTest(unittest.TestCase):
self.assertEqual(args.width, 1600 if width is None else width)
+class WriteFailureTest(unittest.TestCase):
+ def test_a_png_that_cannot_be_written_is_named_without_a_traceback(self):
+ """A write can fail after trace work even when --out passed its argument checks."""
+ with tempfile.TemporaryDirectory() as tmp:
+ trace = Path(tmp, "nav-1.pftrace")
+ trace.write_bytes(b"")
+ out = Path(tmp, "chart.png")
+ close = mock.Mock()
+ refusal = OSError(28, "No space left on device")
+ pil, image = fake_pillow()
+ image.save.side_effect = refusal
+ with mock.patch.dict(sys.modules, {"PIL": pil}), \
+ mock.patch.object(flamechart.trace_report, "opener",
+ return_value=lambda path: (fake_trace(), close)), \
+ contextlib.redirect_stdout(io.StringIO()) as output, \
+ contextlib.redirect_stderr(io.StringIO()) as error, \
+ self.assertRaises(SystemExit) as caught:
+ flamechart.main([str(trace), "--out", str(out)])
+ self.assertEqual(f"cannot write {out}: No space left on device", caught.exception.code)
+ self.assertEqual("", output.getvalue())
+ self.assertEqual("", error.getvalue())
+ image.save.assert_called_once_with(str(out))
+ close.assert_called_once_with()
+
+ def test_a_font_load_failure_is_not_called_an_output_write_failure(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ trace = Path(tmp, "nav-1.pftrace")
+ trace.write_bytes(b"")
+ out = Path(tmp, "chart.png")
+ close = mock.Mock()
+ pil, image = fake_pillow()
+ pil.ImageFont.load_default.side_effect = OSError("cannot open resource")
+ with mock.patch.dict(sys.modules, {"PIL": pil}), \
+ mock.patch.object(flamechart.trace_report, "opener",
+ return_value=lambda path: (fake_trace(), close)), \
+ contextlib.redirect_stdout(io.StringIO()) as output, \
+ contextlib.redirect_stderr(io.StringIO()) as error, \
+ self.assertRaises(OSError) as caught:
+ flamechart.main([str(trace), "--out", str(out)])
+ self.assertEqual("cannot open resource", str(caught.exception))
+ self.assertNotIn("cannot write", str(caught.exception))
+ self.assertEqual("", output.getvalue())
+ self.assertEqual("", error.getvalue())
+ pil.ImageFont.load_default.assert_called_once_with(size=11)
+ pil.Image.new.assert_not_called()
+ image.save.assert_not_called()
+ close.assert_called_once_with()
+
+
class ThreadNamesTest(unittest.TestCase):
NAMES = ("main", "RenderThread", "Binder:100_1", "Jit thread pool", "worker/io", "Thread (pooled)",
"O'Brien", "quote''only", "worker+io", "GL worker [0]", "worker@io", r"worker\io")
diff --git a/tools/flamechart.py b/tools/flamechart.py
index 518f06072..10ba91442 100644
--- a/tools/flamechart.py
+++ b/tools/flamechart.py
@@ -189,7 +189,10 @@ def draw(panels, rx, title, subtitle, out, width=1600):
d.text((x0 + 3, yy + 1), text, fill="white" if s["id"] in hit else "#222", font=small)
y += h + 26
y += 10
- im.save(out)
+ try:
+ im.save(out)
+ except OSError as e:
+ sys.exit(f"cannot write {out}: {e.strerror or e}")
return im.size