Message5256

Author xlorepdarkhelm
Recipients xlorepdarkhelm
Date 2009-10-22.17:20:46
SpamBayes Score 8.106826e-11
Marked as misclassified No
Message-id <1256232048.33.0.664679122924.issue1492@psf.upfronthosting.co.za>
In-reply-to
Content
It appears that Jython isn't handling Java Iterators out of the box.

I built a Java class that emulates a Jython List. With it, I included a hand-tailored 
Iterator, as shown below:

    /**
     * A forward-facing iterator for the Array.
     *
     * @author darkhelm
     */
    public class Iter implements Iterator<Element> {
        /**
         * The actual iterator, able to go forward or backward.
         */
        private ListIterator<Element> iter = arr.listIterator();
       
        /**
         * @return the actual iterator that is used.
         */
        protected ListIterator<Element> getIter() {
            return iter;
        }
       
        /* (non-Javadoc)
         * @see java.util.Iterator#hasNext()
         */
        @Override
        public boolean hasNext() {
            return getIter().hasNext();
        }

        /* (non-Javadoc)
         * @see java.util.Iterator#next()
         */
        @Override
        public Element next() {
            try {
                return getIter().next();
            } catch(NoSuchElementException e) {
                return null;
            }
        }

        /**
         * @return the next element from the iterator.
         */
        public Element __iternext__() {
            return next();
        }
       
        /* (non-Javadoc)
         * @see java.util.Iterator#remove()
         */
        @Override
        public void remove() {
            getIter().remove();
        }
    }

Now, when I use the following Python code, it works perfectly fine (and doesn't use the 
iterator at all):

for pos in range(len(arr)):
    count[arr[pos]] += 1

But this is rather ugly. I am using the index of one list, to look up the value of that list 
and use it as the index of a second list. The two lists are "arr" and "count", both are 
instances of my Java class that emulates a Jython list.

Now, if I try a cleaner piece of code where I just iterate over my arr instance instead:

for val in arr:
    count[val] += 1

It breaks. It appears that the iterator isn't being handled properly... under closer 
investigation, I found that for my Iterator above, I am able to change what kind of error is 
produced by modifying the next() method. The below code will produce a 
"java.lang.NullPointerException()" exception:

        /* (non-Javadoc)
         * @see java.util.Iterator#next()
         */
        @Override
        public Element next() {
            try {
                return getIter().next();
            } catch(NoSuchElementException e) {
                return null;
            }
        }

While the code below will produce a "java.lang.NoSuchElementException()" exception:

        /* (non-Javadoc)
         * @see java.util.Iterator#next()
         */
        @Override
        public Element next() {
            return getIter().next();
        }

According to the documentation from Java, the "java.lang.NoSuchElementException()" is thrown 
when an Iterator's next() method is called and there is no more elements to iterate over (it 
is how Java signals that the iterator is finished for their iterated for(Object item : 
arr){} system). When I look up how Python designates the end of an iterator, the next() 
method is supposed to return None (or null for Java, I'm assuming), which is what I tried to 
get it to do with the try/catch block in the first example.

This seems to also affect the ability to use the max() and min() functions on my Java class, 
since they rely on iterator operations as well.

The only work-around I've had is to not iterate over my Java class, which is not as elegant, 
but it works (I also had to build my own max() and min() methods for the class as I did need 
a max() and min() functionality for getting the maximum and minimum values from my class).
History
Date User Action Args
2009-10-22 17:20:48xlorepdarkhelmsetrecipients: + xlorepdarkhelm
2009-10-22 17:20:48xlorepdarkhelmsetmessageid: <1256232048.33.0.664679122924.issue1492@psf.upfronthosting.co.za>
2009-10-22 17:20:48xlorepdarkhelmlinkissue1492 messages
2009-10-22 17:20:46xlorepdarkhelmcreate