def makeMethod(self, klass, field, mode, methodName): name = field.getName() method = None if mode == "r": method = generatedAccessorWrapper(name) elif mode == "m": method = generatedEditAccessorWrapper(name) elif mode == "w": # the generatedMutator doesn't actually mutate, but calls a # translation mutator on all translations, including self. method = generatedMutatorWrapper(name) elif mode == "t": # The translation mutator that changes data method = generatedTranslationMutatorWrapper(name) else: raise GeneratorError("""Unhandled mode for method creation: %s:%s -> %s:%s""" % (klass.__name__, name, methodName, mode)) # Zope security requires all security protected methods to have a # function name. It uses this name to determine which roles are allowed # to access the method. # This code is renaming the internal name from e.g. generatedAccessor # to methodName. method = function( method.func_code, method.func_globals, methodName, method.func_defaults, method.func_closure, ) method._lp_generated = True # Note that we generated this method method._lp_generated_by = klass.__name__ if mode == 'w': # The method to delegate to method._lp_mutator = self.computeMethodName(field, 't') setattr(klass, methodName, method)
def renamed(f, name): try: f.__name__ = name except TypeError: # 2.3 doesn't allow renames f = function( f.func_code, f.func_globals, name, f.func_defaults, f.func_closure ) return f
def copyfunction(template, funcchanges, codechanges): names = [ "globals", "name", "defaults", "closure", ] values = [ funcchanges.get(name, getattr(template, "__" + name + "__")) for name in names ] return function(copycode(template.__code__, codechanges), *values)
def copyfunction(func, name = None): if isinstance(func, function) or isinstance(func, method): if not name: name = func.func_name tempfunc = function(func.func_code, func.func_globals, name, func.func_defaults, func.func_closure) tempfunc.func_doc = None return tempfunc names = tuple(o.__class__.__name__ for o in (func, name)) raise TypeError("Cannot copy function = %s with name = %s" %names)
def makeMethod(self, klass, field, mode, methodName): name = field.getName() method = None if mode == "r": def generatedAccessor(self, **kw): """Default Accessor.""" if 'schema' in kw: schema = kw['schema'] else: schema = self.Schema() kw['schema'] = schema return schema[name].get(self, **kw) method = generatedAccessor elif mode == "m": def generatedEditAccessor(self, **kw): """Default Edit Accessor.""" if 'schema' in kw: schema = kw['schema'] else: schema = self.Schema() kw['schema'] = schema return schema[name].getRaw(self, **kw) method = generatedEditAccessor elif mode == "w": def generatedMutator(self, value, **kw): """Default Mutator.""" if 'schema' in kw: schema = kw['schema'] else: schema = self.Schema() kw['schema'] = schema return schema[name].set(self, value, **kw) method = generatedMutator else: raise GeneratorError("""Unhandled mode for method creation: %s:%s -> %s:%s""" % (klass.__name__, name, methodName, mode)) # Zope security requires all security protected methods to have a # function name. It uses this name to determine which roles are allowed # to access the method. # This code is renaming the internal name from e.g. generatedAccessor to # methodName. method = function( method.func_code, method.func_globals, methodName, method.func_defaults, method.func_closure, ) setattr(klass, methodName, method)
def makeMethod(self, klass, field, mode, methodName): name = field.getName() method = None if mode == "r": def generatedAccessor(self, **kw): """Default Accessor.""" if kw.has_key('schema'): schema = kw['schema'] else: schema = self.Schema() kw['schema'] = schema return schema[name].get(self, **kw) method = generatedAccessor elif mode == "m": def generatedEditAccessor(self, **kw): """Default Edit Accessor.""" if kw.has_key('schema'): schema = kw['schema'] else: schema = self.Schema() kw['schema'] = schema return schema[name].getRaw(self, **kw) method = generatedEditAccessor elif mode == "w": def generatedMutator(self, value, **kw): """Default Mutator.""" if kw.has_key('schema'): schema = kw['schema'] else: schema = self.Schema() kw['schema'] = schema return schema[name].set(self, value, **kw) method = generatedMutator else: raise GeneratorError("""Unhandled mode for method creation: %s:%s -> %s:%s""" %(klass.__name__, name, methodName, mode)) # Zope security requires all security protected methods to have a # function name. It uses this name to determine which roles are allowed # to access the method. # This code is renaming the internal name from e.g. generatedAccessor to # methodName. method = function(method.func_code, method.func_globals, methodName, method.func_defaults, method.func_closure, ) setattr(klass, methodName, method)
def makeFunction(obj, env=None, p=None): from syntax_expand import expandObj if scheme.debug.debug_settings['jit-crash-on-error']: expandObj(obj) c = write_code(obj, env, p) else: try: expandObj(obj) c = write_code(obj, env, p) except: return obj if not c: return obj if scheme.debug.debug_settings['jit']: import dis print obj.ast dis.dis(c) return function(c, p.env if p else Globals)
def makeMethod(self, klass, field, mode, methodName): name = field.getName() method = None if mode == "r": method = generatedAccessorWrapper(name) elif mode == "m": method = generatedEditAccessorWrapper(name) elif mode == "w": # the generatedMutator doesn't actually mutate, but calls a # translation mutator on all translations, including self. method = generatedMutatorWrapper(name) elif mode == "t": # The translation mutator that changes data method = generatedTranslationMutatorWrapper(name) else: raise GeneratorError("""Unhandled mode for method creation: %s:%s -> %s:%s""" %(klass.__name__, name, methodName, mode)) # Zope security requires all security protected methods to have a # function name. It uses this name to determine which roles are allowed # to access the method. # This code is renaming the internal name from e.g. generatedAccessor # to methodName. method = function(method.func_code, method.func_globals, methodName, method.func_defaults, method.func_closure, ) method._lp_generated = True # Note that we generated this method method._lp_generated_by = klass.__name__ if mode == 'w': # The method to delegate to method._lp_mutator = self.computeMethodName(field, 't') setattr(klass, methodName, method)
def _load_function(pickled_func, globals): """recreates a serialized function""" code_info, func_info, doc = pickle.loads(pickled_func) func = function(code(*code_info), globals, *func_info) func.func_doc = doc return func
def makeMethod(self, klass, field, mode, methodName): name = field.getName() method = None if mode == "r": def generatedAccessor(self, **kw): """Default Accessor.""" if kw.has_key('schema'): schema = kw['schema'] else: schema = self.Schema() kw['schema'] = schema return getattr(schema, name, None) and \ schema[name].get(self, **kw) method = generatedAccessor elif mode == "m": def generatedEditAccessor(self, **kw): """Default Edit Accessor.""" if kw.has_key('schema'): schema = kw['schema'] else: schema = self.Schema() kw['schema'] = schema return schema[name].getRaw(self, **kw) method = generatedEditAccessor elif mode == "w": # the generatedMutator doesn't actually mutate, but calls a # translation mutator on all translations, including self. def generatedMutator(self, value, **kw): """Default Mutator.""" if kw.has_key('schema'): schema = kw['schema'] else: schema = self.Schema() kw['schema'] = schema # translationMethodName is always present, as it is set in the generator translationMethodName = getattr(getattr(self, schema[name].mutator, None), '_lp_mutator', None) if translationMethodName is None: # Houston, we have a problem return schema[name].set(self, value, **kw) # Instead of additional classgen magic, we check the language independent if not schema[name].languageIndependent: return getattr(self, translationMethodName)(value, **kw) # Look up the actual mutator and delegate to it. translations = [t[0] for t in \ hasattr(self, 'getTranslations') and \ self.getTranslations().values() or []] # reverse to return the result of the canonical mutator translations.reverse() res = None for t in translations: res = getattr(t, translationMethodName)(value, **kw) return res method = generatedMutator elif mode == "t": # The translation mutator that changes data def generatedTranslationMutator(self, value, **kw): """Delegated Mutator.""" if kw.has_key('schema'): schema = kw['schema'] else: schema = self.Schema() kw['schema'] = schema return schema[name].set(self, value, **kw) method = generatedTranslationMutator else: raise GeneratorError("""Unhandled mode for method creation: %s:%s -> %s:%s""" %(klass.__name__, name, methodName, mode)) # Zope security requires all security protected methods to have a # function name. It uses this name to determine which roles are allowed # to access the method. # This code is renaming the internal name from e.g. generatedAccessor to # methodName. method = function(method.func_code, method.func_globals, methodName, method.func_defaults, method.func_closure, ) method._lp_generated = True # Note that we generated this method method._lp_generated_by = klass.__name__ if mode == 'w': # The method to delegate to method._lp_mutator = self.computeMethodName(field, 't') setattr(klass, methodName, method)
def makeMethod(self, klass, field, mode, methodName): name = field.getName() method = None if mode == "r": def generatedAccessor(self, **kw): """Default Accessor.""" if kw.has_key('schema'): schema = kw['schema'] else: schema = self.Schema() kw['schema'] = schema return getattr(schema, name, None) and \ schema[name].get(self, **kw) method = generatedAccessor elif mode == "m": def generatedEditAccessor(self, **kw): """Default Edit Accessor.""" if kw.has_key('schema'): schema = kw['schema'] else: schema = self.Schema() kw['schema'] = schema return schema[name].getRaw(self, **kw) method = generatedEditAccessor elif mode == "w": # the generatedMutator doesn't actually mutate, but calls a # translation mutator on all translations, including self. def generatedMutator(self, value, **kw): """Default Mutator.""" if kw.has_key('schema'): schema = kw['schema'] else: schema = self.Schema() kw['schema'] = schema # translationMethodName is always present, as it is set in the generator translationMethodName = getattr( getattr(self, schema[name].mutator, None), '_lp_mutator', None) if translationMethodName is None: # Houston, we have a problem return schema[name].set(self, value, **kw) # Instead of additional classgen magic, we check the language independent if not schema[name].languageIndependent: return getattr(self, translationMethodName)(value, **kw) # Look up the actual mutator and delegate to it. translations = [t[0] for t in \ hasattr(self, 'getTranslations') and \ self.getTranslations().values() or []] # reverse to return the result of the canonical mutator translations.reverse() res = None for t in translations: res = getattr(t, translationMethodName)(value, **kw) return res method = generatedMutator elif mode == "t": # The translation mutator that changes data def generatedTranslationMutator(self, value, **kw): """Delegated Mutator.""" if kw.has_key('schema'): schema = kw['schema'] else: schema = self.Schema() kw['schema'] = schema return schema[name].set(self, value, **kw) method = generatedTranslationMutator else: raise GeneratorError("""Unhandled mode for method creation: %s:%s -> %s:%s""" % (klass.__name__, name, methodName, mode)) # Zope security requires all security protected methods to have a # function name. It uses this name to determine which roles are allowed # to access the method. # This code is renaming the internal name from e.g. generatedAccessor to # methodName. method = function( method.func_code, method.func_globals, methodName, method.func_defaults, method.func_closure, ) method._lp_generated = True # Note that we generated this method method._lp_generated_by = klass.__name__ if mode == 'w': # The method to delegate to method._lp_mutator = self.computeMethodName(field, 't') setattr(klass, methodName, method)