def computeExpressionCall(self, call_node, call_args, call_kw, trace_collection): exception_name = self.getExceptionName() def createBuiltinMakeException(args, name=None, path=None, source_ref=None): if exception_name == "ImportError" and python_version >= 0x300: return ExpressionBuiltinMakeExceptionImportError( exception_name=exception_name, args=args, name=name, path=path, source_ref=source_ref, ) else: # We expect to only get the star arguments for these. assert name is None assert path is None return ExpressionBuiltinMakeException( exception_name=exception_name, args=args, source_ref=source_ref ) new_node = BuiltinParameterSpecs.extractBuiltinArgs( node=call_node, builtin_class=createBuiltinMakeException, builtin_spec=BuiltinParameterSpecs.makeBuiltinExceptionParameterSpec( exception_name=exception_name ), ) assert new_node is not None return new_node, "new_expression", "Detected built-in exception making."
def computeExpressionCall(self, call_node, call_args, call_kw, trace_collection): exception_name = self.getExceptionName() # TODO: Keyword only arguments of it, are not properly handled yet by # the built-in call code. if exception_name == "ImportError" and python_version >= 300: if call_kw is not None and \ (not call_kw.isExpressionConstantRef() or call_kw.getConstant() != {}): return call_node, None, None def createBuiltinMakeException(args, source_ref): return ExpressionBuiltinMakeException( exception_name = exception_name, args = args, source_ref = source_ref ) new_node = BuiltinParameterSpecs.extractBuiltinArgs( node = call_node, builtin_class = createBuiltinMakeException, builtin_spec = BuiltinParameterSpecs.makeBuiltinExceptionParameterSpec( exception_name = exception_name ) ) # TODO: Don't allow this to happen. if new_node is None: return call_node, None, None return new_node, "new_expression", "Detected built-in exception making."
def computeExpressionCall(self, call_node, call_args, call_kw, trace_collection): exception_name = self.getExceptionName() # TODO: Keyword only arguments of it, are not properly handled yet by # the built-in call code. if exception_name == "ImportError" and python_version >= 300: if call_kw is not None and ( not call_kw.isExpressionConstantRef() or call_kw.getConstant() != {} ): return call_node, None, None def createBuiltinMakeException(args, source_ref): return ExpressionBuiltinMakeException( exception_name=exception_name, args=args, source_ref=source_ref ) new_node = BuiltinParameterSpecs.extractBuiltinArgs( node=call_node, builtin_class=createBuiltinMakeException, builtin_spec=BuiltinParameterSpecs.makeBuiltinExceptionParameterSpec( exception_name=exception_name ), ) # TODO: Don't allow this to happen. if new_node is None: return call_node, None, None return new_node, "new_expression", "Detected built-in exception making."
def dict_extractor(node): # The "dict" built-in is a bit strange in that it accepts a position # parameter, or not, but won't have a default value. def wrapExpressionBuiltinDictCreation(positional_args, dict_star_arg, source_ref): if len(positional_args) > 1: result = makeRaiseExceptionReplacementExpressionFromInstance( expression=node, exception=TypeError( "dict expected at most 1 arguments, got %d" % (len(positional_args))), ) result = wrapExpressionWithSideEffects( side_effects=positional_args, old_node=node, new_node=result) if dict_star_arg: result = wrapExpressionWithSideEffects( side_effects=dict_star_arg, old_node=node, new_node=result) return result return ExpressionBuiltinDict( pos_arg=positional_args[0] if positional_args else None, pairs=dict_star_arg, source_ref=source_ref, ) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=wrapExpressionBuiltinDictCreation, builtin_spec=BuiltinParameterSpecs.builtin_dict_spec, )
def dir_extractor(node): locals_scope = node.subnode_called.getLocalsScope() def buildDirEmptyCase(source_ref): source = makeExpressionBuiltinLocals( locals_scope=locals_scope, source_ref=source_ref ) result = makeCallNode( ExpressionAttributeLookup( expression=source, attribute_name="keys", source_ref=source_ref ), source_ref, ) # For Python3, keys doesn't really return values, but instead a handle # only, but we want it to be a list. if python_version >= 300: result = ExpressionBuiltinList(value=result, source_ref=source_ref) return result return BuiltinParameterSpecs.extractBuiltinArgs( node=node, # TODO: Needs locals_scope attached. builtin_class=ExpressionBuiltinDir1, builtin_spec=BuiltinParameterSpecs.builtin_dir_spec, empty_special_class=buildDirEmptyCase, )
def xrange_extractor(node): def selectXrangeBuiltin(low, high, step, source_ref): if high is None: return ExpressionBuiltinXrange1(low=low, source_ref=source_ref) elif step is None: return ExpressionBuiltinXrange2(low=low, high=high, source_ref=source_ref) else: return ExpressionBuiltinXrange3(low=low, high=high, step=step, source_ref=source_ref) def makeXrange0(source_ref): # pylint: disable=unused-argument return makeRaiseExceptionReplacementExpressionFromInstance( expression=node, exception=TypeError( "xrange requires 1-3 int arguments" if python_version < 300 else "range expected 1 arguments, got 0"), ) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=selectXrangeBuiltin, builtin_spec=BuiltinParameterSpecs.builtin_xrange_spec, empty_special_class=makeXrange0, )
def sum_extractor(node): # Split up sumwith and without start value, one is much easier. def selectSumBuiltinClass(sequence, start, source_ref): if start is None: return ExpressionBuiltinSum1(sequence=sequence, source_ref=source_ref) else: return ExpressionBuiltinSum2(sequence=sequence, start=start, source_ref=source_ref) def makeSum0(source_ref): # pylint: disable=unused-argument return makeRaiseExceptionReplacementExpressionFromInstance( expression=node, exception=TypeError("sum expected at least 1 arguments, got 0"), ) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=selectSumBuiltinClass, builtin_spec=BuiltinParameterSpecs.builtin_sum_spec, empty_special_class=makeSum0, )
def dict_extractor(node): # The "dict" built-in is a bit strange in that it accepts a position # parameter, or not, but won't have a default value. def wrapExpressionBuiltinDictCreation(positional_args, dict_star_arg, source_ref): if len(positional_args) > 1: result = makeRaiseExceptionReplacementExpressionFromInstance( expression=node, exception=TypeError( "dict expected at most 1 arguments, got %d" % (len(positional_args)) ), ) result = wrapExpressionWithSideEffects( side_effects=positional_args, old_node=node, new_node=result ) if dict_star_arg: result = wrapExpressionWithSideEffects( side_effects=dict_star_arg, old_node=node, new_node=result ) return result return ExpressionBuiltinDict( pos_arg=positional_args[0] if positional_args else None, pairs=dict_star_arg, source_ref=source_ref, ) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=wrapExpressionBuiltinDictCreation, builtin_spec=BuiltinParameterSpecs.builtin_dict_spec, )
def dir_extractor(node): def buildDirEmptyCase(source_ref): source = makeExpressionBuiltinLocals( provider=node.getParentVariableProvider(), source_ref=source_ref) result = makeCallNode( ExpressionAttributeLookup(source=source, attribute_name="keys", source_ref=source_ref), source_ref, ) # For Python3, keys doesn't really return values, but instead a handle # only, but we want it to be a list. if python_version >= 300: result = ExpressionBuiltinList(value=result, source_ref=source_ref) return result return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=ExpressionBuiltinDir1, builtin_spec=BuiltinParameterSpecs.builtin_dir_spec, empty_special_class=buildDirEmptyCase, )
def xrange_extractor(node): def selectXrangeBuiltin(low, high, step, source_ref): if high is None: return ExpressionBuiltinXrange1(low=low, source_ref=source_ref) elif step is None: return ExpressionBuiltinXrange2(low=low, high=high, source_ref=source_ref) else: return ExpressionBuiltinXrange3( low=low, high=high, step=step, source_ref=source_ref ) def makeXrange0(source_ref): # pylint: disable=unused-argument return makeRaiseExceptionReplacementExpressionFromInstance( expression=node, exception=TypeError( "xrange requires 1-3 int arguments" if python_version < 300 else "range expected 1 arguments, got 0" ), ) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=selectXrangeBuiltin, builtin_spec=BuiltinParameterSpecs.builtin_xrange_spec, empty_special_class=makeXrange0, )
def dir_extractor(node): def buildDirEmptyCase(source_ref): source = makeExpressionBuiltinLocals( provider=node.getParentVariableProvider(), source_ref=source_ref ) result = makeCallNode( ExpressionAttributeLookup( source=source, attribute_name="keys", source_ref=source_ref ), source_ref, ) # For Python3, keys doesn't really return values, but instead a handle # only, but we want it to be a list. if python_version >= 300: result = ExpressionBuiltinList(value=result, source_ref=source_ref) return result return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=ExpressionBuiltinDir1, builtin_spec=BuiltinParameterSpecs.builtin_dir_spec, empty_special_class=buildDirEmptyCase, )
def str_extractor(node): builtin_class = ExpressionBuiltinStrP2 if str is bytes else ExpressionBuiltinStrP3 return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=builtin_class, builtin_spec=builtin_class.builtin_spec, )
def exec_extractor(node): @calledWithBuiltinArgumentNamesDecorator 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 return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=wrapExpressionBuiltinExecCreation, builtin_spec=BuiltinParameterSpecs.builtin_eval_spec, )
def locals_extractor(node): # Note: Locals on the module level is really globals. provider = node.getParentVariableProvider() def makeLocalsNode(source_ref): return makeExpressionBuiltinLocals(provider=provider, source_ref=source_ref) if provider.isCompiledPythonModule(): return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=ExpressionBuiltinGlobals, builtin_spec=BuiltinParameterSpecs.builtin_globals_spec, ) else: return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=makeLocalsNode, builtin_spec=BuiltinParameterSpecs.builtin_locals_spec, )
def repr_extractor(node): def makeReprOperator(operand, source_ref): return ExpressionOperationUnary( operator="Repr", operand=operand, source_ref=source_ref ) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=makeReprOperator, builtin_spec=BuiltinParameterSpecs.builtin_repr_spec, )
def repr_extractor(node): def makeReprOperator(operand, source_ref): return ExpressionOperationUnary(operator="Repr", operand=operand, source_ref=source_ref) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=makeReprOperator, builtin_spec=BuiltinParameterSpecs.builtin_repr_spec, )
def vars_extractor(node): def selectVarsEmptyClass(source_ref): return makeExpressionBuiltinLocals( provider=node.getParentVariableProvider(), source_ref=source_ref) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=ExpressionBuiltinVars, builtin_spec=BuiltinParameterSpecs.builtin_vars_spec, empty_special_class=selectVarsEmptyClass, )
def float_extractor(node): def makeFloat0(source_ref): # pylint: disable=unused-argument return makeConstantReplacementNode(constant=float(), node=node) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=ExpressionBuiltinFloat, builtin_spec=BuiltinParameterSpecs.builtin_float_spec, empty_special_class=makeFloat0, )
def vars_extractor(node): def selectVarsEmptyClass(source_ref): return makeExpressionBuiltinLocals( provider=node.getParentVariableProvider(), source_ref=source_ref ) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=ExpressionBuiltinVars, builtin_spec=BuiltinParameterSpecs.builtin_vars_spec, empty_special_class=selectVarsEmptyClass, )
def locals_extractor(node): locals_scope = node.subnode_called.getLocalsScope() def makeLocalsNode(source_ref): return makeExpressionBuiltinLocals( locals_scope=locals_scope, source_ref=source_ref ) # Note: Locals on the module level is really globals. return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=makeLocalsNode, builtin_spec=BuiltinParameterSpecs.builtin_locals_spec, )
def any_extractor(node): # pylint: disable=unused-argument def makeAny0(source_ref): exception_message = "any() takes exactly one argument (0 given)" return makeRaiseExceptionReplacementExpressionFromInstance( expression=node, exception=TypeError(exception_message)) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=ExpressionBuiltinAny, builtin_spec=BuiltinParameterSpecs.builtin_any_spec, empty_special_class=makeAny0, )
def ord_extractor(node): def makeOrd0(source_ref): # pylint: disable=unused-argument return makeRaiseExceptionReplacementExpressionFromInstance( expression=node, exception=TypeError("ord() takes exactly one argument (0 given)"), ) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=ExpressionBuiltinOrd, builtin_spec=BuiltinParameterSpecs.builtin_ord_spec, empty_special_class=makeOrd0, )
def format_extractor(node): def makeFormat0(source_ref): # pylint: disable=unused-argument return makeRaiseExceptionReplacementExpressionFromInstance( expression=node, exception=TypeError("format() takes at least 1 argument (0 given)"), ) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=ExpressionBuiltinFormat, builtin_spec=BuiltinParameterSpecs.builtin_format_spec, empty_special_class=makeFormat0, )
def iter_extractor(node): @calledWithBuiltinArgumentNamesDecorator def wrapIterCreation(callable_arg, sentinel, source_ref): if sentinel is None: return ExpressionBuiltinIter1(value=callable_arg, source_ref=source_ref) else: return ExpressionBuiltinIter2( callable_arg=callable_arg, sentinel=sentinel, source_ref=source_ref ) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=wrapIterCreation, builtin_spec=BuiltinParameterSpecs.builtin_iter_spec, )
def type_extractor(node): args = node.getCallArgs() if args is None: iter_length = 0 else: iter_length = args.getIterationLength() if iter_length == 1: return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=ExpressionBuiltinType1, builtin_spec=BuiltinParameterSpecs.builtin_type1_spec, ) elif iter_length == 3: return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=ExpressionBuiltinType3, builtin_spec=BuiltinParameterSpecs.builtin_type3_spec, ) else: return makeRaiseExceptionReplacementExpressionFromInstance( expression=node, exception=TypeError("type() takes 1 or 3 arguments") )
def classmethod_extractor(node): def makeStaticmethod0(source_ref): # pylint: disable=unused-argument return makeRaiseExceptionReplacementExpressionFromInstance( expression=node, exception=TypeError("classmethod expected 1 arguments, got 0"), ) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=ExpressionBuiltinClassmethod, builtin_spec=BuiltinParameterSpecs.builtin_classmethod_spec, empty_special_class=makeStaticmethod0, )
def type_extractor(node): args = node.getCallArgs() if args is None: iter_length = 0 else: iter_length = args.getIterationLength() if iter_length == 1: return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=ExpressionBuiltinType1, builtin_spec=BuiltinParameterSpecs.builtin_type1_spec, ) elif iter_length == 3: return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=ExpressionBuiltinType3, builtin_spec=BuiltinParameterSpecs.builtin_type3_spec, ) else: return makeRaiseExceptionReplacementExpressionFromInstance( expression=node, exception=TypeError("type() takes 1 or 3 arguments"))
def vars_extractor(node): locals_scope = node.subnode_called.getLocalsScope() def selectVarsEmptyClass(source_ref): return makeExpressionBuiltinLocals( locals_scope=locals_scope, source_ref=source_ref ) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, # TODO: Needs locals_cope attached builtin_class=ExpressionBuiltinVars, builtin_spec=BuiltinParameterSpecs.builtin_vars_spec, empty_special_class=selectVarsEmptyClass, )
def next_extractor(node): # Split up next with and without defaults, they are not going to behave # really very similar. def selectNextBuiltinClass(iterator, default, source_ref): if default is None: return ExpressionBuiltinNext1(value=iterator, source_ref=source_ref) else: return ExpressionBuiltinNext2( iterator=iterator, default=default, source_ref=source_ref ) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=selectNextBuiltinClass, builtin_spec=BuiltinParameterSpecs.builtin_next_spec, )
def slice_extractor(node): def wrapSlice(start, stop, step, source_ref): if start is not None and stop is None: # Default rules are strange. If one argument is given, it's the # second one then. stop = start start = None return ExpressionBuiltinSlice( start=start, stop=stop, step=step, source_ref=source_ref ) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=wrapSlice, builtin_spec=BuiltinParameterSpecs.builtin_slice_spec, )
def open_extractor(node): def makeOpen0(source_ref): # pylint: disable=unused-argument try: open() except Exception as e: # We want to broad here, pylint: disable=broad-except return makeRaiseExceptionReplacementExpressionFromInstance( expression=node, exception=e) else: raise NuitkaAssumptionError( "open without argument is expected to raise") return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=ExpressionBuiltinOpen, builtin_spec=BuiltinParameterSpecs.builtin_open_spec, empty_special_class=makeOpen0, )
def bytearray_extractor(node): def makeBytearray0(source_ref): return makeConstantRefNode(constant=bytearray(), source_ref=source_ref) def selectNextBuiltinClass(string, encoding, errors, source_ref): if encoding is None: return ExpressionBuiltinBytearray1(value=string, source_ref=source_ref) else: return ExpressionBuiltinBytearray3( string=string, encoding=encoding, errors=errors, source_ref=source_ref ) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=selectNextBuiltinClass, builtin_spec=BuiltinParameterSpecs.builtin_bytearray_spec, empty_special_class=makeBytearray0, )
def int_extractor(node): def makeInt0(source_ref): # pylint: disable=unused-argument return makeConstantReplacementNode(constant=int(), node=node) def selectIntBuiltin(value, base, source_ref): if base is None: return ExpressionBuiltinInt1(value=value, source_ref=source_ref) else: return ExpressionBuiltinInt2(value=value, base=base, source_ref=source_ref) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=selectIntBuiltin, builtin_spec=BuiltinParameterSpecs.builtin_int_spec, empty_special_class=makeInt0, )
def open_extractor(node): def makeOpen0(source_ref): # pylint: disable=unused-argument try: open() except Exception as e: # We want to broad here, pylint: disable=broad-except return makeRaiseExceptionReplacementExpressionFromInstance( expression=node, exception=e ) else: raise NuitkaAssumptionError("open without argument is expected to raise") return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=ExpressionBuiltinOpen, builtin_spec=BuiltinParameterSpecs.builtin_open_spec, empty_special_class=makeOpen0, )
def compile_extractor(node): def wrapExpressionBuiltinCompileCreation( source_code, filename, mode, flags, dont_inherit, optimize=None, source_ref=None ): return ExpressionBuiltinCompile( source_code=source_code, filename=filename, mode=mode, flags=flags, dont_inherit=dont_inherit, optimize=optimize, source_ref=source_ref, ) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=wrapExpressionBuiltinCompileCreation, builtin_spec=BuiltinParameterSpecs.builtin_compile_spec, )
def complex_extractor(node): def makeComplex0(source_ref): # pylint: disable=unused-argument return makeConstantReplacementNode(constant=complex(), node=node) def selectComplexBuiltin(real, imag, source_ref): if imag is None: return ExpressionBuiltinComplex1(value=real, source_ref=source_ref) else: return ExpressionBuiltinComplex2(real=real, imag=imag, source_ref=source_ref) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=selectComplexBuiltin, builtin_spec=BuiltinParameterSpecs.builtin_complex_spec, empty_special_class=makeComplex0, )
def complex_extractor(node): def makeComplex0(source_ref): # pylint: disable=unused-argument return makeConstantReplacementNode(constant=complex(), node=node) def selectComplexBuiltin(real, imag, source_ref): if imag is None: return ExpressionBuiltinComplex1(value=real, source_ref=source_ref) else: return ExpressionBuiltinComplex2( real=real, imag=imag, source_ref=source_ref ) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=selectComplexBuiltin, builtin_spec=BuiltinParameterSpecs.builtin_complex_spec, empty_special_class=makeComplex0, )
def bytes_extractor(node): def makeBytes0(source_ref): # pylint: disable=unused-argument return makeConstantReplacementNode(constant=bytes(), node=node) def selectBytesBuiltin(string, encoding, errors, source_ref): if encoding is None and errors is None: return ExpressionBuiltinBytes1(value=string, source_ref=source_ref) else: return ExpressionBuiltinBytes3( value=string, encoding=encoding, errors=errors, source_ref=source_ref, ) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=selectBytesBuiltin, builtin_spec=BuiltinParameterSpecs.builtin_bytes_spec, empty_special_class=makeBytes0, )
def bytes_extractor(node): def makeBytes0(source_ref): # pylint: disable=unused-argument return makeConstantReplacementNode(constant=bytes(), node=node) def selectBytesBuiltin(string, encoding, errors, source_ref): if encoding is None and errors is None: return ExpressionBuiltinBytes1(value=string, source_ref=source_ref) else: return ExpressionBuiltinBytes3( value=string, encoding=encoding, errors=errors, source_ref=source_ref, ) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=selectBytesBuiltin, builtin_spec=BuiltinParameterSpecs.builtin_bytes_p3_spec, empty_special_class=makeBytes0, )
def sum_extractor(node): # Split up sumwith and without start value, one is much easier. def selectSumBuiltinClass(sequence, start, source_ref): if start is None: return ExpressionBuiltinSum1(sequence=sequence, source_ref=source_ref) else: return ExpressionBuiltinSum2( sequence=sequence, start=start, source_ref=source_ref ) def makeSum0(source_ref): # pylint: disable=unused-argument return makeRaiseExceptionReplacementExpressionFromInstance( expression=node, exception=TypeError("sum expected at least 1 arguments, got 0"), ) return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=selectSumBuiltinClass, builtin_spec=BuiltinParameterSpecs.builtin_sum_spec, empty_special_class=makeSum0, )
def execfile_extractor(node): @calledWithBuiltinArgumentNamesDecorator 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=makeCallNode( ExpressionAttributeLookup( source=ExpressionBuiltinOpen( filename=filename, mode=makeConstantRefNode( constant="rU", source_ref=source_ref ), buffering=None, source_ref=source_ref, ), attribute_name="read", 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 return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=wrapExpressionBuiltinExecfileCreation, builtin_spec=BuiltinParameterSpecs.builtin_execfile_spec, )
def str_extractor(node): return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=ExpressionBuiltinStr, builtin_spec=BuiltinParameterSpecs.builtin_str_spec, )
def unicode_extractor(node): return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=ExpressionBuiltinUnicode, builtin_spec=BuiltinParameterSpecs.builtin_unicode_spec, )
def bool_extractor(node): return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=ExpressionBuiltinBool, builtin_spec=BuiltinParameterSpecs.builtin_bool_spec, )
def globals_extractor(node): return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=ExpressionBuiltinGlobals, builtin_spec=BuiltinParameterSpecs.builtin_globals_spec, )
def super_extractor(node): @calledWithBuiltinArgumentNamesDecorator def wrapSuperBuiltin(type_arg, object_arg, source_ref): if type_arg is None and python_version >= 300: if provider.isCompiledPythonModule(): return makeRaiseExceptionReplacementExpression( expression=node, exception_type="RuntimeError", exception_value="super(): no arguments", ) class_variable = provider.getVariableForReference(variable_name="__class__") provider.trace_collection.getVariableCurrentTrace(class_variable).addUsage() type_arg = ExpressionVariableRef( # Ought to be already closure taken due to "super" flag in # tree building. variable=class_variable, source_ref=source_ref, ) # If we already have this as a local variable, then use that # instead. type_arg_owner = type_arg.getVariable().getOwner() if type_arg_owner is provider or not ( type_arg_owner.isExpressionFunctionBody() or type_arg_owner.isExpressionClassBody() ): type_arg = None if type_arg is None: return makeRaiseExceptionReplacementExpression( expression=node, exception_type="SystemError" if python_version < 331 else "RuntimeError", exception_value="super(): __class__ cell not found", ) if object_arg is None: if ( provider.isExpressionGeneratorObjectBody() or provider.isExpressionCoroutineObjectBody() or provider.isExpressionAsyncgenObjectBody() ): parameter_provider = provider.getParentVariableProvider() else: parameter_provider = provider if parameter_provider.getParameters().getArgumentCount() > 0: par1_name = parameter_provider.getParameters().getArgumentNames()[0] object_variable = provider.getVariableForReference( variable_name=par1_name ) provider.trace_collection.getVariableCurrentTrace( object_variable ).addUsage() object_arg = ExpressionVariableRef( variable=object_variable, source_ref=source_ref ) if not object_arg.getVariable().isParameterVariable(): return makeRaiseExceptionReplacementExpression( expression=node, exception_type="SystemError" if python_version < 300 else "RuntimeError", exception_value="super(): __class__ cell not found", ) else: return makeRaiseExceptionReplacementExpression( expression=node, exception_type="RuntimeError", exception_value="super(): no arguments", ) return ExpressionBuiltinSuper( super_type=type_arg, super_object=object_arg, source_ref=source_ref ) provider = node.getParentVariableProvider().getEntryPoint() if not provider.isCompiledPythonModule(): provider.discardFlag("has_super") return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=wrapSuperBuiltin, builtin_spec=BuiltinParameterSpecs.builtin_super_spec, )
def super_extractor(node): @calledWithBuiltinArgumentNamesDecorator def wrapSuperBuiltin(type_arg, object_arg, source_ref): if type_arg is None and python_version >= 300: if provider.isCompiledPythonModule(): return makeRaiseExceptionReplacementExpression( expression=node, exception_type="RuntimeError", exception_value="super(): no arguments", ) class_variable = provider.getVariableForReference( variable_name="__class__") provider.trace_collection.getVariableCurrentTrace( class_variable).addUsage() type_arg = ExpressionVariableRef( # Ought to be already closure taken due to "super" flag in # tree building. variable=class_variable, source_ref=source_ref, ) # If we already have this as a local variable, then use that # instead. type_arg_owner = type_arg.getVariable().getOwner() if type_arg_owner is provider or not ( type_arg_owner.isExpressionFunctionBody() or type_arg_owner.isExpressionClassBody()): type_arg = None if type_arg is None: return makeRaiseExceptionReplacementExpression( expression=node, exception_type="SystemError" if python_version < 331 else "RuntimeError", exception_value="super(): __class__ cell not found", ) if object_arg is None: if (provider.isExpressionGeneratorObjectBody() or provider.isExpressionCoroutineObjectBody() or provider.isExpressionAsyncgenObjectBody()): parameter_provider = provider.getParentVariableProvider() else: parameter_provider = provider if parameter_provider.getParameters().getArgumentCount() > 0: par1_name = parameter_provider.getParameters( ).getArgumentNames()[0] object_variable = provider.getVariableForReference( variable_name=par1_name) provider.trace_collection.getVariableCurrentTrace( object_variable).addUsage() object_arg = ExpressionVariableRef( variable=object_variable, source_ref=source_ref) if not object_arg.getVariable().isParameterVariable(): return makeRaiseExceptionReplacementExpression( expression=node, exception_type="SystemError" if python_version < 300 else "RuntimeError", exception_value="super(): __class__ cell not found", ) else: return makeRaiseExceptionReplacementExpression( expression=node, exception_type="RuntimeError", exception_value="super(): no arguments", ) return ExpressionBuiltinSuper(super_type=type_arg, super_object=object_arg, source_ref=source_ref) provider = node.getParentVariableProvider().getEntryPoint() if not provider.isCompiledPythonModule(): provider.discardFlag("has_super") return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=wrapSuperBuiltin, builtin_spec=BuiltinParameterSpecs.builtin_super_spec, )
def eval_extractor(node): @calledWithBuiltinArgumentNamesDecorator 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 return BuiltinParameterSpecs.extractBuiltinArgs( node=node, builtin_class=wrapEvalBuiltin, builtin_spec=BuiltinParameterSpecs.builtin_eval_spec, )