changeset 36:8acbab5a9f21

Remove stream buffering on stdout & stderr
author Louis 'Kureuil' Person <louis.person@epitech.eu>
date Sat, 30 Apr 2016 23:48:19 +0200
parents e05900c7967e
children aac47bc07111
files jupyter_c_kernel/kernel.py resources/master.c
diffstat 2 files changed, 38 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/jupyter_c_kernel/kernel.py	Sat Apr 30 22:47:49 2016 +0100
+++ b/jupyter_c_kernel/kernel.py	Sat Apr 30 23:48:19 2016 +0200
@@ -5,7 +5,7 @@
 import subprocess
 import tempfile
 import os
-
+import os.path as path
 
 class JupyterSubprocess(subprocess.Popen):
     def __init__(self, cmd, write_to_stdout, write_to_stderr):
@@ -59,11 +59,17 @@
     def __init__(self, *args, **kwargs):
         super(CKernel, self).__init__(*args, **kwargs)
         self.files = []
+        mastertemp = tempfile.mkstemp(suffix='.out')
+        os.close(mastertemp[0])
+        self.master_path = mastertemp[1]
+        filepath = path.join(path.dirname(path.realpath(__file__)), '..', 'resources', 'master.c')
+        subprocess.call(['gcc', filepath, '-std=c11', '-fPIC', '-shared', '-rdynamic', '-o', self.master_path])
 
     def cleanup_files(self):
         """Remove all the temporary files created by the kernel"""
         for file in self.files:
             os.remove(file)
+        os.remove(self.master_path)
 
     def new_temp_file(self, **kwargs):
         """Create a new temp file to be deleted when the kernel shuts down"""
@@ -86,7 +92,7 @@
                                  lambda contents: self._write_to_stderr(contents.decode()))
 
     def compile_with_gcc(self, source_filename, binary_filename):
-        args = ['gcc', source_filename, '-std=c11', '-o', binary_filename]
+        args = ['gcc', source_filename, '-std=c11', '-fPIC', '-shared', '-rdynamic', '-o', binary_filename]
         return self.create_jupyter_subprocess(args)
 
     def do_execute(self, code, silent, store_history=True,
@@ -106,7 +112,7 @@
                     return {'status': 'ok', 'execution_count': self.execution_count, 'payload': [],
                             'user_expressions': {}}
 
-        p = self.create_jupyter_subprocess([binary_file.name])
+        p = self.create_jupyter_subprocess([self.master_path, binary_file.name])
         while p.poll() is None:
             p.write_contents()
         p.write_contents()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/resources/master.c	Sat Apr 30 23:48:19 2016 +0200
@@ -0,0 +1,29 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <dlfcn.h>
+
+typedef int (*main_t)(int, char **, char **);
+
+int main(int argc, char **argv, char **envp)
+{
+    char *error = NULL;
+
+    setbuf(stdout, NULL);
+    setbuf(stderr, NULL);
+    if (argc < 2) {
+        fprintf(stderr, "USAGE: %s PROGRAM\nWhere PROGRAM is the user's program to supervise\n", argv[0]);
+        return EXIT_FAILURE;
+    }
+    void *userhandle = dlopen(argv[1], RTLD_LAZY);
+    if (userhandle == NULL) {
+        fprintf(stderr, "%s: %s\n", argv[0], dlerror());
+        return EXIT_FAILURE;
+    }
+    dlerror();
+    main_t usermain = dlsym(userhandle, "main");
+    if ((error = dlerror()) != NULL) {
+        fprintf(stderr, "%s: %s\n", argv[0], error);
+        return EXIT_FAILURE;
+    }
+    return usermain(argc, argv, envp);
+}
\ No newline at end of file