SF.net SVN: jython:[7147] trunk/jython

[email protected] Sat, 16 Oct 2010 02:55:33 +0000
Newsgroups gmane.comp.lang.jython.cvs
Message-ID <[email protected]>
Revision: 7147
          http://jython.svn.sourceforge.net/jython/?rev=7147&view=rev
Author:   zyasoft
Date:     2010-10-16 02:55:32 +0000 (Sat, 16 Oct 2010)

Log Message:
-----------
The underlying array used by PyArray allocates extra space when
extended, in case there will be future extension, much like the
support in list. (AbstractArray in fact used to be used by list.)
However, PyArray.__tojava__ needed to shrink the array when unboxing
as a regular Java array. Resolved by simply copying when size !=
capacity.

Fixes 1543.

Modified Paths:
--------------
    trunk/jython/Lib/test/test_array_jy.py
    trunk/jython/NEWS
    trunk/jython/src/org/python/core/PyArray.java

Modified: trunk/jython/Lib/test/test_array_jy.py
===================================================================
--- trunk/jython/Lib/test/test_array_jy.py	2010-10-14 12:46:59 UTC (rev 7146)
+++ trunk/jython/Lib/test/test_array_jy.py	2010-10-16 02:55:32 UTC (rev 7147)
@@ -47,7 +47,15 @@
         Color.RGBtoHSB(0, 255, 255, hsb1)
         self.assertEqual(hsb, hsb1, "hsb float arrays were not equal")
 
+    def test_java_roundtrip(self):
+        # bug 1543
+        from java.lang import Object
+        x = array(Object, [0,1,2])
+        x.append(3)
+        y = array(Object, [x]) # forces an implicit __tojava__
+        self.assertEqual(x, y[0], "Did not shrink to fit")
 
+
 class ToFromfileTestCase(unittest.TestCase):
 
     def tearDown(self):

Modified: trunk/jython/NEWS
===================================================================
--- trunk/jython/NEWS	2010-10-14 12:46:59 UTC (rev 7146)
+++ trunk/jython/NEWS	2010-10-16 02:55:32 UTC (rev 7147)
@@ -2,6 +2,7 @@
 
 Jython 2.5.2rc1
   Bugs Fixed
+    - [ 1543 ] PyArray fails to clean up pre-allocated space
     - [ 1661 ] Error at on exit in TCC/LE
     - [ 1639 ] JBoss 5, vfszip protocol in use for jarFileName in PySystemState
     - [ 1660 ] threading module memory leak

Modified: trunk/jython/src/org/python/core/PyArray.java
===================================================================
--- trunk/jython/src/org/python/core/PyArray.java	2010-10-14 12:46:59 UTC (rev 7146)
+++ trunk/jython/src/org/python/core/PyArray.java	2010-10-16 02:55:32 UTC (rev 7147)
@@ -412,7 +412,13 @@
     public Object __tojava__(Class<?> c) {
         if(c == Object.class
                 || (c.isArray() && c.getComponentType().isAssignableFrom(type))) {
-            return data;
+            if (delegate.capacity != delegate.size) {
+                // when unboxing, need to shrink the array first, otherwise incorrect
+                // results to Java
+                return delegate.copyArray();
+            } else {
+                return data;
+            }
         }
         if(c.isInstance(this))
             return this;


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.

------------------------------------------------------------------------------
Download new Adobe(R) Flash(R) Builder(TM) 4
The new Adobe(R) Flex(R) 4 and Flash(R) Builder(TM) 4 (formerly 
Flex(R) Builder(TM)) enable the development of rich applications that run
across multiple browsers and platforms. Download your free trials today!
http://p.sf.net/sfu/adobe-dev2dev