Author kristjan.jonsson
Recipients kristjan.jonsson
Date 2009-12-10.23:16:27
SpamBayes Score 7.17731e-06
Marked as misclassified No
Message-id <>
I have two solutions for this problem.  The first is a mundane one, and 
what I employed in our production environment:
class RecvAdapter(object):
    def __init__(self, wrapped):
        self.wrapped = wrapped
    def recv(self, amt):
    def close(self):

    fp = socket._fileobject(RecvAdapter(r), close=True)

The second solution is a bit more interesting.  It involves applying 
what I call a weakmethod: A bound method that holds a weak ref to the 
object instance:

import weakref
class WeakMethod(object):
    def __init__(self, bound):
        self.weakself = weakref.proxy(bound.im_self)
        self.methodname = bound.im_func.func_name
    def __call__(self, *args, **kw):
        return getattr(self.weakself, self.methodname)(*args, **kw)

We then do:
    r.recv = WeakMethod(
    fp = socket._fileobject(r, close=True)

I've had many uses for a WeakMethod through the years.  I wonder if such 
a class might be considered useful enough to be put into the weakref 
Date User Action Args
2009-12-10 23:16:29kristjan.jonssonsetrecipients: + kristjan.jonsson
2009-12-10 23:16:29kristjan.jonssonsetmessageid: <>
2009-12-10 23:16:28kristjan.jonssonlinkissue7464 messages
2009-12-10 23:16:27kristjan.jonssoncreate