treecoder
treecoder

Reputation: 45101

Pyramid: how come view callables accept either one or two parameters?

A Pyramid view callable in it's most simple form can be written as:

def myview(request):
    pass

An alternative form is to accept another parameter -- the context:

def myview(context, request):
    pass

How does the Pyramid view lookup machinery know whether view callable accepts a context or not?

Upvotes: 3

Views: 188

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122172

Pyramid inspects the view using the inspect module (specifically the .getargspec() call in the requestonly() function:

def requestonly(view, attr=None):
    ismethod = False
    if attr is None:
        attr = '__call__'
    if inspect.isroutine(view):
        fn = view
    elif inspect.isclass(view):
        try:
            fn = view.__init__
        except AttributeError:
            return False
        ismethod = hasattr(fn, '__call__')
    else:
        try:
            fn = getattr(view, attr)
        except AttributeError:
            return False

    try:
        argspec = inspect.getargspec(fn)
    except TypeError:
        return False

    args = argspec[0]

    if hasattr(fn, im_func) or ismethod:
        # it's an instance method (or unbound method on py2)
        if not args:
            return False
        args = args[1:]
    if not args:
        return False

    if len(args) == 1:
        return True

    defaults = argspec[3]
    if defaults is None:
        defaults = ()

    if args[0] == 'request':
        if len(args) - len(defaults) == 1:
            return True

    return False

The rest of the code then adjusts the code path to omit the context if the view doesn't accept a context.

Upvotes: 4

Related Questions