Esempio n. 1
0
def _generate_init(class_, class_manager):
    """Build an __init__ decorator that triggers ClassManager events."""

    # TODO: we should use the ClassManager's notion of the
    # original '__init__' method, once ClassManager is fixed
    # to always reference that.
    original__init__ = class_.__init__
    assert original__init__

    # Go through some effort here and don't change the user's __init__
    # calling signature, including the unlikely case that it has
    # a return value.
    # FIXME: need to juggle local names to avoid constructor argument
    # clashes.
    func_body = """\
def __init__(%(apply_pos)s):
    new_state = class_manager._new_state_if_none(%(self_arg)s)
    if new_state:
        return new_state.initialize_instance(%(apply_kw)s)
    else:
        return original__init__(%(apply_kw)s)
"""
    func_vars = util.format_argspec_init(original__init__, grouped=False)
    func_text = func_body % func_vars

# start Py3K
    func_defaults = getattr(original__init__, '__defaults__', None)
    func_kw_defaults = getattr(original__init__, '__kwdefaults__', None)
# end Py3K
# start Py2K
#    func = getattr(original__init__, 'im_func', original__init__)
#    func_defaults = getattr(func, 'func_defaults', None)
# end Py2K

    env = locals().copy()
    exec(func_text, env)
    __init__ = env['__init__']
    __init__.__doc__ = original__init__.__doc__
    if func_defaults:
        __init__.__defaults__ = func_defaults
# start Py3K
    if func_kw_defaults:
        __init__.__kwdefaults__ = func_kw_defaults
# end Py3K
    return __init__
Esempio n. 2
0
 def test(fn, wanted):
     if grouped is None:
         parsed = util.format_argspec_init(fn)
     else:
         parsed = util.format_argspec_init(fn, grouped=grouped)
     eq_(parsed, wanted)
Esempio n. 3
0
 def test(fn, wanted):
     if grouped is None:
         parsed = util.format_argspec_init(fn)
     else:
         parsed = util.format_argspec_init(fn, grouped=grouped)
     eq_(parsed, wanted)