Message96224
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. |
|
Date |
User |
Action |
Args |
2009-12-10 23:16:29 | kristjan.jonsson | set | recipients:
+ kristjan.jonsson |
2009-12-10 23:16:29 | kristjan.jonsson | set | messageid: <1260486989.53.0.612807162614.issue7464@psf.upfronthosting.co.za> |
2009-12-10 23:16:28 | kristjan.jonsson | link | issue7464 messages |
2009-12-10 23:16:27 | kristjan.jonsson | create | |
|