Mercurial > hg > Members > aka > jupyter_CbC_kernel
comparison c_kernel/kernel.py @ 0:50ea00cf5896
Initial commit
author | Brendan Rius <brendan@omixy.com> |
---|---|
date | Fri, 25 Mar 2016 11:50:40 +0000 |
parents | |
children | b46b2e5b6c08 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:50ea00cf5896 |
---|---|
1 from ipykernel.kernelbase import Kernel | |
2 import subprocess | |
3 import tempfile | |
4 | |
5 | |
6 class CKernel(Kernel): | |
7 implementation = 'c_kernel' | |
8 implementation_version = '1.0' | |
9 language = 'c' | |
10 language_version = 'C11' | |
11 language_info = {'name': 'c', 'mimetype': 'text/plain', 'file_extension': 'c'} | |
12 _banner = None | |
13 | |
14 @property | |
15 def banner(self): | |
16 if self._banner is None: | |
17 self._banner = subprocess.check_output(['gcc', '-v']).decode('utf-8') | |
18 return self._banner | |
19 | |
20 def do_execute(self, code, silent, store_history=True, | |
21 user_expressions=None, allow_stdin=False): | |
22 code = code.strip() | |
23 if not code: | |
24 return {'status': 'ok', | |
25 'execution_count': self.execution_count, | |
26 'payload': [], | |
27 'user_expressions': {}} | |
28 | |
29 output = '### COMPILATION ###\n' | |
30 try: | |
31 sourcefile = tempfile.NamedTemporaryFile(suffix='.c', delete=False) | |
32 sourcefile.write(code) | |
33 sourcefile.close() | |
34 binaryfile = tempfile.NamedTemporaryFile(suffix='.out', delete=False) | |
35 binaryfile.close() | |
36 output += subprocess.check_output(['gcc', '-std=c11', sourcefile.name, '-o', binaryfile.name], | |
37 stderr=subprocess.STDOUT).decode('utf-8') | |
38 except subprocess.CalledProcessError as e: | |
39 print(e) | |
40 return {'status': 'error', 'ename': 'Compilation error', 'evalue': e.output} | |
41 | |
42 output += '\n### EXECUTION ###\n' | |
43 try: | |
44 output += subprocess.check_output([binaryfile.name], stderr=subprocess.STDOUT).decode('utf-8') | |
45 except subprocess.CalledProcessError as e: | |
46 output += e.output | |
47 if not silent: | |
48 stream_content = {'name': 'stdout', 'text': output} | |
49 self.send_response(self.iopub_socket, 'stream', stream_content) |