"""Frames whose text JSON cannot carry as the daemon reads it. A path decoded with surrogateescape holds a lone surrogate, and a float can be NaN or infinite. The daemon's JSON reader refuses both, so the kernel's link refuses to send them rather than have its kernel abandoned. A frame that only shows text (a file-change trace, an error) must still arrive, and a remote call's result must still reach its caller, exactly when it is a value. Before this, the trace was dropped with an encoder error in the cell's output, and a remote invoke never got its reply. These need a real kernel's frames, with no daemon. """ import math import sys from typing import cast import unittest from kernel_owner_support import ROOT, Kernel sys.path.insert(0, str(ROOT / "python")) import albedo_values # noqa: E402 FUNCTIONS = """ import os def nan(): return float("nan") def infinities(): return [float("inf"), {"low": float("-inf")}] def lone(): return os.fsdecode(b"/tmp/x\\xff") def fails(): raise FileNotFoundError(os.fsdecode(b"/nowhere/\\xff")) """ class KernelFrameTextTest(unittest.TestCase): def setUp(self) -> None: self.kernel = Kernel(["run", "files"]) self.addCleanup(self.kernel.close) def test_a_trace_naming_an_undecodable_path_is_delivered(self) -> None: done = self.kernel.execute( "import os\n" "try:\n" " open(os.fsdecode(b'/nonexistent/\\xff'))\n" "except FileNotFoundError:\n" " pass\n" ) self.assertEqual(done["status"], "ok", done) self.assertNotIn("not delivered", done["output"]) [trace] = [f for f in self.kernel.observed("trace") if f["id"] == done["id"]] targets = [a["target"] for a in trace["trace"]["activities"]] self.assertIn("/nonexistent/\ufffd", targets) def test_a_remote_result_holding_nan_or_a_lone_surrogate_is_answered(self): self.assertEqual(self.kernel.execute(FUNCTIONS)["status"], "ok") nan = self.kernel.invoke("nan", name="nan") self.assertTrue(nan["ok"], nan) self.assertTrue(math.isnan(cast(float, albedo_values.decode(nan["value"])))) infinities = self.kernel.invoke("inf", name="infinities") self.assertEqual( albedo_values.decode(infinities["value"]), [math.inf, {"low": -math.inf}], ) lone = self.kernel.invoke("lone", name="lone") self.assertEqual(albedo_values.decode(lone["value"]), "/tmp/x\udcff") failed = self.kernel.invoke("fails", name="fails") self.assertFalse(failed["ok"]) self.assertEqual(failed["error"]["ename"], "FileNotFoundError") if __name__ == "__main__": unittest.main()