def wrapExpressionBuiltinExecfileCreation(filename, globals_arg,
                                                  locals_arg, source_ref):
            provider = node.getParentVariableProvider()

            temp_scope = provider.allocateTempScope("execfile")

            globals_ref, locals_ref, tried, final = wrapEvalGlobalsAndLocals(
                provider=provider,
                globals_node=globals_arg,
                locals_node=locals_arg,
                temp_scope=temp_scope,
                source_ref=source_ref)

            return ExpressionTryFinally(
                tried=tried,
                final=final,
                expression=ExpressionBuiltinExecfile(
                    source_code=ExpressionCallEmpty(
                        called=ExpressionAttributeLookup(
                            source=ExpressionBuiltinOpen(
                                filename=filename,
                                mode=ExpressionConstantRef(
                                    constant="rU", source_ref=source_ref),
                                buffering=None,
                                source_ref=source_ref),
                            attribute_name="read",
                            source_ref=source_ref),
                        source_ref=source_ref),
                    globals_arg=globals_ref,
                    locals_arg=locals_ref,
                    source_ref=source_ref),
                source_ref=source_ref)
Beispiel #2
0
        def wrapExpressionBuiltinExecCreation(source, globals, locals,
                                              source_ref):
            provider = node.getParentVariableProvider()

            # TODO: Can't really be true, can it?
            if provider.isExpressionFunctionBody():
                provider.markAsExecContaining()

                if provider.isClassDictCreation():
                    provider.markAsUnqualifiedExecContaining( source_ref )

            temp_scope = provider.allocateTempScope("exec")

            globals_ref, locals_ref, tried, final = wrapEvalGlobalsAndLocals(
                provider     = provider,
                globals_node = globals,
                locals_node  = locals,
                temp_scope   = temp_scope,
                source_ref   = source_ref
            )

            return ExpressionTryFinally(
                tried      = tried,
                final      = final,
                expression = ExpressionBuiltinExec(
                    source_code = source,
                    globals_arg = globals_ref,
                    locals_arg  = locals_ref,
                    source_ref  = source_ref
                ),
                source_ref  = source_ref
            )
        def wrapExpressionBuiltinExecfileCreation(filename, globals_arg,
                                                  locals_arg, source_ref):
            outline_body = ExpressionOutlineBody(
                provider   = node.getParentVariableProvider(),
                name       = "execfile_call",
                source_ref = source_ref
            )

            globals_ref, locals_ref, tried, final = wrapEvalGlobalsAndLocals(
                provider     = node.getParentVariableProvider(),
                globals_node = globals_arg,
                locals_node  = locals_arg,
                temp_scope   = outline_body.getOutlineTempScope(),
                source_ref   = source_ref
            )

            tried = makeStatementsSequence(
                statements = (
                    tried,
                    StatementReturn(
                        expression = ExpressionBuiltinExecfile(
                            source_code = ExpressionCallEmpty(
                                called     = ExpressionAttributeLookup(
                                    source         = ExpressionBuiltinOpen(
                                        filename   = filename,
                                        mode       = ExpressionConstantRef(
                                            constant   = "rU",
                                            source_ref = source_ref
                                        ),
                                        buffering  = None,
                                        source_ref = source_ref
                                    ),
                                    attribute_name = "read",
                                    source_ref     = source_ref
                                ),
                                source_ref = source_ref
                            ),
                            globals_arg = globals_ref,
                            locals_arg  = locals_ref,
                            source_ref  = source_ref
                        ),
                        source_ref = source_ref
                    )
                ),
                allow_none = False,
                source_ref = source_ref
            )

            outline_body.setBody(
                makeStatementsSequenceFromStatement(
                    statement = makeTryFinallyStatement(
                        provider   = outline_body,
                        tried      = tried,
                        final      = final,
                        source_ref = source_ref
                    )
                )
            )

            return outline_body
Beispiel #4
0
        def wrapExpressionBuiltinExecfileCreation(filename, globals, locals,
                                                  source_ref):
            provider = node.getParentVariableProvider()

            # TODO: Can't really be true, can it?
            if provider.isExpressionFunctionBody():
                provider.markAsExecContaining()

                if provider.isClassDictCreation():
                    provider.markAsUnqualifiedExecContaining(source_ref)

            globals_wrap, locals_wrap = wrapEvalGlobalsAndLocals(
                provider=provider,
                globals_node=globals,
                locals_node=locals,
                exec_mode=False,
                source_ref=source_ref)

            return ExpressionBuiltinExecfile(source_code=ExpressionCallEmpty(
                called=ExpressionAttributeLookup(
                    expression=ExpressionBuiltinOpen(
                        filename=filename,
                        mode=ExpressionConstantRef(constant="rU",
                                                   source_ref=source_ref),
                        buffering=None,
                        source_ref=source_ref),
                    attribute_name="read",
                    source_ref=source_ref),
                source_ref=source_ref),
                                             globals_arg=globals_wrap,
                                             locals_arg=locals_wrap,
                                             source_ref=source_ref)
        def wrapExpressionBuiltinExecCreation(source, globals, locals,
                                              source_ref):
            provider = node.getParentVariableProvider()

            # TODO: Can't really be true, can it?
            if provider.isExpressionFunctionBody():
                provider.markAsExecContaining()

                if provider.isClassDictCreation():
                    provider.markAsUnqualifiedExecContaining( source_ref )

            temp_scope = provider.allocateTempScope("exec")

            globals_ref, locals_ref, tried, final = wrapEvalGlobalsAndLocals(
                provider     = provider,
                globals_node = globals,
                locals_node  = locals,
                temp_scope   = temp_scope,
                source_ref   = source_ref
            )

            return ExpressionTryFinally(
                tried      = tried,
                final      = final,
                expression = ExpressionBuiltinExec(
                    source_code = source,
                    globals_arg = globals_ref,
                    locals_arg  = locals_ref,
                    source_ref  = source_ref
                ),
                source_ref  = source_ref
            )
        def wrapExpressionBuiltinExecCreation( source, globals, locals,
                                               source_ref ):
            provider = node.getParentVariableProvider()

            # TODO: Can't really be true, can it?
            if provider.isExpressionFunctionBody():
                provider.markAsExecContaining()

                if provider.isClassDictCreation():
                    provider.markAsUnqualifiedExecContaining( source_ref )

            globals_wrap, locals_wrap = wrapEvalGlobalsAndLocals(
                provider   = provider,
                globals    = globals,
                locals     = locals,
                exec_mode  = False,
                source_ref = source_ref
            )

            return ExpressionBuiltinExec(
                source     = source,
                globals    = globals_wrap,
                locals     = locals_wrap,
                source_ref = source_ref
            )
        def wrapExpressionBuiltinExecCreation(source, globals_arg, locals_arg,
                                              source_ref):
            provider = node.getParentVariableProvider()

            outline_body = ExpressionOutlineBody(
                provider   = provider,
                name       = "exec_call",
                body       = None, # later
                source_ref = source_ref
            )

            # TODO: Can't really be true, can it?
            if provider.isExpressionFunctionBody():
                provider.markAsExecContaining()

                if provider.isExpressionClassBody():
                    provider.markAsUnqualifiedExecContaining(source_ref)

            globals_ref, locals_ref, tried, final = wrapEvalGlobalsAndLocals(
                provider     = provider,
                globals_node = globals_arg,
                locals_node  = locals_arg,
                temp_scope   = outline_body.getOutlineTempScope(),
                source_ref   = source_ref
            )

            tried = makeStatementsSequence(
                statements = (
                    tried,
                    StatementReturn(
                        expression = ExpressionBuiltinExec(
                            source_code = source,
                            globals_arg = globals_ref,
                            locals_arg  = locals_ref,
                            source_ref  = source_ref
                        ),
                        source_ref = source_ref
                    ),
                ),
                allow_none = False,
                source_ref = source_ref
            )

            # Hack: Allow some APIs to work already
            tried.parent = outline_body

            outline_body.setBody(
                makeStatementsSequenceFromStatement(
                    statement = makeTryFinallyStatement(
                        provider   = provider,
                        tried      = tried,
                        final      = final,
                        source_ref = source_ref
                    )
                )
            )

            return outline_body
Beispiel #8
0
    def wrapEvalBuiltin(source, globals, locals, source_ref):
        globals_wrap, locals_wrap = wrapEvalGlobalsAndLocals(
            provider=node.getParentVariableProvider(),
            globals_node=globals,
            locals_node=locals,
            exec_mode=False,
            source_ref=source_ref)

        return ExpressionBuiltinEval(source_code=source,
                                     globals_arg=globals_wrap,
                                     locals_arg=locals_wrap,
                                     source_ref=source_ref)
        def wrapExpressionBuiltinExecCreation(source, globals_arg, locals_arg,
                                              source_ref):
            provider = node.getParentVariableProvider()

            outline_body = ExpressionOutlineBody(
                provider   = provider,
                name       = "exec_call",
                source_ref = source_ref
            )

            globals_ref, locals_ref, tried, final = wrapEvalGlobalsAndLocals(
                provider     = provider,
                globals_node = globals_arg,
                locals_node  = locals_arg,
                temp_scope   = outline_body.getOutlineTempScope(),
                source_ref   = source_ref
            )

            tried = makeStatementsSequence(
                statements = (
                    tried,
                    StatementReturn(
                        expression = ExpressionBuiltinExec(
                            source_code = source,
                            globals_arg = globals_ref,
                            locals_arg  = locals_ref,
                            source_ref  = source_ref
                        ),
                        source_ref = source_ref
                    ),
                ),
                allow_none = False,
                source_ref = source_ref
            )

            # Hack: Allow some APIs to work already
            tried.parent = outline_body

            outline_body.setBody(
                makeStatementsSequenceFromStatement(
                    statement = makeTryFinallyStatement(
                        provider   = provider,
                        tried      = tried,
                        final      = final,
                        source_ref = source_ref
                    )
                )
            )

            return outline_body
    def wrapEvalBuiltin( source, globals, locals, source_ref ):
        globals_wrap, locals_wrap = wrapEvalGlobalsAndLocals(
            provider   = node.getParentVariableProvider(),
            globals    = globals,
            locals     = locals,
            exec_mode  = False,
            source_ref = source_ref
        )

        return ExpressionBuiltinEval(
            source     = source,
            globals    = globals_wrap,
            locals     = locals_wrap,
            source_ref = source_ref
        )
        def wrapExpressionBuiltinExecfileCreation(filename, globals_node,
                                                  locals_node, source_ref):
            provider = node.getParentVariableProvider()

            # TODO: Can't really be true, can it?
            if provider.isExpressionFunctionBody():
                provider.markAsExecContaining()

                if provider.isClassDictCreation():
                    provider.markAsUnqualifiedExecContaining( source_ref )

            temp_scope = provider.allocateTempScope("execfile")

            globals_ref, locals_ref, tried, final = wrapEvalGlobalsAndLocals(
                provider     = provider,
                globals_node = globals_node,
                locals_node  = locals_node,
                temp_scope   = temp_scope,
                source_ref   = source_ref
            )

            return ExpressionTryFinally(
                tried      = tried,
                final      = final,
                expression = ExpressionBuiltinExecfile(
                    source_code = ExpressionCallEmpty(
                        called     = ExpressionAttributeLookup(
                            expression     = ExpressionBuiltinOpen(
                                filename   = filename,
                                mode       = ExpressionConstantRef(
                                    constant   = "rU",
                                    source_ref = source_ref
                                ),
                                buffering  = None,
                                source_ref = source_ref
                            ),
                            attribute_name = "read",
                            source_ref     = source_ref
                        ),
                        source_ref = source_ref
                    ),
                    globals_arg = globals_ref,
                    locals_arg  = locals_ref,
                    source_ref  = source_ref
                ),
                source_ref = source_ref
            )
Beispiel #12
0
        def wrapExpressionBuiltinExecCreation(source, globals_arg, locals_arg,
                                              source_ref):
            provider = node.getParentVariableProvider()

            outline_body = ExpressionOutlineBody(
                provider=provider,
                name="exec_call",
                body=None,  # later
                source_ref=source_ref)

            # TODO: Can't really be true, can it?
            if provider.isExpressionFunctionBody():
                provider.markAsExecContaining()

                if provider.isExpressionClassBody():
                    provider.markAsUnqualifiedExecContaining(source_ref)

            globals_ref, locals_ref, tried, final = wrapEvalGlobalsAndLocals(
                provider=provider,
                globals_node=globals_arg,
                locals_node=locals_arg,
                temp_scope=outline_body.getOutlineTempScope(),
                source_ref=source_ref)

            tried = makeStatementsSequence(statements=(
                tried,
                StatementReturn(expression=ExpressionBuiltinExec(
                    source_code=source,
                    globals_arg=globals_ref,
                    locals_arg=locals_ref,
                    source_ref=source_ref),
                                source_ref=source_ref),
            ),
                                           allow_none=False,
                                           source_ref=source_ref)

            # Hack: Allow some APIs to work already
            tried.parent = outline_body

            outline_body.setBody(
                makeStatementsSequenceFromStatement(
                    statement=makeTryFinallyStatement(provider=provider,
                                                      tried=tried,
                                                      final=final,
                                                      source_ref=source_ref)))

            return outline_body
Beispiel #13
0
        def wrapExpressionBuiltinExecfileCreation(filename, globals_arg,
                                                  locals_arg, source_ref):
            provider = node.getParentVariableProvider()

            temp_scope = provider.allocateTempScope("execfile")

            globals_ref, locals_ref, tried, final = wrapEvalGlobalsAndLocals(
                provider     = provider,
                globals_node = globals_arg,
                locals_node  = locals_arg,
                temp_scope   = temp_scope,
                source_ref   = source_ref
            )

            return ExpressionTryFinally(
                tried      = tried,
                final      = final,
                expression = ExpressionBuiltinExecfile(
                    source_code = ExpressionCallEmpty(
                        called     = ExpressionAttributeLookup(
                            source         = ExpressionBuiltinOpen(
                                filename   = filename,
                                mode       = ExpressionConstantRef(
                                    constant   = "rU",
                                    source_ref = source_ref
                                ),
                                buffering  = None,
                                source_ref = source_ref
                            ),
                            attribute_name = "read",
                            source_ref     = source_ref
                        ),
                        source_ref = source_ref
                    ),
                    globals_arg = globals_ref,
                    locals_arg  = locals_ref,
                    source_ref  = source_ref
                ),
                source_ref = source_ref
            )
        def wrapExpressionBuiltinExecfileCreation( filename, globals, locals,
                                                   source_ref ):
            provider = node.getParentVariableProvider()

            # TODO: Can't really be true, can it?
            if provider.isExpressionFunctionBody():
                provider.markAsExecContaining()

                if provider.isClassDictCreation():
                    provider.markAsUnqualifiedExecContaining( source_ref )

            globals_wrap, locals_wrap = wrapEvalGlobalsAndLocals(
                provider   = provider,
                globals    = globals,
                locals     = locals,
                exec_mode  = False,
                source_ref = source_ref
            )

            return ExpressionBuiltinExecfile(
                source_code = ExpressionCallEmpty(
                    called     = ExpressionAttributeLookup(
                        expression     = ExpressionBuiltinOpen(
                            filename   = filename,
                            mode       = ExpressionConstantRef(
                                constant   = "rU",
                                source_ref = source_ref
                            ),
                            buffering  = None,
                            source_ref = source_ref
                        ),
                        attribute_name = "read",
                        source_ref     = source_ref
                    ),
                    source_ref = source_ref
                ),
                globals_arg = globals_wrap,
                locals_arg  = locals_wrap,
                source_ref  = source_ref
            )
Beispiel #15
0
        def wrapExpressionBuiltinExecCreation(source, globals, locals,
                                              source_ref):
            provider = node.getParentVariableProvider()

            # TODO: Can't really be true, can it?
            if provider.isExpressionFunctionBody():
                provider.markAsExecContaining()

                if provider.isClassDictCreation():
                    provider.markAsUnqualifiedExecContaining(source_ref)

            globals_wrap, locals_wrap = wrapEvalGlobalsAndLocals(
                provider=provider,
                globals_node=globals,
                locals_node=locals,
                exec_mode=False,
                source_ref=source_ref)

            return ExpressionBuiltinExec(source_code=source,
                                         globals_arg=globals_wrap,
                                         locals_arg=locals_wrap,
                                         source_ref=source_ref)
    def wrapEvalBuiltin(source, globals, locals, source_ref):
        provider = node.getParentVariableProvider()

        temp_scope = provider.allocateTempScope("eval")

        globals_ref, locals_ref, tried, final = wrapEvalGlobalsAndLocals(
            provider     = provider,
            globals_node = globals,
            locals_node  = locals,
            temp_scope   = temp_scope,
            source_ref   = source_ref
        )

        source_variable = provider.allocateTempVariable(
            temp_scope = temp_scope,
            name       = "source"
        )

        final.setStatements(
            final.getStatements() + (
                StatementDelVariable(
                    variable_ref = ExpressionTargetTempVariableRef(
                        variable   = source_variable.makeReference(
                            provider
                        ),
                        source_ref = source_ref
                    ),
                    tolerant     = True,
                    source_ref   = source_ref
                ),
            )
        )

        strip_choice =  ExpressionConstantRef(
            constant = (" \t",),
            source_ref = source_ref
        )

        if python_version >= 300:
            strip_choice = ExpressionConditional(
                condition = ExpressionComparisonIs(
                    left       = ExpressionBuiltinType1(
                        value      = ExpressionTempVariableRef(
                            variable   = source_variable.makeReference(
                                provider
                            ),
                            source_ref = source_ref
                        ),
                        source_ref = source_ref
                    ),
                    right      = ExpressionBuiltinRef(
                        builtin_name = "bytes",
                        source_ref   = source_ref
                    ),
                    source_ref = source_ref
                ),
                yes_expression = ExpressionConstantRef(
                    constant = (b" \t",),
                    source_ref = source_ref
                ),
                no_expression  = strip_choice,
                source_ref     = source_ref
            )


        # Source needs some special treatment for eval, if it's a string, it
        # must be stripped.
        string_fixup = [
            StatementAssignmentVariable(
                variable_ref = ExpressionTargetTempVariableRef(
                    variable   = source_variable.makeReference(
                        provider
                    ),
                    source_ref = source_ref
                ),
                source = ExpressionCallNoKeywords(
                    called = ExpressionAttributeLookup(
                        expression     = ExpressionTempVariableRef(
                            variable   = source_variable.makeReference(
                                provider
                            ),
                            source_ref = source_ref
                        ),
                        attribute_name = "strip",
                        source_ref     = source_ref
                    ),
                    args         = strip_choice,
                    source_ref   = source_ref
                ),
                source_ref = source_ref
            )
        ]

        statements = (
            StatementAssignmentVariable(
                variable_ref = ExpressionTargetTempVariableRef(
                    variable   = source_variable.makeReference(
                        provider
                    ),
                    source_ref = source_ref
                ),
                source       = source,
                source_ref   = source_ref,
            ),
            StatementConditional(
                condition = ExpressionOperationNOT(
                    operand    = ExpressionBuiltinIsinstance(
                        cls = ExpressionBuiltinAnonymousRef(
                            builtin_name = "code",
                            source_ref   = source_ref,
                        ),
                        instance = ExpressionTempVariableRef(
                            variable   = source_variable.makeReference(
                                provider
                            ),
                            source_ref = source_ref
                        ),
                        source_ref = source_ref
                    ),
                    source_ref = source_ref
                ),
                yes_branch = StatementsSequence(
                    statements = string_fixup,
                    source_ref = source_ref
                ),
                no_branch  = None,
                source_ref = source_ref
            )
        )

        tried.setStatements(
            tried.getStatements() + statements
        )

        return ExpressionTryFinally(
            tried      = tried,
            expression = ExpressionBuiltinEval(
                source_code = ExpressionTempVariableRef(
                    variable   = source_variable.makeReference(
                        provider
                    ),
                    source_ref = source_ref
                ),
                globals_arg = globals_ref,
                locals_arg  = locals_ref,
                source_ref  = source_ref
            ),
            final      = final,
            source_ref = source_ref
        )
    def wrapEvalBuiltin(source, globals_arg, locals_arg, source_ref):
        provider = node.getParentVariableProvider()

        outline_body = ExpressionOutlineBody(
            provider=node.getParentVariableProvider(),
            name="eval_call",
            source_ref=source_ref,
        )

        globals_ref, locals_ref, tried, final = wrapEvalGlobalsAndLocals(
            provider=provider,
            globals_node=globals_arg,
            locals_node=locals_arg,
            temp_scope=outline_body.getOutlineTempScope(),
            source_ref=source_ref,
        )

        # The wrapping should not relocate to the "source_ref".
        assert (globals_arg is None or globals_ref.getSourceReference()
                == globals_arg.getSourceReference())
        assert (locals_arg is None or locals_ref.getSourceReference()
                == locals_arg.getSourceReference())

        source_variable = outline_body.allocateTempVariable(temp_scope=None,
                                                            name="source")

        final.setStatements(final.getStatements() + (StatementDelVariable(
            variable=source_variable, tolerant=True, source_ref=source_ref), ))

        strip_choice = makeConstantRefNode(constant=(" \t", ),
                                           source_ref=source_ref)

        if python_version >= 300:
            strip_choice = ExpressionConditional(
                condition=ExpressionComparisonIs(
                    left=ExpressionBuiltinType1(
                        value=ExpressionTempVariableRef(
                            variable=source_variable, source_ref=source_ref),
                        source_ref=source_ref,
                    ),
                    right=makeExpressionBuiltinRef(builtin_name="bytes",
                                                   source_ref=source_ref),
                    source_ref=source_ref,
                ),
                expression_yes=makeConstantRefNode(constant=(b" \t", ),
                                                   source_ref=source_ref),
                expression_no=strip_choice,
                source_ref=source_ref,
            )

        # Source needs some special treatment for eval, if it's a string, it
        # must be stripped.
        string_fixup = StatementAssignmentVariable(
            variable=source_variable,
            source=makeExpressionCall(
                called=ExpressionAttributeLookup(
                    source=ExpressionTempVariableRef(variable=source_variable,
                                                     source_ref=source_ref),
                    attribute_name="strip",
                    source_ref=source_ref,
                ),
                args=strip_choice,  # This is a tuple
                kw=None,
                source_ref=source_ref,
            ),
            source_ref=source_ref,
        )

        acceptable_builtin_types = [
            ExpressionBuiltinAnonymousRef(builtin_name="code",
                                          source_ref=source_ref)
        ]

        if python_version >= 270:
            acceptable_builtin_types.append(
                makeExpressionBuiltinRef(builtin_name="memoryview",
                                         source_ref=source_ref))

        statements = (
            StatementAssignmentVariable(variable=source_variable,
                                        source=source,
                                        source_ref=source_ref),
            makeStatementConditional(
                condition=ExpressionOperationNOT(
                    operand=ExpressionBuiltinIsinstance(
                        instance=ExpressionTempVariableRef(
                            variable=source_variable, source_ref=source_ref),
                        classes=makeSequenceCreationOrConstant(
                            sequence_kind="tuple",
                            elements=acceptable_builtin_types,
                            source_ref=source_ref,
                        ),
                        source_ref=source_ref,
                    ),
                    source_ref=source_ref,
                ),
                yes_branch=string_fixup,
                no_branch=None,
                source_ref=source_ref,
            ),
            StatementReturn(
                expression=ExpressionBuiltinEval(
                    source_code=ExpressionTempVariableRef(
                        variable=source_variable, source_ref=source_ref),
                    globals_arg=globals_ref,
                    locals_arg=locals_ref,
                    source_ref=source_ref,
                ),
                source_ref=source_ref,
            ),
        )

        tried = makeStatementsSequence(statements=(tried, ) + statements,
                                       allow_none=False,
                                       source_ref=source_ref)

        outline_body.setBody(
            makeStatementsSequenceFromStatement(
                statement=makeTryFinallyStatement(
                    provider=outline_body,
                    tried=tried,
                    final=final,
                    source_ref=source_ref,
                )))

        return outline_body
    def wrapEvalBuiltin(source, globals_arg, locals_arg, source_ref):
        provider = node.getParentVariableProvider()

        outline_body = ExpressionOutlineBody(
            provider=node.getParentVariableProvider(), name="eval_call", body=None, source_ref=source_ref  # later
        )

        globals_ref, locals_ref, tried, final = wrapEvalGlobalsAndLocals(
            provider=provider,
            globals_node=globals_arg,
            locals_node=locals_arg,
            temp_scope=outline_body.getOutlineTempScope(),
            source_ref=source_ref,
        )

        # The wrapping should not relocate to the "source_ref".
        assert globals_arg is None or globals_ref.getSourceReference() == globals_arg.getSourceReference()
        assert locals_arg is None or locals_ref.getSourceReference() == locals_arg.getSourceReference()

        source_variable = outline_body.allocateTempVariable(temp_scope=None, name="source")

        final.setStatements(
            final.getStatements()
            + (
                StatementDelVariable(
                    variable_ref=ExpressionTargetTempVariableRef(variable=source_variable, source_ref=source_ref),
                    tolerant=True,
                    source_ref=source_ref,
                ),
            )
        )

        strip_choice = ExpressionConstantRef(constant=(" \t",), source_ref=source_ref)

        if python_version >= 300:
            strip_choice = ExpressionConditional(
                condition=ExpressionComparisonIs(
                    left=ExpressionBuiltinType1(
                        value=ExpressionTempVariableRef(variable=source_variable, source_ref=source_ref),
                        source_ref=source_ref,
                    ),
                    right=ExpressionBuiltinRef(builtin_name="bytes", source_ref=source_ref),
                    source_ref=source_ref,
                ),
                expression_yes=ExpressionConstantRef(constant=(b" \t",), source_ref=source_ref),
                expression_no=strip_choice,
                source_ref=source_ref,
            )

        # Source needs some special treatment for eval, if it's a string, it
        # must be stripped.
        string_fixup = [
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetTempVariableRef(variable=source_variable, source_ref=source_ref),
                source=ExpressionCallNoKeywords(
                    called=ExpressionAttributeLookup(
                        source=ExpressionTempVariableRef(variable=source_variable, source_ref=source_ref),
                        attribute_name="strip",
                        source_ref=source_ref,
                    ),
                    args=strip_choice,
                    source_ref=source_ref,
                ),
                source_ref=source_ref,
            )
        ]

        statements = (
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetTempVariableRef(variable=source_variable, source_ref=source_ref),
                source=source,
                source_ref=source_ref,
            ),
            StatementConditional(
                condition=ExpressionOperationNOT(
                    operand=ExpressionBuiltinIsinstance(
                        instance=ExpressionTempVariableRef(variable=source_variable, source_ref=source_ref),
                        classes=ExpressionBuiltinAnonymousRef(builtin_name="code", source_ref=source_ref),
                        source_ref=source_ref,
                    ),
                    source_ref=source_ref,
                ),
                yes_branch=StatementsSequence(statements=string_fixup, source_ref=source_ref),
                no_branch=None,
                source_ref=source_ref,
            ),
            StatementReturn(
                expression=ExpressionBuiltinEval(
                    source_code=ExpressionTempVariableRef(variable=source_variable, source_ref=source_ref),
                    globals_arg=globals_ref,
                    locals_arg=locals_ref,
                    source_ref=source_ref,
                ),
                source_ref=source_ref,
            ),
        )

        tried = makeStatementsSequence(statements=(tried,) + statements, allow_none=False, source_ref=source_ref)

        outline_body.setBody(
            makeStatementsSequenceFromStatement(
                statement=makeTryFinallyStatement(
                    provider=outline_body, tried=tried, final=final, source_ref=source_ref
                )
            )
        )

        return outline_body
Beispiel #19
0
    def wrapEvalBuiltin(source, globals, locals, source_ref):
        provider = node.getParentVariableProvider()

        temp_scope = provider.allocateTempScope("eval")

        globals_ref, locals_ref, tried, final = wrapEvalGlobalsAndLocals(
            provider     = provider,
            globals_node = globals,
            locals_node  = locals,
            temp_scope   = temp_scope,
            source_ref   = source_ref
        )

        # The wrapping should not relocate to the "source_ref".
        assert globals is None or \
               globals_ref.getSourceReference() == globals.getSourceReference()
        assert locals is None or \
               locals_ref.getSourceReference() == locals.getSourceReference()

        source_variable = provider.allocateTempVariable(
            temp_scope = temp_scope,
            name       = "source"
        )

        final.setStatements(
            final.getStatements() + (
                StatementDelVariable(
                    variable_ref = ExpressionTargetTempVariableRef(
                        variable   = source_variable.makeReference(
                            provider
                        ),
                        source_ref = source_ref
                    ),
                    tolerant     = True,
                    source_ref   = source_ref
                ),
            )
        )

        strip_choice =  ExpressionConstantRef(
            constant = (" \t",),
            source_ref = source_ref
        )

        if python_version >= 300:
            strip_choice = ExpressionConditional(
                condition = ExpressionComparisonIs(
                    left       = ExpressionBuiltinType1(
                        value      = ExpressionTempVariableRef(
                            variable   = source_variable.makeReference(
                                provider
                            ),
                            source_ref = source_ref
                        ),
                        source_ref = source_ref
                    ),
                    right      = ExpressionBuiltinRef(
                        builtin_name = "bytes",
                        source_ref   = source_ref
                    ),
                    source_ref = source_ref
                ),
                yes_expression = ExpressionConstantRef(
                    constant = (b" \t",),
                    source_ref = source_ref
                ),
                no_expression  = strip_choice,
                source_ref     = source_ref
            )


        # Source needs some special treatment for eval, if it's a string, it
        # must be stripped.
        string_fixup = [
            StatementAssignmentVariable(
                variable_ref = ExpressionTargetTempVariableRef(
                    variable   = source_variable.makeReference(
                        provider
                    ),
                    source_ref = source_ref
                ),
                source = ExpressionCallNoKeywords(
                    called = ExpressionAttributeLookup(
                        expression     = ExpressionTempVariableRef(
                            variable   = source_variable.makeReference(
                                provider
                            ),
                            source_ref = source_ref
                        ),
                        attribute_name = "strip",
                        source_ref     = source_ref
                    ),
                    args         = strip_choice,
                    source_ref   = source_ref
                ),
                source_ref = source_ref
            )
        ]

        statements = (
            StatementAssignmentVariable(
                variable_ref = ExpressionTargetTempVariableRef(
                    variable   = source_variable.makeReference(
                        provider
                    ),
                    source_ref = source_ref
                ),
                source       = source,
                source_ref   = source_ref,
            ),
            StatementConditional(
                condition = ExpressionOperationNOT(
                    operand    = ExpressionBuiltinIsinstance(
                        cls = ExpressionBuiltinAnonymousRef(
                            builtin_name = "code",
                            source_ref   = source_ref,
                        ),
                        instance = ExpressionTempVariableRef(
                            variable   = source_variable.makeReference(
                                provider
                            ),
                            source_ref = source_ref
                        ),
                        source_ref = source_ref
                    ),
                    source_ref = source_ref
                ),
                yes_branch = StatementsSequence(
                    statements = string_fixup,
                    source_ref = source_ref
                ),
                no_branch  = None,
                source_ref = source_ref
            )
        )

        tried.setStatements(
            tried.getStatements() + statements
        )

        return ExpressionTryFinally(
            tried      = tried,
            expression = ExpressionBuiltinEval(
                source_code = ExpressionTempVariableRef(
                    variable   = source_variable.makeReference(
                        provider
                    ),
                    source_ref = source_ref
                ),
                globals_arg = globals_ref,
                locals_arg  = locals_ref,
                source_ref  = source_ref
            ),
            final      = final,
            source_ref = source_ref
        )