(string, want_obj=False, want_context=False, want_arguments=False)
return f
Works like Argument Clinic (PEP 436), to validate function params.
Works like Argument Clinic (PEP 436), to validate function params.
Works like Argument Clinic ( PEP 436 ) to validate function params .
def argument_clinic(string, want_obj=False, want_context=False, want_arguments=False):
    """
    Works like Argument Clinic (PEP 436), to validate function params.
    """
    clinic_args = []
    allow_kwargs = False
    optional = False
    while string:
        # Optional arguments have to begin with a bracket. And should always be
        # at the end of the arguments. This is therefore not a proper argument
        # clinic implementation. `range()` for exmple allows an optional start
        # value at the beginning.
        match = re.match('(?:(?:(\[),? ?|, ?|)(\w+)|, ?/)\]*', string)
        string = string[len(match.group(0)):]
        if not match.group(2):  # A slash -> allow named arguments
            allow_kwargs = True
            continue
        optional = optional or bool(match.group(1))
        word = match.group(2)
        clinic_args.append((word, optional, allow_kwargs))

    def f(func):
        def wrapper(evaluator, obj, arguments):
            debug.dbg('builtin start %s' % obj, color='MAGENTA')
            result = NO_CONTEXTS
            try:
                lst = list(arguments.eval_argument_clinic(clinic_args))
            except ValueError:
                pass
            else:
                kwargs = {}
                if want_context:
                    kwargs['context'] = arguments.context
                if want_obj:
                    kwargs['obj'] = obj
                if want_arguments:
                    kwargs['arguments'] = arguments
                result = func(evaluator, *lst, **kwargs)
            finally:
                debug.dbg('builtin end: %s', result, color='MAGENTA')
            return result

        return wrapper
    return f
def argument_clinic ( string , want_obj = False , want_context = False , want_arguments = False ) : clinic_args = [ ] allow_kwargs = False optional = False while string : # Optional arguments have to begin with a bracket. And should always be # at the end of the arguments. This is therefore not a proper argument # clinic implementation. `range()` for exmple allows an optional start # value at the beginning. match = re . match ( '(?:(?:(\[),? ?|, ?|)(\w+)|, ?/)\]*' , string ) string = string [ len ( match . group ( 0 ) ) : ] if not match . group ( 2 ) : # A slash -> allow named arguments allow_kwargs = True continue optional = optional or bool ( match . group ( 1 ) ) word = match . group ( 2 ) clinic_args . append ( ( word , optional , allow_kwargs ) ) def f ( func ) : def wrapper ( evaluator , obj , arguments ) : debug . dbg ( 'builtin start %s' % obj , color = 'MAGENTA' ) result = NO_CONTEXTS try : lst = list ( arguments . eval_argument_clinic ( clinic_args ) ) except ValueError : pass else : kwargs = { } if want_context : kwargs [ 'context' ] = arguments . context if want_obj : kwargs [ 'obj' ] = obj if want_arguments : kwargs [ 'arguments' ] = arguments result = func ( evaluator , * lst , * * kwargs ) finally : debug . dbg ( 'builtin end: %s' , result , color = 'MAGENTA' ) return result return wrapper return f