##// END OF EJS Templates
log the error stack for a kernel javascript error message
Jason Grout -
Show More
@@ -1,584 +1,584 b''
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2008-2011 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // Kernel
10 10 //============================================================================
11 11
12 12 /**
13 13 * @module IPython
14 14 * @namespace IPython
15 15 * @submodule Kernel
16 16 */
17 17
18 18 var IPython = (function (IPython) {
19 19 "use strict";
20 20
21 21 var utils = IPython.utils;
22 22
23 23 // Initialization and connection.
24 24 /**
25 25 * A Kernel Class to communicate with the Python kernel
26 26 * @Class Kernel
27 27 */
28 28 var Kernel = function (base_url) {
29 29 this.kernel_id = null;
30 30 this.shell_channel = null;
31 31 this.iopub_channel = null;
32 32 this.stdin_channel = null;
33 33 this.base_url = base_url;
34 34 this.running = false;
35 35 this.username = "username";
36 36 this.session_id = utils.uuid();
37 37 this._msg_callbacks = {};
38 38
39 39 if (typeof(WebSocket) !== 'undefined') {
40 40 this.WebSocket = WebSocket;
41 41 } else if (typeof(MozWebSocket) !== 'undefined') {
42 42 this.WebSocket = MozWebSocket;
43 43 } else {
44 44 alert('Your browser does not have WebSocket support, please try Chrome, Safari or Firefox β‰₯ 6. Firefox 4 and 5 are also supported by you have to enable WebSockets in about:config.');
45 45 }
46 46
47 47 this.bind_events();
48 48 this.init_iopub_handlers();
49 49 this.comm_manager = new IPython.CommManager(this);
50 50 };
51 51
52 52
53 53 Kernel.prototype._get_msg = function (msg_type, content, metadata) {
54 54 var msg = {
55 55 header : {
56 56 msg_id : utils.uuid(),
57 57 username : this.username,
58 58 session : this.session_id,
59 59 msg_type : msg_type
60 60 },
61 61 metadata : metadata || {},
62 62 content : content,
63 63 parent_header : {}
64 64 };
65 65 return msg;
66 66 };
67 67
68 68 Kernel.prototype.bind_events = function () {
69 69 var that = this;
70 70 $([IPython.events]).on('send_input_reply.Kernel', function(evt, data) {
71 71 that.send_input_reply(data);
72 72 });
73 73 };
74 74
75 75 // Initialize the iopub handlers
76 76
77 77 Kernel.prototype.init_iopub_handlers = function () {
78 78 var output_types = ['stream', 'display_data', 'pyout', 'pyerr'];
79 79 this._iopub_handlers = {};
80 80 this.register_iopub_handler('status', $.proxy(this._handle_status_message, this));
81 81 this.register_iopub_handler('clear_output', $.proxy(this._handle_clear_output, this));
82 82
83 83 for (var i=0; i < output_types.length; i++) {
84 84 this.register_iopub_handler(output_types[i], $.proxy(this._handle_output_message, this));
85 85 }
86 86 };
87 87
88 88 /**
89 89 * Start the Python kernel
90 90 * @method start
91 91 */
92 92 Kernel.prototype.start = function (params) {
93 93 params = params || {};
94 94 if (!this.running) {
95 95 var qs = $.param(params);
96 96 var url = this.base_url + '?' + qs;
97 97 $.post(url,
98 98 $.proxy(this._kernel_started, this),
99 99 'json'
100 100 );
101 101 }
102 102 };
103 103
104 104 /**
105 105 * Restart the python kernel.
106 106 *
107 107 * Emit a 'status_restarting.Kernel' event with
108 108 * the current object as parameter
109 109 *
110 110 * @method restart
111 111 */
112 112 Kernel.prototype.restart = function () {
113 113 $([IPython.events]).trigger('status_restarting.Kernel', {kernel: this});
114 114 if (this.running) {
115 115 this.stop_channels();
116 116 var url = utils.url_join_encode(this.kernel_url, "restart");
117 117 $.post(url,
118 118 $.proxy(this._kernel_started, this),
119 119 'json'
120 120 );
121 121 }
122 122 };
123 123
124 124
125 125 Kernel.prototype._kernel_started = function (json) {
126 126 console.log("Kernel started: ", json.id);
127 127 this.running = true;
128 128 this.kernel_id = json.id;
129 129 var ws_url = json.ws_url;
130 130 if (ws_url.match(/wss?:\/\//) === null) {
131 131 // trailing 's' in https will become wss for secure web sockets
132 132 var prot = location.protocol.replace('http', 'ws') + "//";
133 133 ws_url = prot + location.host + ws_url;
134 134 }
135 135 this.ws_url = ws_url;
136 136 this.kernel_url = utils.url_join_encode(this.base_url, this.kernel_id);
137 137 this.start_channels();
138 138 };
139 139
140 140
141 141 Kernel.prototype._websocket_closed = function(ws_url, early) {
142 142 this.stop_channels();
143 143 $([IPython.events]).trigger('websocket_closed.Kernel',
144 144 {ws_url: ws_url, kernel: this, early: early}
145 145 );
146 146 };
147 147
148 148 /**
149 149 * Start the `shell`and `iopub` channels.
150 150 * Will stop and restart them if they already exist.
151 151 *
152 152 * @method start_channels
153 153 */
154 154 Kernel.prototype.start_channels = function () {
155 155 var that = this;
156 156 this.stop_channels();
157 157 var ws_url = this.ws_url + this.kernel_url;
158 158 console.log("Starting WebSockets:", ws_url);
159 159 this.shell_channel = new this.WebSocket(ws_url + "/shell");
160 160 this.stdin_channel = new this.WebSocket(ws_url + "/stdin");
161 161 this.iopub_channel = new this.WebSocket(ws_url + "/iopub");
162 162
163 163 var already_called_onclose = false; // only alert once
164 164 var ws_closed_early = function(evt){
165 165 if (already_called_onclose){
166 166 return;
167 167 }
168 168 already_called_onclose = true;
169 169 if ( ! evt.wasClean ){
170 170 that._websocket_closed(ws_url, true);
171 171 }
172 172 };
173 173 var ws_closed_late = function(evt){
174 174 if (already_called_onclose){
175 175 return;
176 176 }
177 177 already_called_onclose = true;
178 178 if ( ! evt.wasClean ){
179 179 that._websocket_closed(ws_url, false);
180 180 }
181 181 };
182 182 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
183 183 for (var i=0; i < channels.length; i++) {
184 184 channels[i].onopen = $.proxy(this._ws_opened, this);
185 185 channels[i].onclose = ws_closed_early;
186 186 }
187 187 // switch from early-close to late-close message after 1s
188 188 setTimeout(function() {
189 189 for (var i=0; i < channels.length; i++) {
190 190 if (channels[i] !== null) {
191 191 channels[i].onclose = ws_closed_late;
192 192 }
193 193 }
194 194 }, 1000);
195 195 this.shell_channel.onmessage = $.proxy(this._handle_shell_reply, this);
196 196 this.iopub_channel.onmessage = $.proxy(this._handle_iopub_message, this);
197 197 this.stdin_channel.onmessage = $.proxy(this._handle_input_request, this);
198 198 };
199 199
200 200 /**
201 201 * Handle a websocket entering the open state
202 202 * sends session and cookie authentication info as first message.
203 203 * Once all sockets are open, signal the Kernel.status_started event.
204 204 * @method _ws_opened
205 205 */
206 206 Kernel.prototype._ws_opened = function (evt) {
207 207 // send the session id so the Session object Python-side
208 208 // has the same identity
209 209 evt.target.send(this.session_id + ':' + document.cookie);
210 210
211 211 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
212 212 for (var i=0; i < channels.length; i++) {
213 213 // if any channel is not ready, don't trigger event.
214 214 if ( !channels[i].readyState ) return;
215 215 }
216 216 // all events ready, trigger started event.
217 217 $([IPython.events]).trigger('status_started.Kernel', {kernel: this});
218 218 };
219 219
220 220 /**
221 221 * Stop the websocket channels.
222 222 * @method stop_channels
223 223 */
224 224 Kernel.prototype.stop_channels = function () {
225 225 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
226 226 for (var i=0; i < channels.length; i++) {
227 227 if ( channels[i] !== null ) {
228 228 channels[i].onclose = null;
229 229 channels[i].close();
230 230 }
231 231 }
232 232 this.shell_channel = this.iopub_channel = this.stdin_channel = null;
233 233 };
234 234
235 235 // Main public methods.
236 236
237 237 // send a message on the Kernel's shell channel
238 238 Kernel.prototype.send_shell_message = function (msg_type, content, callbacks, metadata) {
239 239 var msg = this._get_msg(msg_type, content, metadata);
240 240 this.shell_channel.send(JSON.stringify(msg));
241 241 this.set_callbacks_for_msg(msg.header.msg_id, callbacks);
242 242 return msg.header.msg_id;
243 243 };
244 244
245 245 /**
246 246 * Get info on an object
247 247 *
248 248 * @param objname {string}
249 249 * @param callback {function}
250 250 * @method object_info
251 251 *
252 252 * When calling this method, pass a callback function that expects one argument.
253 253 * The callback will be passed the complete `object_info_reply` message documented
254 254 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information)
255 255 */
256 256 Kernel.prototype.object_info = function (objname, callback) {
257 257 var callbacks;
258 258 if (callback) {
259 259 callbacks = { shell : { reply : callback } };
260 260 }
261 261
262 262 if (typeof(objname) !== null && objname !== null) {
263 263 var content = {
264 264 oname : objname.toString(),
265 265 detail_level : 0,
266 266 };
267 267 return this.send_shell_message("object_info_request", content, callbacks);
268 268 }
269 269 return;
270 270 };
271 271
272 272 /**
273 273 * Execute given code into kernel, and pass result to callback.
274 274 *
275 275 * @async
276 276 * @method execute
277 277 * @param {string} code
278 278 * @param [callbacks] {Object} With the following keys (all optional)
279 279 * @param callbacks.shell.reply {function}
280 280 * @param callbacks.shell.payload.[payload_name] {function}
281 281 * @param callbacks.iopub.output {function}
282 282 * @param callbacks.iopub.clear_output {function}
283 283 * @param callbacks.input {function}
284 284 * @param {object} [options]
285 285 * @param [options.silent=false] {Boolean}
286 286 * @param [options.user_expressions=empty_dict] {Dict}
287 287 * @param [options.user_variables=empty_list] {List od Strings}
288 288 * @param [options.allow_stdin=false] {Boolean} true|false
289 289 *
290 290 * @example
291 291 *
292 292 * The options object should contain the options for the execute call. Its default
293 293 * values are:
294 294 *
295 295 * options = {
296 296 * silent : true,
297 297 * user_variables : [],
298 298 * user_expressions : {},
299 299 * allow_stdin : false
300 300 * }
301 301 *
302 302 * When calling this method pass a callbacks structure of the form:
303 303 *
304 304 * callbacks = {
305 305 * shell : {
306 306 * reply : execute_reply_callback,
307 307 * payload : {
308 308 * set_next_input : set_next_input_callback,
309 309 * }
310 310 * },
311 311 * iopub : {
312 312 * output : output_callback,
313 313 * clear_output : clear_output_callback,
314 314 * },
315 315 * input : raw_input_callback
316 316 * }
317 317 *
318 318 * Each callback will be passed the entire message as a single arugment.
319 319 * Payload handlers will be passed the corresponding payload and the execute_reply message.
320 320 */
321 321 Kernel.prototype.execute = function (code, callbacks, options) {
322 322
323 323 var content = {
324 324 code : code,
325 325 silent : true,
326 326 store_history : false,
327 327 user_variables : [],
328 328 user_expressions : {},
329 329 allow_stdin : false
330 330 };
331 331 callbacks = callbacks || {};
332 332 if (callbacks.input !== undefined) {
333 333 content.allow_stdin = true;
334 334 }
335 335 $.extend(true, content, options);
336 336 $([IPython.events]).trigger('execution_request.Kernel', {kernel: this, content:content});
337 337 return this.send_shell_message("execute_request", content, callbacks);
338 338 };
339 339
340 340 /**
341 341 * When calling this method, pass a function to be called with the `complete_reply` message
342 342 * as its only argument when it arrives.
343 343 *
344 344 * `complete_reply` is documented
345 345 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#complete)
346 346 *
347 347 * @method complete
348 348 * @param line {integer}
349 349 * @param cursor_pos {integer}
350 350 * @param callback {function}
351 351 *
352 352 */
353 353 Kernel.prototype.complete = function (line, cursor_pos, callback) {
354 354 var callbacks;
355 355 if (callback) {
356 356 callbacks = { shell : { reply : callback } };
357 357 }
358 358 var content = {
359 359 text : '',
360 360 line : line,
361 361 block : null,
362 362 cursor_pos : cursor_pos
363 363 };
364 364 return this.send_shell_message("complete_request", content, callbacks);
365 365 };
366 366
367 367
368 368 Kernel.prototype.interrupt = function () {
369 369 if (this.running) {
370 370 $([IPython.events]).trigger('status_interrupting.Kernel', {kernel: this});
371 371 $.post(this.kernel_url + "/interrupt");
372 372 }
373 373 };
374 374
375 375
376 376 Kernel.prototype.kill = function () {
377 377 if (this.running) {
378 378 this.running = false;
379 379 var settings = {
380 380 cache : false,
381 381 type : "DELETE"
382 382 };
383 383 $.ajax(this.kernel_url, settings);
384 384 }
385 385 };
386 386
387 387 Kernel.prototype.send_input_reply = function (input) {
388 388 var content = {
389 389 value : input,
390 390 };
391 391 $([IPython.events]).trigger('input_reply.Kernel', {kernel: this, content:content});
392 392 var msg = this._get_msg("input_reply", content);
393 393 this.stdin_channel.send(JSON.stringify(msg));
394 394 return msg.header.msg_id;
395 395 };
396 396
397 397
398 398 // Reply handlers
399 399
400 400 Kernel.prototype.register_iopub_handler = function (msg_type, callback) {
401 401 this._iopub_handlers[msg_type] = callback;
402 402 };
403 403
404 404 Kernel.prototype.get_iopub_handler = function (msg_type) {
405 405 // get iopub handler for a specific message type
406 406 return this._iopub_handlers[msg_type];
407 407 };
408 408
409 409
410 410 Kernel.prototype.get_callbacks_for_msg = function (msg_id) {
411 411 // get callbacks for a specific message
412 412 return this._msg_callbacks[msg_id];
413 413 };
414 414
415 415
416 416 Kernel.prototype.clear_callbacks_for_msg = function (msg_id) {
417 417 if (this._msg_callbacks[msg_id] !== undefined ) {
418 418 delete this._msg_callbacks[msg_id];
419 419 }
420 420 };
421 421
422 422 /* Set callbacks for a particular message.
423 423 * Callbacks should be a struct of the following form:
424 424 * shell : {
425 425 *
426 426 * }
427 427
428 428 */
429 429 Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) {
430 430 if (callbacks) {
431 431 // shallow-copy mapping, because we will modify it at the top level
432 432 var cbcopy = this._msg_callbacks[msg_id] = {};
433 433 cbcopy.shell = callbacks.shell;
434 434 cbcopy.iopub = callbacks.iopub;
435 435 cbcopy.input = callbacks.input;
436 436 this._msg_callbacks[msg_id] = cbcopy;
437 437 }
438 438 };
439 439
440 440
441 441 Kernel.prototype._handle_shell_reply = function (e) {
442 442 var reply = $.parseJSON(e.data);
443 443 $([IPython.events]).trigger('shell_reply.Kernel', {kernel: this, reply:reply});
444 444 var content = reply.content;
445 445 var metadata = reply.metadata;
446 446 var parent_id = reply.parent_header.msg_id;
447 447 var callbacks = this.get_callbacks_for_msg(parent_id);
448 448 if (!callbacks || !callbacks.shell) {
449 449 return;
450 450 }
451 451 var shell_callbacks = callbacks.shell;
452 452
453 453 // clear callbacks on shell
454 454 delete callbacks.shell;
455 455 delete callbacks.input;
456 456 if (!callbacks.iopub) {
457 457 this.clear_callbacks_for_msg(parent_id);
458 458 }
459 459
460 460 if (shell_callbacks.reply !== undefined) {
461 461 shell_callbacks.reply(reply);
462 462 }
463 463 if (content.payload && shell_callbacks.payload) {
464 464 this._handle_payloads(content.payload, shell_callbacks.payload, reply);
465 465 }
466 466 };
467 467
468 468
469 469 Kernel.prototype._handle_payloads = function (payloads, payload_callbacks, msg) {
470 470 var l = payloads.length;
471 471 // Payloads are handled by triggering events because we don't want the Kernel
472 472 // to depend on the Notebook or Pager classes.
473 473 for (var i=0; i<l; i++) {
474 474 var payload = payloads[i];
475 475 var callback = payload_callbacks[payload.source];
476 476 if (callback) {
477 477 callback(payload, msg);
478 478 }
479 479 }
480 480 };
481 481
482 482 Kernel.prototype._handle_status_message = function (msg) {
483 483 var execution_state = msg.content.execution_state;
484 484 var parent_id = msg.parent_header.msg_id;
485 485
486 486 // dispatch status msg callbacks, if any
487 487 var callbacks = this.get_callbacks_for_msg(parent_id);
488 488 if (callbacks && callbacks.iopub && callbacks.iopub.status) {
489 489 try {
490 490 callbacks.iopub.status(msg);
491 491 } catch (e) {
492 console.log("Exception in status msg handler", e);
492 console.log("Exception in status msg handler", e, e.stack);
493 493 }
494 494 }
495 495
496 496 if (execution_state === 'busy') {
497 497 $([IPython.events]).trigger('status_busy.Kernel', {kernel: this});
498 498 } else if (execution_state === 'idle') {
499 499 // clear callbacks on idle, there can be no more
500 500 if (callbacks !== undefined) {
501 501 delete callbacks.iopub;
502 502 delete callbacks.input;
503 503 if (!callbacks.shell) {
504 504 this.clear_callbacks_for_msg(parent_id);
505 505 }
506 506 }
507 507 // trigger status_idle event
508 508 $([IPython.events]).trigger('status_idle.Kernel', {kernel: this});
509 509 } else if (execution_state === 'restarting') {
510 510 // autorestarting is distinct from restarting,
511 511 // in that it means the kernel died and the server is restarting it.
512 512 // status_restarting sets the notification widget,
513 513 // autorestart shows the more prominent dialog.
514 514 $([IPython.events]).trigger('status_autorestarting.Kernel', {kernel: this});
515 515 $([IPython.events]).trigger('status_restarting.Kernel', {kernel: this});
516 516 } else if (execution_state === 'dead') {
517 517 this.stop_channels();
518 518 $([IPython.events]).trigger('status_dead.Kernel', {kernel: this});
519 519 }
520 520 };
521 521
522 522
523 523 // handle clear_output message
524 524 Kernel.prototype._handle_clear_output = function (msg) {
525 525 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
526 526 if (!callbacks || !callbacks.iopub) {
527 527 return;
528 528 }
529 529 var callback = callbacks.iopub.clear_output;
530 530 if (callback) {
531 531 callback(msg);
532 532 }
533 533 };
534 534
535 535
536 536 // handle an output message (pyout, display_data, etc.)
537 537 Kernel.prototype._handle_output_message = function (msg) {
538 538 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
539 539 if (!callbacks || !callbacks.iopub) {
540 540 return;
541 541 }
542 542 var callback = callbacks.iopub.output;
543 543 if (callback) {
544 544 callback(msg);
545 545 }
546 546 };
547 547
548 548 // dispatch IOPub messages to respective handlers.
549 549 // each message type should have a handler.
550 550 Kernel.prototype._handle_iopub_message = function (e) {
551 551 var msg = $.parseJSON(e.data);
552 552
553 553 var handler = this.get_iopub_handler(msg.header.msg_type);
554 554 if (handler !== undefined) {
555 555 handler(msg);
556 556 }
557 557 };
558 558
559 559
560 560 Kernel.prototype._handle_input_request = function (e) {
561 561 var request = $.parseJSON(e.data);
562 562 var header = request.header;
563 563 var content = request.content;
564 564 var metadata = request.metadata;
565 565 var msg_type = header.msg_type;
566 566 if (msg_type !== 'input_request') {
567 567 console.log("Invalid input request!", request);
568 568 return;
569 569 }
570 570 var callbacks = this.get_callbacks_for_msg(request.parent_header.msg_id);
571 571 if (callbacks) {
572 572 if (callbacks.input) {
573 573 callbacks.input(request);
574 574 }
575 575 }
576 576 };
577 577
578 578
579 579 IPython.Kernel = Kernel;
580 580
581 581 return IPython;
582 582
583 583 }(IPython));
584 584
General Comments 0
You need to be logged in to leave comments. Login now