Exemple #1
0
def _validate_mime_type(mime_type):
    if utils.is_string(mime_type):
        return mime_type.lower().strip()
    # if not a string, mime_type has to be an enum
    elif isinstance(mime_type, MIME_TYPE):
        return mime_type.value

    raise ValueError("Invalid MIME type: %s" % mime_type)
Exemple #2
0
    def do_execute (self, code, silent,
        store_history, user_expressions, allow_stdin):
        try:
            # extract pre/post flight commands, if any
            pre_flight_commands, post_flight_commands, user_code = \
                self.magic_commands._parse_code(code)

            # if there is no user code nor pre/post flight commands, do nothing
            if (user_code.strip() == '') and \
               (len(pre_flight_commands) == 0) and \
               (len(post_flight_commands) == 0):
                return

            # (1/4) execute pre-flight magic commands, if any
            for (mc_name, mc_function) in pre_flight_commands:
                try:
                    # input: string; output: string (or None)
                    mc_output = mc_function(user_code)

                except Exception as exception:
                    future.utils.raise_with_traceback(Exception(
                        "Error while running pre-flight command '%s': "
                        "%s" % (mc_name, exception)))

                # output, if any, becomes the new user code
                if (mc_output is not None):
                    assert utils.is_string(mc_output), (
                        "Invalid return value for pre-flight "
                        "magic command '%s': Must be a string" % mc_name)
                    user_code = mc_output

            # (2/4) pass the user code to the kernel and retrieve frames
            if (user_code.strip() == ''):
                result_frames = []
            else:
                try:
                    _logger.debug("executing:\n%s" % user_code)
                    # input: string; output: generator
                    result_frames = self.do_execute_(user_code)
                    result_frames = renderers.core._check_frames(result_frames)
                    _logger.debug("executing: done")

                except Exception as exception:
                    future.utils.raise_with_traceback(Exception(
                        "Error while evaluating user code: %s" % exception))

            # (3/4) execute post-flight magic commands, if any
            for (mc_name, mc_function) in post_flight_commands:
                try:
                    # input: generator; output: generator
                    mc_output = mc_function(user_code, result_frames)
                    mc_output = renderers.core._check_frames(mc_output)

                except Exception as exception:
                    future.utils.raise_with_traceback(Exception(
                        "Error while running post-flight command '%s': "
                        "%s" % (mc_name, exception)))

                # output becomes the new result frame(s)
                result_frames = mc_output

            # (4/4) render the kernel results and send them to the notebook
            if (silent):
                _logger.debug("emitting nothing (silent notebook)")
            else:
                n_frames, n_subframes = 0, 0
                for (mime_type, content, metadata) in result_frames:
                    # feed the content to any compatible renderer and
                    # send the resulting sub-frame(s) to the notebook
                    try:
                        subframes = renderers.core._render_content(
                            mime_type, content, metadata)

                    except Exception as exception:
                        future.utils.raise_with_traceback(Exception(exception))

                    for (mime_type_, content_, metadata_) in subframes:
                        length_ = len(content_)
                        metadata_ = {} if (metadata_ is None) else metadata_

                        if (mime_type_ == "text/plain"):
                            _logger.debug("emitting text"
                                " (%d %s)" % (
                                    len(content_),
                                    utils.plural("character", length_)))

                            response = ("stream", {
                                "name": "stdout",
                                "text": unicode(content_)})
                        else:
                            _logger.debug("emitting data"
                                " (%s, %d %s, metadata = %s)" % (
                                    mime_type_,
                                    len(content_),
                                    utils.plural("byte", length_),
                                    metadata_))

                            response = ("display_data", {
                                "metadata": metadata_,
                                "data": {mime_type_: content_}})

                        self.send_response(self.iopub_socket, *response)
                        n_subframes += 1

                    n_frames += 1

                _logger.debug("emitted %d %s from %d %s" % (
                    n_subframes, utils.plural("subframe", n_subframes),
                    n_frames, utils.plural("frame", n_frames)))

        except KeyboardInterrupt:
            msg = "Execution aborted by user"

            _logger.error(msg)
            self.send_response(self.iopub_socket, "stream",
                {"name": "stderr", "text": msg})

            return {
                "status": "abort",
                "execution_count": self.execution_count}

        except Exception as exception:
            # retrieve the exception message (last line of traceback)
            exc_type, exc_value, exc_traceback = sys.exc_info()

            msg = traceback.format_exception_only(
                exc_type, exc_value)[0].strip()

            # if the exception is of type Exception, we strip the type
            if (msg.startswith("Exception: ")):
                msg = msg[11:]

            # if the exception has no message attached, says so explicitly
            if (str(exc_value).strip() == ''):
                msg += "(no message)"

            self.send_response(self.iopub_socket, "stream",
                {"name": "stderr", "text": msg})

            # create a more detailed error message for the
            # logger, including the whole stack trace
            stack, msg_ = traceback.extract_tb(exc_traceback), msg
            for (file_name, line, func_name, text) in stack:
                msg_ += "\n%s:%d in %s: %s" % (
                    file_name, line, func_name, text)

            _logger.error(msg_)

            return {
                "status": "error",
                "execution_count": self.execution_count,
                "ename": exc_type.__name__,
                "evalue": msg,
                "traceback": stack}

        # the execution happened without error
        return {
            "status": "ok",
            "execution_count": self.execution_count,
            "payload": [],  # deprecated
            "user_expressions": {}}