TranscryptOrg/Transcrypt

py_next() implementation does not support optional default value on StopIteration

JennaSys opened this issue · 1 comments

The Python version of the built-in next() function supports a 2nd argument whose value is returned when the iterator is exhausted instead of raising a StopIteration exception. This 2nd argument is currently ignored by Transcrypt:

export function py_next (iterator) {               // Called only in a Python context, could receive Python or JavaScript iterator
    try {                                   // Primarily assume Python iterator, for max speed
        var result = iterator.__next__ ();
    }
    catch (exception) {                     // JavaScript iterators are the exception here
        var result = iterator.next ();
        if (result.done) {
            throw StopIteration (new Error ());
        }
        else {
            return result.value;
        }
    }
    if (result == undefined) {
        throw StopIteration (new Error ());
    }
    else {
        return result;
    }
}