本文整理汇总了Python中weakref.getweakrefs函数的典型用法代码示例。如果您正苦于以下问题:Python getweakrefs函数的具体用法?Python getweakrefs怎么用?Python getweakrefs使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了getweakrefs函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_getweakrefs
def test_getweakrefs(self):
o = C()
ref1 = weakref.ref(o, self.callback)
ref2 = weakref.ref(o, self.callback)
del ref1
test_support.gc_collect()
self.assert_(weakref.getweakrefs(o) == [ref2],
"list of refs does not match")
o = C()
ref1 = weakref.ref(o, self.callback)
ref2 = weakref.ref(o, self.callback)
del ref2
test_support.gc_collect()
self.assert_(weakref.getweakrefs(o) == [ref1],
"list of refs does not match")
del ref1
test_support.gc_collect()
self.assert_(weakref.getweakrefs(o) == [],
"list of refs not cleared")
# assumes ints do not support weakrefs
self.assert_(weakref.getweakrefs(1) == [],
"list of refs does not match for int")
示例2: test_getweakrefs
def test_getweakrefs(self):
o = C()
ref1 = weakref.ref(o, self.callback)
ref2 = weakref.ref(o, self.callback)
del ref1
self.assert_(weakref.getweakrefs(o) == [ref2],
"list of refs does not match")
o = C()
ref1 = weakref.ref(o, self.callback)
ref2 = weakref.ref(o, self.callback)
del ref2
self.assert_(weakref.getweakrefs(o) == [ref1],
"list of refs does not match")
示例3: del_Profile
def del_Profile(self, profile, del_users=False, no_archive=False, batch=False):
""" Delete a user profile (LMC.groups is an instance of
GroupsController and is needed to delete the profile group). """
assert ltrace(TRACE_PROFILES, '> del_Profile(%s)' % (profile.name))
# we need to hold the users lock, in case we need to delete users.
assert ltrace(TRACE_LOCKS, ' del_Profile locks: %s, %s, %s' % (self.lock, LMC.groups.lock, LMC.users.lock))
with nested(self.lock, LMC.groups.lock, LMC.users.lock):
# delete the reference in the controller.
del self[profile.gid]
# flush the controler data to disk.
self.serialize()
if profile.group.is_system_restricted:
logging.info(_(u'Kept restricted system group %s.') %
profile.groupName)
else:
try:
LMC.groups.del_Group(profile.group, del_users=del_users,
no_archive=no_archive, batch=batch,
# this one is needed to avoid looping...
check_profiles=False)
except exceptions.DoesntExistException:
logging.info('Group %s does not already exist, skipped.' %
profile.groupName)
name = profile.name
assert ltrace(TRACE_GC, ' profile ref count before del: %d %s' % (
sys.getrefcount(profile), gc.get_referrers(profile)))
# delete the hopefully last reference to the object. This will
# delete it from the reverse mapping caches too.
import weakref
print weakref.getweakrefs(profile)
del profile
# checkpoint, needed for multi-delete (users-groups-profile) operation,
# to avoid collecting the deleted users at the end of the run, making
# throw false-negative operation about non-existing groups.
gc.collect()
logging.notice(_(u'Deleted profile %s.') % stylize(ST_NAME, name))
assert ltrace(TRACE_LOCKS, ' del_Profile locks: %s, %s, %s' % (self.lock, LMC.groups.lock, LMC.users.lock))
示例4: test_subclass_refs_dont_replace_standard_refs
def test_subclass_refs_dont_replace_standard_refs(self):
class MyRef(weakref.ref):
pass
o = Object(42)
r1 = MyRef(o)
r2 = weakref.ref(o)
self.assert_(r1 is not r2)
self.assertEqual(weakref.getweakrefs(o), [r2, r1])
self.assertEqual(weakref.getweakrefcount(o), 2)
r3 = MyRef(o)
self.assertEqual(weakref.getweakrefcount(o), 3)
refs = weakref.getweakrefs(o)
self.assertEqual(len(refs), 3)
self.assert_(r2 is refs[0])
self.assert_(r1 in refs[1:])
self.assert_(r3 in refs[1:])
示例5: _disconnect
def _disconnect(self, cat, slots, **options): #{{{
numdel = int(options.get('count', 1))
if numdel < 0:
numdel = 0
if isinstance(slots, dict):
slots = slots.iteritems()
sigcat = self._categories
order, sigslot = sigcat[cat]
if not slots:
for name, (funchandler, funclist) in sigslot.iteritems():
del funclist[:]
else:
for name, funclist in slots:
slist = sigslot[name][1]
slist_rem = slist.remove
if not funclist:
del slist[:]
continue
for f in funclist:
count = 0
proxies = [pf for pf in getweakrefs(f) if isinstance(pf, CallableProxyType)]
for flist in (proxies, repeat(f)):
for p in flist:
if numdel and count >= numdel:
break
try:
slist_rem(p)
except ValueError:
if p == f:
break
else:
count = count + 1
if cat in ('around', 'replace'):
self.reload()
示例6: simple_demo
def simple_demo():
class obj(object):
def __init__(self, a):
self.a = a
def __repr__(self):
return "obj:{0}".format(self.a)
o = obj(3)
r = weakref.ref(o)
if r():
print "weakref's referent is alive"
print "weakref's reference's references/proxies is ", weakref.getweakrefs(o)
del o
if r():
print "weakref's referent is not alive"
print "weakref's reference's references/proxies is ", weakref.getweakrefs(o)
示例7: test_subclass_refs_dont_replace_standard_refs
def test_subclass_refs_dont_replace_standard_refs(self):
class MyRef(weakref.ref):
pass
o = Object(42)
r1 = MyRef(o)
r2 = weakref.ref(o)
self.assertTrue(r1 is not r2)
self.assertEqual(weakref.getweakrefs(o), [r2, r1])
self.assertEqual(weakref.getweakrefcount(o), 2)
r3 = MyRef(o)
self.assertEqual(weakref.getweakrefcount(o), 3)
refs = weakref.getweakrefs(o)
self.assertEqual(len(refs), 3)
assert set(refs) == set((r1, r2, r3))
if test_support.check_impl_detail():
self.assertTrue(r2 is refs[0])
self.assertIn(r1, refs[1:])
self.assertIn(r3, refs[1:])
示例8: test_getweakrefs
def test_getweakrefs(self):
o = C()
ref1 = weakref.ref(o, self.callback)
ref2 = weakref.ref(o, self.callback)
del ref1
self.assertEqual(weakref.getweakrefs(o), [ref2], "list of refs does not match")
o = C()
ref1 = weakref.ref(o, self.callback)
ref2 = weakref.ref(o, self.callback)
del ref2
self.assertEqual(weakref.getweakrefs(o), [ref1], "list of refs does not match")
del ref1
self.assertEqual(weakref.getweakrefs(o), [], "list of refs not cleared")
# assumes ints do not support weakrefs
self.assertEqual(weakref.getweakrefs(1), [], "list of refs does not match for int")
示例9: test_subclass_refs_dont_replace_standard_refs
def test_subclass_refs_dont_replace_standard_refs(self):
if test_support.due_to_ironpython_bug("http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=21116"):
return
class MyRef(weakref.ref):
pass
o = Object(42)
r1 = MyRef(o)
r2 = weakref.ref(o)
self.assertTrue(r1 is not r2)
self.assertEqual(weakref.getweakrefs(o), [r2, r1])
self.assertEqual(weakref.getweakrefcount(o), 2)
r3 = MyRef(o)
self.assertEqual(weakref.getweakrefcount(o), 3)
refs = weakref.getweakrefs(o)
self.assertEqual(len(refs), 3)
self.assertTrue(r2 is refs[0])
self.assertIn(r1, refs[1:])
self.assertIn(r3, refs[1:])
示例10: __eq__
def __eq__(self, other):
# We want slots to compare equal if they reference the same function/method
if isinstance(other, Slot):
if self.method and not other.method or not self.method and other.method:
return False
elif self.method and other.method:
return self.obj == other.obj and self.name == other.name
else:
return self.call == other.call
# We also want slots to compare equal _to_ the function or method they reference
else:
if self.method:
if hasattr(other, 'im_self'):
return self.obj in weakref.getweakrefs(other.im_self) and self.name == other.__name__
else: # `other` is not a method
return False
else:
return self.call in weakref.getweakrefs(other)
示例11: test_subclass_refs_dont_conflate_callbacks
def test_subclass_refs_dont_conflate_callbacks(self):
class MyRef(weakref.ref):
pass
o = Object(42)
r1 = MyRef(o, id)
r2 = MyRef(o, str)
self.assertTrue(r1 is not r2)
refs = list(weakref.getweakrefs(o))
self.assertIn(r1, refs)
self.assertIn(r2, refs)
示例12: test_subclass_refs_dont_conflate_callbacks
def test_subclass_refs_dont_conflate_callbacks(self):
class MyRef(weakref.ref):
pass
o = Object(42)
r1 = MyRef(o, id)
r2 = MyRef(o, str)
self.assert_(r1 is not r2)
refs = weakref.getweakrefs(o)
self.assert_(r1 in refs)
self.assert_(r2 in refs)
示例13: tearDown
def tearDown(self):
handler = self.loghandler
# All this is necessary to properly shut down the logging system and
# avoid a regrtest complaint. Thanks to Vinay Sajip for the help.
handler.close()
logger.removeHandler(handler)
for ref in weakref.getweakrefs(handler):
logging._removeHandlerRef(ref)
del self.loghandler
logger.setLevel(self._old_level)
super(LoggingCatcher, self).tearDown()
示例14: test_getweakrefs
def test_getweakrefs(self):
def runTest():
o = C()
ref1 = weakref.ref(o, self.callback)
ref2 = weakref.ref(o, self.callback)
del ref1
return o, ref2
o, ref2 = runTest()
test_support.force_gc_collect()
self.assertTrue(weakref.getweakrefs(o) == [ref2],
"list of refs does not match")
def runTest():
def runInnerTest():
o = C()
ref1 = weakref.ref(o, self.callback)
ref2 = weakref.ref(o, self.callback)
del ref2
return o, ref1
o, ref1 = runInnerTest()
test_support.force_gc_collect()
self.assertTrue(weakref.getweakrefs(o) == [ref1],
"list of refs does not match")
del ref1
return o
o = runTest()
test_support.force_gc_collect()
self.assertTrue(weakref.getweakrefs(o) == [],
"list of refs not cleared")
# assumes ints do not support weakrefs
self.assertTrue(weakref.getweakrefs(1) == [],
"list of refs does not match for int")
示例15: test_getweakrefs
def test_getweakrefs(self):
o = C()
ref1 = weakref.ref(o, self.callback)
ref2 = weakref.ref(o, self.callback)
del ref1
extra_collect()
self.assert_(weakref.getweakrefs(o) == [ref2],
"list of refs does not match")
o = C()
ref1 = weakref.ref(o, self.callback)
ref2 = weakref.ref(o, self.callback)
del ref2
extra_collect()
if test_support.is_jython:
# XXX: Likely a Jython bug: the following inline declared
# [ref1] list isn't garbage collected no matter how many
# times we force gc.collect(), which prevents ref1 from
# being garbage collected after it's del'd below. So we
# explicitly delete our list
ref1_list = [ref1]
self.assert_(weakref.getweakrefs(o) == ref1_list,
#self.assert_(weakref.getweakrefs(o) == [ref1],
"list of refs does not match")
del ref1_list
else:
self.assert_(weakref.getweakrefs(o) == [ref1],
"list of refs does not match")
del ref1
extra_collect()
self.assert_(weakref.getweakrefs(o) == [],
"list of refs not cleared")
# assumes ints do not support weakrefs
self.assert_(weakref.getweakrefs(1) == [],
"list of refs does not match for int")