!C99Shell v. 2.0 [PHP 7 Update] [25.02.2019]!

Software: Apache. PHP/7.3.33 

uname -a: Linux web25.us.cloudlogin.co 5.10.237-xeon-hst #1 SMP Mon May 5 15:10:04 UTC 2025 x86_64 

uid=233359(alpastrology) gid=888(tty) groups=888(tty),33(tape) 

Safe-mode: OFF (not secure)

/usr/local/python-3.2/lib/python3.2/test/   drwxr-xr-x
Free 6182.22 GB of 6263.42 GB (98.7%)
Home    Back    Forward    UPDIR    Refresh    Search    Buffer    Encoder    Tools    Proc.    FTP brute    Sec.    SQL    PHP-code    Update    Feedback    Self remove    Logout    


Viewing file:     test_weakref.py (49.67 KB)      -rw-r--r--
Select action/file-type:
(+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
import gc
import sys
import unittest
import collections
import weakref
import operator
import contextlib
import copy

from test import support

# Used in ReferencesTestCase.test_ref_created_during_del() .
ref_from_del = None

class C:
    def method(self):
        pass


class Callable:
    bar = None

    def __call__(self, x):
        self.bar = x


def create_function():
    def f(): pass
    return f

def create_bound_method():
    return C().method


class Object:
    def __init__(self, arg):
        self.arg = arg
    def __repr__(self):
        return "<Object %r>" % self.arg
    def __eq__(self, other):
        if isinstance(other, Object):
            return self.arg == other.arg
        return NotImplemented
    def __lt__(self, other):
        if isinstance(other, Object):
            return self.arg < other.arg
        return NotImplemented
    def __hash__(self):
        return hash(self.arg)

class RefCycle:
    def __init__(self):
        self.cycle = self


class TestBase(unittest.TestCase):

    def setUp(self):
        self.cbcalled = 0

    def callback(self, ref):
        self.cbcalled += 1


class ReferencesTestCase(TestBase):

    def test_basic_ref(self):
        self.check_basic_ref(C)
        self.check_basic_ref(create_function)
        self.check_basic_ref(create_bound_method)

        # Just make sure the tp_repr handler doesn't raise an exception.
        # Live reference:
        o = C()
        wr = weakref.ref(o)
        repr(wr)
        # Dead reference:
        del o
        repr(wr)

    def test_basic_callback(self):
        self.check_basic_callback(C)
        self.check_basic_callback(create_function)
        self.check_basic_callback(create_bound_method)

    def test_multiple_callbacks(self):
        o = C()
        ref1 = weakref.ref(o, self.callback)
        ref2 = weakref.ref(o, self.callback)
        del o
        self.assertTrue(ref1() is None,
                     "expected reference to be invalidated")
        self.assertTrue(ref2() is None,
                     "expected reference to be invalidated")
        self.assertTrue(self.cbcalled == 2,
                     "callback not called the right number of times")

    def test_multiple_selfref_callbacks(self):
        # Make sure all references are invalidated before callbacks are called
        #
        # What's important here is that we're using the first
        # reference in the callback invoked on the second reference
        # (the most recently created ref is cleaned up first).  This
        # tests that all references to the object are invalidated
        # before any of the callbacks are invoked, so that we only
        # have one invocation of _weakref.c:cleanup_helper() active
        # for a particular object at a time.
        #
        def callback(object, self=self):
            self.ref()
        c = C()
        self.ref = weakref.ref(c, callback)
        ref1 = weakref.ref(c, callback)
        del c

    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(ReferenceError, check, ref1)
        self.assertRaises(ReferenceError, check, ref2)
        self.assertRaises(ReferenceError, bool, weakref.proxy(C()))
        self.assertEqual(self.cbcalled, 2)

    def check_basic_ref(self, factory):
        o = factory()
        ref = weakref.ref(o)
        self.assertTrue(ref() is not None,
                     "weak reference to live object should be live")
        o2 = ref()
        self.assertTrue(o is o2,
                     "<ref>() should return original object if live")

    def check_basic_callback(self, factory):
        self.cbcalled = 0
        o = factory()
        ref = weakref.ref(o, self.callback)
        del o
        self.assertTrue(self.cbcalled == 1,
                     "callback did not properly set 'cbcalled'")
        self.assertTrue(ref() is None,
                     "ref2 should be dead after deleting object reference")

    def test_ref_reuse(self):
        o = C()
        ref1 = weakref.ref(o)
        # create a proxy to make sure that there's an intervening creation
        # between these two; it should make no difference
        proxy = weakref.proxy(o)
        ref2 = weakref.ref(o)
        self.assertTrue(ref1 is ref2,
                     "reference object w/out callback should be re-used")

        o = C()
        proxy = weakref.proxy(o)
        ref1 = weakref.ref(o)
        ref2 = weakref.ref(o)
        self.assertTrue(ref1 is ref2,
                     "reference object w/out callback should be re-used")
        self.assertTrue(weakref.getweakrefcount(o) == 2,
                     "wrong weak ref count for object")
        del proxy
        self.assertTrue(weakref.getweakrefcount(o) == 1,
                     "wrong weak ref count for object after deleting proxy")

    def test_proxy_reuse(self):
        o = C()
        proxy1 = weakref.proxy(o)
        ref = weakref.ref(o)
        proxy2 = weakref.proxy(o)
        self.assertTrue(proxy1 is proxy2,
                     "proxy object w/out callback should have been re-used")

    def test_basic_proxy(self):
        o = C()
        self.check_proxy(o, weakref.proxy(o))

        L = collections.UserList()
        p = weakref.proxy(L)
        self.assertFalse(p, "proxy for empty UserList should be false")
        p.append(12)
        self.assertEqual(len(L), 1)
        self.assertTrue(p, "proxy for non-empty UserList should be true")
        p[:] = [2, 3]
        self.assertEqual(len(L), 2)
        self.assertEqual(len(p), 2)
        self.assertIn(3, p, "proxy didn't support __contains__() properly")
        p[1] = 5
        self.assertEqual(L[1], 5)
        self.assertEqual(p[1], 5)
        L2 = collections.UserList(L)
        p2 = weakref.proxy(L2)
        self.assertEqual(p, p2)
        ## self.assertEqual(repr(L2), repr(p2))
        L3 = collections.UserList(range(10))
        p3 = weakref.proxy(L3)
        self.assertEqual(L3[:], p3[:])
        self.assertEqual(L3[5:], p3[5:])
        self.assertEqual(L3[:5], p3[:5])
        self.assertEqual(L3[2:5], p3[2:5])

    def test_proxy_unicode(self):
        # See bug 5037
        class C(object):
            def __str__(self):
                return "string"
            def __bytes__(self):
                return b"bytes"
        instance = C()
        self.assertIn("__bytes__", dir(weakref.proxy(instance)))
        self.assertEqual(bytes(weakref.proxy(instance)), b"bytes")

    def test_proxy_index(self):
        class C:
            def __index__(self):
                return 10
        o = C()
        p = weakref.proxy(o)
        self.assertEqual(operator.index(p), 10)

    def test_proxy_div(self):
        class C:
            def __floordiv__(self, other):
                return 42
            def __ifloordiv__(self, other):
                return 21
        o = C()
        p = weakref.proxy(o)
        self.assertEqual(p // 5, 42)
        p //= 5
        self.assertEqual(p, 21)

    # The PyWeakref_* C API is documented as allowing either NULL or
    # None as the value for the callback, where either means "no
    # callback".  The "no callback" ref and proxy objects are supposed
    # to be shared so long as they exist by all callers so long as
    # they are active.  In Python 2.3.3 and earlier, this guarantee
    # was not honored, and was broken in different ways for
    # PyWeakref_NewRef() and PyWeakref_NewProxy().  (Two tests.)

    def test_shared_ref_without_callback(self):
        self.check_shared_without_callback(weakref.ref)

    def test_shared_proxy_without_callback(self):
        self.check_shared_without_callback(weakref.proxy)

    def check_shared_without_callback(self, makeref):
        o = Object(1)
        p1 = makeref(o, None)
        p2 = makeref(o, None)
        self.assertTrue(p1 is p2, "both callbacks were None in the C API")
        del p1, p2
        p1 = makeref(o)
        p2 = makeref(o, None)
        self.assertTrue(p1 is p2, "callbacks were NULL, None in the C API")
        del p1, p2
        p1 = makeref(o)
        p2 = makeref(o)
        self.assertTrue(p1 is p2, "both callbacks were NULL in the C API")
        del p1, p2
        p1 = makeref(o, None)
        p2 = makeref(o)
        self.assertTrue(p1 is p2, "callbacks were None, NULL in the C API")

    def test_callable_proxy(self):
        o = Callable()
        ref1 = weakref.proxy(o)

        self.check_proxy(o, ref1)

        self.assertTrue(type(ref1) is weakref.CallableProxyType,
                     "proxy is not of callable type")
        ref1('twinkies!')
        self.assertTrue(o.bar == 'twinkies!',
                     "call through proxy not passed through to original")
        ref1(x='Splat.')
        self.assertTrue(o.bar == 'Splat.',
                     "call through proxy not passed through to original")

        # expect due to too few args
        self.assertRaises(TypeError, ref1)

        # expect due to too many args
        self.assertRaises(TypeError, ref1, 1, 2, 3)

    def check_proxy(self, o, proxy):
        o.foo = 1
        self.assertTrue(proxy.foo == 1,
                     "proxy does not reflect attribute addition")
        o.foo = 2
        self.assertTrue(proxy.foo == 2,
                     "proxy does not reflect attribute modification")
        del o.foo
        self.assertTrue(not hasattr(proxy, 'foo'),
                     "proxy does not reflect attribute removal")

        proxy.foo = 1
        self.assertTrue(o.foo == 1,
                     "object does not reflect attribute addition via proxy")
        proxy.foo = 2
        self.assertTrue(
            o.foo == 2,
            "object does not reflect attribute modification via proxy")
        del proxy.foo
        self.assertTrue(not hasattr(o, 'foo'),
                     "object does not reflect attribute removal via proxy")

    def test_proxy_deletion(self):
        # Test clearing of SF bug #762891
        class Foo:
            result = None
            def __delitem__(self, accessor):
                self.result = accessor
        g = Foo()
        f = weakref.proxy(g)
        del f[0]
        self.assertEqual(f.result, 0)

    def test_proxy_bool(self):
        # Test clearing of SF bug #1170766
        class List(list): pass
        lyst = List()
        self.assertEqual(bool(weakref.proxy(lyst)), bool(lyst))

    def test_getweakrefcount(self):
        o = C()
        ref1 = weakref.ref(o)
        ref2 = weakref.ref(o, self.callback)
        self.assertTrue(weakref.getweakrefcount(o) == 2,
                     "got wrong number of weak reference objects")

        proxy1 = weakref.proxy(o)
        proxy2 = weakref.proxy(o, self.callback)
        self.assertTrue(weakref.getweakrefcount(o) == 4,
                     "got wrong number of weak reference objects")

        del ref1, ref2, proxy1, proxy2
        self.assertTrue(weakref.getweakrefcount(o) == 0,
                     "weak reference objects not unlinked from"
                     " referent when discarded.")

        # assumes ints do not support weakrefs
        self.assertTrue(weakref.getweakrefcount(1) == 0,
                     "got wrong number of weak reference objects for int")

    def test_getweakrefs(self):
        o = C()
        ref1 = weakref.ref(o, self.callback)
        ref2 = weakref.ref(o, self.callback)
        del ref1
        self.assertTrue(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.assertTrue(weakref.getweakrefs(o) == [ref1],
                     "list of refs does not match")

        del ref1
        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")

    def test_newstyle_number_ops(self):
        class F(float):
            pass
        f = F(2.0)
        p = weakref.proxy(f)
        self.assertTrue(p + 1.0 == 3.0)
        self.assertTrue(1.0 + p == 3.0)  # this used to SEGV

    def test_callbacks_protected(self):
        # Callbacks protected from already-set exceptions?
        # Regression test for SF bug #478534.
        class BogusError(Exception):
            pass
        data = {}
        def remove(k):
            del data[k]
        def encapsulate():
            f = lambda : ()
            data[weakref.ref(f, remove)] = None
            raise BogusError
        try:
            encapsulate()
        except BogusError:
            pass
        else:
            self.fail("exception not properly restored")
        try:
            encapsulate()
        except BogusError:
            pass
        else:
            self.fail("exception not properly restored")

    def test_sf_bug_840829(self):
        # "weakref callbacks and gc corrupt memory"
        # subtype_dealloc erroneously exposed a new-style instance
        # already in the process of getting deallocated to gc,
        # causing double-deallocation if the instance had a weakref
        # callback that triggered gc.
        # If the bug exists, there probably won't be an obvious symptom
        # in a release build.  In a debug build, a segfault will occur
        # when the second attempt to remove the instance from the "list
        # of all objects" occurs.

        import gc

        class C(object):
            pass

        c = C()
        wr = weakref.ref(c, lambda ignore: gc.collect())
        del c

        # There endeth the first part.  It gets worse.
        del wr

        c1 = C()
        c1.i = C()
        wr = weakref.ref(c1.i, lambda ignore: gc.collect())

        c2 = C()
        c2.c1 = c1
        del c1  # still alive because c2 points to it

        # Now when subtype_dealloc gets called on c2, it's not enough just
        # that c2 is immune from gc while the weakref callbacks associated
        # with c2 execute (there are none in this 2nd half of the test, btw).
        # subtype_dealloc goes on to call the base classes' deallocs too,
        # so any gc triggered by weakref callbacks associated with anything
        # torn down by a base class dealloc can also trigger double
        # deallocation of c2.
        del c2

    def test_callback_in_cycle_1(self):
        import gc

        class J(object):
            pass

        class II(object):
            def acallback(self, ignore):
                self.J

        I = II()
        I.J = J
        I.wr = weakref.ref(J, I.acallback)

        # Now J and II are each in a self-cycle (as all new-style class
        # objects are, since their __mro__ points back to them).  I holds
        # both a weak reference (I.wr) and a strong reference (I.J) to class
        # J.  I is also in a cycle (I.wr points to a weakref that references
        # I.acallback).  When we del these three, they all become trash, but
        # the cycles prevent any of them from getting cleaned up immediately.
        # Instead they have to wait for cyclic gc to deduce that they're
        # trash.
        #
        # gc used to call tp_clear on all of them, and the order in which
        # it does that is pretty accidental.  The exact order in which we
        # built up these things manages to provoke gc into running tp_clear
        # in just the right order (I last).  Calling tp_clear on II leaves
        # behind an insane class object (its __mro__ becomes NULL).  Calling
        # tp_clear on J breaks its self-cycle, but J doesn't get deleted
        # just then because of the strong reference from I.J.  Calling
        # tp_clear on I starts to clear I's __dict__, and just happens to
        # clear I.J first -- I.wr is still intact.  That removes the last
        # reference to J, which triggers the weakref callback.  The callback
        # tries to do "self.J", and instances of new-style classes look up
        # attributes ("J") in the class dict first.  The class (II) wants to
        # search II.__mro__, but that's NULL.   The result was a segfault in
        # a release build, and an assert failure in a debug build.
        del I, J, II
        gc.collect()

    def test_callback_in_cycle_2(self):
        import gc

        # This is just like test_callback_in_cycle_1, except that II is an
        # old-style class.  The symptom is different then:  an instance of an
        # old-style class looks in its own __dict__ first.  'J' happens to
        # get cleared from I.__dict__ before 'wr', and 'J' was never in II's
        # __dict__, so the attribute isn't found.  The difference is that
        # the old-style II doesn't have a NULL __mro__ (it doesn't have any
        # __mro__), so no segfault occurs.  Instead it got:
        #    test_callback_in_cycle_2 (__main__.ReferencesTestCase) ...
        #    Exception exceptions.AttributeError:
        #   "II instance has no attribute 'J'" in <bound method II.acallback
        #       of <?.II instance at 0x00B9B4B8>> ignored

        
class J(object):
            
pass

        
class II:
            
def acallback(selfignore):
                
self.J

        I 
II()
        
I.J
        I
.wr weakref.ref(JI.acallback)

        
del IJII
        gc
.collect()

    
def test_callback_in_cycle_3(self):
        
import gc

        
# This one broke the first patch that fixed the last two.  In this
        # case, the objects reachable from the callback aren't also reachable
        # from the object (c1) *triggering* the callback:  you can get to
        # c1 from c2, but not vice-versa.  The result was that c2's __dict__
        # got tp_clear'ed by the time the c2.cb callback got invoked.

        
class C:
            
def cb(selfignore):
                
self.me
                self
.c1
                self
.wr

        c1
c2 C(), C()

        
c2.me c2
        c2
.c1 c1
        c2
.wr weakref.ref(c1c2.cb)

        
del c1c2
        gc
.collect()

    
def test_callback_in_cycle_4(self):
        
import gc

        
# Like test_callback_in_cycle_3, except c2 and c1 have different
        # classes.  c2's class (C) isn't reachable from c1 then, so protecting
        # objects reachable from the dying object (c1) isn't enough to stop
        # c2's class (C) from getting tp_clear'ed before c2.cb is invoked.
        # The result was a segfault (C.__mro__ was NULL when the callback
        # tried to look up self.me).

        
class C(object):
            
def cb(selfignore):
                
self.me
                self
.c1
                self
.wr

        
class D:
            
pass

        c1
c2 D(), C()

        
c2.me c2
        c2
.c1 c1
        c2
.wr weakref.ref(c1c2.cb)

        
del c1c2CD
        gc
.collect()

    
def test_callback_in_cycle_resurrection(self):
        
import gc

        
# Do something nasty in a weakref callback:  resurrect objects
        # from dead cycles.  For this to be attempted, the weakref and
        # its callback must also be part of the cyclic trash (else the
        # objects reachable via the callback couldn't be in cyclic trash
        # to begin with -- the callback would act like an external root).
        # But gc clears trash weakrefs with callbacks early now, which
        # disables the callbacks, so the callbacks shouldn't get called
        # at all (and so nothing actually gets resurrected).

        
alist = []
        class 
C(object):
            
def __init__(selfvalue):
                
self.attribute value

            def acallback
(selfignore):
                
alist.append(self.c)

        
c1c2 C(1), C(2)
        
c1.c2
        c2
.c1
        c1
.wr weakref.ref(c2c1.acallback)
        
c2.wr weakref.ref(c1c2.acallback)

        
def C_went_away(ignore):
            
alist.append("C went away")
        
wr weakref.ref(CC_went_away)

        
del c1c2C   # make them all trash
        
self.assertEqual(alist, [])  # del isn't enough to reclaim anything

        
gc.collect()
        
# c1.wr and c2.wr were part of the cyclic trash, so should have
        # been cleared without their callbacks executing.  OTOH, the weakref
        # to C is bound to a function local (wr), and wasn't trash, so that
        # callback should have been invoked when C went away.
        
self.assertEqual(alist, ["C went away"])
        
# The remaining weakref should be dead now (its callback ran).
        
self.assertEqual(wr(), None)

        
del alist[:]
        
gc.collect()
        
self.assertEqual(alist, [])

    
def test_callbacks_on_callback(self):
        
import gc

        
# Set up weakref callbacks *on* weakref callbacks.
        
alist = []
        
def safe_callback(ignore):
            
alist.append("safe_callback called")

        class 
C(object):
            
def cb(selfignore):
                
alist.append("cb called")

        
cC(), C()
        
c.other d
        d
.other c
        callback 
c.cb
        c
.wr weakref.ref(dcallback)     # this won't trigger
        
d.wr weakref.ref(callbackd.cb)  # ditto
        
external_wr weakref.ref(callbacksafe_callback)  # but this will
        
self.assertTrue(external_wr() is callback)

        
# The weakrefs attached to c and d should get cleared, so that
        # C.cb is never called.  But external_wr isn't part of the cyclic
        # trash, and no cyclic trash is reachable from it, so safe_callback
        # should get invoked when the bound method object callback (c.cb)
        # -- which is itself a callback, and also part of the cyclic trash --
        # gets reclaimed at the end of gc.

        
del callbackcdC
        self
.assertEqual(alist, [])  # del isn't enough to clean up cycles
        
gc.collect()
        
self.assertEqual(alist, ["safe_callback called"])
        
self.assertEqual(external_wr(), None)

        
del alist[:]
        
gc.collect()
        
self.assertEqual(alist, [])

    
def test_gc_during_ref_creation(self):
        
self.check_gc_during_creation(weakref.ref)

    
def test_gc_during_proxy_creation(self):
        
self.check_gc_during_creation(weakref.proxy)

    
def check_gc_during_creation(selfmakeref):
        
thresholds gc.get_threshold()
        
gc.set_threshold(111)
        
gc.collect()
        class 
A:
            
pass

        def callback
(*args):
            
pass

        referenced 
A()

        
A()
        
a.a
        a
.wr makeref(referenced)

        try:
            
# now make sure the object and the ref get labeled as
            # cyclic trash:
            
A()
            
weakref.ref(referencedcallback)

        finally:
            
gc.set_threshold(*thresholds)

    
def test_ref_created_during_del(self):
        
# Bug #1377858
        # A weakref created in an object's __del__() would crash the
        # interpreter when the weakref was cleaned up since it would refer to
        # non-existent memory.  This test should not segfault the interpreter.
        
class Target(object):
            
def __del__(self):
                global 
ref_from_del
                ref_from_del 
weakref.ref(self)

        
Target()

    
def test_init(self):
        
# Issue 3634
        # <weakref to class>.__init__() doesn't check errors correctly
        
weakref.ref(Exception)
        
self.assertRaises(TypeErrorr.__init__00000)
        
# No exception should be raised here
        
gc.collect()

    
def test_classes(self):
        
# Check that classes are weakrefable.
        
class A(object):
            
pass
        l 
= []
        
weakref.ref(int)
        
weakref.ref(Al.append)
        
None
        gc
.collect()
        
self.assertEqual(a(), None)
        
self.assertEqual(l, [a])

    
def test_equality(self):
        
# Alive weakrefs defer equality testing to their underlying object.
        
Object(1)
        
Object(1)
        
Object(2)
        
weakref.ref(x)
        
weakref.ref(y)
        
weakref.ref(z)
        
weakref.ref(x)
        
# Note how we directly test the operators here, to stress both
        # __eq__ and __ne__.
        
self.assertTrue(== b)
        
self.assertFalse(!= b)
        
self.assertFalse(== c)
        
self.assertTrue(!= c)
        
self.assertTrue(== d)
        
self.assertFalse(!= d)
        
del xyz
        gc
.collect()
        for 
r in abc:
            
# Sanity check
            
self.assertIs(r(), None)
        
# Dead weakrefs compare by identity: whether `a` and `d` are the
        # same weakref object is an implementation detail, since they pointed
        # to the same original object and didn't have a callback.
        # (see issue #16453).
        
self.assertFalse(== b)
        
self.assertTrue(!= b)
        
self.assertFalse(== c)
        
self.assertTrue(!= c)
        
self.assertEqual(== da is d)
        
self.assertEqual(!= da is not d)

    
def test_ordering(self):
        
# weakrefs cannot be ordered, even if the underlying objects can.
        
ops = [operator.ltoperator.gtoperator.leoperator.ge]
        
Object(1)
        
Object(1)
        
weakref.ref(x)
        
weakref.ref(y)
        for 
op in ops:
            
self.assertRaises(TypeErroropab)
        
# Same when dead.
        
del xy
        gc
.collect()
        for 
op in ops:
            
self.assertRaises(TypeErroropab)

    
def test_hashing(self):
        
# Alive weakrefs hash the same as the underlying object
        
Object(42)
        
Object(42)
        
weakref.ref(x)
        
weakref.ref(y)
        
self.assertEqual(hash(a), hash(42))
        
del xy
        gc
.collect()
        
# Dead weakrefs:
        # - retain their hash is they were hashed when alive;
        # - otherwise, cannot be hashed.
        
self.assertEqual(hash(a), hash(42))
        
self.assertRaises(TypeErrorhashb)

    
def test_trashcan_16602(self):
        
# Issue #16602: when a weakref's target was part of a long
        # deallocation chain, the trashcan mechanism could delay clearing
        # of the weakref and make the target object visible from outside
        # code even though its refcount had dropped to 0.  A crash ensued.
        
class C:
            
def __init__(selfparent):
                if 
not parent:
                    return
                
wself weakref.ref(self)
                
def cb(wparent):
                    
wself()
                
self.wparent weakref.ref(parentcb)

        
weakref.WeakKeyDictionary()
        
root C(None)
        for 
n in range(100):
            
d[c] = C(c)
        
del root
        gc
.collect()


class 
SubclassableWeakrefTestCase(TestBase):

    
def test_subclass_refs(self):
        class 
MyRef(weakref.ref):
            
def __init__(selfobcallback=Nonevalue=42):
                
self.value value
                super
().__init__(obcallback)
            
def __call__(self):
                
self.called True
                
return super().__call__()
        
Object("foo")
        
mr MyRef(ovalue=24)
        
self.assertTrue(mr() is o)
        
self.assertTrue(mr.called)
        
self.assertEqual(mr.value24)
        
del o
        self
.assertTrue(mr() is None)
        
self.assertTrue(mr.called)

    
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), [r2r1])
        
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(r1refs[1:])
        
self.assertIn(r3refs[1:])

    
def test_subclass_refs_dont_conflate_callbacks(self):
        class 
MyRef(weakref.ref):
            
pass
        o 
Object(42)
        
r1 MyRef(oid)
        
r2 MyRef(ostr)
        
self.assertTrue(r1 is not r2)
        
refs weakref.getweakrefs(o)
        
self.assertIn(r1refs)
        
self.assertIn(r2refs)

    
def test_subclass_refs_with_slots(self):
        class 
MyRef(weakref.ref):
            
__slots__ "slot1""slot2"
            
def __new__(typeobcallbackslot1slot2):
                return 
weakref.ref.__new__(typeobcallback)
            
def __init__(selfobcallbackslot1slot2):
                
self.slot1 slot1
                self
.slot2 slot2
            def meth
(self):
                return 
self.slot1 self.slot2
        o 
Object(42)
        
MyRef(oNone"abc""def")
        
self.assertEqual(r.slot1"abc")
        
self.assertEqual(r.slot2"def")
        
self.assertEqual(r.meth(), "abcdef")
        
self.assertFalse(hasattr(r"__dict__"))

    
def test_subclass_refs_with_cycle(self):
        
# Bug #3110
        # An instance of a weakref subclass can have attributes.
        # If such a weakref holds the only strong reference to the object,
        # deleting the weakref will delete the object. In this case,
        # the callback must not be called, because the ref object is
        # being deleted.
        
class MyRef(weakref.ref):
            
pass

        
# Use a local callback, for "regrtest -R::"
        # to detect refcounting problems
        
def callback(w):
            
self.cbcalled += 1

        o 
C()
        
r1 MyRef(ocallback)
        
r1.o
        del o

        del r1 
# Used to crash here

        
self.assertEqual(self.cbcalled0)

        
# Same test, with two weakrefs to the same object
        # (since code paths are different)
        
C()
        
r1 MyRef(ocallback)
        
r2 MyRef(ocallback)
        
r1.r2
        r2
.o
        del o
        del r2

        del r1 
# Used to crash here

        
self.assertEqual(self.cbcalled0)


class 
MappingTestCase(TestBase):

    
COUNT 10

    def check_len_cycles
(selfdict_typecons):
        
20
        items 
= [RefCycle() for i in range(N)]
        
dct dict_type(cons(o) for o in items)
        
# Keep an iterator alive
        
it dct.items()
        try:
            
next(it)
        
except StopIteration:
            
pass
        del items
        gc
.collect()
        
n1 len(dct)
        
del it
        gc
.collect()
        
n2 len(dct)
        
# one item may be kept alive inside the iterator
        
self.assertIn(n1, (01))
        
self.assertEqual(n20)

    
def test_weak_keyed_len_cycles(self):
        
self.check_len_cycles(weakref.WeakKeyDictionarylambda k: (k1))

    
def test_weak_valued_len_cycles(self):
        
self.check_len_cycles(weakref.WeakValueDictionarylambda k: (1k))

    
def check_len_race(selfdict_typecons):
        
# Extended sanity checks for len() in the face of cyclic collection
        
self.addCleanup(gc.set_threshold, *gc.get_threshold())
        for 
th in range(1100):
            
20
            gc
.collect(0)
            
gc.set_threshold(ththth)
            
items = [RefCycle() for i in range(N)]
            
dct dict_type(cons(o) for o in items)
            
del items
            
# All items will be collected at next garbage collection pass
            
it dct.items()
            try:
                
next(it)
            
except StopIteration:
                
pass
            n1 
len(dct)
            
del it
            n2 
len(dct)
            
self.assertGreaterEqual(n10)
            
self.assertLessEqual(n1N)
            
self.assertGreaterEqual(n20)
            
self.assertLessEqual(n2n1)

    
def test_weak_keyed_len_race(self):
        
self.check_len_race(weakref.WeakKeyDictionarylambda k: (k1))

    
def test_weak_valued_len_race(self):
        
self.check_len_race(weakref.WeakValueDictionarylambda k: (1k))

    
def test_weak_values(self):
        
#
        #  This exercises d.copy(), d.items(), d[], del d[], len(d).
        #
        
dictobjects self.make_weak_valued_dict()
        for 
o in objects:
            
self.assertEqual(weakref.getweakrefcount(o), 1)
            
self.assertTrue(o is dict[o.arg],
                         
"wrong object returned by weak dict!")
        
items1 = list(dict.items())
        
items2 = list(dict.copy().items())
        
items1.sort()
        
items2.sort()
        
self.assertEqual(items1items2,
                     
"cloning of weak-valued dictionary did not work!")
        
del items1items2
        self
.assertEqual(len(dict), self.COUNT)
        
del objects[0]
        
self.assertEqual(len(dict), self.COUNT 1,
                     
"deleting object did not cause dictionary update")
        
del objectso
        self
.assertEqual(len(dict), 0,
                     
"deleting the values did not clear the dictionary")
        
# regression on SF bug #447152:
        
dict weakref.WeakValueDictionary()
        
self.assertRaises(KeyErrordict.__getitem__1)
        
dict[2] = C()
        
self.assertRaises(KeyErrordict.__getitem__2)

    
def test_weak_keys(self):
        
#
        #  This exercises d.copy(), d.items(), d[] = v, d[], del d[],
        #  len(d), k in d.
        #
        
dictobjects self.make_weak_keyed_dict()
        for 
o in objects:
            
self.assertTrue(weakref.getweakrefcount(o) == 1,
                         
"wrong number of weak references to %r!" o)
            
self.assertTrue(o.arg is dict[o],
                         
"wrong object returned by weak dict!")
        
items1 dict.items()
        
items2 dict.copy().items()
        
self.assertEqual(set(items1), set(items2),
                     
"cloning of weak-keyed dictionary did not work!")
        
del items1items2
        self
.assertEqual(len(dict), self.COUNT)
        
del objects[0]
        
self.assertTrue(len(dict) == (self.COUNT 1),
                     
"deleting object did not cause dictionary update")
        
del objectso
        self
.assertTrue(len(dict) == 0,
                     
"deleting the keys did not clear the dictionary")
        
Object(42)
        
dict[o] = "What is the meaning of the universe?"
        
self.assertIn(odict)
        
self.assertNotIn(34dict)

    
def test_weak_keyed_iters(self):
        
dictobjects self.make_weak_keyed_dict()
        
self.check_iters(dict)

        
# Test keyrefs()
        
refs dict.keyrefs()
        
self.assertEqual(len(refs), len(objects))
        
objects2 = list(objects)
        for 
wr in refs:
            
ob wr()
            
self.assertIn(obdict)
            
self.assertIn(obdict)
            
self.assertEqual(ob.argdict[ob])
            
objects2.remove(ob)
        
self.assertEqual(len(objects2), 0)

        
# Test iterkeyrefs()
        
objects2 = list(objects)
        
self.assertEqual(len(list(dict.keyrefs())), len(objects))
        for 
wr in dict.keyrefs():
            
ob wr()
            
self.assertIn(obdict)
            
self.assertIn(obdict)
            
self.assertEqual(ob.argdict[ob])
            
objects2.remove(ob)
        
self.assertEqual(len(objects2), 0)

    
def test_weak_valued_iters(self):
        
dictobjects self.make_weak_valued_dict()
        
self.check_iters(dict)

        
# Test valuerefs()
        
refs dict.valuerefs()
        
self.assertEqual(len(refs), len(objects))
        
objects2 = list(objects)
        for 
wr in refs:
            
ob wr()
            
self.assertEqual(obdict[ob.arg])
            
self.assertEqual(ob.argdict[ob.arg].arg)
            
objects2.remove(ob)
        
self.assertEqual(len(objects2), 0)

        
# Test itervaluerefs()
        
objects2 = list(objects)
        
self.assertEqual(len(list(dict.itervaluerefs())), len(objects))
        for 
wr in dict.itervaluerefs():
            
ob wr()
            
self.assertEqual(obdict[ob.arg])
            
self.assertEqual(ob.argdict[ob.arg].arg)
            
objects2.remove(ob)
        
self.assertEqual(len(objects2), 0)

    
def check_iters(selfdict):
        
# item iterator:
        
items = list(dict.items())
        for 
item in dict.items():
            
items.remove(item)
        
self.assertFalse(items"items() did not touch all items")

        
# key iterator, via __iter__():
        
keys = list(dict.keys())
        for 
k in dict:
            
keys.remove(k)
        
self.assertFalse(keys"__iter__() did not touch all keys")

        
# key iterator, via iterkeys():
        
keys = list(dict.keys())
        for 
k in dict.keys():
            
keys.remove(k)
        
self.assertFalse(keys"iterkeys() did not touch all keys")

        
# value iterator:
        
values = list(dict.values())
        for 
v in dict.values():
            
values.remove(v)
        
self.assertFalse(values,
                     
"itervalues() did not touch all values")

    
def check_weak_destroy_while_iterating(selfdictobjectsiter_name):
        
len(dict)
        
it iter(getattr(dictiter_name)())
        
next(it)             # Trigger internal iteration
        # Destroy an object
        
del objects[-1]
        
gc.collect()    # just in case
        # We have removed either the first consumed object, or another one
        
self.assertIn(len(list(it)), [len(objects), len(objects) - 1])
        
del it
        
# The removal has been committed
        
self.assertEqual(len(dict), 1)

    
def check_weak_destroy_and_mutate_while_iterating(selfdicttestcontext):
        
# Check that we can explicitly mutate the weak dict without
        # interfering with delayed removal.
        # `testcontext` should create an iterator, destroy one of the
        # weakref'ed objects and then return a new key/value pair corresponding
        # to the destroyed object.
        
with testcontext() as (kv):
            
self.assertNotIn(kdict)
        
with testcontext() as (kv):
            
self.assertRaises(KeyErrordict.__delitem__k)
        
self.assertNotIn(kdict)
        
with testcontext() as (kv):
            
self.assertRaises(KeyErrordict.popk)
        
self.assertNotIn(kdict)
        
with testcontext() as (kv):
            
dict[k] = v
        self
.assertEqual(dict[k], v)
        
ddict copy.copy(dict)
        
with testcontext() as (kv):
            
dict.update(ddict)
        
self.assertEqual(dictddict)
        
with testcontext() as (kv):
            
dict.clear()
        
self.assertEqual(len(dict), 0)

    
def test_weak_keys_destroy_while_iterating(self):
        
# Issue #7105: iterators shouldn't crash when a key is implicitly removed
        
dictobjects self.make_weak_keyed_dict()
        
self.check_weak_destroy_while_iterating(dictobjects'keys')
        
self.check_weak_destroy_while_iterating(dictobjects'items')
        
self.check_weak_destroy_while_iterating(dictobjects'values')
        
self.check_weak_destroy_while_iterating(dictobjects'keyrefs')
        
dictobjects self.make_weak_keyed_dict()
        @
contextlib.contextmanager
        def testcontext
():
            try:
                
it iter(dict.items())
                
next(it)
                
# Schedule a key/value for removal and recreate it
                
objects.pop().arg
                gc
.collect()      # just in case
                
yield Object(v), v
            
finally:
                
it None           # should commit all removals
        
self.check_weak_destroy_and_mutate_while_iterating(dicttestcontext)

    
def test_weak_values_destroy_while_iterating(self):
        
# Issue #7105: iterators shouldn't crash when a key is implicitly removed
        
dictobjects self.make_weak_valued_dict()
        
self.check_weak_destroy_while_iterating(dictobjects'keys')
        
self.check_weak_destroy_while_iterating(dictobjects'items')
        
self.check_weak_destroy_while_iterating(dictobjects'values')
        
self.check_weak_destroy_while_iterating(dictobjects'itervaluerefs')
        
self.check_weak_destroy_while_iterating(dictobjects'valuerefs')
        
dictobjects self.make_weak_valued_dict()
        @
contextlib.contextmanager
        def testcontext
():
            try:
                
it iter(dict.items())
                
next(it)
                
# Schedule a key/value for removal and recreate it
                
objects.pop().arg
                gc
.collect()      # just in case
                
yield kObject(k)
            finally:
                
it None           # should commit all removals
        
self.check_weak_destroy_and_mutate_while_iterating(dicttestcontext)

    
def test_make_weak_keyed_dict_from_dict(self):
        
Object(3)
        
dict weakref.WeakKeyDictionary({o:364})
        
self.assertEqual(dict[o], 364)

    
def test_make_weak_keyed_dict_from_weak_keyed_dict(self):
        
Object(3)
        
dict weakref.WeakKeyDictionary({o:364})
        
dict2 weakref.WeakKeyDictionary(dict)
        
self.assertEqual(dict[o], 364)

    
def make_weak_keyed_dict(self):
        
dict weakref.WeakKeyDictionary()
        
objects = list(map(Objectrange(self.COUNT)))
        for 
o in objects:
            
dict[o] = o.arg
        
return dictobjects

    def test_make_weak_valued_dict_from_dict
(self):
        
Object(3)
        
dict weakref.WeakValueDictionary({364:o})
        
self.assertEqual(dict[364], o)

    
def test_make_weak_valued_dict_from_weak_valued_dict(self):
        
Object(3)
        
dict weakref.WeakValueDictionary({364:o})
        
dict2 weakref.WeakValueDictionary(dict)
        
self.assertEqual(dict[364], o)

    
def make_weak_valued_dict(self):
        
dict weakref.WeakValueDictionary()
        
objects = list(map(Objectrange(self.COUNT)))
        for 
o in objects:
            
dict[o.arg] = o
        
return dictobjects

    def check_popitem
(selfklasskey1value1key2value2):
        
weakdict klass()
        
weakdict[key1] = value1
        weakdict
[key2] = value2
        self
.assertEqual(len(weakdict), 2)
        
kweakdict.popitem()
        
self.assertEqual(len(weakdict), 1)
        if 
k is key1:
            
self.assertTrue(v is value1)
        else:
            
self.assertTrue(v is value2)
        
kweakdict.popitem()
        
self.assertEqual(len(weakdict), 0)
        if 
k is key1:
            
self.assertTrue(v is value1)
        else:
            
self.assertTrue(v is value2)

    
def test_weak_valued_dict_popitem(self):
        
self.check_popitem(weakref.WeakValueDictionary,
                           
"key1"C(), "key2"C())

    
def test_weak_keyed_dict_popitem(self):
        
self.check_popitem(weakref.WeakKeyDictionary,
                           
C(), "value 1"C(), "value 2")

    
def check_setdefault(selfklasskeyvalue1value2):
        
self.assertTrue(value1 is not value2,
                     
"invalid test"
                     " -- value parameters must be distinct objects"
)
        
weakdict klass()
        
weakdict.setdefault(keyvalue1)
        
self.assertTrue(o is value1)
        
self.assertIn(keyweakdict)
        
self.assertTrue(weakdict.get(keyis value1)
        
self.assertTrue(weakdict[keyis value1)

        
weakdict.setdefault(keyvalue2)
        
self.assertTrue(o is value1)
        
self.assertIn(keyweakdict)
        
self.assertTrue(weakdict.get(keyis value1)
        
self.assertTrue(weakdict[keyis value1)

    
def test_weak_valued_dict_setdefault(self):
        
self.check_setdefault(weakref.WeakValueDictionary,
                              
"key"C(), C())

    
def test_weak_keyed_dict_setdefault(self):
        
self.check_setdefault(weakref.WeakKeyDictionary,
                              
C(), "value 1""value 2")

    
def check_update(selfklassdict):
        
#
        #  This exercises d.update(), len(d), d.keys(), k in d,
        #  d.get(), d[].
        #
        
weakdict klass()
        
weakdict.update(dict)
        
self.assertEqual(len(weakdict), len(dict))
        for 
k in weakdict.keys():
            
self.assertIn(kdict"mysterious new key appeared in weak dict")
            
dict.get(k)
            
self.assertTrue(v is weakdict[k])
            
self.assertTrue(v is weakdict.get(k))
        for 
k in dict.keys():
            
self.assertIn(kweakdict"original key disappeared in weak dict")
            
dict[k]
            
self.assertTrue(v is weakdict[k])
            
self.assertTrue(v is weakdict.get(k))

    
def test_weak_valued_dict_update(self):
        
self.check_update(weakref.WeakValueDictionary,
                          {
1C(), 'a'C(), C(): C()})

    
def test_weak_keyed_dict_update(self):
        
self.check_update(weakref.WeakKeyDictionary,
                          {
C(): 1C(): 2C(): 3})

    
def test_weak_keyed_delitem(self):
        
weakref.WeakKeyDictionary()
        
o1 Object('1')
        
o2 Object('2')
        
d[o1] = 'something'
        
d[o2] = 'something'
        
self.assertEqual(len(d), 2)
        
del d[o1]
        
self.assertEqual(len(d), 1)
        
self.assertEqual(list(d.keys()), [o2])

    
def test_weak_valued_delitem(self):
        
weakref.WeakValueDictionary()
        
o1 Object('1')
        
o2 Object('2')
        
d['something'] = o1
        d
['something else'] = o2
        self
.assertEqual(len(d), 2)
        
del d['something']
        
self.assertEqual(len(d), 1)
        
self.assertTrue(list(d.items()) == [('something else'o2)])

    
def test_weak_keyed_bad_delitem(self):
        
weakref.WeakKeyDictionary()
        
Object('1')
        
# An attempt to delete an object that isn't there should raise
        # KeyError.  It didn't before 2.3.
        
self.assertRaises(KeyErrord.__delitem__o)
        
self.assertRaises(KeyErrord.__getitem__o)

        
# If a key isn't of a weakly referencable type, __getitem__ and
        # __setitem__ raise TypeError.  __delitem__ should too.
        
self.assertRaises(TypeErrord.__delitem__,  13)
        
self.assertRaises(TypeErrord.__getitem__,  13)
        
self.assertRaises(TypeErrord.__setitem__,  1313)

    
def test_weak_keyed_cascading_deletes(self):
        
# SF bug 742860.  For some reason, before 2.3 __delitem__ iterated
        # over the keys via self.data.iterkeys().  If things vanished from
        # the dict during this (or got added), that caused a RuntimeError.

        
weakref.WeakKeyDictionary()
        
mutate False

        
class C(object):
            
def __init__(selfi):
                
self.value i
            def __hash__
(self):
                return 
hash(self.value)
            
def __eq__(selfother):
                if 
mutate:
                    
# Side effect that mutates the dict, by removing the
                    # last strong reference to a key.
                    
del objs[-1]
                return 
self.value == other.value

        objs 
= [C(i) for i in range(4)]
        for 
o in objs:
            
d[o] = o.value
        del o   
# now the only strong references to keys are in objs
        # Find the order in which iterkeys sees the keys.
        
objs = list(d.keys())
        
# Reverse it, so that the iteration implementation of __delitem__
        # has to keep looping to find the first object we delete.
        
objs.reverse()

        
# Turn on mutation in C.__eq__.  The first time thru the loop,
        # under the iterkeys() business the first comparison will delete
        # the last item iterkeys() would see, and that causes a
        #     RuntimeError: dictionary changed size during iteration
        # when the iterkeys() loop goes around to try comparing the next
        # key.  After this was fixed, it just deletes the last object *our*
        # "for o in obj" loop would have gotten to.
        
mutate True
        count 
0
        
for o in objs:
            
count += 1
            del d
[o]
        
self.assertEqual(len(d), 0)
        
self.assertEqual(count2)

from test import mapping_tests

class WeakValueDictionaryTestCase(mapping_tests.BasicTestMappingProtocol):
    
"""Check that WeakValueDictionary conforms to the mapping protocol"""
    
__ref = {"key1":Object(1), "key2":Object(2), "key3":Object(3)}
    
type2test weakref.WeakValueDictionary
    def _reference
(self):
        return 
self.__ref.copy()

class 
WeakKeyDictionaryTestCase(mapping_tests.BasicTestMappingProtocol):
    
"""Check that WeakKeyDictionary conforms to the mapping protocol"""
    
__ref = {Object("key1"):1Object("key2"):2Object("key3"):3}
    
type2test weakref.WeakKeyDictionary
    def _reference
(self):
        return 
self.__ref.copy()

libreftest """ Doctest for examples in the library reference: weakref.rst

>>> import weakref
>>> class Dict(dict):
...     pass
...
>>> obj = Dict(red=1, green=2, blue=3)   # this object is weak referencable
>>> r = weakref.ref(obj)
>>> print(r() is obj)
True

>>> import weakref
>>> class Object:
...     pass
...
>>> o = Object()
>>> r = weakref.ref(o)
>>> o2 = r()
>>> o is o2
True
>>> del o, o2
>>> print(r())
None

>>> import weakref
>>> class ExtendedRef(weakref.ref):
...     def __init__(self, ob, callback=None, **annotations):
...         super().__init__(ob, callback)
...         self.__counter = 0
...         for k, v in annotations.items():
...             setattr(self, k, v)
...     def __call__(self):
...         '''Return a pair containing the referent and the number of
...         times the reference has been called.
...         '''
...         ob = super().__call__()
...         if ob is not None:
...             self.__counter += 1
...             ob = (ob, self.__counter)
...         return ob
...
>>> class A:   # not in docs from here, just testing the ExtendedRef
...     pass
...
>>> a = A()
>>> r = ExtendedRef(a, foo=1, bar="
baz")
>>> r.foo
1
>>> r.bar
'baz'
>>> r()[1]
1
>>> r()[1]
2
>>> r()[0] is a
True


>>> import weakref
>>> _id2obj_dict = weakref.WeakValueDictionary()
>>> def remember(obj):
...     oid = id(obj)
...     _id2obj_dict[oid] = obj
...     return oid
...
>>> def id2obj(oid):
...     return _id2obj_dict[oid]
...
>>> a = A()             # from here, just testing
>>> a_id = remember(a)
>>> id2obj(a_id) is a
True
>>> del a
>>> try:
...     id2obj(a_id)
... except KeyError:
...     print('OK')
... else:
...     print('WeakValueDictionary error')
OK

"""

__test__ = {'libreftest' libreftest}

def test_main():
    
support.run_unittest(
        
ReferencesTestCase,
        
MappingTestCase,
        
WeakValueDictionaryTestCase,
        
WeakKeyDictionaryTestCase,
        
SubclassableWeakrefTestCase,
        )
    
support.run_doctest(sys.modules[__name__])


if 
__name__ == "__main__":
    
test_main()

:: Command execute ::

Enter:
 
Select:
 

:: Search ::
  - regexp 

:: Upload ::
 
[ Read-Only ]

:: Make Dir ::
 
[ Read-Only ]
:: Make File ::
 
[ Read-Only ]

:: Go Dir ::
 
:: Go File ::
 

--[ c99shell v. 2.0 [PHP 7 Update] [25.02.2019] maintained by KaizenLouie | C99Shell Github | Generation time: 0.013 ]--