From eca9450a8d0dfb72378fa7d1f3a5f62ced563500 Mon Sep 17 00:00:00 2001 From: Philippe Laporte Date: Wed, 11 Sep 2024 13:36:21 -0400 Subject: [PATCH 1/4] Detect exception in iterator next call and StopIteration --- src/JSObjectProxy.cc | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/JSObjectProxy.cc b/src/JSObjectProxy.cc index 13b3e609..f6c41f24 100644 --- a/src/JSObjectProxy.cc +++ b/src/JSObjectProxy.cc @@ -320,6 +320,13 @@ PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_iter_next(JSObjectProxy PyObject *retVal = JSFunctionProxyMethodDefinitions::JSFunctionProxy_call(nextFunction, PyTuple_New(0), NULL); Py_DECREF(nextFunction); + if (retVal == NULL) { + if (PyErr_Occurred()) { + PyErr_PrintEx(0); + } + PyErr_SetNone(PyExc_StopIteration); + return NULL; + } // check if end of iteration key = PyUnicode_FromString("done"); From face6db48e525fe957a45f71baceb59748cd39c7 Mon Sep 17 00:00:00 2001 From: Philippe Laporte Date: Wed, 11 Sep 2024 13:59:44 -0400 Subject: [PATCH 2/4] added relevant test --- tests/python/test_dict_methods.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/python/test_dict_methods.py b/tests/python/test_dict_methods.py index a303f73a..1fd7fcbb 100644 --- a/tests/python/test_dict_methods.py +++ b/tests/python/test_dict_methods.py @@ -555,3 +555,19 @@ def test_next_operator(): assert (True) fourth = next(myit, 'default') assert fourth == 'default' + + +def test_next_operator_non_iterator(): + make_js_generator = pm.eval(""" + function* sliceGenerator(pyIter) + { + yield python.eval('lambda x: next(x)')(pyIter); + } + sliceGenerator; + """) + + try: + next(make_js_generator(range(0,5))) + assert (False) + except StopIteration as e: + assert (True) \ No newline at end of file From 2d2dcbf3d2eb3664eb3a6bc5166bb8755709c2b5 Mon Sep 17 00:00:00 2001 From: Philippe Laporte Date: Wed, 11 Sep 2024 16:31:55 -0400 Subject: [PATCH 3/4] simplify exception logic --- src/JSObjectProxy.cc | 4 ---- tests/python/test_dict_methods.py | 5 +++-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/JSObjectProxy.cc b/src/JSObjectProxy.cc index f6c41f24..359b072d 100644 --- a/src/JSObjectProxy.cc +++ b/src/JSObjectProxy.cc @@ -321,10 +321,6 @@ PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_iter_next(JSObjectProxy PyObject *retVal = JSFunctionProxyMethodDefinitions::JSFunctionProxy_call(nextFunction, PyTuple_New(0), NULL); Py_DECREF(nextFunction); if (retVal == NULL) { - if (PyErr_Occurred()) { - PyErr_PrintEx(0); - } - PyErr_SetNone(PyExc_StopIteration); return NULL; } diff --git a/tests/python/test_dict_methods.py b/tests/python/test_dict_methods.py index 1fd7fcbb..f5d32a95 100644 --- a/tests/python/test_dict_methods.py +++ b/tests/python/test_dict_methods.py @@ -569,5 +569,6 @@ def test_next_operator_non_iterator(): try: next(make_js_generator(range(0,5))) assert (False) - except StopIteration as e: - assert (True) \ No newline at end of file + except Exception as e: + assert str(type(e)) == "" + assert str(e).__contains__("'range' object is not an iterator") \ No newline at end of file From 9c9ecf917026635261901bdcecc9be31c6ac932f Mon Sep 17 00:00:00 2001 From: Caleb Aikens Date: Wed, 11 Sep 2024 16:50:53 -0400 Subject: [PATCH 4/4] chore(tests): simplify test --- tests/python/test_dict_methods.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/python/test_dict_methods.py b/tests/python/test_dict_methods.py index f5d32a95..d0fccba9 100644 --- a/tests/python/test_dict_methods.py +++ b/tests/python/test_dict_methods.py @@ -569,6 +569,5 @@ def test_next_operator_non_iterator(): try: next(make_js_generator(range(0,5))) assert (False) - except Exception as e: - assert str(type(e)) == "" - assert str(e).__contains__("'range' object is not an iterator") \ No newline at end of file + except pm.SpiderMonkeyError as e: + assert "'range' object is not an iterator" in str(e) \ No newline at end of file