From: <th...@us...> - 2009-07-21 21:54:02
|
Revision: 6556 http://jython.svn.sourceforge.net/jython/?rev=6556&view=rev Author: thobes Date: 2009-07-21 21:53:56 +0000 (Tue, 21 Jul 2009) Log Message: ----------- Fix for a problem I discovered with the thread state being stored on yield in generators. This could have lead to faulty behaviour if the next resumption of the generator is from another thread or memory leaks if the generator is kept around longer than the lifetime of the thread. This fix does not have ideal memory footprint, one slot in the stored stack content array is left empty (where the thread state should have been), but it works, such minor memory optimizations can be saved for later. Modified Paths: -------------- trunk/jython/src/org/python/compiler/CodeCompiler.java Modified: trunk/jython/src/org/python/compiler/CodeCompiler.java =================================================================== --- trunk/jython/src/org/python/compiler/CodeCompiler.java 2009-07-21 20:55:55 UTC (rev 6555) +++ trunk/jython/src/org/python/compiler/CodeCompiler.java 2009-07-21 21:53:56 UTC (rev 6556) @@ -5,6 +5,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collection; +import java.util.ListIterator; import java.util.Map; import java.util.Stack; import java.util.Vector; @@ -635,15 +636,23 @@ code.iconst(stack.size()); code.anewarray("java/lang/Object"); code.astore(array); - for (int i = 0; i < stack.size(); i++) { - code.aload(array); - // Stack: |- ... value array - code.swap(); - code.iconst(i); - code.swap(); - // Stack: |- ... array index value - code.aastore(); - // Stack: |- ... + ListIterator<String> content = stack.listIterator(stack.size()); + for (int i = 0; content.hasPrevious(); i++) { + String signature = content.previous(); + if ("org/python/core/ThreadState".equals(signature)) { + // Stack: ... threadstate + code.pop(); + // Stack: ... + } else { + code.aload(array); + // Stack: |- ... value array + code.swap(); + code.iconst(i++); + code.swap(); + // Stack: |- ... array index value + code.aastore(); + // Stack: |- ... + } } return array; } else { @@ -655,12 +664,16 @@ if (stack.size() > 0) { int i = stack.size() -1; for (String signature : stack) { - code.aload(array); - // Stack: |- ... array - code.iconst(i--); - code.aaload(); - // Stack: |- ... value - code.checkcast(signature); + if ("org/python/core/ThreadState".equals(signature)) { + loadThreadState(); + } else { + code.aload(array); + // Stack: |- ... array + code.iconst(i--); + code.aaload(); + // Stack: |- ... value + code.checkcast(signature); + } } code.freeLocal(array); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |