TranscryptOrg / Transcrypt

Python 3.9 to JavaScript compiler - Lean, fast, open!
https://www.transcrypt.org
Apache License 2.0
2.85k stars 214 forks source link

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

Open JennaSys opened 1 year ago

JennaSys commented 1 year ago

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;
    }
}
JennaSys commented 2 months ago

Will be added in v3.9.4