예제 #1
0
    def unwrap(cls, item: object) -> object:
        """
        Converts a value from the plugin domain to the internal domain.
        """
        if item is None:
            return NoneValue()

        if isinstance(item, DynamicProxy):
            return item._get_instance()

        if isinstance(item, list):
            return [cls.unwrap(x) for x in item]

        if isinstance(item, dict):

            def recurse_dict_item(
                    key_value: Tuple[object, object]) -> Tuple[object, object]:
                (key, value) = key_value
                if not isinstance(key, str):
                    raise RuntimeException(
                        None,
                        "dict keys should be strings, got %s of type %s with dict value %s"
                        % (key, type(key), value))
                return (key, cls.unwrap(value))

            return dict(map(recurse_dict_item, item.items()))

        return item
예제 #2
0
def make_none(p: YaccProduction, token: int) -> Literal:
    assert namespace
    none = Literal(NoneValue())
    none.location = Location(file, p.lineno(token))
    none.namespace = namespace
    none.lexpos = p.lexpos(token)
    return none
예제 #3
0
def test_attribute_validate(multi: bool, nullable: bool) -> None:
    entity: Entity = Entity("DummyEntity", Namespace("dummy_namespace"))
    attribute: Attribute = Attribute(entity, Integer(), "my_attribute",
                                     Location("dummy.cf", 1), multi, nullable)

    def validate(value: object, success: bool) -> None:
        if success:
            attribute.validate(value)
        else:
            with pytest.raises(RuntimeException):
                attribute.validate(value)

    validate(42, not multi)
    validate(NoneValue(), nullable)
    validate([0, 1, 2], multi)
    validate([0, 1, NoneValue()], False)
예제 #4
0
def p_attr_list_null(p):
    "attr : attr_type_multi ID '=' NULL"
    (attr, nullable, _) = p[1]
    p[0] = DefineAttribute(attr,
                           p[2],
                           Literal(NoneValue()),
                           True,
                           nullable=nullable)
    attach_lnr(p, 3)
예제 #5
0
    def call_in_context(
        self, args: List[object], kwargs: Dict[str, object], resolver: Resolver, queue: QueueScheduler, result: ResultVariable
    ) -> None:
        no_unknows = self.plugin.check_args(args, kwargs)

        if not no_unknows and not self.plugin.opts["allow_unknown"]:
            result.set_value(Unknown(self), self.ast_node.location)
            return

        if self.plugin._context != -1:
            args.insert(self.plugin._context, plugins.Context(resolver, queue, self.ast_node, self.plugin, result))

        if self.plugin.opts["emits_statements"]:
            self.plugin(*args, **kwargs)
        else:
            try:
                value = self.plugin(*args, **kwargs)
                result.set_value(value if value is not None else NoneValue(), self.ast_node.location)
            except UnknownException as e:
                result.set_value(e.unknown, self.ast_node.location)
            except UnsetException as e:
                call: str = str(self.plugin)
                location: str = str(self.ast_node.location)
                LOGGER.debug(
                    "Unset value in python code in plugin at call: %s (%s) (Will be rescheduled by compiler)", call, location
                )
                # Don't handle it here!
                # This exception is used by the scheduler to re-queue the unit
                # If it is handled here, the re-queueing can not be done,
                # leading to very subtle errors such as #2787
                raise e
            except RuntimeException as e:
                raise WrappingRuntimeException(self.ast_node, "Exception in plugin %s" % self.ast_node.name, e)
            except plugins.PluginException as e:
                raise ExplicitPluginException(self.ast_node, "PluginException in plugin %s" % self.ast_node.name, e)
            except Exception as e:
                raise ExternalException(self.ast_node, "Exception in plugin %s" % self.ast_node.name, e)
예제 #6
0
 def _get_null_value(self) -> object:
     return NoneValue()
예제 #7
0
def test_unwrap_list_dict_recurse():
    assert DynamicProxy.unwrap([{"null": None, "nulls": [None]}]) == [{"null": NoneValue(), "nulls": [NoneValue()]}]
예제 #8
0
def test_unwrap_none():
    assert DynamicProxy.unwrap(None) == NoneValue()
예제 #9
0
def p_constant_none(p):
    """ constant : NULL
    """
    p[0] = Literal(NoneValue())
    attach_lnr(p)
예제 #10
0
def p_attr_list_dict_null(p):
    "attr : DICT '?'  ID '=' NULL"
    p[0] = DefineAttribute("dict", p[3], Literal(NoneValue()), nullable=True)
    attach_lnr(p, 1)