Python源码示例:weakref.ReferenceError()
示例1
def test_proxy_ref(self):
o = C()
o.bar = 1
ref1 = weakref.proxy(o, self.callback)
ref2 = weakref.proxy(o, self.callback)
del o
def check(proxy):
proxy.bar
extra_collect()
self.assertRaises(weakref.ReferenceError, check, ref1)
self.assertRaises(weakref.ReferenceError, check, ref2)
# XXX: CPython GC collects C() immediately. use ref1 instead on
# Jython
if test_support.is_jython:
self.assertRaises(weakref.ReferenceError, bool, ref1)
else:
self.assertRaises(weakref.ReferenceError, bool, weakref.proxy(C()))
self.assert_(self.cbcalled == 2)
示例2
def _locate_object(address, module=None):
"""get object located at the given memory address (inverse of id(obj))"""
special = [None, True, False] #XXX: more...?
for obj in special:
if address == id(obj): return obj
if module:
if PY3:
objects = iter(module.__dict__.values())
else:
objects = module.__dict__.itervalues()
else: objects = iter(gc.get_objects())
for obj in objects:
if address == id(obj): return obj
# all bad below... nothing found so throw ReferenceError or TypeError
from weakref import ReferenceError
try: address = hex(address)
except TypeError:
raise TypeError("'%s' is not a valid memory address" % str(address))
raise ReferenceError("Cannot reference object at '%s'" % address)
示例3
def _locate_object(address, module=None):
"""get object located at the given memory address (inverse of id(obj))"""
special = [None, True, False] #XXX: more...?
for obj in special:
if address == id(obj): return obj
if module:
if PY3:
objects = iter(module.__dict__.values())
else:
objects = module.__dict__.itervalues()
else: objects = iter(gc.get_objects())
for obj in objects:
if address == id(obj): return obj
# all bad below... nothing found so throw ReferenceError or TypeError
from weakref import ReferenceError
try: address = hex(address)
except TypeError:
raise TypeError("'%s' is not a valid memory address" % str(address))
raise ReferenceError("Cannot reference object at '%s'" % address)
示例4
def test_proxy_ref(self):
o = C()
o.bar = 1
ref1 = weakref.proxy(o, self.callback)
ref2 = weakref.proxy(o, self.callback)
del o
gc.collect()
def check(proxy):
proxy.bar
self.assertRaises(weakref.ReferenceError, check, ref1)
self.assertRaises(weakref.ReferenceError, check, ref2)
# Need to add extra step for Jython - in the orginal test, ref counting had already removed C()
# self.assertRaises(weakref.ReferenceError, bool, weakref.proxy(C()))
o2 = C()
ref3 = weakref.proxy(o2)
del o2
extra_collect()
self.assertRaises(weakref.ReferenceError, bool, ref3)
self.assertTrue(self.cbcalled == 2)
示例5
def test_proxy_ref(self):
o = C()
o.bar = 1
ref1 = weakref.proxy(o, self.callback)
ref2 = weakref.proxy(o, self.callback)
del o
gc.collect()
def check(proxy):
proxy.bar
self.assertRaises(weakref.ReferenceError, check, ref1)
self.assertRaises(weakref.ReferenceError, check, ref2)
# Need to add extra step for Jython - in the orginal test, ref counting had already removed C()
# self.assertRaises(weakref.ReferenceError, bool, weakref.proxy(C()))
o2 = C()
ref3 = weakref.proxy(o2)
del o2
extra_collect()
self.assertRaises(weakref.ReferenceError, bool, ref3)
self.assertTrue(self.cbcalled == 2)
示例6
def test_proxy_ref(self):
o = C()
o.bar = 1
ref1 = weakref.proxy(o, self.callback)
ref2 = weakref.proxy(o, self.callback)
del o
def check(proxy):
proxy.bar
self.assertRaises(weakref.ReferenceError, check, ref1)
self.assertRaises(weakref.ReferenceError, check, ref2)
self.assertRaises(weakref.ReferenceError, bool, weakref.proxy(C()))
self.assertEqual(self.cbcalled, 2)
示例7
def test_proxy_ref(self):
o = C()
o.bar = 1
ref1 = weakref.proxy(o, self.callback)
ref2 = weakref.proxy(o, self.callback)
del o
def check(proxy):
proxy.bar
self.assertRaises(weakref.ReferenceError, check, ref1)
self.assertRaises(weakref.ReferenceError, check, ref2)
self.assertRaises(weakref.ReferenceError, bool, weakref.proxy(C()))
self.assertTrue(self.cbcalled == 2)
示例8
def test_proxy_ref(self):
o = C()
o.bar = 1
ref1 = weakref.proxy(o, self.callback)
ref2 = weakref.proxy(o, self.callback)
del o
def check(proxy):
proxy.bar
self.assertRaises(weakref.ReferenceError, check, ref1)
self.assertRaises(weakref.ReferenceError, check, ref2)
self.assertRaises(weakref.ReferenceError, bool, weakref.proxy(C()))
self.assertEqual(self.cbcalled, 2)
示例9
def handle_event(self, event):
"return True if the event was handled. Return False if the application should stop sending this event."
if event.name not in self.event_handlers:
return False
else:
remove_list = []
for handler in self.event_handlers[event.name]:
try:
handler(event)
except weakref.ReferenceError:
remove_list.append(handler)
for dead_handler in remove_list:
self.event_handlers[event.name].remove(handler)
return True
示例10
def test_proxy_ref(self):
o = C()
o.bar = 1
ref1 = weakref.proxy(o, self.callback)
ref2 = weakref.proxy(o, self.callback)
del o
def check(proxy):
proxy.bar
self.assertRaises(weakref.ReferenceError, check, ref1)
self.assertRaises(weakref.ReferenceError, check, ref2)
self.assertRaises(weakref.ReferenceError, bool, weakref.proxy(C()))
self.assertEqual(self.cbcalled, 2)
示例11
def handle_event(self, event):
"return True if the event was handled. Return False if the application should stop sending this event."
if event.name not in self.event_handlers:
return False
else:
remove_list = []
for handler in self.event_handlers[event.name]:
try:
handler(event)
except weakref.ReferenceError:
remove_list.append(handler)
for dead_handler in remove_list:
self.event_handlers[event.name].remove(handler)
return True
示例12
def ndarraysubclassinstance(obj):
try: # check if is ndarray, and elif is subclass of ndarray
if getattr(obj, '__class__', type) is NumpyArrayType: return False
elif not isinstance(obj, NumpyArrayType): return False
except ReferenceError: return False # handle 'R3' weakref in 3.x
# verify that __reduce__ has not been overridden
NumpyInstance = NumpyArrayType((0,),'int8')
if id(obj.__reduce_ex__) == id(NumpyInstance.__reduce_ex__) and \
id(obj.__reduce__) == id(NumpyInstance.__reduce__): return True
return False
示例13
def save_dictproxy(pickler, obj):
log.info("Dp: %s" % obj)
attr = obj.get('__dict__')
#pickler.save_reduce(_create_dictproxy, (attr,'nested'), obj=obj)
if type(attr) == GetSetDescriptorType and attr.__name__ == "__dict__" \
and getattr(attr.__objclass__, "__dict__", None) == obj:
pickler.save_reduce(getattr, (attr.__objclass__,"__dict__"),obj=obj)
return
# all bad below... so throw ReferenceError or TypeError
from weakref import ReferenceError
raise ReferenceError("%s does not reference a class __dict__" % obj)
示例14
def save_weakproxy(pickler, obj):
refobj = _locate_object(_proxy_helper(obj))
try: log.info("R2: %s" % obj)
except ReferenceError: log.info("R3: %s" % sys.exc_info()[1])
#callable = bool(getattr(refobj, '__call__', None))
if type(obj) is CallableProxyType: callable = True
else: callable = False
pickler.save_reduce(_create_weakproxy, (refobj, callable), obj=obj)
return
示例15
def ndarraysubclassinstance(obj):
try: # check if is ndarray, and elif is subclass of ndarray
if getattr(obj, '__class__', type) is NumpyArrayType: return False
elif not isinstance(obj, NumpyArrayType): return False
except ReferenceError: return False # handle 'R3' weakref in 3.x
# verify that __reduce__ has not been overridden
NumpyInstance = NumpyArrayType((0,),'int8')
if id(obj.__reduce_ex__) == id(NumpyInstance.__reduce_ex__) and \
id(obj.__reduce__) == id(NumpyInstance.__reduce__): return True
return False
示例16
def save_weakproxy(pickler, obj):
refobj = _locate_object(_proxy_helper(obj))
try: log.info("R2: %s" % obj)
except ReferenceError: log.info("R3: %s" % sys.exc_info()[1])
#callable = bool(getattr(refobj, '__call__', None))
if type(obj) is CallableProxyType: callable = True
else: callable = False
pickler.save_reduce(_create_weakproxy, (refobj, callable), obj=obj)
return
示例17
def __closeCursors(self, doclose=0):
"""__closeCursors() - closes all cursors associated with this connection"""
if self.__anyCursorsLeft():
cursors = map(lambda x: x(), self.cursors.data.values())
for cursor in cursors:
try:
if doclose:
cursor.close()
else:
cursor._reset()
except weakref.ReferenceError:
pass
示例18
def handle_event(self, event):
"return True if the event was handled. Return False if the application should stop sending this event."
if event.name not in self.event_handlers:
return False
else:
remove_list = []
for handler in self.event_handlers[event.name]:
try:
handler(event)
except weakref.ReferenceError:
remove_list.append(handler)
for dead_handler in remove_list:
self.event_handlers[event.name].remove(handler)
return True
示例19
def handle_event(self, event):
"return True if the event was handled. Return False if the application should stop sending this event."
if event.name not in self.event_handlers:
return False
else:
remove_list = []
for handler in self.event_handlers[event.name]:
try:
handler(event)
except weakref.ReferenceError:
remove_list.append(handler)
for dead_handler in remove_list:
self.event_handlers[event.name].remove(handler)
return True