This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author kristjan.jonsson
Recipients kristjan.jonsson
Date 2009-12-10.23:16:27
SpamBayes Score 7.1773147e-06
Marked as misclassified No
Message-id <1260486989.53.0.612807162614.issue7464@psf.upfronthosting.co.za>
In-reply-to
Content
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):
        return self.wrapped.read(amt)
    def close(self):
        self.wrapped.close()

...
    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(r.read)
    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 
module.
History
Date User Action Args
2009-12-10 23:16:29kristjan.jonssonsetrecipients: + kristjan.jonsson
2009-12-10 23:16:29kristjan.jonssonsetmessageid: <1260486989.53.0.612807162614.issue7464@psf.upfronthosting.co.za>
2009-12-10 23:16:28kristjan.jonssonlinkissue7464 messages
2009-12-10 23:16:27kristjan.jonssoncreate