-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathswift_kernel.py
548 lines (460 loc) · 19.6 KB
/
swift_kernel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#!/usr/bin/python
#
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import lldb
import os
import re
import signal
import subprocess
import sys
import tempfile
import threading
from ipykernel.kernelbase import Kernel
from jupyter_client.jsonutil import squash_dates
class ExecutionResult:
"""Base class for the result of executing code."""
pass
class ExecutionResultSuccess(ExecutionResult):
"""Base class for the result of successfully executing code."""
pass
class ExecutionResultError(ExecutionResult):
"""Base class for the result of unsuccessfully executing code."""
def description(self):
raise NotImplementedError()
class SuccessWithoutValue(ExecutionResultSuccess):
"""The code executed successfully, and did not produce a value."""
def __repr__(self):
return 'SuccessWithoutValue()'
class SuccessWithValue(ExecutionResultSuccess):
"""The code executed successfully, and produced a value."""
def __init__(self, result):
self.result = result # SBValue
def __repr__(self):
return 'SuccessWithValue(result=%s, description=%s)' % (
repr(self.result), repr(self.result.description))
class PreprocessorError(ExecutionResultError):
"""There was an error preprocessing the code."""
def __init__(self, exception):
self.exception = exception # PreprocessorException
def description(self):
return str(self.exception)
def __repr__(self):
return 'PreprocessorError(exception=%s)' % repr(self.exception)
class PreprocessorException(Exception):
pass
class SwiftError(ExecutionResultError):
"""There was a compile or runtime error."""
def __init__(self, result):
self.result = result # SBValue
def description(self):
return self.result.error.description
def __repr__(self):
return 'SwiftError(result=%s, description=%s)' % (
repr(self.result), repr(self.description()))
class SIGINTHandler(threading.Thread):
"""Interrupts currently-executing code whenever the process receives a
SIGINT."""
def __init__(self, kernel):
super(SIGINTHandler, self).__init__()
self.kernel = kernel
def run(self):
try:
while True:
signal.sigwait([signal.SIGINT])
self.kernel.process.SendAsyncInterrupt()
except Exception as e:
self.kernel.log.error('Exception in SIGINTHandler: %s' % str(e))
class StdoutHandler(threading.Thread):
"""Collects stdout from the Swift process and sends it to the client."""
def __init__(self, kernel):
super(StdoutHandler, self).__init__()
self.kernel = kernel
self.stop_event = threading.Event()
self.had_stdout = False
def _get_stdout(self):
while True:
BUFFER_SIZE = 1000
stdout_buffer = self.kernel.process.GetSTDOUT(BUFFER_SIZE)
if len(stdout_buffer) == 0:
break
yield stdout_buffer
def _get_and_send_stdout(self):
stdout = ''.join([buf for buf in self._get_stdout()])
if len(stdout) > 0:
self.had_stdout = True
self.kernel.send_response(self.kernel.iopub_socket, 'stream', {
'name': 'stdout',
'text': stdout
})
def run(self):
try:
while True:
if self.stop_event.wait(0.1):
break
self._get_and_send_stdout()
self._get_and_send_stdout()
except Exception as e:
self.kernel.log.error('Exception in StdoutHandler: %s' % str(e))
class SwiftKernel(Kernel):
implementation = 'SwiftKernel'
implementation_version = '0.1'
banner = ''
language_info = {
'name': 'swift',
'mimetype': 'text/x-swift',
'file_extension': '.swift',
'version': '',
}
def __init__(self, **kwargs):
super(SwiftKernel, self).__init__(**kwargs)
# Whether to do code completion. (Code completion currently crashes
# the kernel a lot so it is opt-in for now).
self.completion_enabled = False
self._init_repl_process()
self._init_kernel_communicator()
self._init_int_bitwidth()
self._init_sigint_handler()
def _init_repl_process(self):
self.debugger = lldb.SBDebugger.Create()
if not self.debugger:
raise Exception('Could not start debugger')
self.debugger.SetAsync(False)
# LLDB crashes while trying to load some Python stuff on Mac. Maybe
# something is misconfigured? This works around the problem by telling
# LLDB not to load the Python scripting stuff, which we don't use
# anyways.
self.debugger.SetScriptLanguage(lldb.eScriptLanguageNone)
repl_swift = os.environ['REPL_SWIFT_PATH']
self.target = self.debugger.CreateTargetWithFileAndArch(repl_swift, '')
if not self.target:
raise Exception('Could not create target %s' % repl_swift)
self.main_bp = self.target.BreakpointCreateByName(
'repl_main', self.target.GetExecutable().GetFilename())
if not self.main_bp:
raise Exception('Could not set breakpoint')
repl_env = []
script_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
repl_env.append('PYTHONPATH=%s' % script_dir)
env_var_blacklist = [
'PYTHONPATH',
'REPL_SWIFT_PATH'
]
for key in os.environ:
if key in env_var_blacklist:
continue
repl_env.append('%s=%s' % (key, os.environ[key]))
self.process = self.target.LaunchSimple(None,
repl_env,
os.getcwd())
if not self.process:
raise Exception('Could not launch process')
self.expr_opts = lldb.SBExpressionOptions()
self.swift_language = lldb.SBLanguageRuntime.GetLanguageTypeFromString(
'swift')
self.expr_opts.SetLanguage(self.swift_language)
self.expr_opts.SetREPLMode(True)
self.expr_opts.SetUnwindOnError(False)
self.expr_opts.SetGenerateDebugInfo(True)
# Sets an infinite timeout so that users can run aribtrarily long
# computations.
self.expr_opts.SetTimeoutInMicroSeconds(0)
self.main_thread = self.process.GetThreadAtIndex(0)
def _init_kernel_communicator(self):
result = self._preprocess_and_execute(
'%include "KernelCommunicator.swift"')
if isinstance(result, ExecutionResultError):
raise Exception('Error initing KernelCommunicator: %s' % result)
session_key = self.session.key.decode('utf8')
decl_code = """
enum JupyterKernel {
static var communicator = KernelCommunicator(
jupyterSession: KernelCommunicator.JupyterSession(
id: %s, key: %s, username: %s))
}
""" % (json.dumps(self.session.session), json.dumps(session_key),
json.dumps(self.session.username))
result = self._preprocess_and_execute(decl_code)
if isinstance(result, ExecutionResultError):
raise Exception('Error declaring JupyterKernel: %s' % result)
def _init_int_bitwidth(self):
result = self._execute('Int.bitWidth')
if not isinstance(result, SuccessWithValue):
raise Exception('Expected value from Int.bitWidth, but got: %s' %
result)
self._int_bitwidth = int(result.result.description)
def _init_sigint_handler(self):
self.sigint_handler = SIGINTHandler(self)
self.sigint_handler.start()
def _file_name_for_source_location(self):
return '<Cell %d>' % self.execution_count
def _preprocess_and_execute(self, code):
try:
preprocessed = self._preprocess(code)
except PreprocessorException as e:
return PreprocessorError(e)
return self._execute(preprocessed)
def _preprocess(self, code):
lines = code.split('\n')
preprocessed_lines = [
self._preprocess_line(i, line) for i, line in enumerate(lines)]
return '\n'.join(preprocessed_lines)
def _handle_enable_completion(self):
if not hasattr(self.target, 'CompleteCode'):
self.send_response(self.iopub_socket, 'stream', {
'name': 'stdout',
'text': 'Completion NOT enabled because toolchain does not ' +
'have CompleteCode API.\n'
})
return
self.completion_enabled = True
self.send_response(self.iopub_socket, 'stream', {
'name': 'stdout',
'text': 'Completion enabled!\n'
})
def _preprocess_line(self, line_index, line):
include_match = re.match(r'^\s*%include (.*)$', line)
if include_match is not None:
return self._read_include(line_index, include_match.group(1))
enable_completion_match = re.match(r'^\s*%enableCompletion\s*$', line)
if enable_completion_match is not None:
self._handle_enable_completion()
return ''
return line
def _read_include(self, line_index, rest_of_line):
name_match = re.match(r'^\s*"([^"]+)"\s*', rest_of_line)
if name_match is None:
raise PreprocessorException(
'Line %d: %%include must be followed by a name in quotes' % (
line_index + 1))
name = name_match.group(1)
include_paths = [
os.path.dirname(os.path.realpath(sys.argv[0])),
os.path.realpath("."),
]
code = None
for include_path in include_paths:
try:
with open(os.path.join(include_path, name), 'r') as f:
code = f.read()
except IOError:
continue
if code is None:
raise PreprocessorException(
'Line %d: Could not find "%s". Searched %s.' % (
line_index + 1, name, include_paths))
return '\n'.join([
'#sourceLocation(file: "%s", line: 1)' % name,
code,
'#sourceLocation(file: "%s", line: %d)' % (
self._file_name_for_source_location(), line_index + 1),
''
])
def _execute(self, code):
locationDirective = '#sourceLocation(file: "%s", line: 1)' % (
self._file_name_for_source_location())
codeWithLocationDirective = locationDirective + '\n' + code
result = self.target.EvaluateExpression(
codeWithLocationDirective, self.expr_opts)
if result.error.type == lldb.eErrorTypeInvalid:
return SuccessWithValue(result)
elif result.error.type == lldb.eErrorTypeGeneric:
return SuccessWithoutValue()
else:
return SwiftError(result)
def _after_successful_execution(self):
result = self._execute(
'JupyterKernel.communicator.triggerAfterSuccessfulExecution()')
if not isinstance(result, SuccessWithValue):
self.log.error(
'Expected value from triggerAfterSuccessfulExecution(), '
'but got: %s' % result)
return
messages = self._read_jupyter_messages(result.result)
self._send_jupyter_messages(messages)
def _read_jupyter_messages(self, sbvalue):
return {
'display_messages': [
self._read_display_message(display_message_sbvalue)
for display_message_sbvalue
in sbvalue
]
}
def _read_display_message(self, sbvalue):
return [self._read_byte_array(part) for part in sbvalue]
def _read_byte_array(self, sbvalue):
get_position_error = lldb.SBError()
position = sbvalue \
.GetChildMemberWithName('_position') \
.GetData() \
.GetAddress(get_position_error, 0)
if get_position_error.Fail():
raise Exception('getting position: %s' % str(get_position_error))
get_count_error = lldb.SBError()
count_data = sbvalue \
.GetChildMemberWithName('count') \
.GetData()
if self._int_bitwidth == 32:
count = count_data.GetSignedInt32(get_count_error, 0)
elif self._int_bitwidth == 64:
count = count_data.GetSignedInt64(get_count_error, 0)
else:
raise Exception('Unsupported integer bitwidth %d' %
self._int_bitwidth)
if get_count_error.Fail():
raise Exception('getting count: %s' % str(get_count_error))
# ReadMemory requires that count is positive, so early-return an empty
# byte array when count is 0.
if count == 0:
return bytes()
get_data_error = lldb.SBError()
data = self.process.ReadMemory(position, count, get_data_error)
if get_data_error.Fail():
raise Exception('getting data: %s' % str(get_data_error))
return data
def _send_jupyter_messages(self, messages):
for display_message in messages['display_messages']:
self.iopub_socket.send_multipart(display_message)
def _set_parent_message(self):
result = self._execute("""
JupyterKernel.communicator.updateParentMessage(
to: KernelCommunicator.ParentMessage(json: %s))
""" % json.dumps(json.dumps(squash_dates(self._parent_header))))
if isinstance(result, ExecutionResultError):
raise Exception('Error setting parent message: %s' % result)
def _get_pretty_main_thread_stack_trace(self):
stack_trace = []
for frame in self.main_thread:
# Do not include frames without source location information. These
# are frames in libraries and frames that belong to the LLDB
# expression execution implementation.
if not frame.line_entry.file:
continue
# Do not include <compiler-generated> frames. These are
# specializations of library functions.
if frame.line_entry.file.fullpath == '<compiler-generated>':
continue
stack_trace.append(str(frame))
return stack_trace
def _make_error_message(self, traceback):
return {
'status': 'error',
'execution_count': self.execution_count,
'ename': '',
'evalue': '',
'traceback': traceback
}
def _send_exception_report(self, while_doing, e):
error_message = self._make_error_message([
'Kernel is in a bad state. Try restarting the kernel.',
'',
'Exception in `%s`:' % while_doing,
str(e)
])
self.send_response(self.iopub_socket, 'error', error_message)
return error_message
def _execute_cell(self, code):
self._set_parent_message()
result = self._preprocess_and_execute(code)
if isinstance(result, ExecutionResultSuccess):
self._after_successful_execution()
return result
def do_execute(self, code, silent, store_history=True,
user_expressions=None, allow_stdin=False):
# Start up a new thread to collect stdout.
stdout_handler = StdoutHandler(self)
stdout_handler.start()
# Execute the cell, handle unexpected exceptions, and make sure to
# always clean up the stdout handler.
try:
result = self._execute_cell(code)
except Exception as e:
return self._send_exception_report('_execute_cell', e)
finally:
stdout_handler.stop_event.set()
stdout_handler.join()
# Send values/errors and status to the client.
if isinstance(result, SuccessWithValue):
self.send_response(self.iopub_socket, 'execute_result', {
'execution_count': self.execution_count,
'data': {
'text/plain': result.result.description
},
'metadata': {}
})
return {
'status': 'ok',
'execution_count': self.execution_count,
'payload': [],
'user_expressions': {}
}
elif isinstance(result, SuccessWithoutValue):
return {
'status': 'ok',
'execution_count': self.execution_count,
'payload': [],
'user_expressions': {}
}
elif isinstance(result, ExecutionResultError):
if stdout_handler.had_stdout:
# When there is stdout, it is a runtime error. Stdout, which we
# have already sent to the client, contains the error message
# (plus some other ugly traceback that we should eventually
# figure out how to suppress), so this block of code only needs
# to add a traceback.
traceback = []
traceback.append('Current stack trace:')
traceback += [
'\t%s' % frame
for frame in self._get_pretty_main_thread_stack_trace()
]
error_message = self._make_error_message(traceback)
self.send_response(self.iopub_socket, 'error', error_message)
return error_message
# There is no stdout, so it must be a compile error. Simply return
# the error without trying to get a stack trace.
error_message = self._make_error_message([result.description()])
self.send_response(self.iopub_socket, 'error', error_message)
return error_message
def do_complete(self, code, cursor_pos):
if not self.completion_enabled:
return
code_to_cursor = code[:cursor_pos]
sbresponse = self.target.CompleteCode(
self.swift_language, None, code_to_cursor)
prefix = sbresponse.GetPrefix()
insertable_matches = []
for i in range(sbresponse.GetNumMatches()):
sbmatch = sbresponse.GetMatchAtIndex(i)
insertable_match = prefix + sbmatch.GetInsertable()
if insertable_match.startswith("_"):
continue
insertable_matches.append(insertable_match)
return {
'status': 'ok',
'matches': insertable_matches,
'cursor_start': cursor_pos - len(prefix),
'cursor_end': cursor_pos,
}
if __name__ == '__main__':
# Jupyter sends us SIGINT when the user requests execution interruption.
# Here, we block all threads from receiving the SIGINT, so that we can
# handle it in a specific handler thread.
signal.pthread_sigmask(signal.SIG_BLOCK, [signal.SIGINT])
from ipykernel.kernelapp import IPKernelApp
# We pass the kernel name as a command-line arg, since Jupyter gives those
# highest priority (in particular overriding any system-wide config).
IPKernelApp.launch_instance(
argv=sys.argv + ['--IPKernelApp.kernel_class=__main__.SwiftKernel'])