OSDN Git Service

Capture stdout so commands like words and trace work.
authorSimon Forman <sforman@hushmail.com>
Tue, 23 Nov 2021 23:12:59 +0000 (15:12 -0800)
committerSimon Forman <sforman@hushmail.com>
Tue, 23 Nov 2021 23:12:59 +0000 (15:12 -0800)
docs/Joy in Jupyter.ipynb
docs/jupyter_kernel/joy_kernel.py

index 5ad9314..895bbcf 100644 (file)
     {
      "name": "stdout",
      "output_type": "stream",
-     "text": []
+     "text": [
+      "!= % & * *fraction *fraction0 + ++ - -- / // /floor < << <= <> = > >= >> ? ^ _Tree_add_Ee _Tree_delete_R0 _Tree_delete_clear_stuff _Tree_get_E abs add anamorphism and app1 app2 app3 at average b binary bool branch ccons choice clear cleave cmp codireco concat cond cons dinfrirst dip dipd dipdd disenstacken div divmod down_to_zero drop dup dupd dupdd dupdip dupdipd enstacken eq first first_two flatten floor floordiv fork fourth gcd gcd2 ge genrec getitem gt help i id ifte ii infra inscribe le least_fraction loop lshift lt make_generator map max min mod modulus mul ne neg not nullary of or over pam parse pick pm pop popd popdd popop popopd popopdd pow pred primrec product quoted range range_to_zero rem remainder remove rest reverse roll< roll> rolldown rollup round rrest rshift run second select sharing shunt size sort sqr sqrt stack step step_zero stuncons stununcons sub succ sum swaack swap swoncat swons tailrec take ternary third times trace truthy tuck unary uncons unique unit unquoted unstack unswons void warranty while words x xor zip •\n",
+      "\n"
+     ]
     }
    ],
    "source": [
      "name": "stdout",
      "output_type": "stream",
      "text": [
+      "    • 1 2 +\n",
+      "  1 • 2 +\n",
+      "1 2 • +\n",
+      "  3 • \n",
+      "\n",
       "3"
      ]
     }
    "source": [
     "trace"
    ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "id": "b8a3f442",
-   "metadata": {},
-   "outputs": [],
-   "source": []
   }
  ],
  "metadata": {
index 7a2f745..8f4f7d1 100644 (file)
@@ -1,3 +1,4 @@
+import sys
 from ipykernel.kernelbase import Kernel
 from joy.library import initialize, inscribe
 from joy.joy import run
@@ -8,6 +9,28 @@ from joy.utils.pretty_print import trace
 inscribe(trace)
 
 
+class FileFaker:
+
+    def __init__(self):
+        self.output = ''
+
+    def write(self, text):
+        self.output += text
+
+    def flush(self):
+        pass
+
+    @classmethod
+    def hook(class_, f):
+        o = class_()
+        sys.stdout, old_stdout = o, sys.stdout
+        try:
+            f()
+        finally:
+            sys.stdout = old_stdout
+        return o.output
+
+
 class JoyKernel(Kernel):
     implementation = 'Joypy'
     implementation_version = '1.0'
@@ -33,11 +56,16 @@ class JoyKernel(Kernel):
       user_expressions=None,
       allow_stdin=False,
       ):
-      self.S = run(code, self.S, self.D)[0]
+      def f():
+        self.S = run(code, self.S, self.D)[0]
+      output = FileFaker.hook(f)
       if not silent:
+        text = stack_to_string(self.S)
+        if output:
+          text = '%s\n%s' % (output, text)
         stream_content = {
           'name': 'stdout',
-          'text': stack_to_string(self.S),
+          'text': text,
           }
         self.send_response(self.iopub_socket, 'stream', stream_content)