__init__.py 848 Bytes
Newer Older
1
"""cubicweb-bootstrap application package
Adrien Di Mascio's avatar
Adrien Di Mascio committed
2
3
4


"""
5
6
import inspect

7
8
from cubicweb.__pkginfo__ import numversion as cubicweb_version

9
CW_323 = cubicweb_version[:2] >= (3, 23)
10
11
CW_325 = cubicweb_version[:2] >= (3, 25)

12
13
14

def monkeypatch_default_value(func, arg, value):
    # work on the underlying function object if func is a method, that's
15
    # where '__defaults__' is actually stored.
16
    if inspect.ismethod(func):
17
        func = func.__func__
18
19
20
21
    argspec = inspect.getargspec(func)
    # ArgSpec.args contains regular and named parameters, only keep the latter
    named_args = argspec.args[-len(argspec.defaults):]
    idx = named_args.index(arg)
22
23
24
    # generate and inject a new '__defaults__' tuple with the new default value
    new_defaults = func.__defaults__[:idx] + (value,) + func.__defaults__[idx + 1:]
    func.__defaults__ = new_defaults