Пример #1
0
    def makeStaticmethod0(source_ref):
        # pylint: disable=unused-argument

        return makeRaiseExceptionReplacementExpressionFromInstance(
            expression=node,
            exception=TypeError("classmethod expected 1 arguments, got 0"),
        )
Пример #2
0
    def wrapExpressionBuiltinDictCreation(positional_args, dict_star_arg,
                                          source_ref):
        if len(positional_args) > 1:
            from nuitka.nodes.NodeMakingHelpers import (
                makeRaiseExceptionReplacementExpressionFromInstance,
                wrapExpressionWithSideEffects)

            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)
Пример #3
0
    def makeOrd0(source_ref):
        # pylint: disable=unused-argument

        return makeRaiseExceptionReplacementExpressionFromInstance(
            expression=node,
            exception=TypeError("ord() takes exactly one argument (0 given)"),
        )
Пример #4
0
def extractBuiltinArgs(node,
                       builtin_spec,
                       builtin_class,
                       empty_special_class=None):
    try:
        kw = node.getCallKw()

        # TODO: Could check for too many / too few, even if they are unknown, we might
        # raise that error, but that need not be optimized immediately.
        if not kw.isMappingWithConstantStringKeys():
            return None

        pairs = kw.getMappingStringKeyPairs()

        if pairs and not builtin_spec.allowsKeywords():
            raise TooManyArguments(
                TypeError(builtin_spec.getKeywordRefusalText()))

        args = node.getCallArgs()

        if not args.canPredictIterationValues():
            return None

        positional = args.getIterationValues()

        if not positional and not pairs and empty_special_class is not None:
            return empty_special_class(source_ref=node.getSourceReference())

        args_dict = matchCall(
            func_name=builtin_spec.getName(),
            args=builtin_spec.getArgumentNames(),
            star_list_arg=builtin_spec.getStarListArgumentName(),
            star_dict_arg=builtin_spec.getStarDictArgumentName(),
            num_defaults=builtin_spec.getDefaultCount(),
            positional=positional,
            pairs=pairs)
    except TooManyArguments as e:
        from nuitka.nodes.NodeMakingHelpers import (
            makeRaiseExceptionReplacementExpressionFromInstance,
            wrapExpressionWithSideEffects)

        return wrapExpressionWithSideEffects(
            new_node=makeRaiseExceptionReplacementExpressionFromInstance(
                expression=node, exception=e.getRealException()),
            old_node=node,
            side_effects=node.extractPreCallSideEffects())

    args_list = []

    for argument_name in builtin_spec.getArgumentNames():
        args_list.append(args_dict[argument_name])

    if builtin_spec.getStarListArgumentName() is not None:
        args_list.append(args_dict[builtin_spec.getStarListArgumentName()])

    if builtin_spec.getStarDictArgumentName() is not None:
        args_list.append(args_dict[builtin_spec.getStarDictArgumentName()])

    # Using list reference for passing the arguments without names, pylint: disable=W0142
    return builtin_class(*args_list, source_ref=node.getSourceReference())
Пример #5
0
def type_extractor(node):
    args = node.getCallArgs()

    if args is None:
        iter_length = 0
    else:
        iter_length = args.getIterationLength()

    if iter_length == 1:
        return BuiltinOptimization.extractBuiltinArgs(
            node          = node,
            builtin_class = ExpressionBuiltinType1,
            builtin_spec  = BuiltinOptimization.builtin_type1_spec
        )
    elif iter_length == 3:
        return BuiltinOptimization.extractBuiltinArgs(
            node          = node,
            builtin_class = ExpressionBuiltinType3,
            builtin_spec  = BuiltinOptimization.builtin_type3_spec
        )
    else:
        return makeRaiseExceptionReplacementExpressionFromInstance(
            expression = node,
            exception  = TypeError("type() takes 1 or 3 arguments")
        )
Пример #6
0
    def makeFormat0(source_ref):
        # pylint: disable=unused-argument

        return makeRaiseExceptionReplacementExpressionFromInstance(
            expression = node,
            exception  = TypeError("format() takes at least 1 argument (0 given)")
        )
Пример #7
0
    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
        )
Пример #8
0
    def makeFormat0(source_ref):
        # pylint: disable=unused-argument

        return makeRaiseExceptionReplacementExpressionFromInstance(
            expression=node,
            exception=TypeError("format() takes at least 1 argument (0 given)"),
        )
Пример #9
0
def type_extractor(node):
    args = node.getCallArgs()

    if args is None:
        iter_length = 0
    else:
        iter_length = args.getIterationLength()

    if iter_length == 1:
        return BuiltinOptimization.extractBuiltinArgs(
            node          = node,
            builtin_class = ExpressionBuiltinType1,
            builtin_spec  = BuiltinOptimization.builtin_type1_spec
        )
    elif iter_length == 3:
        return BuiltinOptimization.extractBuiltinArgs(
            node          = node,
            builtin_class = ExpressionBuiltinType3,
            builtin_spec  = BuiltinOptimization.builtin_type3_spec
        )
    else:
        return makeRaiseExceptionReplacementExpressionFromInstance(
            expression = node,
            exception  = TypeError("type() takes 1 or 3 arguments")
        )
Пример #10
0
    def makeSum0(source_ref):
        # pylint: disable=W0613

        return makeRaiseExceptionReplacementExpressionFromInstance(
            expression = node,
            exception  = TypeError("sum expected at least 1 arguments, got 0")
        )
Пример #11
0
    def makeStaticmethod0(source_ref):
        # pylint: disable=unused-argument

        return makeRaiseExceptionReplacementExpressionFromInstance(
            expression=node,
            exception=TypeError("classmethod expected 1 arguments, got 0"),
        )
Пример #12
0
    def makeOrd0(source_ref):
        # pylint: disable=unused-argument

        return makeRaiseExceptionReplacementExpressionFromInstance(
            expression=node,
            exception=TypeError("ord() takes exactly one argument (0 given)"),
        )
Пример #13
0
    def makeRange0(source_ref):
        # pylint: disable=unused-argument

        return makeRaiseExceptionReplacementExpressionFromInstance(
            expression=node,
            exception=TypeError("range expected at least 1 arguments, got 0"),
        )
Пример #14
0
    def makeRange0(source_ref):
        # pylint: disable=unused-argument

        return makeRaiseExceptionReplacementExpressionFromInstance(
            expression=node,
            exception=TypeError("range expected at least 1 arguments, got 0"),
        )
Пример #15
0
    def makeOpen0(source_ref):
        # pylint: disable=W0613

        return makeRaiseExceptionReplacementExpressionFromInstance(
            expression=node,
            exception=TypeError("Required argument 'name' (pos 1) not found"
                                if python_version < 300 else
                                "Required argument 'file' (pos 1) not found"))
Пример #16
0
    def makeXrange0(source_ref):
        # pylint: disable=W0613

        return makeRaiseExceptionReplacementExpressionFromInstance(
            expression=node,
            exception=TypeError(
                "xrange requires 1-3 int arguments" if python_version < 300
                else "range expected 1 arguments, got 0"))
Пример #17
0
    def computeExpressionCall( self, call_node, constraint_collection ):
        # TODO: Until we have something to re-order the arguments, we need to skip this. For
        # the immediate need, we avoid this complexity, as a re-ordering will be needed.
        if call_node.getNamedArgumentPairs():
            return call_node, None, None

        call_spec = self.getParameters()

        try:
            args_dict = matchCall(
                func_name     = self.getName(),
                args          = call_spec.getArgumentNames(),
                star_list_arg = call_spec.getStarListArgumentName(),
                star_dict_arg = call_spec.getStarDictArgumentName(),
                num_defaults  = call_spec.getDefaultCount(),
                positional    = call_node.getPositionalArguments(),
                pairs         = ()
            )

            values = []

            for positional_arg in call_node.getPositionalArguments():
                for _arg_name, arg_value in iterItems( args_dict ):
                    if arg_value is positional_arg:
                        values.append( arg_value )

            result = ExpressionFunctionCall(
                function_body = self,
                values        = values,
                source_ref    = call_node.getSourceReference()
            )

            return (
                result,
                "new_statements", # TODO: More appropiate tag maybe.
                "Replaced call to created function body '%s' with direct function call" % self.getName()
            )

        except TooManyArguments as e:
            from nuitka.nodes.NodeMakingHelpers import (
                makeRaiseExceptionReplacementExpressionFromInstance,
                wrapExpressionWithSideEffects
            )

            result = wrapExpressionWithSideEffects(
                new_node = makeRaiseExceptionReplacementExpressionFromInstance(
                    expression     = call_node,
                    exception      = e.getRealException()
                ),
                old_node           = call_node,
                side_effects = call_node.extractPreCallSideEffects()
            )

            return (
                result,
                "new_statements,new_raise", # TODO: More appropiate tag maybe.
                "Replaced call to created function body '%s' to argument error" % self.getName()
            )
Пример #18
0
 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")
Пример #19
0
    def makeSum0(source_ref):
        # pylint: disable=unused-argument

        return makeRaiseExceptionReplacementExpressionFromInstance(
            expression=node,
            exception=TypeError(
                "sum expected at least 1 arguments, got 0"
                if python_version < 380 else
                "sum() takes at least 1 positional argument (0 given)"),
        )
Пример #20
0
 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")
Пример #21
0
    def makeXrange0(source_ref):
        # pylint: disable=unused-argument
        if python_version < 300:
            exception_message = "xrange requires 1-3 int arguments"
        elif python_version < 380:
            exception_message = "range expected 1 arguments, got 0"
        else:
            exception_message = "range expected 1 argument, got 0"

        return makeRaiseExceptionReplacementExpressionFromInstance(
            expression=node, exception=TypeError(exception_message))
Пример #22
0
    def makeOpen0(source_ref):
        # pylint: disable=W0613

        return makeRaiseExceptionReplacementExpressionFromInstance(
            expression = node,
            exception  = TypeError(
                "Required argument 'name' (pos 1) not found"
                  if python_version < 300 else
                "Required argument 'file' (pos 1) not found"
            )
        )
Пример #23
0
    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"
            ),
        )
Пример #24
0
    def makeAny0(source_ref):
        exception_message = "any() takes exactly one argument (0 given)"

        return makeRaiseExceptionReplacementExpressionFromInstance(
            expression=node, exception=TypeError(exception_message))
Пример #25
0
def extractBuiltinArgs(node, builtin_spec, builtin_class,
                       empty_special_class = None):
    try:
        kw = node.getCallKw()

        # TODO: Could check for too many / too few, even if they are unknown, we
        # might raise that error, but that need not be optimized immediately.
        if not kw.isMappingWithConstantStringKeys():
            return None

        pairs = kw.getMappingStringKeyPairs()

        if pairs and not builtin_spec.allowsKeywords():
            raise TooManyArguments(
                TypeError(builtin_spec.getKeywordRefusalText())
            )

        args = node.getCallArgs()

        if not args.canPredictIterationValues():
            return None

        positional = args.getIterationValues()

        if not positional and not pairs and empty_special_class is not None:
            return empty_special_class(source_ref = node.getSourceReference())

        args_dict = matchCall(
            func_name     = builtin_spec.getName(),
            args          = builtin_spec.getArgumentNames(),
            star_list_arg = builtin_spec.getStarListArgumentName(),
            star_dict_arg = builtin_spec.getStarDictArgumentName(),
            num_defaults  = builtin_spec.getDefaultCount(),
            positional    = positional,
            pairs         = pairs
        )
    except TooManyArguments as e:
        from nuitka.nodes.NodeMakingHelpers import (
            makeRaiseExceptionReplacementExpressionFromInstance,
            wrapExpressionWithSideEffects
        )

        return wrapExpressionWithSideEffects(
            new_node     = makeRaiseExceptionReplacementExpressionFromInstance(
                expression     = node,
                exception      = e.getRealException()
            ),
            old_node     = node,
            side_effects = node.extractPreCallSideEffects()
        )

    args_list = []

    for argument_name in builtin_spec.getArgumentNames():
        args_list.append(args_dict[argument_name])

    if builtin_spec.getStarListArgumentName() is not None:
        args_list.append(args_dict[builtin_spec.getStarListArgumentName()])

    if builtin_spec.getStarDictArgumentName() is not None:
        args_list.append(args_dict[builtin_spec.getStarDictArgumentName()])

    # Using list reference for passing the arguments without names,
    # pylint: disable=W0142
    return builtin_class(
        *args_list,
        source_ref = node.getSourceReference()
    )
def extractBuiltinArgs(node,
                       builtin_spec,
                       builtin_class,
                       empty_special_class=None):
    # Many cases to deal with, pylint: disable=too-many-branches

    try:
        kw = node.subnode_kwargs

        # TODO: Could check for too many / too few, even if they are unknown, we
        # might raise that error, but that need not be optimized immediately.
        if kw is not None:
            if not kw.isMappingWithConstantStringKeys():
                return None

            pairs = kw.getMappingStringKeyPairs()

            if pairs and not builtin_spec.allowsKeywords():
                raise TooManyArguments(
                    TypeError(builtin_spec.getKeywordRefusalText()))
        else:
            pairs = ()

        args = node.subnode_args

        if args:
            if not args.canPredictIterationValues():
                return None

            positional = args.getIterationValues()
        else:
            positional = ()

        if not positional and not pairs and empty_special_class is not None:
            return empty_special_class(source_ref=node.getSourceReference())

        args_dict = matchCall(
            func_name=builtin_spec.getName(),
            args=builtin_spec.getArgumentNames(),
            kw_only_args=builtin_spec.getKwOnlyParameterNames(),
            star_list_arg=builtin_spec.getStarListArgumentName(),
            star_dict_arg=builtin_spec.getStarDictArgumentName(),
            num_defaults=builtin_spec.getDefaultCount(),
            num_posonly=builtin_spec.getPosOnlyParameterCount(),
            positional=positional,
            pairs=pairs,
        )
    except TooManyArguments as e:
        from nuitka.nodes.NodeMakingHelpers import (
            makeRaiseExceptionReplacementExpressionFromInstance,
            wrapExpressionWithSideEffects,
        )

        return wrapExpressionWithSideEffects(
            new_node=makeRaiseExceptionReplacementExpressionFromInstance(
                expression=node, exception=e.getRealException()),
            old_node=node,
            side_effects=node.extractSideEffectsPreCall(),
        )

    # Using list reference for passing the arguments without names where it
    # it possible, otherwise dictionary to make those distinuishable.
    args_list = []

    for argument_name in builtin_spec.getArgumentNames():
        args_list.append(args_dict[argument_name])

    if builtin_spec.getStarListArgumentName() is not None:
        args_list.append(args_dict[builtin_spec.getStarListArgumentName()])

    if builtin_spec.getStarDictArgumentName() is not None:
        args_list.append(args_dict[builtin_spec.getStarDictArgumentName()])

    for argument_name in builtin_spec.getKwOnlyParameterNames():
        args_list.append(args_dict[argument_name])

    # Using list reference for passing the arguments without names,
    result = builtin_class(*args_list, source_ref=node.getSourceReference())

    if python_version < 0x380:
        result.setCompatibleSourceReference(
            node.getCompatibleSourceReference())

    return result
Пример #27
0
    def makeSum0(source_ref):
        # pylint: disable=W0613

        return makeRaiseExceptionReplacementExpressionFromInstance(
            expression=node,
            exception=TypeError("sum expected at least 1 arguments, got 0"))