(self, func, restype=NOTHING, *argtypes)
The constructor accepts any Python callable object.

        If the callable has parameter and return type annotations, they are used as the block's parameter and return
        types. This allows using :class:`Block` as a decorator:

        .. code-block:: python

            @Block
            def the_block(arg: NSInteger) -> NSUInteger:
                return abs(arg)

        For callables without type annotations, the parameter and return types need to be passed to the :class:`Block`
        constructor in the ``restype`` and ``argtypes`` arguments:

        .. code-block:: python

            the_block = Block(abs, NSUInteger, NSInteger)
The constructor accepts any Python callable object.
The constructor accepts any Python callable object .
def __init__(self, func, restype=NOTHING, *argtypes):
        """The constructor accepts any Python callable object.

        If the callable has parameter and return type annotations, they are used as the block's parameter and return
        types. This allows using :class:`Block` as a decorator:

        .. code-block:: python

            @Block
            def the_block(arg: NSInteger) -> NSUInteger:
                return abs(arg)

        For callables without type annotations, the parameter and return types need to be passed to the :class:`Block`
        constructor in the ``restype`` and ``argtypes`` arguments:

        .. code-block:: python

            the_block = Block(abs, NSUInteger, NSInteger)
        """

        if not callable(func):
            raise TypeError('Blocks must be callable')

        self.func = func

        if restype is NOTHING:
            if argtypes:
                # This can't happen unless the caller does something hacky, but guard against it just in case.
                raise ValueError('Cannot pass argtypes without a restype')

            # No explicit restype/argtypes were passed into the constructor,
            # so try to extract them from the function's type annotations.

            try:
                signature = inspect.signature(func)
            except (TypeError, ValueError):
                raise ValueError(
                    'Could not retrieve function signature information - '
                    'please pass return and argument types directly into Block'
                )

            if signature.return_annotation == inspect.Signature.empty:
                raise ValueError(
                    'Function has no return type annotation - '
                    'please add one, or pass return and argument types directly into Block'
                )

            restype = signature.return_annotation
            argtypes = []
            for name, param in signature.parameters.items():
                if param.annotation == inspect.Parameter.empty:
                    raise ValueError(
                        'Function has no argument type annotation for parameter ' + repr(name)
                        + ' - please add one, or pass return and argument types directly into Block'
                    )

                argtypes.append(param.annotation)

        signature = tuple(ctype_for_type(tp) for tp in argtypes)

        restype = ctype_for_type(restype)

        self.cfunc_type = CFUNCTYPE(restype, c_void_p, *signature)

        self.literal = BlockLiteral()
        self.literal.isa = addressof(_NSConcreteStackBlock)
        self.literal.flags = BlockConsts.HAS_STRET | BlockConsts.HAS_SIGNATURE | BlockConsts.HAS_COPY_DISPOSE
        self.literal.reserved = 0
        self.cfunc_wrapper = self.cfunc_type(self.wrapper)
        self.literal.invoke = cast(self.cfunc_wrapper, c_void_p)
        self.descriptor = BlockDescriptor()
        self.descriptor.reserved = 0
        self.descriptor.size = sizeof(BlockLiteral)

        self.cfunc_copy_helper = _cfunc_type_block_copy(self.copy_helper)
        self.cfunc_dispose_helper = _cfunc_type_block_dispose(self.dispose_helper)
        self.descriptor.copy_helper = self.cfunc_copy_helper
        self.descriptor.dispose_helper = self.cfunc_dispose_helper

        self.descriptor.signature = encoding_for_ctype(restype) + b'@?' + b''.join(
            encoding_for_ctype(arg) for arg in signature
        )
        self.literal.descriptor = cast(byref(self.descriptor), c_void_p)
        self.block = cast(byref(self.literal), objc_block)
        self._as_parameter_ = self.block
def __init__ ( self , func , restype = NOTHING , * argtypes ) : if not callable ( func ) : raise TypeError ( 'Blocks must be callable' ) self . func = func if restype is NOTHING : if argtypes : # This can't happen unless the caller does something hacky, but guard against it just in case. raise ValueError ( 'Cannot pass argtypes without a restype' ) # No explicit restype/argtypes were passed into the constructor, # so try to extract them from the function's type annotations. try : signature = inspect . signature ( func ) except ( TypeError , ValueError ) : raise ValueError ( 'Could not retrieve function signature information - ' 'please pass return and argument types directly into Block' ) if signature . return_annotation == inspect . Signature . empty : raise ValueError ( 'Function has no return type annotation - ' 'please add one, or pass return and argument types directly into Block' ) restype = signature . return_annotation argtypes = [ ] for name , param in signature . parameters . items ( ) : if param . annotation == inspect . Parameter . empty : raise ValueError ( 'Function has no argument type annotation for parameter ' + repr ( name ) + ' - please add one, or pass return and argument types directly into Block' ) argtypes . append ( param . annotation ) signature = tuple ( ctype_for_type ( tp ) for tp in argtypes ) restype = ctype_for_type ( restype ) self . cfunc_type = CFUNCTYPE ( restype , c_void_p , * signature ) self . literal = BlockLiteral ( ) self . literal . isa = addressof ( _NSConcreteStackBlock ) self . literal . flags = BlockConsts . HAS_STRET | BlockConsts . HAS_SIGNATURE | BlockConsts . HAS_COPY_DISPOSE self . literal . reserved = 0 self . cfunc_wrapper = self . cfunc_type ( self . wrapper ) self . literal . invoke = cast ( self . cfunc_wrapper , c_void_p ) self . descriptor = BlockDescriptor ( ) self . descriptor . reserved = 0 self . descriptor . size = sizeof ( BlockLiteral ) self . cfunc_copy_helper = _cfunc_type_block_copy ( self . copy_helper ) self . cfunc_dispose_helper = _cfunc_type_block_dispose ( self . dispose_helper ) self . descriptor . copy_helper = self . cfunc_copy_helper self . descriptor . dispose_helper = self . cfunc_dispose_helper self . descriptor . signature = encoding_for_ctype ( restype ) + b'@?' + b'' . join ( encoding_for_ctype ( arg ) for arg in signature ) self . literal . descriptor = cast ( byref ( self . descriptor ) , c_void_p ) self . block = cast ( byref ( self . literal ) , objc_block ) self . _as_parameter_ = self . block