Python new.function() Examples

The following are 30 code examples of new.function(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module new , or try the search function .
Example #1
Source File: util.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
def spewer(frame, s, ignored):
    """A trace function for sys.settrace that prints every function or method call."""
    from twisted.python import reflect
    if frame.f_locals.has_key('self'):
        se = frame.f_locals['self']
        if hasattr(se, '__class__'):
            k = reflect.qual(se.__class__)
        else:
            k = reflect.qual(type(se))
        print 'method %s of %s at %s' % (
            frame.f_code.co_name, k, id(se)
        )
    else:
        print 'function %s in %s, line %s' % (
            frame.f_code.co_name,
            frame.f_code.co_filename,
            frame.f_lineno) 
Example #2
Source File: test_java_integration.py    From CTFCrackTools with GNU General Public License v3.0 6 votes vote down vote up
def test_java_serialization_pyfunction(self):
        # Not directly supported due to lack of general utility
        # (globals will usually be in the function object in
        # func_globals), and problems with unserialization
        # vulnerabilities. Users can always subclass from PyFunction
        # for specific cases, as seen in PyCascading
        import new
        def f():
            return 6 * 7 + max(0, 1, 2)
        # However, using the new module, it's possible to create a
        # function with no globals, which means the globals will come
        # from the current context
        g = new.function(f.func_code, {}, "g")
        # But still forbid Java deserialization of this function
        # object. Use pickling or other support instead.
        with self.assertRaises(UnsupportedOperationException):
            roundtrip_serialization(g) 
Example #3
Source File: test_java_integration.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
def test_java_serialization_pyfunction(self):
        # Not directly supported due to lack of general utility
        # (globals will usually be in the function object in
        # func_globals), and problems with unserialization
        # vulnerabilities. Users can always subclass from PyFunction
        # for specific cases, as seen in PyCascading
        import new
        def f():
            return 6 * 7 + max(0, 1, 2)
        # However, using the new module, it's possible to create a
        # function with no globals, which means the globals will come
        # from the current context
        g = new.function(f.func_code, {}, "g")
        # But still forbid Java deserialization of this function
        # object. Use pickling or other support instead.
        with self.assertRaises(UnsupportedOperationException):
            roundtrip_serialization(g) 
Example #4
Source File: util.py    From BitTorrent with GNU General Public License v3.0 6 votes vote down vote up
def mergeFunctionMetadata(f, g):
    """
    Overwrite C{g}'s docstring and name with values from C{f}.  Update
    C{g}'s instance dictionary with C{f}'s.
    """
    try:
        g.__doc__ = f.__doc__
    except (TypeError, AttributeError):
        pass
    try:
        g.__dict__.update(f.__dict__)
    except (TypeError, AttributeError):
        pass
    try:
        g.__name__ = f.__name__
    except TypeError:
        try:
            g = new.function(
                g.func_code, g.func_globals,
                f.__name__, inspect.getargspec(g)[-1],
                g.func_closure)
        except TypeError:
            pass
    return g 
Example #5
Source File: util.py    From BitTorrent with GNU General Public License v3.0 6 votes vote down vote up
def spewer(frame, s, ignored):
    """A trace function for sys.settrace that prints every function or method call."""
    from twisted.python import reflect
    if frame.f_locals.has_key('self'):
        se = frame.f_locals['self']
        if hasattr(se, '__class__'):
            k = reflect.qual(se.__class__)
        else:
            k = reflect.qual(type(se))
        print 'method %s of %s at %s' % (
            frame.f_code.co_name, k, id(se)
        )
    else:
        print 'function %s in %s, line %s' % (
            frame.f_code.co_name,
            frame.f_code.co_filename,
            frame.f_lineno) 
Example #6
Source File: util.py    From BitTorrent with GNU General Public License v3.0 6 votes vote down vote up
def makeStatBar(width, maxPosition, doneChar = '=', undoneChar = '-', currentChar = '>'):
    """Creates a function that will return a string representing a progress bar.
    """
    aValue = width / float(maxPosition)
    def statBar(position, force = 0, last = ['']):
        assert len(last) == 1, "Don't mess with the last parameter."
        done = int(aValue * position)
        toDo = width - done - 2
        result = "[%s%s%s]" % (doneChar * done, currentChar, undoneChar * toDo)
        if force:
            last[0] = result
            return result
        if result == last[0]:
            return ''
        last[0] = result
        return result

    statBar.__doc__ = """statBar(position, force = 0) -> '[%s%s%s]'-style progress bar

    returned string is %d characters long, and the range goes from 0..%d.
    The 'position' argument is where the '%s' will be drawn.  If force is false,
    '' will be returned instead if the resulting progress bar is identical to the
    previously returned progress bar.
""" % (doneChar * 3, currentChar, undoneChar * 3, width, maxPosition, currentChar)
    return statBar 
Example #7
Source File: cloudpickle.py    From spark-cluster-deployment with Apache License 2.0 6 votes vote down vote up
def _make_skel_func(code, num_closures, base_globals = None):
    """ Creates a skeleton function object that contains just the provided
        code and the correct number of cells in func_closure.  All other
        func attributes (e.g. func_globals) are empty.
    """
    #build closure (cells):
    if not ctypes:
        raise Exception('ctypes failed to import; cannot build function')

    cellnew = ctypes.pythonapi.PyCell_New
    cellnew.restype = ctypes.py_object
    cellnew.argtypes = (ctypes.py_object,)
    dummy_closure = tuple(map(lambda i: cellnew(None), range(num_closures)))

    if base_globals is None:
        base_globals = {}
    base_globals['__builtins__'] = __builtins__

    return types.FunctionType(code, base_globals,
                              None, None, dummy_closure)

# this piece of opaque code is needed below to modify 'cell' contents 
Example #8
Source File: cloudpickle.py    From spark-cluster-deployment with Apache License 2.0 6 votes vote down vote up
def _modules_to_main(modList):
    """Force every module in modList to be placed into main"""
    if not modList:
        return

    main = sys.modules['__main__']
    for modname in modList:
        if type(modname) is str:
            try:
                mod = __import__(modname)
            except Exception, i: #catch all...
                sys.stderr.write('warning: could not import %s\n.  Your function may unexpectedly error due to this import failing; \
A version mismatch is likely.  Specific error was:\n' % modname)
                print_exec(sys.stderr)
            else:
                setattr(main,mod.__name__, mod)
        else:
            #REVERSE COMPATIBILITY FOR CLOUD CLIENT 1.5 (WITH EPD)
            #In old version actual module was sent
            setattr(main,modname.__name__, modname)

#object generators: 
Example #9
Source File: cloudpickle.py    From spark-cluster-deployment with Apache License 2.0 6 votes vote down vote up
def save_timeseries(self, obj):
        import scikits.timeseries.tseries as ts

        func, reduce_args, state = obj.__reduce__()
        if func != ts._tsreconstruct:
            raise pickle.PicklingError('timeseries using unexpected reconstruction function %s' % str(func))
        state = (1,
                         obj.shape,
                         obj.dtype,
                         obj.flags.fnc,
                         obj._data.tostring(),
                         ts.getmaskarray(obj).tostring(),
                         obj._fill_value,
                         obj._dates.shape,
                         obj._dates.__array__().tostring(),
                         obj._dates.dtype, #added -- preserve type
                         obj.freq,
                         obj._optinfo,
                         )
        return self.save_reduce(_genTimeSeries, (reduce_args, state)) 
Example #10
Source File: util.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
def setIDFunction(idFunction):
    """
    Change the function used by L{unsignedID} to determine the integer id value
    of an object.  This is largely useful for testing to give L{unsignedID}
    deterministic, easily-controlled behavior.

    @param idFunction: A function with the signature of L{id}.
    @return: The previous function being used by L{unsignedID}.
    """
    global _idFunction
    oldIDFunction = _idFunction
    _idFunction = idFunction
    return oldIDFunction


# A value about twice as large as any Python int, to which negative values
# from id() will be added, moving them into a range which should begin just
# above where positive values from id() leave off. 
Example #11
Source File: util.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
def makeStatBar(width, maxPosition, doneChar = '=', undoneChar = '-', currentChar = '>'):
    """Creates a function that will return a string representing a progress bar.
    """
    aValue = width / float(maxPosition)
    def statBar(position, force = 0, last = ['']):
        assert len(last) == 1, "Don't mess with the last parameter."
        done = int(aValue * position)
        toDo = width - done - 2
        result = "[%s%s%s]" % (doneChar * done, currentChar, undoneChar * toDo)
        if force:
            last[0] = result
            return result
        if result == last[0]:
            return ''
        last[0] = result
        return result

    statBar.__doc__ = """statBar(position, force = 0) -> '[%s%s%s]'-style progress bar

    returned string is %d characters long, and the range goes from 0..%d.
    The 'position' argument is where the '%s' will be drawn.  If force is false,
    '' will be returned instead if the resulting progress bar is identical to the
    previously returned progress bar.
""" % (doneChar * 3, currentChar, undoneChar * 3, width, maxPosition, currentChar)
    return statBar 
Example #12
Source File: Util.py    From pivy with ISC License 6 votes vote down vote up
def uniquer(seq, idfun=None):
    if idfun is None:
        def idfun(x): return x
    seen = {}
    result = []
    for item in seq:
        marker = idfun(item)
        # in old Python versions:
        # if seen.has_key(marker)
        # but in new ones:
        if marker in seen: continue
        seen[marker] = 1
        result.append(item)
    return result

# A more efficient implementation of Alex's uniquer(), this avoids the
# idfun() argument and function-call overhead by assuming that all
# items in the sequence are hashable. 
Example #13
Source File: CLI.py    From pycopia with Apache License 2.0 6 votes vote down vote up
def get_generic_cmd(obj, ui, cliclass=GenericCLI, aliases=None, gbl=None):
    """get a GenericCLI (or other) command set wrapping any class instance
    object. The wrapped objects public methods have CLI command counterparts
    automatically created."""
    import new
    from pycopia.methodholder import MethodHolder
    cmd = cliclass(ui, aliases)
    if gbl is None:
        gbl = globals()
    hashfilter = {}
    for name in _get_methodnames(obj):
        if hasattr(cmd, name):
            continue # don't override already defined methods
        # all this mess does is introspect the object methods and map it to a CLI
        # object method of the same name, with a docstring showing the attributes
        # and their default values, and the actual code mirroring the
        # _generic_call method in the GenericCLI class.
        else:
            obj_meth = getattr(obj, name)
            if id(obj_meth.__func__) in hashfilter: # filter out aliases
                continue
            else:
                hashfilter[id(obj_meth.__func__)] = True
            mh = MethodHolder(obj_meth)
            doc = "%s  *\n%s" % (mh, obj_meth.__doc__ or "")
            code = cliclass._generic_call.func_code
            nc = new.code(code.co_argcount, code.co_nlocals, code.co_stacksize,
                code.co_flags, code.co_code,
                (doc,)+code.co_consts[1:], # replace docstring
                code.co_names, code.co_varnames, code.co_filename,
                code.co_name, code.co_firstlineno, code.co_lnotab)
            f = new.function(nc, gbl, name)
            m = new.instancemethod(f, cmd, cliclass)
            setattr(cmd, name, m)
    cmd._setup(obj, "Object:%s> " % (obj.__class__.__name__,))
    return cmd 
Example #14
Source File: Util.py    From pivy with ISC License 6 votes vote down vote up
def MD5collect(signatures):
    """
    Collects a list of signatures into an aggregate signature.

    signatures - a list of signatures
    returns - the aggregate signature
    """
    if len(signatures) == 1:
        return signatures[0]
    else:
        return MD5signature(string.join(signatures, ', '))



# Wrap the intern() function so it doesn't throw exceptions if ineligible
# arguments are passed. The intern() function was moved into the sys module in
# Python 3. 
Example #15
Source File: cloudpickle.py    From spark-cluster-deployment with Apache License 2.0 5 votes vote down vote up
def extract_func_data(self, func):
        """
        Turn the function into a tuple of data necessary to recreate it:
            code, globals, defaults, closure, dict
        """
        code = func.func_code

        # extract all global ref's
        func_global_refs = CloudPickler.extract_code_globals(code)
        if code.co_consts:   # see if nested function have any global refs
            for const in code.co_consts:
                if type(const) is types.CodeType and const.co_names:
                    func_global_refs = func_global_refs.union( CloudPickler.extract_code_globals(const))
        # process all variables referenced by global environment
        f_globals = {}
        for var in func_global_refs:
            #Some names, such as class functions are not global - we don't need them
            if func.func_globals.has_key(var):
                f_globals[var] = func.func_globals[var]

        # defaults requires no processing
        defaults = func.func_defaults

        def get_contents(cell):
            try:
                return cell.cell_contents
            except ValueError, e: #cell is empty error on not yet assigned
                raise pickle.PicklingError('Function to be pickled has free variables that are referenced before assignment in enclosing scope')


        # process closure 
Example #16
Source File: Util.py    From pivy with ISC License 5 votes vote down vote up
def render_tree(root, child_func, prune=0, margin=[0], visited={}):
    """
    Render a tree of nodes into an ASCII tree view.
    root - the root node of the tree
    child_func - the function called to get the children of a node
    prune - don't visit the same node twice
    margin - the format of the left margin to use for children of root.
       1 results in a pipe, and 0 results in no pipe.
    visited - a dictionary of visited nodes in the current branch if not prune,
       or in the whole tree if prune.
    """

    rname = str(root)

    children = child_func(root)
    retval = ""
    for pipe in margin[:-1]:
        if pipe:
            retval = retval + "| "
        else:
            retval = retval + "  "

    if rname in visited:
        return retval + "+-[" + rname + "]\n"

    retval = retval + "+-" + rname + "\n"
    if not prune:
        visited = copy.copy(visited)
    visited[rname] = 1

    for i in range(len(children)):
        margin.append(i<len(children)-1)
        retval = retval + render_tree(children[i], child_func, prune, margin, visited
)
        margin.pop()

    return retval 
Example #17
Source File: Util.py    From pivy with ISC License 5 votes vote down vote up
def RegGetValue(root, key):
        """This utility function returns a value in the registry
        without having to open the key first.  Only available on
        Windows platforms with a version of Python that can read the
        registry.  Returns the same thing as
        SCons.Util.RegQueryValueEx, except you just specify the entire
        path to the value, and don't have to bother opening the key
        first.  So:

        Instead of:
          k = SCons.Util.RegOpenKeyEx(SCons.Util.HKEY_LOCAL_MACHINE,
                r'SOFTWARE\Microsoft\Windows\CurrentVersion')
          out = SCons.Util.RegQueryValueEx(k,
                'ProgramFilesDir')

        You can write:
          out = SCons.Util.RegGetValue(SCons.Util.HKEY_LOCAL_MACHINE,
                r'SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir')
        """
        # I would use os.path.split here, but it's not a filesystem
        # path...
        p = key.rfind('\\') + 1
        keyp = key[:p-1]          # -1 to omit trailing slash
        val = key[p:]
        k = RegOpenKeyEx(root, keyp)
        return RegQueryValueEx(k,val) 
Example #18
Source File: Util.py    From pivy with ISC License 5 votes vote down vote up
def make_path_relative(path):
    """ makes an absolute path name to a relative pathname.
    """
    if os.path.isabs(path):
        drive_s,path = os.path.splitdrive(path)

        import re
        if not drive_s:
            path=re.compile("/*(.*)").findall(path)[0]
        else:
            path=path[1:]

    assert( not os.path.isabs( path ) ), path
    return path



# The original idea for AddMethod() and RenameFunction() come from the
# following post to the ActiveState Python Cookbook:
#
#	ASPN: Python Cookbook : Install bound methods in an instance
#	http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/223613
#
# That code was a little fragile, though, so the following changes
# have been wrung on it:
#
# * Switched the installmethod() "object" and "function" arguments,
#   so the order reflects that the left-hand side is the thing being
#   "assigned to" and the right-hand side is the value being assigned.
#
# * Changed explicit type-checking to the "try: klass = object.__class__"
#   block in installmethod() below so that it still works with the
#   old-style classes that SCons uses.
#
# * Replaced the by-hand creation of methods and functions with use of
#   the "new" module, as alluded to in Alex Martelli's response to the
#   following Cookbook post:
#
#	ASPN: Python Cookbook : Dynamically added methods to a class
#	http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81732 
Example #19
Source File: Util.py    From pivy with ISC License 5 votes vote down vote up
def AddMethod(object, function, name = None):
    """
    Adds either a bound method to an instance or an unbound method to
    a class. If name is ommited the name of the specified function
    is used by default.
    Example:
      a = A()
      def f(self, x, y):
        self.z = x + y
      AddMethod(f, A, "add")
      a.add(2, 4)
      print a.z
      AddMethod(lambda self, i: self.l[i], a, "listIndex")
      print a.listIndex(5)
    """
    import new

    if name is None:
        name = function.__name__
    else:
        function = RenameFunction(function, name)

    try:
        klass = object.__class__
    except AttributeError:
        # "object" is really a class, so it gets an unbound method.
        object.__dict__[name] = new.instancemethod(function, None, object)
    else:
        # "object" is really an instance, so it gets a bound method.
        object.__dict__[name] = new.instancemethod(function, object, klass) 
Example #20
Source File: cloudpickle.py    From spark-cluster-deployment with Apache License 2.0 5 votes vote down vote up
def _change_cell_value(cell, newval):
    """ Changes the contents of 'cell' object to newval """
    return new.function(cell_changer_code, {}, None, (), (cell,))(newval) 
Example #21
Source File: CLI.py    From pycopia with Apache License 2.0 5 votes vote down vote up
def get_commands(self):
        if self._command_list is None:
            hashfilter = {}
            for name in filter(self._command_filt, dir(self)):
                ## this filters out aliases (same function id)
                meth = getattr(self, name)
                hashfilter[id(meth.im_func)] = meth.im_func.func_name
            self._command_list = hashfilter.values()
            self._command_list.sort()
        return self._command_list

    # user visible commands are methods that don't have a leading underscore,
    # and do have a docstring. 
Example #22
Source File: CLI.py    From pycopia with Apache License 2.0 5 votes vote down vote up
def get_generic_cmd(obj, ui, cliclass=GenericCLI, aliases=None, gbl=None):
    """get a GenericCLI (or other) command set wrapping any class instance
    object. The wrapped objects public methods have CLI command counterparts
    automatically created."""
    import new
    from methodholder import MethodHolder
    cmd = cliclass(ui, aliases)
    if gbl is None:
        gbl = globals()
    hashfilter = {}
    for name in _get_methodnames(obj):
        if hasattr(cmd, name):
            continue # don't override already defined methods
        # all this mess does is introspect the object methods and map it to a CLI
        # object method of the same name, with a docstring showing the attributes
        # and their default values, and the actual code mirroring the
        # _generic_call method in the GenericCLI class.
        else:
            obj_meth = getattr(obj, name)
            if id(obj_meth.im_func) in hashfilter: # filter out aliases
                continue
            else:
                hashfilter[id(obj_meth.im_func)] = True
            mh = MethodHolder(obj_meth)
            doc = "%s  *\n%s" % (mh, obj_meth.__doc__ or "")
            code = cliclass._generic_call.func_code
            nc = new.code(code.co_argcount, code.co_nlocals, code.co_stacksize, 
                code.co_flags, code.co_code, 
                (doc,)+code.co_consts[1:], # replace docstring
                code.co_names, code.co_varnames, code.co_filename, 
                code.co_name, code.co_firstlineno, code.co_lnotab)
            f = new.function(nc, gbl, name)
            m = new.instancemethod(f, cmd, cliclass)
            setattr(cmd, name, m)
    cmd._setup(obj, "Object:%s> " % (obj.__class__.__name__,))
    return cmd 
Example #23
Source File: cloudpickle.py    From spark-cluster-deployment with Apache License 2.0 5 votes vote down vote up
def save_ufunc(self, obj):
        """Hack function for saving numpy ufunc objects"""
        name = obj.__name__
        for tst_mod_name in self.numpy_tst_mods:
            tst_mod = sys.modules.get(tst_mod_name, None)
            if tst_mod:
                if name in tst_mod.__dict__:
                    self.save_reduce(_getobject, (tst_mod_name, name))
                    return
        raise pickle.PicklingError('cannot save %s. Cannot resolve what module it is defined in' % str(obj)) 
Example #24
Source File: bind.py    From clonedigger with GNU General Public License v3.0 5 votes vote down vote up
def bind(f, globals):
    """Returns a new function whose code object has been
    bound by bind_code()"""
    newcode = bind_code(f.__code__, globals)
    defaults = f.__defaults__ or ()
    return make_function(newcode, f.__globals__, f.__name__, defaults) 
Example #25
Source File: test_new.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_closure(func, closure, exc):
    try:
        new.function(func.func_code, {}, "", None, closure)
    except exc:
        pass
    else:
        print "corrupt closure accepted" 
Example #26
Source File: util.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
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 
Example #27
Source File: CLI.py    From pycopia with Apache License 2.0 5 votes vote down vote up
def get_commands(self):
        if self._command_list is None:
            hashfilter = {}
            for name in filter(self._command_filt, dir(self)):
                ## this filters out aliases (same function id)
                meth = getattr(self, name)
                hashfilter[id(meth.__func__)] = meth.__func__.__name__
            self._command_list = list(hashfilter.values())
            self._command_list.sort()
        return self._command_list

    # user visible commands are methods that don't have a leading underscore,
    # and do have a docstring. 
Example #28
Source File: util.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def moduleMovedForSplit(origModuleName, newModuleName, moduleDesc,
                        projectName, projectURL, globDict):
    """
    No-op function; only present for backwards compatibility.  There is no
    reason to call this function.
    """
    warnings.warn(
        "moduleMovedForSplit is deprecated since Twisted 9.0.",
        DeprecationWarning, stacklevel=2) 
Example #29
Source File: recipe-572213.py    From code with MIT License 5 votes vote down vote up
def save_module_dict(self, obj, main_dict=vars(__import__('__main__'))):
    """ Special-case __main__.__dict__. Useful for a function's func_globals member. """
    if obj is main_dict:
        save_global_byname(self, obj, '__main__', '__dict__')
    else:
        return pickle.Pickler.save_dict(self, obj)      # fallback to original 
Example #30
Source File: recipe-572213.py    From code with MIT License 5 votes vote down vote up
def save_function(self, obj):
    """ Save functions by value if they are defined interactively """
    if obj.__module__ == '__main__' or obj.func_name == '<lambda>':
        args = (obj.func_code, obj.func_globals, obj.func_name, obj.func_defaults, obj.func_closure)
        self.save_reduce(new.function, args, obj=obj)
    else:
        pickle.Pickler.save_global(self, obj)