[PATCH] D142531: [llvm][TableGen][Jupyter] Show llvm-tblgen not found error in notebook

David Spickett via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 25 03:39:34 PST 2023


DavidSpickett created this revision.
Herald added a project: All.
DavidSpickett requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Previously this message was only shown on the command line,
which is not much help if you can't see that.
(you've full screened th browser or you aren't running Jupyter
on the same machine)

Instead return the error as stderr which will get printed in
the notebook just like stderr from llvm-tblgen would.

I've refactored the message sending along the way. Note that
even when we do not send a stream, we still need to send the
status reply. The send_... methods will do that for you.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D142531

Files:
  llvm/utils/TableGen/jupyter/tablegen_kernel/kernel.py


Index: llvm/utils/TableGen/jupyter/tablegen_kernel/kernel.py
===================================================================
--- llvm/utils/TableGen/jupyter/tablegen_kernel/kernel.py
+++ llvm/utils/TableGen/jupyter/tablegen_kernel/kernel.py
@@ -64,8 +64,7 @@
     def banner(self):
         return "llvm-tblgen kernel %s" % __version__
 
-    @property
-    def executable(self):
+    def get_executable(self):
         """If this is the first run, search for llvm-tblgen.
         Otherwise return the cached path to it."""
         if self._executable is None:
@@ -75,7 +74,10 @@
             else:
                 path = shutil.which("llvm-tblgen")
                 if path is None:
-                    raise OSError("llvm-tblgen not found, please see README")
+                    raise OSError(
+                        "llvm-tblgen not found. Put it on your PATH or set the"
+                        " environment variable LLVM_TBLGEN_EXECUTABLE to point to it."
+                    )
                 self._executable = path
 
         return self._executable
@@ -157,17 +159,43 @@
 
         return self._previous_code, self._previous_magic.get("args", [])
 
+    def make_status(self):
+        return {
+            "status": "ok",
+            "execution_count": self.execution_count,
+            "payload": [],
+            "user_expressions": {},
+        }
+
+    def send_stream(self, name, content):
+        self.send_response(self.iopub_socket, "stream", {"name": name, "text": content})
+
+        return self.make_status()
+
+    def send_stderr(self, stderr):
+        return self.send_stream("stderr", stderr)
+
+    def send_stdout(self, stdout):
+        return self.send_stream("stdout", stdout)
+
     def do_execute(
         self, code, silent, store_history=True, user_expressions=None, allow_stdin=False
     ):
         """Execute user code using llvm-tblgen binary."""
         all_code, args = self.get_code_and_args(code)
 
+        # If we cannot find llvm-tblgen, propogate the error to the notebook.
+        # (in case the user is not able to see the output from the Jupyter server)
+        try:
+            executable = self.get_executable()
+        except Exception as e:
+            return self.send_stderr(str(e))
+
         with tempfile.TemporaryFile("w+") as f:
             f.write(all_code)
             f.seek(0)
             got = subprocess.run(
-                [self.executable, *args],
+                [executable, *args],
                 stdin=f,
                 stderr=subprocess.PIPE,
                 stdout=subprocess.PIPE,
@@ -176,20 +204,11 @@
 
         if not silent:
             if got.stderr:
-                self.send_response(
-                    self.iopub_socket, "stream", {"name": "stderr", "text": got.stderr}
-                )
+                return self.send_stderr(got.stderr)
             else:
-                self.send_response(
-                    self.iopub_socket, "stream", {"name": "stdout", "text": got.stdout}
-                )
-
-        return {
-            "status": "ok",
-            "execution_count": self.execution_count,
-            "payload": [],
-            "user_expressions": {},
-        }
+                return self.send_stdout(got.stdout)
+        else:
+            return self.make_status()
 
 
 if __name__ == "__main__":


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142531.492052.patch
Type: text/x-patch
Size: 3353 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230125/d10d42e2/attachment.bin>


More information about the llvm-commits mailing list