def buildDeleteStatementFromDecoded(node, kind, detail, source_ref):
    if kind in ("Name", "Name_Exception"):
        # Note: Name_Exception is a "del" for exception handlers that doesn't
        # insist on the variable being defined, user code may do it too, and
        # that will be fine, so make that tolerant.
        variable_ref = detail

        return StatementDelVariable(
            variable_ref=variable_ref,
            tolerant=kind == "Name_Exception",
            source_ref=source_ref.atColumnNumber(node.col_offset + 1))
    elif kind == "Attribute":
        lookup_source, attribute_name = detail

        return StatementDelAttribute(expression=lookup_source,
                                     attribute_name=attribute_name,
                                     source_ref=source_ref)
    elif kind == "Subscript":
        subscribed, subscript = detail

        return StatementDelSubscript(expression=subscribed,
                                     subscript=subscript,
                                     source_ref=source_ref)
    elif kind == "Slice":
        lookup_source, lower, upper = detail

        use_sliceobj = python_version >= 300

        if use_sliceobj:
            return StatementDelSubscript(expression=lookup_source,
                                         subscript=ExpressionBuiltinSlice(
                                             start=lower,
                                             stop=upper,
                                             step=None,
                                             source_ref=source_ref),
                                         source_ref=source_ref)
        else:
            return StatementDelSlice(expression=lookup_source,
                                     lower=lower,
                                     upper=upper,
                                     source_ref=source_ref)
    elif kind == "Tuple":
        result = []

        for sub_node in detail:
            result.append(
                buildDeleteStatementFromDecoded(node=node,
                                                kind=sub_node[0],
                                                detail=sub_node[1],
                                                source_ref=source_ref))

        return makeStatementsSequenceOrStatement(statements=result,
                                                 source_ref=source_ref)
    else:
        assert False, (kind, detail, source_ref)
Beispiel #2
0
def buildDeleteStatementFromDecoded(provider, kind, detail, source_ref):
    # This function is a case driven by returns, pylint: disable=too-many-return-statements

    if kind in ("Name", "Name_Exception"):
        # Note: Name_Exception is a "del" for exception handlers that doesn't
        # insist on the variable being defined, user code may do it too, and
        # that will be fine, so make that tolerant.
        return StatementDelVariableName(
            provider=provider,
            variable_name=detail,
            tolerant=kind == "Name_Exception",
            source_ref=source_ref,
        )
    elif kind == "Attribute":
        lookup_source, attribute_name = detail

        return StatementDelAttribute(
            expression=lookup_source,
            attribute_name=mangleName(attribute_name, provider),
            source_ref=source_ref,
        )
    elif kind == "Subscript":
        subscribed, subscript = detail

        return StatementDelSubscript(subscribed=subscribed,
                                     subscript=subscript,
                                     source_ref=source_ref)
    elif kind == "Slice":
        lookup_source, lower, upper = detail

        use_sliceobj = python_version >= 0x300

        if use_sliceobj:
            return StatementDelSubscript(
                subscribed=lookup_source,
                subscript=makeExpressionBuiltinSlice(start=lower,
                                                     stop=upper,
                                                     step=None,
                                                     source_ref=source_ref),
                source_ref=source_ref,
            )
        else:
            return StatementDelSlice(
                expression=lookup_source,
                lower=lower,
                upper=upper,
                source_ref=source_ref,
            )
    elif kind == "Tuple":
        result = []

        for sub_node in detail:
            result.append(
                buildDeleteStatementFromDecoded(
                    provider=provider,
                    kind=sub_node[0],
                    detail=sub_node[1],
                    source_ref=source_ref,
                ))

        if result:
            return makeStatementsSequenceOrStatement(statements=result,
                                                     source_ref=source_ref)
        else:
            return None
    else:
        assert False, (kind, detail, source_ref)