예제 #1
0
def optimize_module_2(m, globals_consts, bind_builtins=1):
    if not inspect.ismodule(m):
        raise TypeError
    consts_dict = {}
    consts_list = []
    if type(globals_consts) == list or type(globals_consts) == tuple:
        globals = {}
        for i in globals_consts:
            v = m.__dict__.get(i)
            globals[i] = v
    else:
        globals = globals_consts
    if bind_builtins:
        for builtin_name, builtin_value in list(m.__builtins__.items()):
            # this way it is possible to redefine a builtin in globals_consts
            globals.setdefault(builtin_name, builtin_value)
    functions = {}
    for name, f in list(m.__dict__.items()):
        if inspect.isfunction(f):
            functions[name] = f
            analyze_code(f.__code__, globals, consts_dict, consts_list)
    consts_list = tuple(consts_list)
    for name, f in list(functions.items()):
        newcode = rewrite_code(f.__code__, consts_dict, consts_list)
        defaults = f.__defaults__ or ()
        m.__dict__[name] = make_function(newcode, f.__globals__, f.__name__,
                                         defaults)
예제 #2
0
def optimize_module_2(m, globals_consts, bind_builtins=1):
    if not inspect.ismodule(m):
        raise TypeError
    consts_dict = {}
    consts_list = []
    if type(globals_consts) == list or type(globals_consts) == tuple:
        globals = {}
        for i in globals_consts:
            v = m.__dict__.get(i)
            globals[i] = v
    else:
        globals = globals_consts
    if bind_builtins:
        for builtin_name, builtin_value in list(m.__builtins__.items()):
            # this way it is possible to redefine a builtin in globals_consts
            globals.setdefault(builtin_name, builtin_value)
    functions = {}
    for name, f in list(m.__dict__.items()):
        if inspect.isfunction(f):
            functions[name] = f
            analyze_code(f.__code__, globals, consts_dict, consts_list)
    consts_list = tuple(consts_list)
    for name, f in list(functions.items()):
        newcode = rewrite_code(f.__code__, consts_dict, consts_list)
        defaults = f.__defaults__ or ()
        m.__dict__[name] = make_function(newcode, f.__globals__, f.__name__,
                                         defaults)
예제 #3
0
def bind(f, globals):
    """Returns a new function whose code object has been
    bound by bind_code()"""
    newcode = bind_code(f.__code__, globals)
    defaults = f.__defaults__ or ()
    return make_function(newcode, f.__globals__, f.__name__, defaults)
예제 #4
0
def extract_function(func, subfunction_name):
	code = None
	for const in func.func_code.co_consts.__iter__():
		if hasattr(const, 'co_name') and const.co_name == subfunction_name:
			code = const
	return make_function(code, func.func_globals)
예제 #5
0
def bind(f, globals):
    """Returns a new function whose code object has been
    bound by bind_code()"""
    newcode = bind_code(f.func_code, globals)
    defaults = f.func_defaults or ()
    return make_function(newcode, f.func_globals, f.func_name, defaults)
예제 #6
0
def bind(f, globals):
    """Returns a new function whose code object has been
    bound by bind_code()"""
    newcode = bind_code(f.__code__, globals)
    defaults = f.__defaults__ or ()
    return make_function(newcode, f.__globals__, f.__name__, defaults)
예제 #7
0
def bind(f, globals):
    """Returns a new function whose code object has been
    bound by bind_code()"""
    newcode = bind_code(f.func_code, globals)
    defaults = f.func_defaults or ()
    return make_function(newcode, f.func_globals, f.func_name, defaults)