コード例 #1
0
def buildDictionaryUnpackingArgs(provider, keys, values, source_ref):
    result = []

    for key, value in zip(keys, values):
        # TODO: We could be a lot cleverer about the dictionaries for non-starred
        # arguments, but lets get this to work first.
        if key is None:
            result.append(buildNode(provider, value, source_ref))
        elif type(key) is str:
            result.append(
                makeExpressionMakeDict(
                    pairs=(ExpressionKeyValuePair(
                        key=makeConstantRefNode(constant=key,
                                                source_ref=source_ref),
                        value=buildNode(provider, value, source_ref),
                        source_ref=source_ref,
                    ), ),
                    source_ref=source_ref,
                ))
        else:
            result.append(
                makeExpressionMakeDict(
                    pairs=(ExpressionKeyValuePair(
                        key=buildNode(provider, key, source_ref),
                        value=buildNode(provider, value, source_ref),
                        source_ref=source_ref,
                    ), ),
                    source_ref=source_ref,
                ))

    return result
コード例 #2
0
def makeDictCreationOrConstant2(keys, values, source_ref):
    # Create dictionary node. Tries to avoid it for constant values that are not
    # mutable. Keys are Python strings here.

    assert len(keys) == len(values)
    for value in values:
        if not value.isExpressionConstantRef():
            constant = False
            break
    else:
        constant = True

    # Note: This would happen in optimization instead, but lets just do it
    # immediately to save some time.
    if constant:
        # Unless told otherwise, create the dictionary in its full size, so
        # that no growing occurs and the constant becomes as similar as possible
        # before being marshaled.
        result = makeConstantRefNode(
            constant=Constants.createConstantDict(
                keys=keys, values=[value.getCompileTimeConstant() for value in values]
            ),
            user_provided=True,
            source_ref=source_ref,
        )
    else:
        result = makeExpressionMakeDict(
            pairs=[
                ExpressionKeyValuePair(
                    key=makeConstantRefNode(
                        constant=key,
                        source_ref=value.getSourceReference(),
                        user_provided=True,
                    ),
                    value=value,
                    source_ref=value.getSourceReference(),
                )
                for key, value in zip(keys, values)
            ],
            source_ref=source_ref,
        )

    if values:
        result.setCompatibleSourceReference(
            source_ref=values[-1].getCompatibleSourceReference()
        )

    return result