コード例 #1
0
ファイル: FSPythonScript.py プロジェクト: goschtl/zope
    def _exec(self, bound_names, args, kw):
        """Call a Python Script

        Calling a Python Script is an actual function invocation.
        """
        self._updateFromFS()
        # Prepare the function.
        f = self._v_f

        __traceback_info__ = bound_names, args, kw, self.func_defaults

        if bound_names:
            # Updating func_globals directly is not thread safe here.
            # In normal PythonScripts, every thread has its own
            # copy of the function.  But in FSPythonScripts
            # there is only one copy.  So here's another way.
            new_globals = f.func_globals.copy()
            new_globals.update(bound_names)
            if f.func_defaults:
                f = new.function(f.func_code, new_globals, f.func_name,
                                 f.func_defaults)
            else:
                f = new.function(f.func_code, new_globals, f.func_name)

        # Execute the function in a new security context.
        security=getSecurityManager()
        security.addContext(self)
        try:
            result = apply(f, args, kw)
            return result
        finally:
            security.removeContext(self)
コード例 #2
0
def test_closure(func, closure, exc):
    try:
        new.function(func.func_code, {}, "", None, closure)
    except exc:
        pass
    else:
        print "corrupt closure accepted"
コード例 #3
0
    def _exec(self, bound_names, args, kw):
        """Call a Python Script

        Calling a Python Script is an actual function invocation.
        """
        # do caching
        keyset = None
        if self.ZCacheable_isCachingEnabled():
            # Prepare a cache key.
            keyset = kw.copy()
            asgns = self.getBindingAssignments()
            name_context = asgns.getAssignedName('name_context', None)
            if name_context:
                keyset[name_context] = self.aq_parent.getPhysicalPath()
            name_subpath = asgns.getAssignedName('name_subpath', None)
            if name_subpath:
                keyset[name_subpath] = self._getTraverseSubpath()
            # Note: perhaps we should cache based on name_ns also.
            keyset['*'] = args
            result = self.ZCacheable_get(keywords=keyset, default=_marker)
            if result is not _marker:
                # Got a cached value.
                return result

        # Prepare the function.
        f = self._v_f
        if f is None:
            # The script has errors.
            __traceback_supplement__ = (FSPythonScriptTracebackSupplement,
                                        self, 0)
            raise RuntimeError, '%s has errors.' % self._filepath

        # Updating func_globals directly is not thread safe here.
        # In normal PythonScripts, every thread has its own
        # copy of the function.  But in FSPythonScripts
        # there is only one copy.  So here's another way.
        new_globals = f.func_globals.copy()
        new_globals['__traceback_supplement__'] = (
            FSPythonScriptTracebackSupplement, self)
        new_globals['__file__'] = self._filepath
        new_globals['__name__'] = self.id
        if bound_names:
            new_globals.update(bound_names)
        if f.func_defaults:
            f = new.function(f.func_code, new_globals, f.func_name,
                             f.func_defaults)
        else:
            f = new.function(f.func_code, new_globals, f.func_name)

        # Execute the function in a new security context.
        security = getSecurityManager()
        security.addContext(self)
        try:
            result = f(*args, **kw)
            if keyset is not None:
                # Store the result in the cache.
                self.ZCacheable_set(result, keywords=keyset)
            return result
        finally:
            security.removeContext(self)
コード例 #4
0
def test_closure(func, closure, exc):
    try:
        new.function(func.func_code, {}, "", None, closure)
    except exc:
        pass
    else:
        print "corrupt closure accepted"
コード例 #5
0
    def _exec(self, bound_names, args, kw):
        """Call a Python Script

        Calling a Python Script is an actual function invocation.
        """
        # Prepare the function.
        f = self._v_f
        if f is None:
            # The script has errors.
            raise RuntimeError, '%s has errors.' % self._filepath

        __traceback_info__ = bound_names, args, kw, self.func_defaults

        if bound_names:
            # Updating func_globals directly is not thread safe here.
            # In normal PythonScripts, every thread has its own
            # copy of the function.  But in FSPythonScripts
            # there is only one copy.  So here's another way.
            new_globals = f.func_globals.copy()
            new_globals.update(bound_names)
            if f.func_defaults:
                f = new.function(f.func_code, new_globals, f.func_name,
                                 f.func_defaults)
            else:
                f = new.function(f.func_code, new_globals, f.func_name)

        # Execute the function in a new security context.
        security = getSecurityManager()
        security.addContext(self)
        try:
            result = apply(f, args, kw)
            return result
        finally:
            security.removeContext(self)
コード例 #6
0
ファイル: FSPythonScript.py プロジェクト: goschtl/zope
    def _exec(self, bound_names, args, kw):
        """Call a Python Script

        Calling a Python Script is an actual function invocation.
        """
        # do caching
        keyset = None
        if self.ZCacheable_isCachingEnabled():
            # Prepare a cache key.
            keyset = kw.copy()
            asgns = self.getBindingAssignments()
            name_context = asgns.getAssignedName('name_context', None)
            if name_context:
                keyset[name_context] = self.aq_parent.getPhysicalPath()
            name_subpath = asgns.getAssignedName('name_subpath', None)
            if name_subpath:
                keyset[name_subpath] = self._getTraverseSubpath()
            # Note: perhaps we should cache based on name_ns also.
            keyset['*'] = args
            result = self.ZCacheable_get(keywords=keyset, default=_marker)
            if result is not _marker:
                # Got a cached value.
                return result

        # Prepare the function.
        f = self._v_f
        if f is None:
            # The script has errors.
            __traceback_supplement__ = (
                FSPythonScriptTracebackSupplement, self, 0)
            raise RuntimeError, '%s has errors.' % self._filepath

        # Updating func_globals directly is not thread safe here.
        # In normal PythonScripts, every thread has its own
        # copy of the function.  But in FSPythonScripts
        # there is only one copy.  So here's another way.
        new_globals = f.func_globals.copy()
        new_globals['__traceback_supplement__'] = (
            FSPythonScriptTracebackSupplement, self)
        new_globals['__file__'] = self._filepath
        new_globals['__name__'] = self.id
        if bound_names:
            new_globals.update(bound_names)
        if f.func_defaults:
            f = new.function(f.func_code, new_globals, f.func_name,
                             f.func_defaults)
        else:
            f = new.function(f.func_code, new_globals, f.func_name)

        # Execute the function in a new security context.
        security=getSecurityManager()
        security.addContext(self)
        try:
            result = f(*args, **kw)
            if keyset is not None:
                # Store the result in the cache.
                self.ZCacheable_set(result, keywords=keyset)
            return result
        finally:
            security.removeContext(self)
コード例 #7
0
ファイル: folder.py プロジェクト: jyukopla/collective.flow
def validate(form, code, data):
    # errors
    errors = {}

    # build re-usable restricted function components like in PythonScript
    path = 'undefined.py'
    code, g, defaults = prepare_restricted_function(
        'context, data, errors',
        code,
        'validate',
        path,
        [],
    )

    # update globals
    g = g.copy()
    g['__file__'] = path

    # validate
    new.function(code, g, None, defaults)(form.context, data, errors)

    # set errors
    for name, message in errors.items():

        # resolve field
        widget = None
        try:
            widget = form.widgets[name]
        except KeyError:
            for group in (form.groups or ()):
                try:
                    widget = group.widgets[name]
                    break
                except KeyError:
                    pass
        if widget is None:
            continue

        # set error
        error = Invalid(message)
        snippet = getMultiAdapter(
            (error, form.request, widget, widget.field, form, form.context),
            IErrorViewSnippet,
        )
        snippet.update()
        widget.error = snippet
        errors[name] = snippet

    # return errors
    return errors.values()
コード例 #8
0
    def __init__(self, obj, query=None):
        self.hit = obj[0]
        self.doc = obj[1]
        self.values = dict()

        if len(obj) > 2:
            self.explanation = obj[2]

        self.query = query
        self.index_model = sys.MODELS_REGISTERED.get_index_model(
            self.doc.get(constant.FIELD_NAME_INDEX_MODEL))

        self._meta = self.index_model._meta
        self.meta = self._meta

        # attach local_attrs, except '__unicode__'.
        func_unicode = None
        for i in self.index_model.local_attrs:
            if i.func_name == "__unicode__":
                func_unicode = i
                continue

            setattr(
                self, i.func_name,
                new.instancemethod(
                    new.function(
                        i.im_func.func_code,
                        i.im_func.func_globals,
                        i.func_name,
                    ),
                    self,
                    self.__class__,
                ))

        # attach '__unicode__' method.
        # If index_model does not have '__unicode__' method, use the indexed field, '__unicode__'.
        if func_unicode:
            __im_func = func_unicode.im_func
            func_unicode = new.instancemethod(
                new.function(
                    __im_func.func_code,
                    __im_func.func_globals,
                    "__unicode_index_model__",
                ),
                self,
                self.__class__,
            )
            self.__unicode_index_model__ = func_unicode
コード例 #9
0
ファイル: __init__.py プロジェクト: techdragon/withhacks
 def __exit__(self,*args):
     frame = self._get_context_frame()
     retcode = super(namespace,self).__exit__(*args)
     funcode = copy.deepcopy(self.bytecode)
     #  Ensure it's a properly formed func by always returning something
     funcode.code.append((LOAD_CONST,None))
     funcode.code.append((RETURN_VALUE,None))
     #  Switch LOAD/STORE/DELETE_FAST/NAME to LOAD/STORE/DELETE_ATTR
     to_replace = []
     for (i,(op,arg)) in enumerate(funcode.code):
         repl = self._replace_opcode((op,arg),frame)
         if repl:
             to_replace.append((i,repl))
     offset = 0
     for (i,repl) in to_replace:
         funcode.code[i+offset:i+offset+1] = repl
         offset += len(repl) - 1
     #  Create function object to do the manipulation
     funcode.args = ("_[namespace]",)
     funcode.varargs = False
     funcode.varkwargs = False
     funcode.name = "<withhack>"
     gs = self._get_context_frame().f_globals
     func = new.function(funcode.to_code(),gs)
     #  Execute bytecode in context of namespace
     retval = func(self.namespace)
     if self.as_name is not None:
         self._set_context_locals({self.as_name:self.namespace})
     return retcode
コード例 #10
0
ファイル: compiler.py プロジェクト: AlexBaranosky/clojure-py
def compileMultiFn(comp, name, form):
    s = form
    argdefs = []
    while s is not None:
        argdefs.append(MultiFn(comp, s.first()))
        s = s.next()
    argdefs = sorted(argdefs, lambda x, y: len(x.args) < len(y.args))
    if len(filter(lambda x: x.lastisargs, argdefs)) > 1:
        raise CompilerException("Only one function overload may have variable number of arguments", form)


    code = []
    args = []
    for x in argdefs:
        code.extend(x.code)
        for x in x.args:
            if x not in args:
                args.append(x)

    code.append((LOAD_CONST, Exception))
    code.append((CALL_FUNCTION, 0))
    code.append((RAISE_VARARGS, 1))

    c = Code(code, comp.closureList(), ["__argsv__"], True, False, True, str(Symbol.intern(comp.getNS().__name__, name.name)), "./clj/clojure/core.clj", 0, None)

    fn = new.function(c.to_code(), comp.ns.__dict__, name.name)

    return [(LOAD_CONST, fn)]
コード例 #11
0
ファイル: compiler.py プロジェクト: AlexBaranosky/clojure-py
def compileFn(comp, name, form, orgform):
    locals, args, lastisargs, argsname = unpackArgs(form.first())

    comp.pushLocals(locals)
    if orgform.meta() is not None:
        line = orgform.meta()[LINE_KEY]
    else:
        line = 0
    code = [(SetLineno,line if line is not None else 0)]
    recurlabel = Label("recurLabel")
    recur = {"label": recurlabel,
             "args": args}
    code.append((recurlabel, None))
    comp.pushRecur(recur)
    code.extend(compileImplcitDo(comp, form.next()))
    comp.popRecur()

    code.append((RETURN_VALUE,None))
    comp.popLocals(locals)
    clist = comp.closureList()

    c = Code(code, comp.closureList(), args, lastisargs, False, True, str(Symbol.intern(comp.getNS().__name__, name.name)), "./clj/clojure/core.clj", 0, None)
    if not clist:
        c = new.function(c.to_code(), comp.ns.__dict__, name.name)

    return [(LOAD_CONST, c)]
コード例 #12
0
ファイル: easytree.py プロジェクト: drmingdrmer/py-aluminium
def breadth_first_traversal( _old ):
    
    s = []
    
    def _savenode( *args, **kwargs ):
        s.append( (args,kwargs) )
    
    newglobal = _old.func_globals.copy()
    newglobal[_old.__name__] = _savenode
    
    _oldBF = new.function( _old.func_code, newglobal )
    
    def _new( *args, **kwargs ):
        
        s.append( (args,kwargs) )
        
        while( len(s)!=0 ):
            a, kwa = s.pop(0)
            _oldBF( *a, **kwa )
    
    _new._decorator = _old
    _new.__name__ = _old.__name__
    _new.__doc__  = _old.__doc__
    
    return _new
コード例 #13
0
 def testFilenameAndOffset(self):
     src = u"a=1\nraise RuntimeError"
     globs = {}
     co = compile_function(src,
                           "foo.py",
                           "func_name", ["x", "y", "z"],
                           lineno=100)
     exec co in globs
     f = globs['func_name']
     # re-create the function - so we can bind to different globals
     # Note we need to manually setup __builtins__ for this new globs.
     globs = {
         'g': 'call_globs',
         '__debug__': __debug__,
         "__builtins__": globs["__builtins__"]
     }
     f = new.function(f.func_code, globs, f.func_name)
     try:
         f(1, 2, 3)
         raise AssertionError, "not reached!"
     except RuntimeError:
         # The function line number is 1 frames back. It should point to
         # line 102
         tb = sys.exc_info()[2].tb_next
         self.failUnlessEqual(tb.tb_frame.f_lineno, 102)
         self.failUnlessEqual(tb.tb_frame.f_code.co_filename, "foo.py")
コード例 #14
0
def maybe_bind(func, bindings):
    """Apply expression bindings to arguments, if applicable"""

    if not bindings or not hasattr(func, 'func_code'):
        return func  # no bindings or not a function

    args, varargs, varkw, defaults = inspect.getargspec(func)
    if not args or isinstance(args[0], basestring):
        return func  # no args or first arg isn't a tuple

    for arg in args[0]:
        if not isinstance(arg, basestring):  # nested tuple arg, not a binding
            return func

    for arg in args[0]:
        if arg in bindings:
            for arg in args[0]:
                if arg not in bindings:
                    raise TypeError("Missing binding for %r" % arg)
            break
    else:
        return func  # none of the tuple args are in the binding

    argtuple = Tuple([bindings[arg] for arg in args[0]])

    c = Code.from_spec(func.func_name, args[1:], varargs, varkw)
    f = new.function(c.code(), func.func_globals, func.func_name,
                     func.func_defaults)
    f.func_code = c.code()  # make f's signature match func w/out tuple
    c.return_(call_thru(f, Const(func), [argtuple]))  # call to match that
    f.func_code = c.code()  # now include the actual body
    f.__predicate_bindings__ = bindings, func  # mark for later optimization

    return f
コード例 #15
0
def changed_name_function(f, newname):
    import new
    if matplotlib._python23:
        newf =  new.function(f.func_code, f.func_globals, newname,
                             f.func_defaults, f.func_closure)
    else:
        #if f.func_defaults is None:
        #    argdefs = ()
        #else:
        argdefs = f.func_defaults
        newf =  new.function(f.func_code, f.func_globals, newname,
                             argdefs)

    print f.func_defaults
    newf.__doc__ = f.__doc__
    return newf
コード例 #16
0
def RegisterConfig(name, *args):
    def y():
        pass

    source = """def __init__(self, a):
                        print "ACAAAAAAAAAA" """

    
    #compiled_code = compile(source, name, "single")

    code = compile(source, name, "single")

    nlocals = 10

    compiled_code = types.CodeType(1, nlocals, code.co_stacksize, code.co_flags,
    code.co_code, code.co_consts, code.co_names,
    code.co_varnames, code.co_filename,
    code.co_name,
    code.co_firstlineno, code.co_lnotab,
    code.co_freevars,
    code.co_cellvars)
    #compiled_code = y.func_code
    f = new.function(compiled_code, globals(), "init")
     
    print f("a")

    clase = new.classobj(name, (), {})

    im = new.instancemethod(f, name, ())

    setattr(clase, "__init__", im)    
    
    return clase
コード例 #17
0
ファイル: tests.py プロジェクト: davelab6/html2markdown
def __makeTest(testName, html, text):
    testName = "test%s" % (testName,)
    testLambda = lambda self: self._testHTML2Markdown(html, text)
    testFunction = new.function(testLambda.func_code, {}, testName,
                                closure=testLambda.func_closure)
    setattr(HTML2MarkdownTests, testName,
            new.instancemethod(testFunction, None, HTML2MarkdownTests))
コード例 #18
0
ファイル: decorators.py プロジェクト: VDOMBoxGroup/runtime2.0
def uncover(function):
    name = function.func_name
    code = function.func_code

    name = name[1:] if name[0] == "_" else name
    arguments = tuple((name[1:] if name[0] == "_" else name)
        for name in code.co_varnames[:code.co_argcount])

    new_code = new.code(
        code.co_argcount,
        code.co_nlocals,
        code.co_stacksize,
        code.co_flags,
        code.co_code,
        code.co_consts,
        code.co_names,
        arguments + code.co_names,
        code.co_filename,
        code.co_name,
        code.co_firstlineno,
        code.co_lnotab)

    print function.func_closure

    new_function = new.function(
        new_code,
        function.func_globals,
        name,
        function.func_defaults,
        function.func_closure)

    return new_function
コード例 #19
0
ファイル: java_parser.py プロジェクト: openGDA/diamond-jython
def setup_a_rules_to_p_funcs(ns):
    rules = []
    for name, val in ns.items():
        if name.startswith('a_'):
            name = name[2:]
            rule, action_maker = val
            # call all pre before all make_action, so that synth classes are
            # pre-defined (in pre methods) and can be used freely by make_actions
            action_maker.pre(name)
            rules.append((name, rule, action_maker))

    for name, rule, action_maker in rules:
        action = action_maker.make_action()
        name = 'p_%s' % name
        # cannot set __name__ on a function and spark uses func.__name__ to gather rule name so
        ns[name] = new.function(action.func_code, action.func_globals, name,
                                action.func_defaults, action.func_closure)
        if hasattr(action, '_spec'):  # copy _spec
            ns[name]._spec = action._spec
        if hasattr(action, 'making'):  # copy making
            ns[name].making = action.making
        ns[name].__doc__ = rule

    # add preprocessing, produced p_funcs expect a spec argument
    # wrap them appropriately
    def preprocess(self, rule, func):
        if hasattr(func, '_spec') and func._spec is not None:
            spec = func._spec
        else:
            spec = rule[1]
        return rule, lambda args: func(spec, args)

    ns['preprocess'] = preprocess
コード例 #20
0
ファイル: test_objspace.py プロジェクト: Debug-Orz/Sypy
    def test_callmethod_opcode(self):
        """ Tests code generated by pypy-c compiled with CALL_METHOD
        bytecode
        """
        self.patch_opcodes('CALL_METHOD', 'LOOKUP_METHOD')
        try:
            class X:
                def m(self):
                    return 3

            def f():
                x = X()
                return x.m()

            # this code is generated by pypy-c when compiling above f
            pypy_code = 't\x00\x00\x83\x00\x00}\x00\x00|\x00\x00\xc9\x01\x00\xca\x00\x00S'
            new_c = self.monkey_patch_code(f.func_code, 3, 3, pypy_code, ('X', 'x', 'm'), ('x',))
            f2 = new.function(new_c, locals(), 'f')

            graph = self.codetest(f2)
            all_ops = self.all_operations(graph)
            assert all_ops['simple_call'] == 2
            assert all_ops['getattr'] == 1
        finally:
            self.unpatch_opcodes('CALL_METHOD', 'LOOKUP_METHOD')
コード例 #21
0
ファイル: compat.py プロジェクト: Frihet/sqlalchemy-patches
def _function_named(fn, newname):
    try:
        fn.__name__ = newname
    except:
        fn = new.function(fn.func_code, fn.func_globals, newname,
                          fn.func_defaults, fn.func_closure)
    return fn
コード例 #22
0
ファイル: compiler.py プロジェクト: redalastor/clojure-py
def compileMultiFn(comp, name, form):
    s = form
    argdefs = []

    while s is not None:
        argdefs.append(MultiFn(comp, s.first()))
        s = s.next()
    argdefs = sorted(argdefs, lambda x, y: len(x.args) < len(y.args))
    if len(filter(lambda x: x.lastisargs, argdefs)) > 1:
        raise CompilerException("Only one function overload may have variable number of arguments", form)

    code = []
    if len(argdefs) == 1 and not argdefs[0].lastisargs:
        hasvararg = False
        argslist = argdefs[0].args
        code.extend(argdefs[0].bodycode)
    else:
        hasvararg = True
        argslist = ["__argsv__"]
        for x in argdefs:
            code.extend(x.argcode)
            code.extend(x.bodycode)

        code.append((LOAD_CONST, Exception))
        code.append((CALL_FUNCTION, 0))
        code.append((RAISE_VARARGS, 1))

    clist = map(lambda x: x.sym.name, comp.closureList())
    code = expandMetas(code, comp)
    c = Code(code, clist, argslist, hasvararg, False, True, str(symbol(comp.getNS().__name__, name.name)), comp.filename, 0, None)
    if not clist:
        c = new.function(c.to_code(), comp.ns.__dict__, name.name)
    return [(LOAD_CONST, c)], c
コード例 #23
0
ファイル: test_objspace.py プロジェクト: purepython/pypy
    def test_callmethod_opcode(self):
        """ Tests code generated by pypy-c compiled with CALL_METHOD
        bytecode
        """
        flow_meth_names = flowcontext.FlowSpaceFrame.opcode_method_names
        pyframe_meth_names = PyFrame.opcode_method_names
        for name in ['CALL_METHOD', 'LOOKUP_METHOD']:
            num = bytecode_spec.opmap[name]
            locals()['old_' + name] = flow_meth_names[num]
            flow_meth_names[num] = pyframe_meth_names[num]
        try:
            class X:
                def m(self):
                    return 3

            def f():
                x = X()
                return x.m()

            # this code is generated by pypy-c when compiling above f
            pypy_code = 't\x00\x00\x83\x00\x00}\x00\x00|\x00\x00\xc9\x01\x00\xca\x00\x00S'
            new_c = self.monkey_patch_code(f.func_code, 3, 3, pypy_code, ('X', 'x', 'm'), ('x',))
            f2 = new.function(new_c, locals(), 'f')

            graph = self.codetest(f2)
            all_ops = self.all_operations(graph)
            assert all_ops['simple_call'] == 2
            assert all_ops['getattr'] == 1
        finally:
            for name in ['CALL_METHOD', 'LOOKUP_METHOD']:
                num = bytecode_spec.opmap[name]
                flow_meth_names[num] = locals()['old_' + name]
コード例 #24
0
def maybe_bind(func, bindings):
    """Apply expression bindings to arguments, if applicable"""

    if not bindings or not hasattr(func, 'func_code'):
        return func     # no bindings or not a function

    args, varargs, varkw, defaults = inspect.getargspec(func) 
    if not args or isinstance(args[0], basestring):
        return func # no args or first arg isn't a tuple

    for arg in args[0]:
        if not isinstance(arg, basestring):  # nested tuple arg, not a binding
            return func

    for arg in args[0]:
        if arg in bindings:
            for arg in args[0]:
                if arg not in bindings:
                    raise TypeError("Missing binding for %r" % arg)
            break
    else:
        return func     # none of the tuple args are in the binding

    argtuple = Tuple([bindings[arg] for arg in args[0]])

    c = Code.from_spec(func.func_name, args[1:], varargs, varkw)
    f = new.function(
        c.code(), func.func_globals, func.func_name, func.func_defaults
    )
    f.func_code = c.code()  # make f's signature match func w/out tuple
    c.return_(call_thru(f, Const(func), [argtuple]))    # call to match that
    f.func_code = c.code()  # now include the actual body
    f.__predicate_bindings__ = bindings, func   # mark for later optimization

    return f
コード例 #25
0
ファイル: Scanner.py プロジェクト: maxywb/OPPpy
    def makeDeclFuncts(self, keyList, classNames, classes):
        functs = dict()
        for key in keyList:

            className = classNames.get(key)
            functName = className + "Const"

            functStr = (
                "\
def "
                + functName
                + "(scanner,token):\
node= "
                + className
                + '(); node.type = "'
                + className
                + '"; node.value = token; return node'
            )
            functCo = compile(functStr, "", "exec")
            ns = {}
            exec functCo in ns
            funct = new.function(ns[functName].func_code, globals(), functName)
            globals()[functName] = funct
            functs[key] = functName

        return functs
コード例 #26
0
def mergeFunctionMetadata(f, g):
    """
	Overwrite C{g}'s name and docstring with values from C{f}.  Update
	C{g}'s instance dictionary with C{f}'s.

	To use this function safely you must use the return value. In Python 2.3,
	L{mergeFunctionMetadata} will create a new function. In later versions of
	Python, C{g} will be mutated and returned.

	@return: A function that has C{g}'s behavior and metadata merged from
		C{f}.
	"""
    try:
        g.__name__ = f.__name__
    except TypeError:
        try:
            merged = new.function(g.func_code, g.func_globals, f.__name__,
                                  inspect.getargspec(g)[-1], g.func_closure)
        except TypeError:
            pass
    else:
        merged = g
    try:
        merged.__doc__ = f.__doc__
    except (TypeError, AttributeError):
        pass
    try:
        merged.__dict__.update(g.__dict__)
        merged.__dict__.update(f.__dict__)
    except (TypeError, AttributeError):
        pass
    merged.__module__ = f.__module__
    return merged
コード例 #27
0
ファイル: compiler.py プロジェクト: redalastor/clojure-py
def compileFn(comp, name, form, orgform):
    locals, args, lastisargs, argsname = unpackArgs(form.first())

    for x in locals:
        comp.pushAlias(x, FnArgument(x))

    if orgform.meta() is not None:
        line = orgform.meta()[LINE_KEY]
    else:
        line = 0
    code = [(SetLineno,line if line is not None else 0)]
    if lastisargs:
        code.extend(cleanRest(argsname.name))

    recurlabel = Label("recurLabel")

    recur = {"label": recurlabel,
    "args": map(lambda x: comp.getAlias(symbol(x)).compileSet(comp), args)}

    code.append((recurlabel, None))
    comp.pushRecur(recur)
    code.extend(compileImplcitDo(comp, form.next()))
    comp.popRecur()
    code.append((RETURN_VALUE,None))
    comp.popAliases(locals)

    clist = map(lambda x: x.sym.name, comp.closureList())
    code = expandMetas(code, comp)
    c = Code(code, clist, args, lastisargs, False, True, str(symbol(comp.getNS().__name__, name.name)), comp.filename, 0, None)
    if not clist:
        c = new.function(c.to_code(), comp.ns.__dict__, name.name)

    return [(LOAD_CONST, c)], c
コード例 #28
0
ファイル: test_objspace.py プロジェクト: gorakhargosh/pypy
    def test_callmethod_opcode(self):
        """ Tests code generated by pypy-c compiled with CALL_METHOD
        bytecode
        """
        flow_meth_names = flowcontext.FlowSpaceFrame.opcode_method_names
        pyframe_meth_names = PyFrame.opcode_method_names
        for name in ['CALL_METHOD', 'LOOKUP_METHOD']:
            num = bytecode_spec.opmap[name]
            locals()['old_' + name] = flow_meth_names[num]
            flow_meth_names[num] = pyframe_meth_names[num]
        try:
            class X:
                def m(self):
                    return 3

            def f():
                x = X()
                return x.m()

            # this code is generated by pypy-c when compiling above f
            pypy_code = 't\x00\x00\x83\x00\x00}\x00\x00|\x00\x00\xc9\x01\x00\xca\x00\x00S'
            new_c = self.monkey_patch_code(f.func_code, 3, 3, pypy_code, ('X', 'x', 'm'), ('x',))
            f2 = new.function(new_c, locals(), 'f')

            graph = self.codetest(f2)
            all_ops = self.all_operations(graph)
            assert all_ops['simple_call'] == 2
            assert all_ops['getattr'] == 1
        finally:
            for name in ['CALL_METHOD', 'LOOKUP_METHOD']:
                num = bytecode_spec.opmap[name]
                flow_meth_names[num] = locals()['old_' + name]
コード例 #29
0
ファイル: _util.py プロジェクト: jml/deferred
def mergeFunctionMetadata(f, g):
    """
    Overwrite C{g}'s name and docstring with values from C{f}.  Update
    C{g}'s instance dictionary with C{f}'s.

    To use this function safely you must use the return value. In Python 2.3,
    L{mergeFunctionMetadata} will create a new function. In later versions of
    Python, C{g} will be mutated and returned.

    @return: A function that has C{g}'s behavior and metadata merged from
        C{f}.
    """
    try:
        g.__name__ = f.__name__
    except TypeError:
        try:
            merged = new.function(
                g.func_code, g.func_globals,
                f.__name__, inspect.getargspec(g)[-1],
                g.func_closure)
        except TypeError:
            pass
    else:
        merged = g
    try:
        merged.__doc__ = f.__doc__
    except (TypeError, AttributeError):
        pass
    try:
        merged.__dict__.update(g.__dict__)
        merged.__dict__.update(f.__dict__)
    except (TypeError, AttributeError):
        pass
    merged.__module__ = f.__module__
    return merged
コード例 #30
0
ファイル: test_objspace.py プロジェクト: nipengadmaster/pypy
    def test_build_list_from_arg_opcode(self):
        """ Tests code generated by pypy-c compiled with BUILD_LIST_FROM_ARG
        bytecode
        """
        if sys.version_info < (2, 7):
            py.test.skip("2.7 only test")
        self.patch_opcodes('BUILD_LIST_FROM_ARG')
        try:

            def f():
                return [i for i in "abc"]

            # this code is generated by pypy-c when compiling above f
            pypy_code = 'd\x01\x00\xcb\x00\x00D]\x0c\x00}\x00\x00|\x00\x00^\x02\x00q\x07\x00S'
            new_c = self.monkey_patch_code(f.func_code, 3, 67, pypy_code, (),
                                           ('i', ))
            f2 = new.function(new_c, locals(), 'f')

            graph = self.codetest(f2)
            all_ops = self.all_operations(graph)
            assert all_ops == {
                'newlist': 1,
                'getattr': 1,
                'simple_call': 1,
                'iter': 1,
                'next': 1
            }
        finally:
            self.unpatch_opcodes('BUILD_LIST_FROM_ARG')
コード例 #31
0
ファイル: test_objspace.py プロジェクト: nipengadmaster/pypy
    def test_callmethod_opcode(self):
        """ Tests code generated by pypy-c compiled with CALL_METHOD
        bytecode
        """
        self.patch_opcodes('CALL_METHOD', 'LOOKUP_METHOD')
        try:

            class X:
                def m(self):
                    return 3

            def f():
                x = X()
                return x.m()

            # this code is generated by pypy-c when compiling above f
            pypy_code = 't\x00\x00\x83\x00\x00}\x00\x00|\x00\x00\xc9\x01\x00\xca\x00\x00S'
            new_c = self.monkey_patch_code(f.func_code, 3, 3, pypy_code,
                                           ('X', 'x', 'm'), ('x', ))
            f2 = new.function(new_c, locals(), 'f')

            graph = self.codetest(f2)
            all_ops = self.all_operations(graph)
            assert all_ops['simple_call'] == 2
            assert all_ops['getattr'] == 1
        finally:
            self.unpatch_opcodes('CALL_METHOD', 'LOOKUP_METHOD')
コード例 #32
0
    def __init__(self, target, deep_decoration = True):
        """
        Init a decorator. "deep_decoration" activates a wrapping of the
        original methods. If true, direct calls to the inner object methods
        will go through all decorators. This is especially sensible, when the
        inner object calls methods of its own.
        """

        # the only datamember of a decorator
        self.__dict__['decoratee']  = target
        self.__dict__['dec_par'] = dict()

        # this is automatically forwarded to the inner decoratee
        target._outermost_decorator = self

        if deep_decoration and not isinstance(target, CRDecorator):
            # make the inner object decorator-aware
            # the mechanism is the same as if @decorator_sensitive would be
            # applied to each of the inner objects methods.

            # in some cases needed
            target._inner_decoratee = target

            # get methods
            members = getmembers(target)
            methods = [m for m in members if ismethod(m[1])]
            for m in methods:

                # wrap methods properly with decorator_sensitive(...)
                m_func = function(m[1].func_code, m[1].func_globals)
                m_func = decorator_sensitive(m_func)
                m_func = instancemethod(m_func, target)

                # do the monkey-'wrap'
                setattr(target, m[0], m_func)
コード例 #33
0
def setup_a_rules_to_p_funcs(ns):
    rules = []
    for name,val in ns.items():
        if name.startswith('a_'):
            name = name[2:]
            rule,action_maker = val
            # call all pre before all make_action, so that synth classes are
            # pre-defined (in pre methods) and can be used freely by make_actions
            action_maker.pre(name)
            rules.append((name,rule,action_maker))
            
    for name,rule,action_maker in rules:
        action = action_maker.make_action()
        name = 'p_%s' % name
        # cannot set __name__ on a function and spark uses func.__name__ to gather rule name so
        ns[name] = new.function(action.func_code,action.func_globals,name,action.func_defaults,action.func_closure)
        if hasattr(action,'_spec'): # copy _spec
            ns[name]._spec = action._spec
        if hasattr(action,'making'): # copy making
            ns[name].making = action.making
        ns[name].__doc__ = rule

    # add preprocessing, produced p_funcs expect a spec argument
    # wrap them appropriately
    def preprocess(self,rule,func):
        if hasattr(func,'_spec') and func._spec is not None:
            spec = func._spec
        else:
            spec = rule[1]
        return rule,lambda args: func(spec,args)

    ns['preprocess'] = preprocess
コード例 #34
0
    def __getattr__(self, name):
        blacklist = ['im_func', 'func_code', 'index_html']
        if name.startswith('_') or name in blacklist:
            raise AttributeError(name)

        # Check if there is views/<self.__name__>.<name>.py in the theme, if not raise  # noqa
        currentTheme = getCurrentTheme()
        if currentTheme is None:
            raise AttributeError(name)

        themeDirectory = queryResourceDirectory(THEME_RESOURCE_NAME, currentTheme)  # noqa
        if themeDirectory is None:
            raise AttributeError(name)

        script = None

        scriptPath = "%s/%s.py" % (FRAGMENTS_DIRECTORY, self.__name__)
        if themeDirectory.isFile(scriptPath):
            script = themeDirectory.readFile(scriptPath)
            if 'def {0:}(self'.format(name) in script:
                script += '\n\nreturn {0:s}(self)'.format(name)
            else:
                script = None

        scriptPath = "%s/%s.%s.py" % (FRAGMENTS_DIRECTORY, self.__name__, name)
        if script is None and themeDirectory.isFile(scriptPath):
            script = themeDirectory.readFile(scriptPath)

        if script is None:
            raise AttributeError(name)

        # Set the default PythonScript bindings as globals
        script_globals = {
            'script': self,
            'context': self.context,
            'container': Acquisition.aq_parent(self.context),
            'traverse_subpath': ''
        }

        # Build re-usable restricted function components like in PythonScript
        try:
            code, g, defaults = prepare_restricted_function(
                'self,*args,**kwargs',
                script or 'pass',
                name,
                scriptPath,
                script_globals.keys()
            )
        except SyntaxError:
            raise AttributeError(name)

        # Update globals
        g = g.copy()
        g.update(script_globals)
        g['__file__'] = scriptPath
        func = new.function(code, g, None, defaults)

        # Return the func as instancemethod
        return types.MethodType(func, self)
コード例 #35
0
def __makeTest(testName, html, text):
    testName = "test%s" % (testName, )
    testLambda = lambda self: self._testHTML2Markdown(html, text)
    testFunction = new.function(testLambda.func_code, {},
                                testName,
                                closure=testLambda.func_closure)
    setattr(HTML2MarkdownTests, testName,
            new.instancemethod(testFunction, None, HTML2MarkdownTests))
コード例 #36
0
ファイル: serialize.py プロジェクト: qingfeng/dpark
def load_func((flag, bytes)):
    if flag == 1:
        return cPickle.loads(bytes)
    code, glob, name, defaults, closure = marshal.loads(bytes)
    glob = dict((k, load_object(v)) for k, v in glob.items())
    glob["__builtins__"] = __builtins__
    closure = closure and reconstruct_closure([load_object(c) for c in closure]) or None
    return new.function(code, glob, name, defaults, closure)
コード例 #37
0
ファイル: python.py プロジェクト: bcorfman/opioid2d
    def __call__(self, func):
        retvalue = self.opts.get("retvalue", None)
        if retvalue is opitypes.VectorType:
            retvalue = opitypes.VectorValue
        fargs = inspect.getargs(func.func_code)[0]
        args = [VarRef(arg, typ) for arg, typ in zip(fargs, self.types)]
        code = func.func_code
        names = code.co_names
        dct = {}
        dct.update(func.func_globals)
        for n in names:
            if n in opiexpr.__all__:
                dct[n] = getattr(opiexpr, n)
            elif n not in func.func_globals:
                dct[n] = VarRef(n)

        dct["Return"] = opiexpr.Return(retvalue)

        newfunc = new.function(code,
                               dct,
                               func.func_name,
                               closure=func.func_closure)
        #args = dict((arg,VarRef(arg,typ)) for arg,typ in zip(args,self.types))
        statements = newfunc(*args)
        ops = []
        for a in reversed(args):
            ops.append(
                opcode("setvar" + a._type.varcode, a._name, None,
                       -a._type.stacksize))
        for s in statements:
            try:
                ops.extend(s._get_stmt_ops())
            except:
                t, v, tb = sys.exc_info()
                for l in traceback.format_list(s._tb):
                    sys.stderr.write(l)
                for l in traceback.format_exception_only(t, v):
                    sys.stderr.write(l)
                tb = None
                raise
                return
        if not ops or ops[-1][0] != "exit":
            if retvalue is not None:
                ops.extend(opiexpr._get_ops(retvalue.default))
            ops.append(opcode("exit"))
        cc = CompilerFrame()
        cc.stack = sum(a._type.stacksize for a in args)
        cc.max_stack = cc.stack
        cc.debugout = self.opts.get("debugout", False)
        for op in ops:
            cc.compile(op)
        if cc.debugout:
            print "max. stack usage: %i bytes" % cc.max_stack
            print cc.__dict__
        code = O2DCode(cc)
        code.retvalue = retvalue
        code.argspec = self.types
        return code
コード例 #38
0
ファイル: __init__.py プロジェクト: cephdon/meta-core
def get_lapack_funcs(names, arrays=(), debug=0, force_clapack=1):
    """Return available LAPACK function objects with names.
    arrays are used to determine the optimal prefix of
    LAPACK routines.
    If force_clapack is True then available Atlas routine
    is returned for column major storaged arrays with
    rowmajor argument set to False.
    """
    force_clapack = 0  # XXX: Don't set it true! The feature is unreliable
    #     and may cause incorrect results.
    #     See test_basic.test_solve.check_20Feb04_bug.

    ordering = []
    for i in range(len(arrays)):
        t = arrays[i].dtype.char
        if t not in _type_conv:
            t = 'd'
        ordering.append((t, i))
    if ordering:
        ordering.sort()
        required_prefix = _type_conv[ordering[0][0]]
    else:
        required_prefix = 'd'
    dtypechar = _inv_type_conv[required_prefix]
    # Default lookup:
    if ordering and arrays[ordering[0][1]].flags['FORTRAN']:
        # prefer Fortran code for leading array with column major order
        m1, m2 = flapack, clapack
    else:
        # in all other cases, C code is preferred
        m1, m2 = clapack, flapack
    if not _use_force_clapack:
        force_clapack = 0
    funcs = []
    m1_name = m1.__name__.split('.')[-1]
    m2_name = m2.__name__.split('.')[-1]
    for name in names:
        func_name = required_prefix + name
        func = getattr(m1, func_name, None)
        if func is None:
            func = getattr(m2, func_name)
            func.module_name = m2_name
        else:
            func.module_name = m1_name
            if force_clapack and m1 is flapack:
                func2 = getattr(m2, func_name, None)
                if func2 is not None:
                    import new
                    func_code = None  # defined in exec
                    exec(_colmajor_func_template % {'func_name': func_name})
                    func = new.function(func_code, {'clapack_func': func2},
                                        func_name)
                    func.module_name = m2_name
                    func.__doc__ = func2.__doc__
        func.prefix = required_prefix
        func.dtypechar = dtypechar
        funcs.append(func)
    return tuple(funcs)
コード例 #39
0
def load_func((flag, bytes)):
    if flag == 1:
        return cPickle.loads(bytes)
    code, glob, name, defaults, closure = marshal.loads(bytes)
    glob = dict((k, load_object(v)) for k, v in glob.items())
    glob['__builtins__'] = __builtins__
    closure = closure and reconstruct_closure(
        [load_object(c) for c in closure]) or None
    return new.function(code, glob, name, defaults, closure)
コード例 #40
0
ファイル: resolver.py プロジェクト: goldmine-dms/server
def get(method, user):
    fn = Resolver().resolve(method)
    debug("Internal resolve to function '%s'" % (method), module="api-resolve-internal")
    if isinstance(fn, apimethod):
        return fn.as_user(user)
    else:
        glob = fn.func_globals.copy()
        glob.update({"user": user})
        return new.function(fn.func_code, glob, argdefs=fn.func_defaults)
コード例 #41
0
ファイル: __init__.py プロジェクト: hitej/meta-core
def get_lapack_funcs(names,arrays=(),debug=0,force_clapack=1):
    """Return available LAPACK function objects with names.
    arrays are used to determine the optimal prefix of
    LAPACK routines.
    If force_clapack is True then available Atlas routine
    is returned for column major storaged arrays with
    rowmajor argument set to False.
    """
    force_clapack = 0  # XXX: Don't set it true! The feature is unreliable
                     #     and may cause incorrect results.
                     #     See test_basic.test_solve.check_20Feb04_bug.

    ordering = []
    for i in range(len(arrays)):
        t = arrays[i].dtype.char
        if t not in _type_conv:
            t = 'd'
        ordering.append((t,i))
    if ordering:
        ordering.sort()
        required_prefix = _type_conv[ordering[0][0]]
    else:
        required_prefix = 'd'
    dtypechar = _inv_type_conv[required_prefix]
    # Default lookup:
    if ordering and arrays[ordering[0][1]].flags['FORTRAN']:
        # prefer Fortran code for leading array with column major order
        m1,m2 = flapack,clapack
    else:
        # in all other cases, C code is preferred
        m1,m2 = clapack,flapack
    if not _use_force_clapack:
        force_clapack = 0
    funcs = []
    m1_name = m1.__name__.split('.')[-1]
    m2_name = m2.__name__.split('.')[-1]
    for name in names:
        func_name = required_prefix + name
        func = getattr(m1,func_name,None)
        if func is None:
            func = getattr(m2,func_name)
            func.module_name = m2_name
        else:
            func.module_name = m1_name
            if force_clapack and m1 is flapack:
                func2 = getattr(m2,func_name,None)
                if func2 is not None:
                    import new
                    func_code = None   # defined in exec
                    exec(_colmajor_func_template % {'func_name':func_name})
                    func = new.function(func_code,{'clapack_func':func2},func_name)
                    func.module_name = m2_name
                    func.__doc__ = func2.__doc__
        func.prefix = required_prefix
        func.dtypechar = dtypechar
        funcs.append(func)
    return tuple(funcs)
コード例 #42
0
ファイル: sourcetools.py プロジェクト: xx312022850/pypy
 def func_with_new_name(func, newname):
     """Make a renamed copy of a function."""
     f = new.function(func.func_code, func.func_globals,
                         newname, func.func_defaults,
                         func.func_closure)
     if func.func_dict: 
         f.func_dict = {}
         f.func_dict.update(func.func_dict) 
     return f 
コード例 #43
0
ファイル: assembler.py プロジェクト: hanitawfik/Pill
def with_name(f, name):
    try:
        f.__name__=name
        return f
    except (TypeError,AttributeError):
        return function(
            getattr(f,CODE), getattr(f,GLOBALS), name, getattr(f,DEFAULTS),
            getattr(f,CLOSURE)
        )
コード例 #44
0
ファイル: __init__.py プロジェクト: abed-hawa/amara
    def compile(self, name, args=None, docstring=None, filename=None, firstlineno=0):
        # Generate the code object
        if args is None:
            args = ("context",)
        if filename is None:
            filename = "<ast-%d>" % xpathcompiler._nasts
            xpathcompiler._nasts += 1
        code = self._graph.assemble(name, args, docstring, filename, firstlineno)
        # Make the function
        if 0:
            title = "%s (%s)" % (filename, name)
            print "--", title, "-" * (60 - len(title))
            print ">>", docstring
            import dis

            dis.dis(code)
            return new.function(code, {"__lltrace__": 1})
        return new.function(code, {})
コード例 #45
0
 def func(self):
     """Return a true Python function based on this function/code object"""
     c = self.code()
     f = new.function(c, self.func_globals, self.func_name,
                      self.func_defaults or ())
     f.func_dict = self.func_dict
     f.func_doc = self.func_doc
     #f.func_closure = self.func_closure
     return f
コード例 #46
0
ファイル: utils.py プロジェクト: safvan010/123
 def _build_function(self, name):
     code_args = self.code_arguments[:]
     code_args[9] = name
     # code_args[8] = <modulename>
     codeobj = new.code(*code_args)
     return new.function(codeobj, {
         '__funcname__': name,
         '__builtins__': __builtins__
     }, name)
コード例 #47
0
ファイル: recipe-577283.py プロジェクト: kaestnja/pystrict3
def persistent_locals(f):
    """Function decorator to expose local variables after execution.

    Modify the function such that, at the exit of the function
    (regular exit or exceptions), the local dictionary is copied to a
    read-only function property 'locals'.

    This decorator wraps the function in a callable object, and
    modifies its bytecode by adding an external try...finally
    statement equivalent to the following:

    def f(self, *args, **kwargs):
        try:
            ... old code ...
        finally:
            self._locals = locals().copy()
            del self._locals['self']
    """

    # ### disassemble f
    f_code = bp.Code.from_code(f.func_code)

    # ### use bytecode injection to add try...finally statement around code
    finally_label = bp.Label()
    # try:
    code_before = (bp.SETUP_FINALLY, finally_label)
    #     [original code here]
    # finally:
    code_after = [
        (finally_label, None),
        # self._locals = locals().copy()
        (bp.LOAD_GLOBAL, 'locals'),
        (bp.CALL_FUNCTION, 0),
        (bp.LOAD_ATTR, 'copy'),
        (bp.CALL_FUNCTION, 0),
        (bp.LOAD_FAST, 'self'),
        (bp.STORE_ATTR, '_locals'),
        #   del self._locals['self']
        (bp.LOAD_FAST, 'self'),
        (bp.LOAD_ATTR, '_locals'),
        (bp.LOAD_CONST, 'self'),
        (bp.DELETE_SUBSCR, None),
        (bp.END_FINALLY, None),
        (bp.LOAD_CONST, None),
        (bp.RETURN_VALUE, None)
    ]

    f_code.code.insert(0, code_before)
    f_code.code.extend(code_after)

    # ### re-assemble
    f_code.args = ('self', ) + f_code.args
    func = new.function(f_code.to_code(), f.func_globals, f.func_name,
                        f.func_defaults, f.func_closure)

    return PersistentLocalsFunction(func)
コード例 #48
0
    def __getattr__(self, name):
        blacklist = ['im_func', 'func_code', 'index_html']
        if name.startswith('_') or name in blacklist:
            raise AttributeError(name)

        # Check if there is views/<self.__name__>.<name>.py in the theme, if not raise  # noqa
        currentTheme = getCurrentTheme()
        if currentTheme is None:
            raise AttributeError(name)

        themeDirectory = queryResourceDirectory(THEME_RESOURCE_NAME,
                                                currentTheme)  # noqa
        if themeDirectory is None:
            raise AttributeError(name)

        script = None

        scriptPath = "%s/%s.py" % (FRAGMENTS_DIRECTORY, self.__name__)
        if themeDirectory.isFile(scriptPath):
            script = themeDirectory.readFile(scriptPath)
            if 'def {0:}(self'.format(name) in script:
                script += '\n\nreturn {0:s}(self)'.format(name)
            else:
                script = None

        scriptPath = "%s/%s.%s.py" % (FRAGMENTS_DIRECTORY, self.__name__, name)
        if script is None and themeDirectory.isFile(scriptPath):
            script = themeDirectory.readFile(scriptPath)

        if script is None:
            raise AttributeError(name)

        # Set the default PythonScript bindings as globals
        script_globals = {
            'script': self,
            'context': self.context,
            'container': Acquisition.aq_parent(self.context),
            'traverse_subpath': ''
        }

        # Build re-usable restricted function components like in PythonScript
        try:
            code, g, defaults = prepare_restricted_function(
                'self,*args,**kwargs', script or 'pass', name, scriptPath,
                script_globals.keys())
        except SyntaxError:
            raise AttributeError(name)

        # Update globals
        g = g.copy()
        g.update(script_globals)
        g['__file__'] = scriptPath
        func = new.function(code, g, None, defaults)

        # Return the func as instancemethod
        return types.MethodType(func, self)
コード例 #49
0
def get_cell_value(cell):
    def make_closure_that_returns_value(use_this_value):
        def closure_that_returns_value():
            return use_this_value
        return closure_that_returns_value
    dummy_function = make_closure_that_returns_value(0)
    dummy_function_code = dummy_function.func_code
    our_function = new.function(dummy_function_code, {}, None, None, (cell,))
    value_from_cell = our_function()
    return value_from_cell
コード例 #50
0
ファイル: __init__.py プロジェクト: mredar/amara
 def compile(self, name, args=None, docstring=None, filename=None,
             firstlineno=0):
     # Generate the code object
     if args is None:
         args = ('context',)
     if filename is None:
         filename = '<ast-%d>' % xpathcompiler._nasts
         xpathcompiler._nasts += 1
     code = self._graph.assemble(name, args, docstring, filename,
                                 firstlineno)
     # Make the function
     if 0:
         title = '%s (%s)' % (filename, name)
         print '--', title, '-'*(60 - len(title))
         print '>>', docstring
         import dis
         dis.dis(code)
         return new.function(code, {'__lltrace__': 1})
     return new.function(code, {})
コード例 #51
0
ファイル: Debug.py プロジェクト: alejandrohomsp/Lima
 def real_fn(*arg, **kw):
     sys.exc_clear()
     fn_globals = dict(fn.func_globals)
     deb_obj = DebObj(deb_params, fn.func_name, '', filename, lineno)
     fn_globals['deb'] = deb_obj
     if deb_container is not None:
         deb_container.add(deb_obj)
     new_fn = new.function(fn.func_code, fn_globals, fn.func_name,
                           fn.func_defaults)
     return new_fn(*arg, **kw)
コード例 #52
0
ファイル: Debug.py プロジェクト: BUSSIERES/Lima
 def real_fn(*arg, **kw):
     sys.exc_clear()
     fn_globals = dict(fn.func_globals)
     deb_obj = DebObj(deb_params, fn.func_name, '', filename, lineno)
     fn_globals['deb'] = deb_obj
     if deb_container is not None:
         deb_container.add(deb_obj)
     new_fn = new.function(fn.func_code, fn_globals, fn.func_name,
                           fn.func_defaults)
     return new_fn(*arg, **kw)
コード例 #53
0
ファイル: DynamicFunctions.py プロジェクト: maxywb/OPPpy
def funct(a):
    strfunc = "\
def foo(): \
x = "+str(a)+"; return type(x)"
    co = compile ( strfunc, '', 'exec' )
    
    ns = {}
    exec co in ns
    nfunc = new.function(ns["foo"].func_code, globals(), 'foo' )
    globals()['foo'] = nfunc
コード例 #54
0
ファイル: macroit.py プロジェクト: MaxMorais/Code2Case
def get_cell_value(cell):
    def make_closure_that_returns_value(use_this_value):
        def closure_that_returns_value():
            return use_this_value
        return closure_that_returns_value
    dummy_function = make_closure_that_returns_value(0)
    dummy_function_code = dummy_function.func_code
    our_function = new.function(dummy_function_code, {}, None, None, (cell,))
    value_from_cell = our_function()
    return value_from_cell
コード例 #55
0
    def closure(func, locs = {}):
        """
        Returns a function whose global namespace is a **copy of** *func*'s 
        globals, augmented with the local variables of the calling function.
        This is a common requirement for functional programming. This can
        be duplicated in standard Python using default arguments, but it
        is generally more cumbersome. Note there is little reason for this
        function under Python 2.1, which provides this capability directly.

        Example:

        #A function which returns a function which returns 1 if an item is
        #in *sequence*, 0 otherwise
        def contained(sequence):    
            def contained_helper(x):
                return x in sequence
            return closure(contained_helper)

        >>>lst = [1,2,3]
        >>>is123 = contained(lst)
        >>>is123(2)
        1
        >>>


        #An example of 'static' variables, somewhat like in C:
        def bindCallCount(func):
            callCount = ref(0)
            return closure(func)

        #Note this function uses a variable, callCount, that is not defined.
        def printAndIncrement():
            print callCount.val
            callCount.val = callCount.val + 1

        #But with closures, we can make this function work:
        >>>printAndInc = bindCallCount(printAndIncrement)
        >>>printAndInc()
        0
        >>>printAndInc()
        1    
        >>>

        So callCount is preserved across function invocations, but is not taking
        up space in the module namespace.
        """
        frame = getStackFrame().f_back
        if not locs:
            locs = frame.f_locals
        globs = {}
        globs.update(func.func_globals)
        globs.update(locs)
        newfunc = new.function(func.func_code, globs)
        globs[func.__name__] = newfunc
        return newfunc
コード例 #56
0
def fake_import(fake_module):
    module_name = 'martiantest.fake.' + fake_module.__name__
    module = new.module(module_name)
    module_name_parts = module_name.split('.')
    module.__file__ =  '/' + '/'.join(module_name_parts)
    
    glob = {}
    for name in dir(fake_module):
        if name.startswith('__') and '.' not in name:
            continue
        obj = getattr(fake_module, name)
        glob[name] = obj
        try:
            obj = obj.im_func
        except AttributeError:
            pass
        __module__ = None
        try:
            __module__ == obj.__dict__.get('__module__')
        except AttributeError:
            try:
                __module__ = obj.__module__
            except AttributeError:
                pass
        if __module__ is None or __module__ == '__builtin__':
            try:
                obj.__module__ = module.__name__
            except AttributeError:
                pass
        setattr(module, name, obj)

    # provide correct globals for functions
    for name in dir(module):
        if name.startswith('__'):
            continue
        obj = getattr(module, name)
        try:
            code = obj.func_code
            new_func = new.function(code, glob, name)
            new_func.__module__ = module.__name__
            setattr(module, name, new_func)
            glob[name] = new_func
        except AttributeError:
            pass

    if not 'martiantest' in sys.modules:
        sys.modules['martiantest'] = new.module('martiantest')
        sys.modules['martiantest.fake'] = new.module('martiantest.fake')
        sys.modules['martiantest'].fake = sys.modules['martiantest.fake']

    sys.modules[module_name] = module
    setattr(sys.modules['martiantest.fake'], module_name.split('.')[-1],
            module)
    
    return module
コード例 #57
0
ファイル: indigo.py プロジェクト: mojca/indigo
 def _make_wrapper_func (wrapper, func):
   """Return wrapper function with changed name 
   """    
   name = func.__name__ + "_wrapper"
   c = wrapper.func_code
   newcode = new.code( c.co_argcount, c.co_nlocals, c.co_stacksize,
                       c.co_flags, c.co_code, c.co_consts, c.co_names,
                       c.co_varnames, "indigo core", name, 1, c.co_lnotab, c.co_freevars, c.co_cellvars )
              
   new_wrapper = new.function(newcode, globals(), name=name, closure=wrapper.func_closure, argdefs=wrapper.func_defaults)
   return new_wrapper