Skip to content

Commit 8f048dd

Browse files
Implement minimal builtins.anext() (RustPython#6226)
* Implement minimal builtins.anext() * Removed expectedFailure for builtins.anext() tests * Removed expectedFailure from test_contextlib_async tests fixed by anext --------- Signed-off-by: Yash Suthar <yashsuthar983@gmail.com>
1 parent fda9cee commit 8f048dd

File tree

3 files changed

+16
-38
lines changed

3 files changed

+16
-38
lines changed

Lib/test/test_asyncgen.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -603,8 +603,6 @@ async def check_anext_returning_iterator(self, aiter_class):
603603
await awaitable
604604
return "completed"
605605

606-
# TODO: RUSTPYTHON, NameError: name 'anext' is not defined
607-
@unittest.expectedFailure
608606
def test_anext_return_iterator(self):
609607
class WithIterAnext:
610608
def __aiter__(self):
@@ -614,8 +612,6 @@ def __anext__(self):
614612
result = self.loop.run_until_complete(self.check_anext_returning_iterator(WithIterAnext))
615613
self.assertEqual(result, "completed")
616614

617-
# TODO: RUSTPYTHON, NameError: name 'anext' is not defined
618-
@unittest.expectedFailure
619615
def test_anext_return_generator(self):
620616
class WithGenAnext:
621617
def __aiter__(self):
@@ -625,8 +621,6 @@ def __anext__(self):
625621
result = self.loop.run_until_complete(self.check_anext_returning_iterator(WithGenAnext))
626622
self.assertEqual(result, "completed")
627623

628-
# TODO: RUSTPYTHON, NameError: name 'anext' is not defined
629-
@unittest.expectedFailure
630624
def test_anext_await_raises(self):
631625
class RaisingAwaitable:
632626
def __await__(self):

Lib/test/test_contextlib_async.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ async def __aexit__(self, *args):
4040
manager = DefaultAsyncContextManager()
4141
manager.var = 42
4242

43-
# TODO: RUSTPYTHON
44-
@unittest.expectedFailure
4543
async def test_async_gen_propagates_generator_exit(self):
4644
# A regression test for https://bugs.python.org/issue33786.
4745

@@ -94,8 +92,6 @@ class NoneAexit(ManagerFromScratch):
9492

9593
class AsyncContextManagerTestCase(unittest.IsolatedAsyncioTestCase):
9694

97-
# TODO: RUSTPYTHON
98-
@unittest.expectedFailure
9995
async def test_contextmanager_plain(self):
10096
state = []
10197
@asynccontextmanager
@@ -109,8 +105,6 @@ async def woohoo():
109105
state.append(x)
110106
self.assertEqual(state, [1, 42, 999])
111107

112-
# TODO: RUSTPYTHON
113-
@unittest.expectedFailure
114108
async def test_contextmanager_finally(self):
115109
state = []
116110
@asynccontextmanager
@@ -185,8 +179,6 @@ class StopAsyncIterationSubclass(StopAsyncIteration):
185179
self.assertEqual(frames[0].name, 'test_contextmanager_traceback')
186180
self.assertEqual(frames[0].line, 'raise stop_exc')
187181

188-
# TODO: RUSTPYTHON
189-
@unittest.expectedFailure
190182
async def test_contextmanager_no_reraise(self):
191183
@asynccontextmanager
192184
async def whee():
@@ -196,8 +188,6 @@ async def whee():
196188
# Calling __aexit__ should not result in an exception
197189
self.assertFalse(await ctx.__aexit__(TypeError, TypeError("foo"), None))
198190

199-
# TODO: RUSTPYTHON
200-
@unittest.expectedFailure
201191
async def test_contextmanager_trap_yield_after_throw(self):
202192
@asynccontextmanager
203193
async def whoo():
@@ -213,8 +203,6 @@ async def whoo():
213203
# The "gen" attribute is an implementation detail.
214204
self.assertFalse(ctx.gen.ag_suspended)
215205

216-
# TODO: RUSTPYTHON
217-
@unittest.expectedFailure
218206
async def test_contextmanager_trap_no_yield(self):
219207
@asynccontextmanager
220208
async def whoo():
@@ -224,8 +212,6 @@ async def whoo():
224212
with self.assertRaises(RuntimeError):
225213
await ctx.__aenter__()
226214

227-
# TODO: RUSTPYTHON
228-
@unittest.expectedFailure
229215
async def test_contextmanager_trap_second_yield(self):
230216
@asynccontextmanager
231217
async def whoo():
@@ -239,8 +225,6 @@ async def whoo():
239225
# The "gen" attribute is an implementation detail.
240226
self.assertFalse(ctx.gen.ag_suspended)
241227

242-
# TODO: RUSTPYTHON
243-
@unittest.expectedFailure
244228
async def test_contextmanager_non_normalised(self):
245229
@asynccontextmanager
246230
async def whoo():
@@ -254,8 +238,6 @@ async def whoo():
254238
with self.assertRaises(SyntaxError):
255239
await ctx.__aexit__(RuntimeError, None, None)
256240

257-
# TODO: RUSTPYTHON
258-
@unittest.expectedFailure
259241
async def test_contextmanager_except(self):
260242
state = []
261243
@asynccontextmanager
@@ -301,8 +283,6 @@ class StopAsyncIterationSubclass(StopAsyncIteration):
301283
else:
302284
self.fail(f'{stop_exc} was suppressed')
303285

304-
# TODO: RUSTPYTHON
305-
@unittest.expectedFailure
306286
async def test_contextmanager_wrap_runtimeerror(self):
307287
@asynccontextmanager
308288
async def woohoo():
@@ -346,17 +326,13 @@ def test_contextmanager_doc_attrib(self):
346326
baz = self._create_contextmanager_attribs()
347327
self.assertEqual(baz.__doc__, "Whee!")
348328

349-
# TODO: RUSTPYTHON
350-
@unittest.expectedFailure
351329
@support.requires_docstrings
352330
async def test_instance_docstring_given_cm_docstring(self):
353331
baz = self._create_contextmanager_attribs()(None)
354332
self.assertEqual(baz.__doc__, "Whee!")
355333
async with baz:
356334
pass # suppress warning
357335

358-
# TODO: RUSTPYTHON
359-
@unittest.expectedFailure
360336
async def test_keywords(self):
361337
# Ensure no keyword arguments are inhibited
362338
@asynccontextmanager
@@ -365,8 +341,6 @@ async def woohoo(self, func, args, kwds):
365341
async with woohoo(self=11, func=22, args=33, kwds=44) as target:
366342
self.assertEqual(target, (11, 22, 33, 44))
367343

368-
# TODO: RUSTPYTHON
369-
@unittest.expectedFailure
370344
async def test_recursive(self):
371345
depth = 0
372346
ncols = 0
@@ -393,8 +367,6 @@ async def recursive():
393367
self.assertEqual(ncols, 10)
394368
self.assertEqual(depth, 0)
395369

396-
# TODO: RUSTPYTHON
397-
@unittest.expectedFailure
398370
async def test_decorator(self):
399371
entered = False
400372

@@ -413,8 +385,6 @@ async def test():
413385
await test()
414386
self.assertFalse(entered)
415387

416-
# TODO: RUSTPYTHON
417-
@unittest.expectedFailure
418388
async def test_decorator_with_exception(self):
419389
entered = False
420390

@@ -437,8 +407,6 @@ async def test():
437407
await test()
438408
self.assertFalse(entered)
439409

440-
# TODO: RUSTPYTHON
441-
@unittest.expectedFailure
442410
async def test_decorating_method(self):
443411

444412
@asynccontextmanager

vm/src/stdlib/builtins.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,22 @@ mod builtins {
534534
iter_target.get_aiter(vm)
535535
}
536536

537+
#[pyfunction]
538+
fn anext(
539+
aiter: PyObjectRef,
540+
default_value: OptionalArg<PyObjectRef>,
541+
vm: &VirtualMachine,
542+
) -> PyResult {
543+
let awaitable = vm.call_method(&aiter, "__anext__", ())?;
544+
545+
if default_value.is_missing() {
546+
Ok(awaitable)
547+
} else {
548+
// TODO: Implement CPython like PyAnextAwaitable to properly handle the default value.
549+
Ok(awaitable)
550+
}
551+
}
552+
537553
#[pyfunction]
538554
fn len(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
539555
obj.length(vm)

0 commit comments

Comments
 (0)