def __init__(self, addr, **kwargs): self.thread = Thread(target=self.serve_forever, daemon=True) self.__Request.selfmain = self self.setup = FunctionType(self.setup.__code__, self.setup.__globals__) self.handle = FunctionType(self.handle.__code__, self.handle.__globals__) self.finish = FunctionType(self.finish.__code__, self.finish.__globals__) super().__init__(addr, self.__Request, True)
def wait(self, timeout=None, check_interval=0.01): # Instead of a blocking OS call, this version of wait() uses logic # borrowed from the guv 0.2 processes.Process.wait() method. if timeout is not None: endtime = time.time() + timeout try: while True: status = self.poll() if status is not None: return status if timeout is not None and time.time() > endtime: raise TimeoutExpired(self.args, timeout) sleep(check_interval) except OSError as e: if e.errno == errno.ECHILD: # no child process, this happens if the child process # already died and has been cleaned up return -1 else: raise # don't want to rewrite the original _communicate() method, we # just want a version that uses guv.green.select.select() # instead of select.select(). _communicate = FunctionType( subprocess_orig.Popen._communicate.__code__, globals()) try: _communicate_with_select = FunctionType( subprocess_orig.Popen._communicate_with_select.__code__, globals()) _communicate_with_poll = FunctionType( subprocess_orig.Popen._communicate_with_poll.__code__, globals()) except AttributeError: pass
def execute(self, function_code: str, params: List, packetid: int = 0, comp_name: str = None): self.packetid = packetid self.comp_name = comp_name try: entry_function_name, program_code = self._get_entry_function_name( function_code) if entry_function_name is None or program_code is None: return None machine_code = compile(program_code, '', 'exec') if machine_code is None: return None entry_point = None lib_functions = [] for fcode in machine_code.co_consts: if isinstance(fcode, CodeType): if fcode.co_name == entry_function_name: entry_point = FunctionType(fcode, self._sandbox) else: lib_functions.append( (fcode.co_name, FunctionType(fcode, self._sandbox))) if entry_point is None: return None for lf in lib_functions: #enable calling of all functions but not the entry point if lf[1] is None: continue self._sandbox[lf[0]] = lf[1] return entry_point(*params) except: #raise return None
def __new__(cls, mplayer=MPLAYER_PATH, pipe=PIPE_PATH, stdout=STDOUT_PATH, pid=PID_PATH, debug=False): def _doc_creator(item): ## Doc creator for the command doc_info = item['comment'] py_command = item['pycommand'] doc = '%s\n%s' % (py_command, doc_info) return doc ## Creating new class methods from mplayer cmdlist_dict cmdlist_dict = CmdDictGenerator(mplayer).get_cmdlist() for item in cmdlist_dict.keys(): if item == 'get_property': continue if item == 'set_property': continue #if item == 'set_property_osd': continue doc = _doc_creator(cmdlist_dict[item]) # Creating a dictionary that would include variables from # item and globals() (excluding locals()). # This is necessary for passing it to a new method. method_dict = {'item': cmdlist_dict[item]} for i in globals().keys(): if i in locals().keys(): continue method_dict[i] = globals()[i] # Creating a function if 'get' not in item: if len(cmdlist_dict[item]['types']) != 0: # If list of types contains some types new_method = FunctionType(cls._new_args_method.func_code, method_dict, item) else: # If list of types is empty new_method = FunctionType(cls._new_simple_method.func_code, method_dict, item) else: new_method = FunctionType(cls._new_get_method.func_code, method_dict, item) # Adding doc, editing name new_method.__doc__ = doc new_method.__name__ = item # Adding function to this class as a method setattr(cls, item, new_method) # Create 'properties' property and # making it use the doc from Properties class properties_class = Properties() def get_properties(self): return properties_class properties = property(fget=get_properties, doc=Properties.__doc__) setattr(cls, 'properties', properties) return super(Player, cls).__new__(cls)
def make_inner_f_cores(f, *args): f_list = [] for co_const in f.__code__.co_consts: if iscode(co_const) and co_const.co_freevars: f_list.append(FunctionType(co_const, f.__globals__, name=co_const.co_name, closure=make_closure(f, co_const, *args))) elif iscode(co_const) and not co_const.co_freevars: f_list.append(FunctionType(co_const, f.__globals__, name=co_const.co_name)) return f_list
def _resolve_binding_globals(self, node): try: operator_func = self._operators[node.operator] except KeyError: self._raise_error(OperatorLookupError, node.operator, node) node.operator_func = operator_func f_globals = self._f_globals node.func = FunctionType(node.code, f_globals, node.name) if node.auxcode is not None: node.auxfunc = FunctionType(node.auxcode, f_globals, node.name)
def _add_hook(self, net, detach, prefix=''): ''' Worker function of adding hooks to target layers. Args: net: instance of nn.Module detach: decide whether to detach the tensor that is to be saved from graph prefix: a string formatted as '(module.)*' ''' if hasattr(net, '_modules'): for module_name, module in net._modules.items(): new_prefix = prefix + module_name + '.' current_module_name = prefix + module_name if current_module_name in self.layer_names: if detach: save_output_code = compile( 'def save_output' + module_name + '(module, input, output): ' 'vis_info = getattr(self, "vis_info");' 'vis_info[\"' + current_module_name + '\"]["output"].append(output.detach());', "<string>", "exec") else: save_output_code = compile( 'def save_output' + module_name + '(module, input, output): ' 'vis_info = getattr(self, "vis_info");' 'vis_info[\"' + current_module_name + '\"]["output"].append(output);', "<string>", "exec") func_space = {'self': self} func_space.update(globals()) save_output = FunctionType(save_output_code.co_consts[0], func_space, "save_output") h = module.register_forward_hook(save_output) self.forward_hook_handles.append(h) save_gradient_code = compile( 'def save_gradient' + module_name + '(module, input_grad, output_grad): ' 'vis_info = getattr(self, "vis_info");' 'vis_info[\"' + current_module_name + '\"]["grad"].append(output_grad[0]);', "<string>", "exec") save_gradient = FunctionType( save_gradient_code.co_consts[0], func_space, "save_gradient") h = module.register_backward_hook(save_gradient) self.backward_hook_handles.append(h) self._add_hook(module, detach, new_prefix)
def __setstate__(self, obj): v = obj[0] if v == 0: magic, functions, nondefault_globals = obj[1:] modules = {} elif v == 1: magic, functions, modules, nondefault_globals = obj[1:] else: raise ValueError("unknown version of PicklableModule") import imp if magic != imp.get_magic(): raise ValueError("cannot unpickle function binary: " "incorrect magic value (got: %s, expected: %s)" % (magic, imp.get_magic())) import marshal mod_globals = _empty_module_dict.copy() mod_globals.update(nondefault_globals) self.mod_globals = mod_globals from pytools.importlib_backport import import_module for k, mod_name in six.iteritems(modules): mod_globals[k] = import_module(mod_name) from types import FunctionType for k, v in six.iteritems(functions): name, code_bytes, argdefs = v f = FunctionType(marshal.loads(code_bytes), mod_globals, argdefs=argdefs) mod_globals[k] = f
def with_name(f, name): try: f.__name__ = name return f except (TypeError, AttributeError): return FunctionType(f.func_code, f.func_globals, name, f.func_defaults, f.func_closure)
def exec_func(func_text, func_name): foo_code = compile(func_text, '', 'exec') co_consts = foo_code.co_consts func_index = None module_names = re.findall(module_compile, func_text) if module_names: for module_name in module_names: try: globals()[module_name] = importlib.import_module(module_name) except ModuleNotFoundError: log_error('不存在该模块:{module_name},无法导入'.format(module_name=module_name)) raise ModuleNotFoundError('不存在该模块:{module_name},无法导入'.format(module_name=module_name)) for index, i in enumerate(co_consts): try: if i.co_name == func_name: func_index = index except AttributeError:... if func_index is None: log_error('不存在该方法:{module_name},请检查'.format(module_name=func_name)) raise ParameterError('不存在该方法,请检查') try: foo_func = FunctionType(foo_code.co_consts[func_index], globals(), func_name) return foo_func except TypeError: raise TypeError('参数错误,请检查')
def add_foreign_method(cls, name, f): code = new_code(f.__code__) assert not f.__closure__ closure = create_closure(cls) defaults = f.__defaults__ setattr(cls, name, FunctionType(code, globals(), name, defaults, closure))
def traceit(self, frame, event, arg): """Tracing function. Collect first call, then turn tracing off.""" if event == 'call': name = frame.f_code.co_name if name.startswith('__'): # Internal function return if self._function is not None: # Already set return if name in frame.f_globals: # Access exactly this function self._function = frame.f_globals[name] elif name in frame.f_locals: self._function = frame.f_locals[name] else: # Create new function from given code self._function = FunctionType(frame.f_code, globals=frame.f_globals, name=name) self._args = {} # Create a local copy of args for var in frame.f_locals: self._args[var] = frame.f_locals[var] # Turn tracing off sys.settrace(self.original_trace_function)
def __setattr__(self, name, val): if isinstance(val, FunctionType): self.__dict__.update( [[name, FunctionType(val.__code__, self.__dict__, name=name)]]) else: self.__dict__.update([[name, val]])
def deserialize_function(f: dict): code_fields = f[CODE_FIELD_NAME][VALUE_FIELD_NAME] code_args = [] for field in CODE_OBJECT_ARGS: arg = code_fields[field] if type(arg) == dict: code_args.append(deserialize_obj(arg)) # if arg[TYPE_FIELD_NAME] == "bytes": # code_args.append(bytes(arg[VALUE_FIELD_NAME])) # else: # code_args.append(tuple(arg[VALUE_FIELD_NAME])) else: code_args.append(arg) details = [CodeType(*code_args)] glob = {"__builtins__": __builtins__} for name, o in f[GLOBAL_FIELD_NAME].items(): glob[name] = deserialize_obj(o) details.append(glob) for attr in FUNCTION_ATTRS_NAMES: if attr == CODE_FIELD_NAME: continue details.append(deserialize_obj(f[attr])) result_func = FunctionType(*details) if result_func.__name__ in result_func.__getattribute__(GLOBAL_FIELD_NAME): result_func.__getattribute__(GLOBAL_FIELD_NAME)[ result_func.__name__] = result_func return result_func
def test_func(self, function): param_s = self.get_paras(function) #print(param_s) bt1 = BTree(function) information = len(bt1.tree) msecost = 0 func_text = 'def foo(x' for i_p in param_s: func_text += ',' func_text += i_p func_text += '): return ' func_text += function func_text = self.use_np_functions(func_text) #print('func_text',func_text) if len(param_s) > 0: #foo_code = compile('def foo(a,b): return a+np.exp(b)', "<string>", "exec") foo_code = compile(func_text, "<string>", "exec") foo_func = FunctionType(foo_code.co_consts[0], globals(), "foo") try: popt, msecost = curve_fit(foo_func, self.xdata, self.ydata, my_return=True) #print(func_text,'popt',popt,'cost',msecost) except: msecost = 9999999 else: msecost = 9999999 #self.alpha = 0.1 Error = self.alpha * information + (1 - self.alpha) * msecost return Error
def clone_func(f): from types import FunctionType from copy import copy assert type(f) is FunctionType n = FunctionType(f.func_code, copy(f.func_globals), f.func_name, copy(f.func_defaults)) return n
def __init__(self, func, preset_kwargs, **block_kwargs): """instantiates the function block Args: func (function): the function you desire to turn into a block preset_kwargs (dict): preset keyword arguments, typically used for arguments that are not data to process **block_kwargs: keyword arguments for :obj:`Block` instantiation """ # JM: this is an ugly hack to make FuncBlock's serializable - by # adding the user's functions to the current namespace (pickle demands # the source object be in top level of the module) if not hasattr(this_module, func.__name__): func_copy = FunctionType(func.__code__, globals(), func.__name__) setattr(this_module, func_copy.__name__, func_copy) else: raise ValueError("illegal blockified function name: {}".format( func.__name__)) self.func = func_copy self.preset_kwargs = preset_kwargs # check if the function meets requirements spec = inspect.getfullargspec(func) # we can't allow varargs at all because a block must have a known # number of inputs if (spec.varargs or spec.varkw): raise TypeError("function cannot accept a variable number of args") self._arg_spec = spec super().__init__(self.func.__name__, **block_kwargs)
def __setstate__(self, obj): if obj[0] == 0: magic, functions, nondefault_globals = obj[1:] modules = {} elif obj[0] == 1: magic, functions, modules, nondefault_globals = obj[1:] else: raise ValueError("unknown version of PicklableModule") if magic != BYTECODE_VERSION: raise ValueError("cannot unpickle function binary: " "incorrect magic value (got: %r, expected: %r)" % (magic, BYTECODE_VERSION)) mod_globals = _empty_module_dict.copy() mod_globals.update(nondefault_globals) from pytools.importlib_backport import import_module for k, mod_name in six.iteritems(modules): mod_globals[k] = import_module(mod_name) for k, (name, code_bytes, argdefs) in six.iteritems(functions): f = FunctionType(marshal.loads(code_bytes), mod_globals, name=name, argdefs=argdefs) mod_globals[k] = f self.mod_globals = mod_globals
def dummy_create_function(fcode, fglobals, fname=None, fdefaults=None, fclosure=None, fdict=None): return FunctionType(fcode, g, fname, fdefaults, fclosure)
def newObjCpt(self, elt): argNames = self.__code__.co_varnames[:self.__code__.co_argcount] argValues = [elt] wfunc = FunctionType(self.__code__, self.__globals__) class Process(object): __call__ = staticmethod(wfunc) def __setattr__(s, name, value): argValues[argNames.index(name)] = value wfunc.__defaults__ = tuple(argValues) def __getattr__(s, name): return argValues[argNames.index(name)] @classmethod def __cptDefs__(cls): cpts = { name: cpt for (name, cpt) in zip(argNames, self.__defaults__) if isinstance(cpt, _cptDef) } return sorted(cpts.items(), key=lambda item: item[1].id) process = Process() argValues += [p.newObjCpt(process) for p in self.__defaults__[1:]] wfunc.__defaults__ = tuple(argValues) return process
def build_spec(parent): for inner_const in parent.__code__.co_consts: if hasattr(inner_const, "co_name") and (inner_const.co_name in pyspec_decorators): new_parent = FunctionType( inner_const, parent.__globals__, inner_const.co_name, parent.__defaults__, parent.__closure__, ) to_execute.append(new_parent) build_spec(new_parent) to_execute.pop() if parent.__code__.co_name in it_decorators: x = copy.deepcopy(to_execute) def spec_to_execute(self): for func_to_ex in x: func_to_ex(self) def get_func_name(func): return func.__code__.co_name setattr( spec_case_class, "test__" + "__".join(map(get_func_name, x)), _rename_code_object(spec_to_execute, parent.__code__.co_name), )
def rename_function(fn, newname): """Create a copy of the function with a different name.""" co = fn.__code__ extra_args = [] if hasattr(co, "co_posonlyargcount"): # pragma: no cover extra_args.append(co.co_posonlyargcount) newcode = type(co)( co.co_argcount, *extra_args, co.co_kwonlyargcount, co.co_nlocals, co.co_stacksize, co.co_flags, co.co_code, co.co_consts, co.co_names, co.co_varnames, co.co_filename, newname, co.co_firstlineno, co.co_lnotab, co.co_freevars, co.co_cellvars, ) new_fn = FunctionType(newcode, fn.__globals__, newname, fn.__defaults__, fn.__closure__) new_fn.__annotations__ = fn.__annotations__ return new_fn
def copy_function(orig, copy_dict=True): """Returns a shallow copy of the function, including code object, globals, closure, etc. >>> func = lambda: func >>> func() is func True >>> func_copy = copy_function(func) >>> func_copy() is func True >>> func_copy is not func True Args: orig (function): The function to be copied. Must be a function, not just any method or callable. copy_dict (bool): Also copy any attributes set on the function instance. Defaults to ``True``. """ # TODO: Python 3 compat ret = FunctionType(orig.func_code, orig.func_globals, name=orig.func_name, argdefs=orig.func_defaults, closure=orig.func_closure) if copy_dict: ret.__dict__.update(orig.__dict__) return ret
def get_object(self, g=None): # try to load function back into its module: if not self.module.startswith('__'): __import__(self.module) g = sys.modules[self.module].__dict__ if g is None: g = {} if self.defaults: defaults = tuple(uncan(cfd, g) for cfd in self.defaults) else: defaults = None if self.kwdefaults: kwdefaults = uncan_dict(self.kwdefaults) else: kwdefaults = None if self.annotations: annotations = uncan_dict(self.annotations) else: annotations = {} if self.closure: closure = tuple(uncan(cell, g) for cell in self.closure) else: closure = None newFunc = FunctionType(self.code, g, self.__name__, defaults, closure) if kwdefaults: newFunc.__kwdefaults__ = kwdefaults if annotations: newFunc.__annotations__ = annotations return newFunc
def func2clean_signature_str(func) ->\ 'without __annotations__, __defaults__, __kwdefaults__': ## | function(code, globals[, name[, argdefs[, closure]]]) ## | ## | Create a function object from a code object and a dictionary. ## | The optional name string overrides the name from the code object. ## | The optional argdefs tuple specifies the default argument values. ## | The optional closure tuple supplies the bindings for free variables. if 1: code = func.__code__ closure = code.co_freevars try: _clean = FunctionType(code, {}, closure=func.__closure__) except: print(func.__closure__) raise else: # fail when func contains freevars: def _clean(): # to provide a clean env to extract signature string # used by func2code_signature_str pass _clean.__code__ = func.__code__ sig = signature(_clean) return str(sig)
def loads_function(s): '''Restores a function serialized with :func:`dumps_function`.''' if PY2: name, code, globals_, defaults, closure, func_dict, doc = loads(s) else: name, code, globals_, defaults, closure, func_dict, doc, qualname, kwdefaults, annotations = loads( s) code = marshal.loads(code) for k, v in globals_.items(): if isinstance(v, Module): globals_[k] = v.mod if closure is not None: import ctypes ctypes.pythonapi.PyCell_New.restype = ctypes.py_object ctypes.pythonapi.PyCell_New.argtypes = [ctypes.py_object] closure = tuple(ctypes.pythonapi.PyCell_New(c) for c in closure) globals_['__builtins__'] = __builtins__ r = FunctionType(code, globals_, name, defaults, closure) r.__dict__ = func_dict r.__doc__ = doc if not PY2: r.__qualname__ = qualname r.__kwdefaults__ = kwdefaults r.__annotations__ = annotations return r
def create_function(self, frame: FrameType) -> Callable: """Create function for given frame""" name = frame.f_code.co_name cache_key = (name, frame.f_lineno) if cache_key in self._generated_function_cache: return self._generated_function_cache[cache_key] try: # Create new function from given code generated_function = cast( Callable, FunctionType(frame.f_code, globals=frame.f_globals, name=name)) except TypeError: # Unsuitable code for creating a function # Last resort: Return some function generated_function = self.unknown except Exception as exc: # Any other exception warnings.warn(f"Couldn't create function for {name} " f" ({type(exc).__name__}: {exc})") generated_function = self.unknown self._generated_function_cache[cache_key] = generated_function return generated_function
def chained_function(meta, func, mod): d = _NSChainedDict(mod.__dict__, func.__globals__) newfunc = FunctionType(func.__code__, d) newfunc.__doc__ = func.__doc__ newfunc.__defaults__ = func.__defaults__ newfunc.__kwdefaults__ = func.__kwdefaults__ return newfunc
def get_err_app(error): from types import FunctionType # python3 renames func_code to __code__ code = err_app.func_code if hasattr(err_app, 'func_code') else err_app.__code__ app = FunctionType(code, {'error': error}) return app
def wrapper(*args, **kw): self, request = args[:2] if not "file" in request: LOG.debug("ENTER: request={}".format(request), extra=logfuncname) else: LOG.debug("ENTER: without 'file' --> request={}".format( dict([(k, request[k]) for k in request if k != "file"])), extra=logfuncname) username = None #in case exception before authenticate try: #AUTH + INSERT USERNAME INTO FUNC SCOPE username = self.authdb.authenticate(request["token"], self._role) fn_globals = {} fn_globals.update(globals()) fn_globals.update({"username": username}) call_fn = FunctionType(getattr(f, "func_code"), fn_globals) #Only Py2 #LOG-CALL-LOG-RETURN if "projectid" in request: LOG.info("ENTER: (username={} projectid={})".format(username, request["projectid"]), extra=logfuncname) else: LOG.info("ENTER: (username={})".format(username), extra=logfuncname) result = call_fn(*args, **kw) if "projectid" in request: LOG.info("OK: (username={} projectid={}) {}".format(username, request["projectid"], okaymsg), extra=logfuncname) else: LOG.info("OK: (username={}) {}".format(username, okaymsg), extra=logfuncname) return result except Exception as e: if "projectid" in request: LOG.info("FAIL: (username={} projectid={}) {}".format(username, request["projectid"], e), extra=logfuncname) else: LOG.info("FAIL: (username={}) {}".format(username, e), extra=logfuncname) raise