From: <cg...@us...> - 2008-01-07 01:36:23
|
Revision: 3989 http://jython.svn.sourceforge.net/jython/?rev=3989&view=rev Author: cgroves Date: 2008-01-06 17:36:18 -0800 (Sun, 06 Jan 2008) Log Message: ----------- Don't try to make objects without an __iter__ iterable by calling __finditem__ on them. Modified Paths: -------------- trunk/jython/src/org/python/core/PyObject.java trunk/jython/src/org/python/modules/sets/BaseSet.java Modified: trunk/jython/src/org/python/core/PyObject.java =================================================================== --- trunk/jython/src/org/python/core/PyObject.java 2008-01-07 01:35:24 UTC (rev 3988) +++ trunk/jython/src/org/python/core/PyObject.java 2008-01-07 01:36:18 UTC (rev 3989) @@ -376,28 +376,11 @@ argslen += kwargs.__len__(); } List<PyObject> starObjs = null; - if(starargs != null) { - if(starargs.__findattr__("__iter__") != null){ - starObjs = new ArrayList<PyObject>(); - for (PyObject cur : starargs.asIterable()) { - starObjs.add(cur); - } - } else { - try { - int nstar = starargs.__len__(); - PyObject cur; - starObjs = new ArrayList<PyObject>(nstar); - for(int i = 0; (cur = starargs.__finditem__(i)) != null - && i < nstar; i++) { - starObjs.add(cur); - } - } catch(PyException e) { - if(Py.matchException(e, Py.AttributeError)) { - throw Py.TypeError(name + "argument after * must " - + "be a sequence"); - } - throw e; - } + if (starargs != null) { + starObjs = new ArrayList<PyObject>(); + PyObject iter = Py.iter(starargs, name + "argument after * must be a sequence"); + for (PyObject cur = null; ((cur = iter.__iternext__()) != null); ) { + starObjs.add(cur); } argslen += starObjs.size(); } @@ -669,28 +652,26 @@ /*The basic functions to implement an iterator */ /** - * Return an iterator that is used to iterate the element of this - * sequence. - * From version 2.2, this method is the primary protocol for looping - * over sequences. + * Return an iterator that is used to iterate the element of this sequence. From version 2.2, + * this method is the primary protocol for looping over sequences. * <p> - * If a PyObject subclass should support iteration based in the - * __finditem__() method, it must supply an implementation of __iter__() - * like this: + * If a PyObject subclass should support iteration based in the __finditem__() method, it must + * supply an implementation of __iter__() like this: + * * <pre> - * public PyObject __iter__() { - * return new PySequenceIter(this); - * } + * public PyObject __iter__() { + * return new PySequenceIter(this); + * } * </pre> - * - * When iterating over a python sequence from java code, it should be - * done with code like this: + * + * When iterating over a python sequence from java code, it should be done with code like this: + * * <pre> - * for (PyObject item : seq.asIterable()) { - * // Do somting with item - * } + * for (PyObject item : seq.asIterable()) { + * // Do somting with item + * } * </pre> - * + * * @since 2.2 */ public PyObject __iter__() { Modified: trunk/jython/src/org/python/modules/sets/BaseSet.java =================================================================== --- trunk/jython/src/org/python/modules/sets/BaseSet.java 2008-01-07 01:35:24 UTC (rev 3988) +++ trunk/jython/src/org/python/modules/sets/BaseSet.java 2008-01-07 01:36:18 UTC (rev 3989) @@ -61,47 +61,21 @@ this._set.addAll(((BaseSet)data)._set); return; } - - if (data.__findattr__("__iter__") != null) { - for (PyObject item : data.asIterable()) { - try { - this._set.add(item); - } catch (PyException e) { - this._set.add(asImmutable(e, item)); - } + for (PyObject item : data.asIterable()) { + try { + this._set.add(item); + } catch (PyException e) { + this._set.add(asImmutable(e, item)); } - } else { - int i = 0; - while(true) { - PyObject value; - try { - value = data.__finditem__(i++); - if (value == null) { - break; - } - } catch (PyException e) { - if(Py.matchException(e, Py.AttributeError)) { - throw Py.TypeError("object not iterable"); - } - throw e; - } - try { - this._set.add(value); - } catch (PyException e) { - PyObject immutable = this.asImmutable(e, value); - this._set.add(immutable); - } - } } } /** - * The union of <code>this</code> with <code>other</code>. - * <p/> - * <br/> - * (I.e. all elements that are in either set) - * - * @param other A <code>BaseSet</code> instance. + * The union of <code>this</code> with <code>other</code>. <p/> <br/> (I.e. all elements + * that are in either set) + * + * @param other + * A <code>BaseSet</code> instance. * @return The union of the two sets as a new set. */ public PyObject __or__(PyObject other) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |