Ejemplo n.º 1
0
    def __init__(self, constant, source_ref, user_provided=False):
        CompileTimeConstantExpressionBase.__init__(self, source_ref=source_ref)

        assert isConstant(constant), repr(constant)

        self.constant = constant

        # Memory saving method, have the attribute only where necessary.
        self.user_provided = user_provided

        if not user_provided and isDebug():
            try:
                if type(constant) in (str, unicode, bytes):
                    max_size = 1000
                elif type(constant) is xrange:
                    max_size = None
                else:
                    max_size = 256

                if max_size is not None and len(constant) > max_size:
                    warning("Too large constant (%s %d) encountered at %s.",
                            type(constant), len(constant),
                            source_ref.getAsString())
            except TypeError:
                pass
Ejemplo n.º 2
0
    def __init__(self, constant, source_ref, user_provided=False):
        CompileTimeConstantExpressionBase.__init__(self, source_ref=source_ref)

        assert isConstant(constant), repr(constant)

        self.constant = constant

        # Memory saving method, have the attribute only where necessary.
        self.user_provided = user_provided

        if not user_provided and isDebug():
            try:
                if type(constant) in (str, unicode, bytes):
                    max_size = 1000
                elif type(constant) is xrange:
                    max_size = None
                else:
                    max_size = 256

                if max_size is not None and len(constant) > max_size:
                    warning(
                        "Too large constant (%s %d) encountered at %s.",
                        type(constant),
                        len(constant),
                        source_ref.getAsString(),
                    )
            except TypeError:
                pass
Ejemplo n.º 3
0
    def __init__(self, constant, source_ref, user_provided = False):
        NodeBase.__init__( self, source_ref = source_ref )
        CompileTimeConstantExpressionMixin.__init__( self )

        assert isConstant( constant ), constant

        self.constant = constant
        self.user_provided = user_provided

        if not user_provided and isDebug():
            try:
                size = len( constant )

                if type( constant ) in ( str, unicode ):
                    max_size = 1000
                else:
                    max_size = 256

                if size > max_size:
                    warning(
                        "Too large constant (%s %d) encountered at %s.",
                        type( constant ),
                        size,
                        source_ref.getAsString()
                    )
            except TypeError:
                pass
Ejemplo n.º 4
0
    def __init__(self, constant, source_ref, user_provided=False):
        NodeBase.__init__(self, source_ref=source_ref)
        CompileTimeConstantExpressionMixin.__init__(self)

        assert isConstant(constant), constant

        self.constant = constant

        if user_provided:
            self.user_provided = user_provided

        if not user_provided and isDebug():
            try:
                size = len(constant)

                if type(constant) in (str, unicode):
                    max_size = 1000
                else:
                    max_size = 256

                if size > max_size:
                    warning("Too large constant (%s %d) encountered at %s.",
                            type(constant), size, source_ref.getAsString())
            except TypeError:
                pass
Ejemplo n.º 5
0
def makeCompileTimeConstantReplacementNode(value, node, user_provided):
    # This needs to match code in isCompileTimeConstantValue
    if isConstant(value):
        return makeConstantReplacementNode(
            constant=value, node=node, user_provided=user_provided
        )
    elif type(value) is type:
        if value.__name__ in builtin_names:
            from .BuiltinRefNodes import makeExpressionBuiltinRef

            # Need not provide locals_scope, not used for these kinds of built-in refs that
            # refer to types.
            return makeExpressionBuiltinRef(
                builtin_name=value.__name__,
                locals_scope=None,
                source_ref=node.getSourceReference(),
            )
        else:
            return node
    elif GenericAlias is not None and isinstance(value, GenericAlias):
        from .BuiltinTypeNodes import ExpressionConstantGenericAlias

        return ExpressionConstantGenericAlias(
            generic_alias=value,
            source_ref=node.getSourceReference(),
        )
    elif UnionType is not None and isinstance(value, UnionType):
        from .BuiltinTypeNodes import ExpressionConstantUnionType

        return ExpressionConstantUnionType(
            union_type=value,
            source_ref=node.getSourceReference(),
        )
    else:
        return node
Ejemplo n.º 6
0
    def __init__(self, constant, source_ref):
        NodeBase.__init__(self, source_ref=source_ref)
        CompileTimeConstantExpressionMixin.__init__(self)

        assert isConstant(constant), constant

        self.constant = constant
Ejemplo n.º 7
0
    def __init__( self, constant, source_ref ):
        NodeBase.__init__( self, source_ref = source_ref )
        CompileTimeConstantExpressionMixin.__init__( self )

        assert isConstant( constant ), constant

        self.constant = constant
Ejemplo n.º 8
0
def isCompileTimeConstantValue( value ):
    # This needs to match code in makeCompileTimeConstantReplacementNode
    if isConstant( value ):
        return True
    elif type( value ) is type:
        return True
    else:
        return False
Ejemplo n.º 9
0
def isCompileTimeConstantValue(value):
    # This needs to match code in makeCompileTimeConstantReplacementNode
    if isConstant(value):
        return True
    elif type(value) is type:
        return True
    else:
        return False
Ejemplo n.º 10
0
def makeCompileTimeConstantReplacementNode(value, node):
    # This needs to match code in isCompileTimeConstantValue
    if isConstant(value):
        return makeConstantReplacementNode(constant=value, node=node)
    elif type(value) is type:
        if value.__name__ in builtin_names:
            return ExpressionBuiltinRef(builtin_name=value.__name__,
                                        source_ref=node.getSourceReference())
        else:
            return node
    else:
        return node
Ejemplo n.º 11
0
def makeCompileTimeConstantReplacementNode(value, node):
    # This needs to match code in isCompileTimeConstantValue
    if isConstant(value):
        return makeConstantReplacementNode(constant=value, node=node)
    elif type(value) is type:
        if value.__name__ in builtin_names:
            from .BuiltinRefNodes import makeExpressionBuiltinRef

            return makeExpressionBuiltinRef(
                builtin_name=value.__name__, source_ref=node.getSourceReference()
            )
        else:
            return node
    else:
        return node
Ejemplo n.º 12
0
def makeCompileTimeConstantReplacementNode(value, node):
    # This needs to match code in isCompileTimeConstantValue
    if isConstant(value):
        return makeConstantReplacementNode(constant=value, node=node)
    elif type(value) is type:
        if value.__name__ in builtin_names:
            from .BuiltinRefNodes import makeExpressionBuiltinRef

            # Need not provide locals_scope, not used for these kinds of built-in refs that
            # refer to types.
            return makeExpressionBuiltinRef(
                builtin_name=value.__name__,
                locals_scope=None,
                source_ref=node.getSourceReference(),
            )
        else:
            return node
    else:
        return node
Ejemplo n.º 13
0
def makeConstantRefNode(constant, source_ref, user_provided=False):
    # This is dispatching per constant value and types, every case
    # to be a return statement, pylint: disable=too-many-branches,too-many-return-statements,too-many-statements

    # Dispatch based on constants first.
    if constant is None:
        return ExpressionConstantNoneRef(source_ref=source_ref)
    elif constant is True:
        return ExpressionConstantTrueRef(source_ref=source_ref)
    elif constant is False:
        return ExpressionConstantFalseRef(source_ref=source_ref)
    elif constant is Ellipsis:
        return ExpressionConstantEllipsisRef(source_ref=source_ref)
    else:
        # Next, dispatch based on type.
        constant_type = type(constant)

        if constant_type is int:
            return ExpressionConstantIntRef(constant=constant,
                                            source_ref=source_ref)
        elif constant_type is str:
            return ExpressionConstantStrRef(
                constant=constant,
                user_provided=user_provided,
                source_ref=source_ref,
            )
        elif constant_type is float:
            return ExpressionConstantFloatRef(constant=constant,
                                              source_ref=source_ref)
        elif constant_type is long:
            return ExpressionConstantLongRef(
                constant=constant,
                user_provided=user_provided,
                source_ref=source_ref,
            )
        elif constant_type is unicode:
            return ExpressionConstantUnicodeRef(
                constant=constant,
                user_provided=user_provided,
                source_ref=source_ref,
            )
        elif constant_type is bytes:
            return ExpressionConstantBytesRef(
                constant=constant,
                user_provided=user_provided,
                source_ref=source_ref,
            )
        elif constant_type is dict:
            if constant:
                assert isConstant(constant), repr(constant)

                return ExpressionConstantDictRef(
                    constant=constant,
                    user_provided=user_provided,
                    source_ref=source_ref,
                )
            else:
                return ExpressionConstantDictEmptyRef(
                    user_provided=user_provided,
                    source_ref=source_ref,
                )
        elif constant_type is tuple:
            if constant:
                assert isConstant(constant), repr(constant)

                if isMutable(constant):
                    return ExpressionConstantTupleMutableRef(
                        constant=constant,
                        user_provided=user_provided,
                        source_ref=source_ref,
                    )
                else:
                    return ExpressionConstantTupleRef(
                        constant=constant,
                        user_provided=user_provided,
                        source_ref=source_ref,
                    )
            else:
                return ExpressionConstantTupleEmptyRef(
                    user_provided=user_provided,
                    source_ref=source_ref,
                )
        elif constant_type is list:
            if constant:
                assert isConstant(constant), repr(constant)

                return ExpressionConstantListRef(
                    constant=constant,
                    user_provided=user_provided,
                    source_ref=source_ref,
                )
            else:
                return ExpressionConstantListEmptyRef(
                    user_provided=user_provided,
                    source_ref=source_ref,
                )
        elif constant_type is set:
            if constant:
                assert isConstant(constant), repr(constant)

                return ExpressionConstantSetRef(
                    constant=constant,
                    user_provided=user_provided,
                    source_ref=source_ref,
                )
            else:
                return ExpressionConstantSetEmptyRef(
                    user_provided=user_provided,
                    source_ref=source_ref,
                )
        elif constant_type is frozenset:
            if constant:
                assert isConstant(constant), repr(constant)

                return ExpressionConstantFrozensetRef(
                    constant=constant,
                    user_provided=user_provided,
                    source_ref=source_ref,
                )
            else:
                return ExpressionConstantFrozensetEmptyRef(
                    user_provided=user_provided,
                    source_ref=source_ref,
                )
        elif constant_type is complex:
            return ExpressionConstantComplexRef(
                constant=constant,
                source_ref=source_ref,
            )
        elif constant_type is slice:
            return ExpressionConstantSliceRef(
                constant=constant,
                source_ref=source_ref,
            )
        elif constant_type is type:
            return ExpressionConstantTypeRef(constant=constant,
                                             source_ref=source_ref)
        elif constant_type is xrange:
            return ExpressionConstantXrangeRef(
                constant=constant,
                source_ref=source_ref,
            )
        elif constant_type is bytearray:
            return ExpressionConstantBytearrayRef(
                constant=constant,
                user_provided=user_provided,
                source_ref=source_ref,
            )
        elif constant in builtin_anon_values:
            from .BuiltinRefNodes import ExpressionBuiltinAnonymousRef

            return ExpressionBuiltinAnonymousRef(
                builtin_name=builtin_anon_values[constant],
                source_ref=source_ref,
            )
        elif constant in builtin_named_values:
            from .BuiltinRefNodes import ExpressionBuiltinRef

            return ExpressionBuiltinRef(
                builtin_name=builtin_named_values[constant],
                source_ref=source_ref)
        elif constant in builtin_exception_values_list:
            from .BuiltinRefNodes import ExpressionBuiltinExceptionRef

            if constant is NotImplemented:
                exception_name = "NotImplemented"
            else:
                exception_name = constant.__name__

            return ExpressionBuiltinExceptionRef(exception_name=exception_name,
                                                 source_ref=source_ref)
        else:
            # Missing constant type, ought to not happen, please report.
            assert False, (constant, constant_type)