finalizers depend on Java import
"Stefan Richthofer" <[email protected]>
| Newsgroups | gmane.comp.lang.jython.devel |
|---|---|
| Message-ID | <trinity-a57804b4-c552-4854-ba1c-6f05952228a3-1408222167252@3capp-gmx-bs03> |
Hello everybody,
I am currently doing experiments how Jython behaves regarding to finalizers (with the goal to develop a fix for issue 1057). However I observed the following strange behavior. When I encapsulate the java import in test_finalizer_builtin_oldStyleClass (see program appended at the bottom) like this
A = DummyClass("A")
if platform.system() == "Java":
from java.lang import System
#print System.identityHashCode(A)
A = None
runGCIfJython()
the test in the appended program fails. But these variants both pass:
A = DummyClass("A")
A = None
if platform.system() == "Java":
from java.lang import System
#print System.identityHashCode(A)
runGCIfJython()
if platform.system() == "Java":
from java.lang import System
#print System.identityHashCode(A)
A = DummyClass("A")
A = None
runGCIfJython()
I know, it is not so beautiful to do imports at arbitrary positions in the program, but afaIk it should still work and encapsulating Java imports produces a program that can be cross-tested in CPython without modification - so it's rather handy.
The line
print System.identityHashCode(A)
was the original usecase, but could be removed whithout changing the strange behavior.
Does somone have any clue how this comes?
Does the import establish some obscure reference to A that keeps it alive? But why does this only happen if the import happens after between A = DummyClass("A") and A = None? I looked through the Jython code, but could not yet find that a Java-import would cache existing Python objects or something. I am sure that the gc actually runs, since I also tested the variant
A = DummyClass("A")
if platform.system() == "Java":
from java.lang import System
#print System.identityHashCode(A)
A = None
B = DummyClass("B")
B = None
runGCIfJython()
It prints "B finalized (DummyClass)", so B is finalized as expected and A not.
Thanks in advance for any hints!
Stefan
import platform
import unittest
import time
def runGCIfJython():
if platform.system() == "Java":
from java.lang import System
System.gc()
time.sleep(0.1)
finalizeMsgList = []
class DummyClass():
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
def __del__(self):
finalizeMsgList.append(str(self)+" finalized (DummyClass)")
print str(self)+" finalized (DummyClass)"
class TestFinalizers(unittest.TestCase):
def test_finalizer_builtin_oldStyleClass(self):
A = DummyClass("A")
if platform.system() == "Java":
from java.lang import System
#print System.identityHashCode(A)
A = None
runGCIfJython()
assert("A finalized (DummyClass)" in finalizeMsgList)
if __name__ == '__main__':
unittest.main()
------------------------------------------------------------------------------
_______________________________________________
Jython-dev mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jython-dev