示例#1
0
    def computeExpression(self, constraint_collection):
        pos_arg = self.getPositionalArgument()

        if self.hasOnlyConstantArguments():
            if pos_arg is not None:
                pos_args = (pos_arg, )
            elif python_version >= 340:
                # Doing this here, because calling dict built-in apparently
                # mutates existing dictionaries in Python 3.4
                result = {}

                for pair in reversed(self.getNamedArgumentPairs()):
                    arg_name = pair.getKey().getCompileTimeConstant()
                    arg_value = pair.getValue().getCompileTimeConstant()

                    result[arg_name] = arg_value

                new_node = makeConstantReplacementNode(constant=result,
                                                       node=self)

                return new_node, "new_expression", "Replace 'dict' built-in call with constant arguments."
            else:
                pos_args = None

            from .NodeMakingHelpers import getComputationResult

            return getComputationResult(
                node=self,
                computation=lambda: builtin_dict_spec.simulateCall(
                    (pos_args, self.getNamedArgumentPairs())),
                description="Replace 'dict' call with constant arguments.")
        elif pos_arg is None:
            new_node = ExpressionMakeDict(pairs=self.getNamedArgumentPairs(),
                                          lazy_order=False,
                                          source_ref=self.source_ref)

            return (
                new_node, "new_expression",
                "Replace 'dict' built-in call dictionary creation from arguments."
            )
        elif pos_arg.getIterationLength() == 0:
            new_node = ExpressionMakeDict(pairs=self.getNamedArgumentPairs(),
                                          lazy_order=False,
                                          source_ref=self.source_ref)

            # Maintain potential side effects from the positional arguments.
            new_node = wrapExpressionWithNodeSideEffects(old_node=pos_arg,
                                                         new_node=new_node)

            return (
                new_node, "new_expression",
                "Replace 'dict' built-in call dictionary creation from arguments."
            )
        else:
            return self, None, None
示例#2
0
    def computeExpression(self, constraint_collection):
        pos_arg = self.getPositionalArgument()
        pairs = self.getNamedArgumentPairs()

        if pos_arg is None:
            new_node = ExpressionMakeDict(pairs=self.getNamedArgumentPairs(),
                                          source_ref=self.source_ref)

            # This cannot raise anymore than its arguments, as the keys will
            # be known as hashable, due to being Python parameters before.

            return (
                new_node, "new_expression",
                "Replace 'dict' built-in call dictionary creation from arguments."
            )

        pos_iteration_length = pos_arg.getIterationLength()

        if pos_iteration_length == 0:
            new_node = ExpressionMakeDict(pairs=self.getNamedArgumentPairs(),
                                          source_ref=self.source_ref)

            # Maintain potential side effects from the positional arguments.
            new_node = wrapExpressionWithNodeSideEffects(
                old_node=ExpressionBuiltinIter1(value=pos_arg,
                                                source_ref=self.source_ref),
                new_node=new_node)

            # Just in case, the iteration may do that.
            if pos_arg.mayRaiseExceptionIter(BaseException):
                constraint_collection.onExceptionRaiseExit(BaseException)

            return (
                new_node, "new_expression",
                "Replace 'dict' built-in call dictionary creation from arguments."
            )

        if pos_iteration_length is not None and \
           pos_iteration_length + len(pairs) < 256 and \
           self.hasOnlyConstantArguments():
            if pos_arg is not None:
                pos_args = (pos_arg, )
            else:
                pos_args = None

            return constraint_collection.getCompileTimeComputationResult(
                node=self,
                computation=lambda: builtin_dict_spec.simulateCall(
                    (pos_args, self.getNamedArgumentPairs())),
                description="Replace 'dict' call with constant arguments.")
        else:
            constraint_collection.onExceptionRaiseExit(BaseException)

            return self, None, None
示例#3
0
    def computeExpression(self, constraint_collection):
        pos_arg = self.getPositionalArgument()

        if self.hasOnlyConstantArguments():
            if pos_arg is not None:
                pos_args = (
                    pos_arg,
                )
            elif python_version >= 340:
                # Doing this here, because calling dict built-in apparently
                # mutates existing dictionaries in Python 3.4
                result = {}

                for pair in reversed(self.getNamedArgumentPairs()):
                    arg_name = pair.getKey().getCompileTimeConstant()
                    arg_value = pair.getValue().getCompileTimeConstant()

                    result[arg_name] = arg_value

                new_node = makeConstantReplacementNode(
                    constant = result,
                    node     = self
                )

                return new_node, "new_expression", "Replace 'dict' built-in call with constant arguments."
            else:
                pos_args = None

            from .NodeMakingHelpers import getComputationResult

            return getComputationResult(
                node        = self,
                computation = lambda : builtin_dict_spec.simulateCall(
                    (pos_args, self.getNamedArgumentPairs())
                ),
                description = "Replace 'dict' call with constant arguments."
            )
        elif pos_arg is None:
            new_node = ExpressionMakeDict(
                pairs      = self.getNamedArgumentPairs(),
                lazy_order = False,
                source_ref = self.source_ref
            )

            return (
                new_node,
                "new_expression",
                "Replace 'dict' built-in call dictionary creation from arguments."
            )
        elif pos_arg.getIterationLength() == 0:
            new_node = ExpressionMakeDict(
                pairs      = self.getNamedArgumentPairs(),
                lazy_order = False,
                source_ref = self.source_ref
            )

            # Maintain potential side effects from the positional arguments.
            new_node = wrapExpressionWithNodeSideEffects(
                old_node = pos_arg,
                new_node = new_node
            )

            return (
                new_node,
                "new_expression",
                "Replace 'dict' built-in call dictionary creation from arguments."
            )
        else:
            return self, None, None
示例#4
0
    def computeExpression(self, trace_collection):
        pos_arg = self.getPositionalArgument()
        pairs = self.getNamedArgumentPairs()

        if pos_arg is None:
            new_node = ExpressionMakeDict(
                pairs      = self.getNamedArgumentPairs(),
                source_ref = self.source_ref
            )

            # This cannot raise anymore than its arguments, as the keys will
            # be known as hashable, due to being Python parameters before.

            return (
                new_node,
                "new_expression",
                "Replace 'dict' built-in call dictionary creation from arguments."
            )

        pos_iteration_length = pos_arg.getIterationLength()

        if pos_iteration_length == 0:
            new_node = ExpressionMakeDict(
                pairs      = self.getNamedArgumentPairs(),
                source_ref = self.source_ref
            )

            # Maintain potential side effects from the positional arguments.
            new_node = wrapExpressionWithNodeSideEffects(
                old_node = ExpressionBuiltinIter1(
                    value      = pos_arg,
                    source_ref = self.source_ref
                ),
                new_node = new_node
            )

            # Just in case, the iteration may do that.
            if not pos_arg.hasShapeSlotIter():
                trace_collection.onExceptionRaiseExit(BaseException)

            return (
                new_node,
                "new_expression",
                "Replace 'dict' built-in call dictionary creation from arguments."
            )

        if pos_iteration_length is not None and \
           pos_iteration_length + len(pairs) < 256 and \
           self.hasOnlyConstantArguments():
            if pos_arg is not None:
                pos_args = (
                    pos_arg,
                )
            else:
                pos_args = None

            return trace_collection.getCompileTimeComputationResult(
                node        = self,
                computation = lambda : builtin_dict_spec.simulateCall(
                    (pos_args, self.getNamedArgumentPairs())
                ),
                description = "Replace 'dict' call with constant arguments."
            )
        else:
            trace_collection.onExceptionRaiseExit(BaseException)

            return self, None, None
示例#5
0
    def computeExpression(self, constraint_collection):
        pos_arg = self.getPositionalArgument()

        if self.hasOnlyConstantArguments():
            if pos_arg is not None:
                pos_args = (pos_arg, )
            elif python_version >= 340:
                # Doing this here, because calling dict built-in apparently
                # mutates existing dictionaries in Python 3.4
                result = {}

                for pair in reversed(self.getNamedArgumentPairs()):
                    arg_name = pair.getKey().getCompileTimeConstant()
                    arg_value = pair.getValue().getCompileTimeConstant()

                    result[arg_name] = arg_value

                new_node = makeConstantReplacementNode(constant=result,
                                                       node=self)

                return new_node, "new_expression", "Replace 'dict' built-in call with constant arguments."
            else:
                pos_args = None

            return constraint_collection.getCompileTimeComputationResult(
                node=self,
                computation=lambda: builtin_dict_spec.simulateCall(
                    (pos_args, self.getNamedArgumentPairs())),
                description="Replace 'dict' call with constant arguments.")
        elif pos_arg is None:
            new_node = ExpressionMakeDict(pairs=self.getNamedArgumentPairs(),
                                          lazy_order=False,
                                          source_ref=self.source_ref)

            # This cannot raise anymore than its arguments, as the keys will
            # be known as hashable, due to being Python parameters before.

            return (
                new_node, "new_expression",
                "Replace 'dict' built-in call dictionary creation from arguments."
            )
        elif pos_arg.getIterationLength() == 0:
            new_node = ExpressionMakeDict(pairs=self.getNamedArgumentPairs(),
                                          lazy_order=False,
                                          source_ref=self.source_ref)

            # Maintain potential side effects from the positional arguments.
            new_node = wrapExpressionWithNodeSideEffects(
                old_node=ExpressionBuiltinIter1(value=pos_arg,
                                                source_ref=self.source_ref),
                new_node=new_node)

            # Just in case, the iteration may do that.
            if pos_arg.mayRaiseExceptionIter(BaseException):
                constraint_collection.onExceptionRaiseExit(BaseException)

            return (
                new_node, "new_expression",
                "Replace 'dict' built-in call dictionary creation from arguments."
            )
        else:
            constraint_collection.onExceptionRaiseExit(BaseException)

            return self, None, None
示例#6
0
    def computeExpression(self, constraint_collection):
        pos_arg = self.getPositionalArgument()

        if self.hasOnlyConstantArguments():
            if pos_arg is not None:
                pos_args = (
                    pos_arg,
                )
            elif python_version >= 340:
                # Doing this here, because calling dict built-in apparently
                # mutates existing dictionaries in Python 3.4
                result = {}

                for pair in reversed(self.getNamedArgumentPairs()):
                    arg_name = pair.getKey().getCompileTimeConstant()
                    arg_value = pair.getValue().getCompileTimeConstant()

                    result[arg_name] = arg_value

                new_node = makeConstantReplacementNode(
                    constant = result,
                    node     = self
                )

                return new_node, "new_expression", "Replace 'dict' built-in call with constant arguments."
            else:
                pos_args = None

            return constraint_collection.getCompileTimeComputationResult(
                node        = self,
                computation = lambda : builtin_dict_spec.simulateCall(
                    (pos_args, self.getNamedArgumentPairs())
                ),
                description = "Replace 'dict' call with constant arguments."
            )
        elif pos_arg is None:
            new_node = ExpressionMakeDict(
                pairs      = self.getNamedArgumentPairs(),
                lazy_order = False,
                source_ref = self.source_ref
            )

            # This cannot raise anymore than its arguments, as the keys will
            # be known as hashable, due to being Python parameters before.

            return (
                new_node,
                "new_expression",
                "Replace 'dict' built-in call dictionary creation from arguments."
            )
        elif pos_arg.getIterationLength() == 0:
            new_node = ExpressionMakeDict(
                pairs      = self.getNamedArgumentPairs(),
                lazy_order = False,
                source_ref = self.source_ref
            )

            # Maintain potential side effects from the positional arguments.
            new_node = wrapExpressionWithNodeSideEffects(
                old_node = ExpressionBuiltinIter1(
                    value      = pos_arg,
                    source_ref = self.source_ref
                ),
                new_node = new_node
            )

            # Just in case, the iteration may do that.
            if pos_arg.mayRaiseExceptionIter(BaseException):
                constraint_collection.onExceptionRaiseExit(BaseException)

            return (
                new_node,
                "new_expression",
                "Replace 'dict' built-in call dictionary creation from arguments."
            )
        else:
            constraint_collection.onExceptionRaiseExit(BaseException)

            return self, None, None