Fw: A cumbersome weakref-observation

"Stefan Richthofer" <[email protected]>
Newsgroups gmane.comp.lang.jython.devel
Message-ID <trinity-4e30d881-369e-4f39-a0b6-61acd9028f53-1437324610437@3capp-gmx-bs21>
Okay, sorry for bothering you. I already found out the reason for all this.

When handing a number to a Java-style weakref (or any other non-Jython-aware
Java method), Jython implicitly converts it to a Java-Float or Integer object,
while tuples and lists are kept PyTuple or PyList.
So the Java-style weakref actually points to a distinct object than the Jython-style one.

The reason for strings beeing "overpersistent" regarding Java-weakrefs is internation
I suppose. Jython hands the interned string to the weakref, but the interned string
is immortal anyway, so the weakref is never cleared.

Regards

Stefan


> Gesendet: Sonntag, 19. Juli 2015 um 18:43 Uhr
> Von: "Stefan Richthofer" <[email protected]>
> An: "Jython Developers" <[email protected]>
> Betreff: Fw: A cumbersome weakref-observation
>
> Update: If insert a del-statement, strings are overpersistent for Java
> style wekarefs:
> 
> import time
> from java.lang import System
> from java.lang.ref import WeakReference
> import weakref
> 
> l = "test"
> wkl = WeakReference(l)
> wkr = weakref.ref(l)
> print "wkl: "+str(wkl.get())
> print "wkr: "+str(wkr())
> del l
> System.gc()
> time.sleep(2)
> print "wkl2: "+str(wkl.get())
> print "wkr2: "+str(wkr())
> 
> 
> ----
> Output is:
> wkl: test
> wkr: test
> wkl2: test
> wkr2: None
> 
> However with tuples and lists both variants work fine.
> 
> 
> > Gesendet: Sonntag, 19. Juli 2015 um 18:06 Uhr
> > Von: "Stefan Richthofer" <[email protected]>
> > An: "Jython Developers" <[email protected]>
> > Betreff: A cumbersome weakref-observation
> >
> > Hello all, I just stumbled over some (seemingly?) strange behavior
> > regarding weak references:
> > 
> > import time
> > from java.lang import System
> > from java.lang.ref import WeakReference
> > import weakref
> > 
> > l = 123999.7
> > wkl = WeakReference(l)
> > wkr = weakref.ref(l)
> > print "wkl: "+str(wkl.get())
> > print "wkr: "+str(wkr())
> > System.gc()
> > time.sleep(2)
> > print "wkl2: "+str(wkl.get())
> > print "wkr2: "+str(wkr())
> > 
> > 
> > ----
> > Output is:
> > wkl: 123999.7
> > wkr: 123999.7
> > wkl2: None
> > wkr2: 123999.7
> > ----
> > With l = "test" the output is:
> > wkl: test
> > wkr: test
> > wkl2: test
> > wkr2: test
> > ----
> > "wkl2: None" happens for floats and ints, but not for strings, tuples and lists.
> > (These are the ones I tested so far.)
> > 
> > So the Java-style weak reference breaks for number-types, although the referent
> > is not gc'ed. Can anyone tell me how this comes? What is special about numbers
> > regarding java-style weak references?
> > 
> > I describe this behavior as "cumbersome" rather than a bug, because the official
> > weak-ref variant works fine (Why?). Still, there is no obvious reason why the Java-variant
> > should not work and I need to understand this issue. Maybe it even points to a subtle
> > bug. Looking at the weakref-module code I don't see special treatment of number-types
> > so far. What do I overlook?
> > 
> > Thanks in advance!
> > 
> > Stefan

------------------------------------------------------------------------------
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.