Exemplo n.º 1
0
    def test_python_fn_kwargs(self):

        weakrefs = []
        sentinel = []

        def watch(obj, kwarg=True):
            self.assertEqual(kwarg, True)
            # log the death of the reference by appending to the sentinel
            ref = weakref.ref(obj, sentinel.append)
            weakrefs.append(ref)

        self.assertTrue(not sentinel)

        thunk1 = lambda: None
        watch(thunk1)
        self.assertTrue(not sentinel)

        del thunk1
        extra_collect()
        self.assertTrue(sentinel)

        del sentinel[:]

        thunk2 = lambda: None
        watch(thunk2, kwarg=True)  # <--- only difference: called with a kwarg
        self.assertTrue(not sentinel)

        del thunk2
        extra_collect()
        self.assertTrue(sentinel)
Exemplo n.º 2
0
    def testLeaks(self):
        class Foo:
            count = 0

            def __init__(self):
                Foo.count += 1

            def __del__(self):
                Foo.count -= 1

        def f1():
            x = Foo()

            def f2():
                return x

            f2()

        for i in range(100):
            f1()

        if is_jython:
            from test_weakref import extra_collect
            # A lot of garbage
            for i in range(3):
                extra_collect()
        self.assertEqual(Foo.count, 0)
Exemplo n.º 3
0
    def testLeaks(self):

        class Foo:
            count = 0

            def __init__(self):
                Foo.count += 1

            def __del__(self):
                Foo.count -= 1

        def f1():
            x = Foo()
            def f2():
                return x
            f2()

        for i in range(100):
            f1()

        if is_jython:
            from test_weakref import extra_collect
            # A lot of garbage
            for i in range(3):
                extra_collect()
        self.assertEqual(Foo.count, 0)
Exemplo n.º 4
0
 def test_weakref(self):
     s = array.array(self.typecode, self.example)
     p = proxy(s)
     self.assertEqual(p.tostring(), s.tostring())
     s = None
     if test_support.is_jython:
         extra_collect()
     self.assertRaises(ReferenceError, len, p)
Exemplo n.º 5
0
 def test_weakref(self):
     s = array.array(self.typecode, self.example)
     p = proxy(s)
     self.assertEqual(p.tostring(), s.tostring())
     s = None
     if test_support.is_jython:
         extra_collect()
     self.assertRaises(ReferenceError, len, p)
Exemplo n.º 6
0
 def test_weakref(self):
     d = deque('gallahad')
     p = proxy(d)
     self.assertEqual(str(p), str(d))
     d = None
     if test_support.is_jython:
         from test_weakref import extra_collect
         extra_collect()
     self.assertRaises(ReferenceError, str, p)
Exemplo n.º 7
0
 def test_weakref(self):
     f = self.thetype(int, base=16)
     p = proxy(f)
     self.assertEqual(f.func, p.func)
     f = None
     if test_support.is_jython:
         from test_weakref import extra_collect
         extra_collect()
     self.assertRaises(ReferenceError, getattr, p, 'func')
Exemplo n.º 8
0
 def test_weakref(self):
     d = deque('gallahad')
     p = weakref.proxy(d)
     self.assertEqual(str(p), str(d))
     d = None
     if test_support.is_jython:
         from test_weakref import extra_collect
         extra_collect()
     self.assertRaises(ReferenceError, str, p)
Exemplo n.º 9
0
 def test_weakref(self):
     f = self.thetype(int, base=16)
     p = proxy(f)
     self.assertEqual(f.func, p.func)
     f = None
     if test_support.is_jython:
         from test_weakref import extra_collect
         extra_collect()
     self.assertRaises(ReferenceError, getattr, p, 'func')
Exemplo n.º 10
0
 def testWeakRefs(self):
     # verify weak references
     p = proxy(self.f)
     p.write('teststring')
     self.assertEquals(self.f.tell(), p.tell())
     self.f.close()
     self.f = None
     if is_jython:
         from test_weakref import extra_collect
         extra_collect()
     self.assertRaises(ReferenceError, getattr, p, 'tell')
Exemplo n.º 11
0
 def testWeakRefs(self):
     # verify weak references
     p = proxy(self.f)
     p.write('teststring')
     self.assertEquals(self.f.tell(), p.tell())
     self.f.close()
     self.f = None
     if is_jython:
         from test_weakref import extra_collect
         extra_collect()
     self.assertRaises(ReferenceError, getattr, p, 'tell')
Exemplo n.º 12
0
 def testWeakRefs(self):
     # verify weak references
     p = proxy(self.f)
     p.write('teststring')
     self.assertEqual(self.f.tell(), p.tell())
     self.f.close()
     self.f = None
     if test_support.is_jython: # GC is not immediate: borrow a trick
         from test_weakref import extra_collect
         extra_collect()
     self.assertRaises(ReferenceError, getattr, p, 'tell')
Exemplo n.º 13
0
 def testWeakRefs(self):
     # verify weak references
     p = proxy(self.f)
     p.write('teststring')
     self.assertEqual(self.f.tell(), p.tell())
     self.f.close()
     self.f = None
     if test_support.is_jython:  # GC is not immediate: borrow a trick
         from test_weakref import extra_collect
         extra_collect()
     self.assertRaises(ReferenceError, getattr, p, 'tell')
Exemplo n.º 14
0
 def tearDownClass(cls):
     if test.test_support.is_jython:
         # GC is not immediate, so if Popen.__del__ may be delayed.
         # Try to force any Popen.__del__ errors within scope of test.
         from test_weakref import extra_collect
         extra_collect()
Exemplo n.º 15
0
testme < 1
testme > 1
testme <> 1
testme != 1
1 == testme
1 < testme
1 > testme
1 <> testme
1 != testme

# This test has to be last (duh.)

del testme
if sys.platform[:4] == 'java':
    from test_weakref import extra_collect
    extra_collect()

# Interfering tests

class ExtraTests:
    def __getattr__(self, *args):
        print "__getattr__:", args
        return "SomeVal"

    def __setattr__(self, *args):
        print "__setattr__:", args

    def __delattr__(self, *args):
        print "__delattr__:", args

testme = ExtraTests()
Exemplo n.º 16
0
    x = Foo()

    def f2():
        return x

    f2()


for i in range(100):
    f1()

if is_jython:
    from test_weakref import extra_collect
    # A lot of garbage
    for i in range(3):
        extra_collect()
vereq(Foo.count, 0)

print "17. class and global"


def test(x):
    class Foo:
        global x

        def __call__(self, y):
            return x + y

    return Foo()

Exemplo n.º 17
0
 def tearDownClass(cls):
     if is_jython:
         # GC is not immediate, so if Popen.__del__ may be delayed.
         # Try to force Popen.__del__ within scope of test.
         from test_weakref import extra_collect
         extra_collect()
Exemplo n.º 18
0
    def test_unpack_iter(self):
        a, b = 1, 2
        self.assertEqual((a, b), (1, 2))

        a, b, c = IteratingSequenceClass(3)
        self.assertEqual((a, b, c), (0, 1, 2))

        try:    # too many values
            a, b = IteratingSequenceClass(3)
        except ValueError:
            pass
        else:
            self.fail("should have raised ValueError")

        try:    # not enough values
            a, b, c = IteratingSequenceClass(2)
        except ValueError:
            pass
        else:
            self.fail("should have raised ValueError")

        try:    # not iterable
            a, b, c = len
        except TypeError:
            pass
        else:
            self.fail("should have raised TypeError")

        a, b, c = iter({1: 42, 2: 42, 3: 42}.values())
        self.assertEqual((a, b, c), (42, 42, 42))

        f = open(TESTFN, "w")
        lines = ("a\n", "bb\n", "ccc\n")
        try:
            for line in lines:
                f.write(line)
        finally:
            f.close()
        f = open(TESTFN, "r")
        try:
            a, b, c = f
            self.assertEqual((a, b, c), lines)
        finally:
            f.close()
            try:
                unlink(TESTFN)
            except OSError:
                pass

        (a, b), (c,) = IteratingSequenceClass(2), {42: 24}
        self.assertEqual((a, b, c), (0, 1, 42))

        # Test reference count behavior

        # XXX: Jython new style objects don't support __del__ yet
        from test_weakref import extra_collect
        #class C(object):
        class C:
            count = 0
            #def __new__(cls):
            def __init__(self):
                cls = C
                cls.count += 1
                #return object.__new__(cls)
            def __del__(self):
                cls = self.__class__
                assert cls.count > 0
                cls.count -= 1
        x = C()
        self.assertEqual(C.count, 1)
        del x
        extra_collect()
        self.assertEqual(C.count, 0)
        l = [C(), C(), C()]
        self.assertEqual(C.count, 3)
        try:
            a, b = iter(l)
        except ValueError:
            pass
        del l
        extra_collect()
        self.assertEqual(C.count, 0)
Exemplo n.º 19
0
 def tearDownClass(cls):
     if test.test_support.is_jython:
         # GC is not immediate, so Popen.__del__ may be delayed.
         # Try to force any Popen.__del__ errors within scope of test.
         from test_weakref import extra_collect
         extra_collect()
Exemplo n.º 20
0
    def test_unpack_iter(self):
        a, b = 1, 2
        self.assertEqual((a, b), (1, 2))

        a, b, c = IteratingSequenceClass(3)
        self.assertEqual((a, b, c), (0, 1, 2))

        try:  # too many values
            a, b = IteratingSequenceClass(3)
        except ValueError:
            pass
        else:
            self.fail("should have raised ValueError")

        try:  # not enough values
            a, b, c = IteratingSequenceClass(2)
        except ValueError:
            pass
        else:
            self.fail("should have raised ValueError")

        try:  # not iterable
            a, b, c = len
        except TypeError:
            pass
        else:
            self.fail("should have raised TypeError")

        a, b, c = {1: 42, 2: 42, 3: 42}.itervalues()
        self.assertEqual((a, b, c), (42, 42, 42))

        f = open(TESTFN, "w")
        lines = ("a\n", "bb\n", "ccc\n")
        try:
            for line in lines:
                f.write(line)
        finally:
            f.close()
        f = open(TESTFN, "r")
        try:
            a, b, c = f
            self.assertEqual((a, b, c), lines)
        finally:
            f.close()
            try:
                unlink(TESTFN)
            except OSError:
                pass

        (a, b), (c, ) = IteratingSequenceClass(2), {42: 24}
        self.assertEqual((a, b, c), (0, 1, 42))

        # Test reference count behavior

        # XXX: Jython new style objects don't support __del__ yet
        from test_weakref import extra_collect

        #class C(object):
        class C:
            count = 0

            #def __new__(cls):
            def __init__(self):
                cls = C
                cls.count += 1
                #return object.__new__(cls)
            def __del__(self):
                cls = self.__class__
                assert cls.count > 0
                cls.count -= 1

        x = C()
        self.assertEqual(C.count, 1)
        del x
        extra_collect()
        self.assertEqual(C.count, 0)
        l = [C(), C(), C()]
        self.assertEqual(C.count, 3)
        try:
            a, b = iter(l)
        except ValueError:
            pass
        del l
        extra_collect()
        self.assertEqual(C.count, 0)