66
77from test import test_support
88
9+ if test_support .is_jython :
10+ import time
11+
12+ def extra_collect ():
13+ """Kick Java's GC into gear"""
14+ gc .collect ()
15+ time .sleep (0.1 )
16+ gc .collect ()
17+ gc .collect ()
18+ else :
19+ def extra_collect ():
20+ pass
21+
922# Used in ReferencesTestCase.test_ref_created_during_del() .
1023ref_from_del = None
1124
@@ -69,6 +82,7 @@ def test_multiple_callbacks(self):
6982 ref1 = weakref .ref (o , self .callback )
7083 ref2 = weakref .ref (o , self .callback )
7184 del o
85+ extra_collect ()
7286 self .assert_ (ref1 () is None ,
7387 "expected reference to be invalidated" )
7488 self .assert_ (ref2 () is None ,
@@ -104,9 +118,15 @@ def test_proxy_ref(self):
104118 def check (proxy ):
105119 proxy .bar
106120
121+ extra_collect ()
107122 self .assertRaises (weakref .ReferenceError , check , ref1 )
108123 self .assertRaises (weakref .ReferenceError , check , ref2 )
109- self .assertRaises (weakref .ReferenceError , bool , weakref .proxy (C ()))
124+ # XXX: CPython GC collects C() immediately. use ref1 instead on
125+ # Jython
126+ if test_support .is_jython :
127+ self .assertRaises (weakref .ReferenceError , bool , ref1 )
128+ else :
129+ self .assertRaises (weakref .ReferenceError , bool , weakref .proxy (C ()))
110130 self .assert_ (self .cbcalled == 2 )
111131
112132 def check_basic_ref (self , factory ):
@@ -123,6 +143,7 @@ def check_basic_callback(self, factory):
123143 o = factory ()
124144 ref = weakref .ref (o , self .callback )
125145 del o
146+ extra_collect ()
126147 self .assert_ (self .cbcalled == 1 ,
127148 "callback did not properly set 'cbcalled'" )
128149 self .assert_ (ref () is None ,
@@ -147,6 +168,7 @@ def test_ref_reuse(self):
147168 self .assert_ (weakref .getweakrefcount (o ) == 2 ,
148169 "wrong weak ref count for object" )
149170 del proxy
171+ extra_collect ()
150172 self .assert_ (weakref .getweakrefcount (o ) == 1 ,
151173 "wrong weak ref count for object after deleting proxy" )
152174
@@ -292,6 +314,7 @@ def test_getweakrefcount(self):
292314 "got wrong number of weak reference objects" )
293315
294316 del ref1 , ref2 , proxy1 , proxy2
317+ extra_collect ()
295318 self .assert_ (weakref .getweakrefcount (o ) == 0 ,
296319 "weak reference objects not unlinked from"
297320 " referent when discarded." )
@@ -305,17 +328,32 @@ def test_getweakrefs(self):
305328 ref1 = weakref .ref (o , self .callback )
306329 ref2 = weakref .ref (o , self .callback )
307330 del ref1
331+ extra_collect ()
308332 self .assert_ (weakref .getweakrefs (o ) == [ref2 ],
309333 "list of refs does not match" )
310334
311335 o = C ()
312336 ref1 = weakref .ref (o , self .callback )
313337 ref2 = weakref .ref (o , self .callback )
314338 del ref2
315- self .assert_ (weakref .getweakrefs (o ) == [ref1 ],
316- "list of refs does not match" )
339+ extra_collect ()
340+ if test_support .is_jython :
341+ # XXX: Likely a Jython bug: the following inline declared
342+ # [ref1] list isn't garbage collected no matter how many
343+ # times we force gc.collect(), which prevents ref1 from
344+ # being garbage collected after it's del'd below. So we
345+ # explicitly delete our list
346+ ref1_list = [ref1 ]
347+ self .assert_ (weakref .getweakrefs (o ) == ref1_list ,
348+ #self.assert_(weakref.getweakrefs(o) == [ref1],
349+ "list of refs does not match" )
350+ del ref1_list
351+ else :
352+ self .assert_ (weakref .getweakrefs (o ) == [ref1 ],
353+ "list of refs does not match" )
317354
318355 del ref1
356+ extra_collect ()
319357 self .assert_ (weakref .getweakrefs (o ) == [],
320358 "list of refs not cleared" )
321359
@@ -594,6 +632,7 @@ def cb(self, ignore):
594632 del callback , c , d , C
595633 self .assertEqual (alist , []) # del isn't enough to clean up cycles
596634 gc .collect ()
635+ extra_collect ()
597636 self .assertEqual (alist , ["safe_callback called" ])
598637 self .assertEqual (external_wr (), None )
599638
@@ -608,9 +647,10 @@ def test_gc_during_proxy_creation(self):
608647 self .check_gc_during_creation (weakref .proxy )
609648
610649 def check_gc_during_creation (self , makeref ):
611- # XXX: threshold not applicable to Jython
612- #thresholds = gc.get_threshold()
613- #gc.set_threshold(1, 1, 1)
650+ # XXX: threshold not applicable to Jython
651+ if not test_support .is_jython :
652+ thresholds = gc .get_threshold ()
653+ gc .set_threshold (1 , 1 , 1 )
614654 gc .collect ()
615655 class A :
616656 pass
@@ -632,8 +672,8 @@ def callback(*args):
632672
633673 finally :
634674 # XXX: threshold not applicable to Jython
635- #gc.set_threshold(*thresholds)
636- pass
675+ if not test_support . is_jython :
676+ gc . set_threshold ( * thresholds )
637677
638678 def test_ref_created_during_del (self ):
639679 # Bug #1377858
@@ -743,15 +783,18 @@ def test_weak_values(self):
743783 del items1 , items2
744784 self .assert_ (len (dict ) == self .COUNT )
745785 del objects [0 ]
786+ extra_collect ()
746787 self .assert_ (len (dict ) == (self .COUNT - 1 ),
747788 "deleting object did not cause dictionary update" )
748789 del objects , o
790+ extra_collect ()
749791 self .assert_ (len (dict ) == 0 ,
750792 "deleting the values did not clear the dictionary" )
751793 # regression on SF bug #447152:
752794 dict = weakref .WeakValueDictionary ()
753795 self .assertRaises (KeyError , dict .__getitem__ , 1 )
754796 dict [2 ] = C ()
797+ extra_collect ()
755798 self .assertRaises (KeyError , dict .__getitem__ , 2 )
756799
757800 def test_weak_keys (self ):
@@ -772,9 +815,11 @@ def test_weak_keys(self):
772815 del items1 , items2
773816 self .assert_ (len (dict ) == self .COUNT )
774817 del objects [0 ]
818+ extra_collect ()
775819 self .assert_ (len (dict ) == (self .COUNT - 1 ),
776820 "deleting object did not cause dictionary update" )
777821 del objects , o
822+ extra_collect ()
778823 self .assert_ (len (dict ) == 0 ,
779824 "deleting the keys did not clear the dictionary" )
780825 o = Object (42 )
@@ -1083,6 +1128,7 @@ def _reference(self):
10831128>>> o is o2
10841129True
10851130>>> del o, o2
1131+ >>> extra_collect()
10861132>>> print r()
10871133None
10881134
@@ -1135,6 +1181,7 @@ def _reference(self):
11351181>>> id2obj(a_id) is a
11361182True
11371183>>> del a
1184+ >>> extra_collect()
11381185>>> try:
11391186... id2obj(a_id)
11401187... except KeyError:
@@ -1148,6 +1195,16 @@ def _reference(self):
11481195__test__ = {'libreftest' : libreftest }
11491196
11501197def test_main ():
1198+ if test_support .is_jython :
1199+ # Probably CPython GC specific (possibly even Jython bugs)
1200+ del ReferencesTestCase .test_callback_in_cycle_resurrection
1201+ del ReferencesTestCase .test_callbacks_on_callback
1202+
1203+ # Jython's allows objects to be weakref'd that CPyton doesn't
1204+ del MappingTestCase .test_weak_keyed_bad_delitem
1205+
1206+ # CPython GC specific
1207+ del MappingTestCase .test_weak_keyed_cascading_deletes
11511208 test_support .run_unittest (
11521209 ReferencesTestCase ,
11531210 MappingTestCase ,
0 commit comments