~twisted-dev/deferred/trunk

« back to all changes in this revision

Viewing changes to deferred/_util.py

  • Committer: Jonathan Lange
  • Date: 2009-10-25 13:41:44 UTC
  • Revision ID: git-v1:855a0bbab367a22eabf14b4cf9b149037ab48c5f
Move the stuff that's not in defer.py originally to a separate file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import inspect
 
2
import new
 
3
import sys
 
4
 
 
5
 
 
6
_HUGEINT = (sys.maxint + 1L) * 2L
 
7
 
 
8
 
 
9
# Copied from twisted.python.util.unsignedID
 
10
def unsignedID(obj):
 
11
    """
 
12
    Return the id of an object as an unsigned number so that its hex
 
13
    representation makes sense
 
14
    """
 
15
    rval = id(obj)
 
16
    if rval < 0:
 
17
        rval += _HUGEINT
 
18
    return rval
 
19
 
 
20
 
 
21
# Copied from twisted.python.util.mergeFunctionMetadata.
 
22
def mergeFunctionMetadata(f, g):
 
23
    """
 
24
    Overwrite C{g}'s name and docstring with values from C{f}.  Update
 
25
    C{g}'s instance dictionary with C{f}'s.
 
26
 
 
27
    To use this function safely you must use the return value. In Python 2.3,
 
28
    L{mergeFunctionMetadata} will create a new function. In later versions of
 
29
    Python, C{g} will be mutated and returned.
 
30
 
 
31
    @return: A function that has C{g}'s behavior and metadata merged from
 
32
        C{f}.
 
33
    """
 
34
    try:
 
35
        g.__name__ = f.__name__
 
36
    except TypeError:
 
37
        try:
 
38
            merged = new.function(
 
39
                g.func_code, g.func_globals,
 
40
                f.__name__, inspect.getargspec(g)[-1],
 
41
                g.func_closure)
 
42
        except TypeError:
 
43
            pass
 
44
    else:
 
45
        merged = g
 
46
    try:
 
47
        merged.__doc__ = f.__doc__
 
48
    except (TypeError, AttributeError):
 
49
        pass
 
50
    try:
 
51
        merged.__dict__.update(g.__dict__)
 
52
        merged.__dict__.update(f.__dict__)
 
53
    except (TypeError, AttributeError):
 
54
        pass
 
55
    merged.__module__ = f.__module__
 
56
    return merged