def execute_from_string(code, globals={}, **opts):

    __traceback_hidden_variables__ = [
        "env", "current", "__traceback_hidden_variables__"
    ]

    # this is creating a custom __loader__ that is returning the source code
    # traceback serializers is inspecting global variables and looking for a standard loader that can return source code.

    env = EvaluationEnvironment(code=code, **opts)
    result = None
    expressions = list(
        compile(
            code,
            filename="<unknown>",
            mode="exec",
            flags=ast.PyCF_ONLY_AST | unicode_literals.compiler_flag,
        ).body)

    if not expressions:
        return

    last_expr = last(expressions)

    if isinstance(last_expr, ast.Expr):
        result = expressions.pop(-1)

    if expressions:
        exec(compile(ast.Module(expressions), "", "exec"), env)

    if result:
        return eval(compile(ast.Expression(result.value), "", "eval"), env)

    elif isinstance(last_expr, (ast.FunctionDef, ast.ClassDef)):
        return env[last_expr.name]
Пример #2
0
def generate_http_response(session, request, expression):
    wl_req = django_request_meta(request)

    response = process_generate_httpresponse_expression(
        auto_wait(
            session.evaluate(
                make_generate_httpresponse_expression(wl_req, expression))))

    http = HttpResponse(content=response.get("BodyByteArray", b""),
                        status=response.get("StatusCode", 200))

    for rule in response.get("Headers", ()):
        http[first(rule.args)] = last(rule.args)

    return http
def get_wl_handler(path):
    return EXTENSIONS[last(os.path.splitext(path)).lower()]
    array_type_token = ARRAY_TYPES[wl_type]
    if array_type_token not in VALID_PACKED_ARRAY_TYPES:
        raise ValueError("Invalid PackedArray type %s" % array_type_token)
    return array_to_wxf(WXF_CONSTANTS.PackedArray, data, dimensions,
                        array_type_token)


def array_to_list(data, dimensions, wl_type):
    for dimension in dimensions:
        valid_dimension_or_fail(dimension)
    return _array_to_list(data, dimensions, wl_type)


if hasattr(memoryview, "cast"):
    unpack_mapping = Settings(
        (k, last(v.format)) for k, v in STRUCT_MAPPING.items())

    def _to_complex(array, max_depth, curr_depth):
        # recursivelly traverse the array until the last (real) dimension is reached
        # it correspond to an array of (fake) array of two elements (real and im parts).
        if curr_depth < max_depth - 1:
            for sub in array:
                _to_complex(sub, max_depth, curr_depth + 1)
            return
        # iterate over the pairs
        for index, complex_pair in enumerate(array):
            array[index] = complex(*complex_pair)

    def _array_to_list(data, shape, array_type):
        view = memoryview(data)
        if array_type == "ComplexReal32" or array_type == "ComplexReal64":