Ejemplo n.º 1
0
        internal void doShell()
        {
            var preprocessedCode = preprocessCode(headerCode); // not sure if this is needed

            Evaluation.EvalInteraction(preprocessedCode);

            logMessage(Evaluation.sbErr.ToString());
            logMessage(Evaluation.sbOut.ToString());

            while (true)
            {
                var msg = recvMessage(shellSocket);

                try
                {
                    switch (msg.Content)
                    {
                    case KernelRequest r:
                        kernelInfoRequest(msg, r);
                        break;

                    case ExecuteRequest r:
                        executeRequest(msg, r);
                        break;

                    case CompleteRequest r:
                        completeRequest(msg, r);
                        break;

                    case IntellisenseRequest r:
                        intellisenseRequest(msg, r);
                        break;

                    case ConnectRequest r:
                        connectRequest(msg, r);
                        break;

                    case ShutdownRequest r:
                        shutdownRequest(msg, r);
                        break;

                    case HistoryRequest r:
                        historyRequest(msg, r);
                        break;

                    case ObjectInfoRequest r:
                        objectInfoRequest(msg, r);
                        break;

                    case InspectRequest r:
                        inspectRequest(msg, r);
                        break;

                    default:
                        logMessage($"Unexpected content type on shell. msg_type is {msg.Header.msg_type} ");
                        break;
                    }
                }
                catch (Exception ex)
                {
                    handleException(ex);
                }
            }
        }
Ejemplo n.º 2
0
        internal void executeRequest(KernelMessage msg, ExecuteRequest content)
        {
            Evaluation.sbOut.Clear();
            Evaluation.sbErr.Clear();
            Evaluation.sbPrint.Clear();
            payload.Clear();

            if (!content.silent)
            {
                executionCount++;
            }

            // sending busy
            sendStateBusy(msg);
            sendMessage(ioSocket, msg, "pyin", new Pyin()
            {
                code = content.code, execution_count = executionCount
            });

            // preprocess
            var newCode = preprocessCode(content.code);

            if (!String.IsNullOrEmpty(newCode))
            {
                // evaluate
                try
                {
                    (var value, var errors) = Evaluation.EvalInteractionNonThrowing(newCode);

                    if (errors.Length > 0)
                    {
                        var sb = errors.Aggregate(new StringBuilder(), (ag, n) => ag.AppendLine(n));
                        sendError(sb.ToString(), msg);
                    }
                    var executeReply = new ExecuteReplyOk()
                    {
                        status           = "ok",
                        execution_count  = executionCount,
                        payload          = payload.ToArray(),
                        user_variables   = new Dictionary <string, object>(),
                        user_expressions = new Dictionary <string, object>()
                    };

                    sendMessage(shellSocket, msg, "execute_reply", executeReply);

                    // send all the data
                    if (!content.silent)
                    {
                        var lastExpression = Evaluation.GetLastExpression();

                        if (lastExpression != null)
                        {
                            var printedResult = Printers.PrintVariable(lastExpression);
                            sendDisplayData(printedResult.ContentType, printedResult.Data, "pyout");
                        }
                    }
                }
                catch (AggregateException ae)
                {
                    var esb = new StringBuilder();
                    esb.AppendLine(ae.Message);

                    foreach (var e in ae.InnerExceptions)
                    {
                        esb.AppendLine(e.Message);
                    }

                    sendError(esb.ToString(), msg);
                }
                catch (Exception ex)
                {
                    var error = $"Expression evaluation failed: {ex.Message}\r\n {ex.StackTrace}";
                    sendError(error, msg);
                }
            }

            if (Evaluation.sbPrint.Length > 0)
            {
                sendDisplayData("text/plain", Evaluation.sbPrint.ToString(), "display_data");
            }

            sendStateIdle(msg);
        }