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( ExpressionMakeDict(pairs=(ExpressionKeyValuePair( key=ExpressionConstantRef(constant=key, source_ref=source_ref), value=buildNode(provider, value, source_ref), source_ref=source_ref), ), source_ref=source_ref)) else: result.append( ExpressionMakeDict(pairs=(ExpressionKeyValuePair( key=buildNode(provider, key, source_ref), value=buildNode(provider, value, source_ref), source_ref=source_ref), ), source_ref=source_ref)) return result
def buildDictionaryUnpacking(provider, node, source_ref): helper_args = [] for key, value in zip(node.keys, node.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: helper_args.append(buildNode(provider, value, source_ref), ) else: helper_args.append( ExpressionMakeDict( pairs=(ExpressionKeyValuePair( key=buildNode(provider, key, source_ref), value=buildNode(provider, value, source_ref), source_ref=source_ref), ), source_ref=source_ref, lazy_order=False, )) result = ExpressionFunctionCall( function=ExpressionFunctionCreation(function_ref=ExpressionFunctionRef( function_body=getDictUnpackingHelper(), source_ref=source_ref), defaults=(), kw_defaults=None, annotations=None, source_ref=source_ref), values=(ExpressionMakeTuple(helper_args, source_ref), ), source_ref=source_ref, ) result.setCompatibleSourceReference( helper_args[-1].getCompatibleSourceReference()) return result
def makeDictCreationOrConstant(keys, values, source_ref): # Create dictionary node. Tries to avoid it for constant values that are not # mutable. assert len(keys) == len(values) for key, value in zip(keys, values): if not key.isExpressionConstantRef() or not key.isKnownToBeHashable(): constant = False break 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 = [ key.getConstant() for key in keys ], values = [ value.getConstant() for value in values ] ), user_provided = True, source_ref = source_ref ) else: result = ExpressionMakeDict( pairs = [ ExpressionKeyValuePair( key = key, value = value, source_ref = key.getSourceReference() ) for key, value in zip(keys, values) ], source_ref = source_ref ) if values: result.setCompatibleSourceReference( source_ref = values[-1].getCompatibleSourceReference() ) return result
def __init__(self, pos_arg, pairs, source_ref): assert type(pos_arg) not in (tuple, list), source_ref assert type(pairs) in (tuple, list), source_ref ExpressionChildrenHavingBase.__init__( self, values={ "pos_arg": pos_arg, "pairs": tuple( ExpressionKeyValuePair( ExpressionConstantRef(key, source_ref), value, value.getSourceReference()) for key, value in pairs) }, source_ref=source_ref)