Ejemplo n.º 1
0
 def extract_arguments(self):
     sig = inspect.signature(self.func)
     if sig.return_annotation not in (inspect._empty, None):
         self.return_type = sig.return_annotation
     params = sig.parameters
     arg_names = params.keys()
     for i, arg_name in enumerate(arg_names):
         param = params[arg_name]
         if param.kind == inspect.Parameter.VAR_KEYWORD:
             raise TaichiSyntaxError(
                 'Taichi kernels do not support variable keyword parameters (i.e., **kwargs)'
             )
         if param.kind == inspect.Parameter.VAR_POSITIONAL:
             raise TaichiSyntaxError(
                 'Taichi kernels do not support variable positional parameters (i.e., *args)'
             )
         if param.default is not inspect.Parameter.empty:
             raise TaichiSyntaxError(
                 'Taichi kernels do not support default values for arguments'
             )
         if param.kind == inspect.Parameter.KEYWORD_ONLY:
             raise TaichiSyntaxError(
                 'Taichi kernels do not support keyword parameters')
         if param.kind != inspect.Parameter.POSITIONAL_OR_KEYWORD:
             raise TaichiSyntaxError(
                 'Taichi kernels only support "positional or keyword" parameters'
             )
         annotation = param.annotation
         if param.annotation is inspect.Parameter.empty:
             if i == 0 and self.classkernel:  # The |self| parameter
                 annotation = template()
             else:
                 raise TaichiSyntaxError(
                     'Taichi kernels parameters must be type annotated')
         else:
             if isinstance(
                     annotation,
                 (template, ndarray_type.NdarrayType,
                  texture_type.TextureType, texture_type.RWTextureType)):
                 pass
             elif id(annotation) in primitive_types.type_ids:
                 pass
             elif isinstance(annotation, sparse_matrix_builder):
                 pass
             elif isinstance(annotation, MatrixType):
                 pass
             else:
                 raise TaichiSyntaxError(
                     f'Invalid type annotation (argument {i}) of Taichi kernel: {annotation}'
                 )
         self.arguments.append(
             KernelArgument(annotation, param.name, param.default))
Ejemplo n.º 2
0
 def extract_arguments(self):
     sig = inspect.signature(self.func)
     if sig.return_annotation not in (inspect._empty, None):
         self.return_type = sig.return_annotation
     params = sig.parameters
     arg_names = params.keys()
     for i, arg_name in enumerate(arg_names):
         param = params[arg_name]
         if param.kind == inspect.Parameter.VAR_KEYWORD:
             raise TaichiSyntaxError(
                 'Taichi functions do not support variable keyword parameters (i.e., **kwargs)'
             )
         if param.kind == inspect.Parameter.VAR_POSITIONAL:
             raise TaichiSyntaxError(
                 'Taichi functions do not support variable positional parameters (i.e., *args)'
             )
         if param.kind == inspect.Parameter.KEYWORD_ONLY:
             raise TaichiSyntaxError(
                 'Taichi functions do not support keyword parameters')
         if param.kind != inspect.Parameter.POSITIONAL_OR_KEYWORD:
             raise TaichiSyntaxError(
                 'Taichi functions only support "positional or keyword" parameters'
             )
         annotation = param.annotation
         if annotation is inspect.Parameter.empty:
             if i == 0 and self.classfunc:
                 annotation = template()
             # TODO: pyfunc also need type annotation check when real function is enabled,
             #       but that has to happen at runtime when we know which scope it's called from.
             elif not self.pyfunc and impl.get_runtime(
             ).experimental_real_function:
                 raise TaichiSyntaxError(
                     f'Taichi function `{self.func.__name__}` parameter `{arg_name}` must be type annotated'
                 )
         else:
             if not id(annotation
                       ) in primitive_types.type_ids and not isinstance(
                           annotation, template):
                 raise TaichiSyntaxError(
                     f'Invalid type annotation (argument {i}) of Taichi function: {annotation}'
                 )
         self.argument_annotations.append(annotation)
         self.argument_names.append(param.name)