def _serialize_variables(variables):

    hidden = variables.get("__traceback_hidden_variables__", ())

    if hidden is True:
        return

    if not isinstance(hidden, (tuple, list)):
        hidden = ()

    variables = tuple((safe_force_text(key), safe_force_text(value))
                      for key, value in variables.items() if not key in hidden)

    if variables:
        yield wl.OpenerView([
            "Local variables",
            wl.Grid(
                iterate((("Key", "Value"), ), variables),
                Background=[None, [wl.LightGray]],
                Alignment=wl.Left,
                Frame=wl.LightGray,
            ),
        ])
    else:
        yield "No local variables"
Esempio n. 2
0
def serialize_traceback(exc_type, exc_value, tb, **opts):
    return wl.OpenerView([
        wl.Row([
            safe_force_text(exc_type.__name__), " ",
            safe_force_text(exc_value)
        ]),
        wl.Style(
            wl.Column(_serialize_traceback(exc_type, exc_value, tb, **opts)),
            FontFamily="Courier")
    ], True)
Esempio n. 3
0
def safe_wl_execute(function,
                    args=(),
                    opts={},
                    export_opts={},
                    exception_class=WolframLanguageException):

    __traceback_hidden_variables__ = True

    try:
        return export(function(*args, **opts), **export_opts)
    except Exception as export_exception:
        try:
            try:

                # The user can provide an exception class, and it can be broken, in which case we are running another
                # try / except to return errors that are happening during class serialization

                if isinstance(export_exception, WolframLanguageException):
                    try:
                        export_exception.set_traceback(*sys.exc_info())
                        return export(export_exception, **export_opts)
                    except Exception:
                        pass

                if exception_class is WolframLanguageException:
                    return export(
                        WolframLanguageException(export_exception,
                                                 exec_info=sys.exc_info()),
                        **export_opts)

                # A custom error class might fail, if this is happening then we can try to use the built in one
                return export(
                    exception_class(export_exception,
                                    exec_info=sys.exc_info()), **export_opts)
            except Exception as exception_export_err:
                return export(
                    WolframLanguageException(exception_export_err,
                                             exec_info=sys.exc_info()),
                    target_format=export_opts.get('target_format',
                                                  DEFAULT_FORMAT),
                    encoder=
                    'wolframclient.serializers.encoders.builtin.encoder',
                )

        except Exception as unknown_exception:

            # This is the last resort.
            # Everything went wrong, including the code that was supposed to return a traceback, or the custom
            # normalizer is doing something it should not. This should never happen.
            try:
                return export(
                    wl.Failure(
                        "PythonFailure", {
                            "MessageTemplate":
                            safe_force_text(unknown_exception),
                            "MessageParameters": {},
                            "FailureCode":
                            safe_force_text(
                                unknown_exception.__class__.__name__),
                            "Traceback":
                            force_text(traceback.format_exc())
                        }),
                    target_format=export_opts.get('target_format',
                                                  DEFAULT_FORMAT),
                    encoder=
                    'wolframclient.serializers.encoders.builtin.encoder',
                )
            except Exception:
                # Something went worst.
                # this might happen with import errors / syntax errors in third party pluging that are loading the
                # exporter and doing some real damage to the dispatcher we are using.
                return DEFAULT_UNKNOWN_FAILURE[export_opts.get(
                    'target_format', DEFAULT_FORMAT)]
Esempio n. 4
0
 def failure_code(self):
     if isinstance(self.payload, Exception):
         return safe_force_text(self.payload.__class__.__name__)
Esempio n. 5
0
 def failure_template(self):
     return safe_force_text(self.payload)