Changeset 64

Show
Ignore:
Timestamp:
03/16/02 00:51:40 (6 years ago)
Author:
sholloway
Message:

Added BindMethod? to ContextApply? module -- makes Method proxies that do not keep references out to their classes. Uses weakref.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/RBFoundation/RBFoundation/ContextApply.py

    r44 r64  
    44 
    55import weakref 
     6import types 
    67 
    78#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    910#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    1011 
    11 def JoinArgs_p_s(self, passed, self_vars): 
    12     return passed + self_vars 
    13 def JoinArgs_s_p(self, passed, self_vars): 
    14     return self_vars + passed 
    15 def JoinArgs_p(self, passed, self_vars): 
    16     return passed 
    17 def JoinArgs_s(self, passed, self_vars): 
    18     return self_vars 
    19 def JoinArgs_0(self, passed, self_vars): 
    20     return tuple() 
    21  
    22 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    23  
    24 def JoinKW_p_s(self, passed, self_vars): 
    25     result = {} 
    26     result.update(self_vars) 
    27     result.update(passed) 
     12def _JoinKW(dict1, dict2): 
     13    result = dict2.copy() 
     14    result.update(dict1) 
    2815    return result 
    29 def JoinKW_s_p(self, passed, self_vars): 
    30     result = {} 
    31     result.update(passed) 
    32     result.update(self_vars) 
    33     return result 
    34 def JoinKW_p(self, passed, self_vars): 
    35     return passed 
    36 def JoinKW_s(self, passed, self_vars): 
    37     return self_vars 
    38 def JoinKW_0(self, passed, self_vars): 
    39     return {} 
    4016 
    4117#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    4319#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    4420 
    45 class __ContextApply: 
     21class BindMethod: 
     22    def __init__(self, callback): 
     23        if isinstance(callback, (types.MethodType, types.UnboundMethodType)): 
     24            self.im_self = weakref.ref(callback.im_self) 
     25            self.im_func = callback.im_func 
     26        else: 
     27            self.im_self = None 
     28            self.im_func = callback 
     29  
     30    def __nonzero__(self): 
     31        return self.im_func and (not self.im_self or self.im_self()) and 1 or 0 
     32 
     33    def __call__(self, *args, **kw): 
     34        return self._DoCall(args, kw) 
     35 
     36    def _DoCall(self, args, kw): 
     37        if self.im_self: 
     38            return apply(self.im_func, (self.im_self(),) + args, kw) 
     39        else: return apply(self.im_func, args, kw) 
     40 
     41#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     42 
     43class _ContextApply(BindMethod): 
    4644    def __init__(self, callback, *args, **kw): 
    47         self.callback = callback 
     45        BindMethod.__init__(self, callback) 
    4846        self.args = args 
    4947        self.kw = kw 
    5048 
     49class ContextApply_p_s(_ContextApply): 
    5150    def __call__(self, *args, **kw): 
    52         return apply(self.callback, self.JoinArgs(args, self.args), self.JoinKW(kw, self.kw)) 
     51        return self._DoCall(args + self.args, _JoinKW(kw, self.kw)) 
    5352 
    54 class ContextApply_p_s(__ContextApply): 
    55     JoinArgs = JoinArgs_p_s 
    56     JoinKW = JoinKW_p_s 
     53class ContextApply_s_p(_ContextApply): 
     54    def __call__(self, *args, **kw): 
     55        return self._DoCall(self.args + args, _JoinKW(self.kw, kw)) 
    5756 
    58 class ContextApply_s_p(__ContextApply): 
    59     JoinArgs = JoinArgs_s_p 
    60     JoinKW = JoinKW_s_p 
     57class ContextApply_p(_ContextApply): 
     58    def __call__(self, *args, **kw): 
     59        return self._DoCall(args, kw) 
    6160 
    62 def ContextApply_p(callback, *args, **kw): 
    63     # ContextApply_p is simply the callback itself... lets reduce the overhead 
    64     return callback 
     61class ContextApply_s(_ContextApply): 
     62    def __call__(self, *args, **kw): 
     63        return self._DoCall(self.args, self.kw) 
    6564 
    66 class ContextApply_s(__ContextApply): 
    67     JoinArgs = JoinArgs_s 
    68     JoinKW = JoinKW_s 
    69  
    70 class ContextApply_0(__ContextApply): 
    71     JoinArgs = JoinArgs_0 
    72     JoinKW = JoinKW_0 
     65class ContextApply_0(_ContextApply): 
     66    def __call__(self, *args, **kw): 
     67        return self._DoCall(tuple(), {}) 
    7368 
    7469ContextApply = ContextApply_p_s 
    75  
    76 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    77  
    78 class WeakInstanceApply: 
    79     def __init__(self, instance, method): 
    80         self.instance = weakref.ref(instance) 
    81         self.method = method 
    82     def __call__(self, *args, **kw): 
    83         return apply(self.method, (self.instance(), ) + args, kw) 
    8470 
    8571#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    122108        print 
    123109        print "Testing Class:", Class.__name__ 
    124         a_s = tuple(range(1,4)
    125         a_p = tuple(range(10, 40, 10)
     110        a_s = (1,2,3
     111        a_p = (10,20,30
    126112        k_s = {'a':1,'b':2} 
    127113        k_p = {'b':20, 'c':30} 
    128114        cb = apply(Class, (callback,) + a_s, k_s) 
    129         return apply(cb, a_p, k_p) 
     115        result = apply(cb, a_p, k_p) 
     116        print "$ %s $" % `result` 
     117        return result 
    130118     
    131119    assert(test(ContextApply_p_s) == ((10, 20, 30, 1, 2, 3), {'a': 1, 'c': 30, 'b': 20})) 
  • trunk/RBJabber/RBJabber/JabberConnection.py

    r61 r64  
    55from Foundation.XMLBuilder import XMLBuilderMixin, XMLBuilderObjectBase, ParserCreate 
    66from Foundation import SmartSelect 
    7 from Foundation.ContextApply import WeakInstanceApply 
     7from Foundation.ContextApply import BindMethod 
    88from JID import * 
    99import socket 
     
    5050        self._parser = ParserCreate('ASCII', self._seperator) 
    5151        self._parser.returns_unicode = 0 
    52         self._parser.StartElementHandler = WeakInstanceApply(self, Client._start_element) 
    53         self._parser.EndElementHandler = WeakInstanceApply(self, Client._end_element) 
    54         self._parser.CharacterDataHandler = WeakInstanceApply(self, Client._char_data) 
     52        self._parser.StartElementHandler = BindMethod(self._start_element) 
     53        self._parser.EndElementHandler = BindMethod(self._end_element) 
     54        self._parser.CharacterDataHandler = BindMethod(self._char_data) 
    5555 
    5656        # Send the initial header