Skip to content

Commit 2494d46

Browse files
author
Adam Hrbac
committed
[GR-28389] Add basic asyncio
PullRequest: graalpython/2517
2 parents 46e90c8 + cccf417 commit 2494d46

39 files changed

+3604
-441
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/compiler/CompilerTests.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -1055,6 +1055,24 @@ public void testYieldOutsideFunction() {
10551055
checkSyntaxErrorMessage("class C: yield 1", "'yield' outside function");
10561056
}
10571057

1058+
@Test
1059+
public void testReturnFromAsyncWith() {
1060+
String source = "async def f():\n" +
1061+
" async with a:\n" +
1062+
" return";
1063+
doTest(source);
1064+
}
1065+
1066+
@Test
1067+
public void testReturnFromAsyncWithT() {
1068+
String source = "async def f():\n" +
1069+
" async with a:\n" +
1070+
" async with b:\n" +
1071+
" return";
1072+
doTest(source);
1073+
1074+
}
1075+
10581076
private void doTest(String src) {
10591077
doTest(src, InputType.FILE);
10601078
}

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_asyncio.txt

Lines changed: 1655 additions & 0 deletions
Large diffs are not rendered by default.

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_coroutines.txt

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,32 @@
44
*graalpython.lib-python.3.test.test_coroutines.CAPITest.test_tp_await_1
55
*graalpython.lib-python.3.test.test_coroutines.CAPITest.test_tp_await_2
66
*graalpython.lib-python.3.test.test_coroutines.CAPITest.test_tp_await_3
7-
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_func_3
8-
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_func_4
9-
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_func_5
7+
*graalpython.lib-python.3.test.test_coroutines.CoroAsyncIOCompatTest.test_asyncio_1
8+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_await_1
9+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_await_10
10+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_await_11
11+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_await_13
12+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_await_16
13+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_await_2
14+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_await_4
15+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_await_5
16+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_await_6
17+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_await_7
18+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_await_8
19+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_await_9
20+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_comp_1
21+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_comp_10
22+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_comp_2
23+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_comp_8
24+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_func_19
25+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_func_6
26+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_func_8
1027
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_gen_1
28+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_stack_in_coroutine_throw
29+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_with_1
30+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_with_10
31+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_with_12
32+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_with_5
33+
*graalpython.lib-python.3.test.test_coroutines.CoroutineTest.test_with_9
1134
*graalpython.lib-python.3.test.test_coroutines.TokenizerRegrTest.test_oneline_defs
1235
*graalpython.lib-python.3.test.test_coroutines.UnawaitedWarningDuringShutdownTest.test_unawaited_warning_during_shutdown
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
Disassembly of <module>:
2+
1:0 - 3:11 0 MAKE_FUNCTION 0 (f)
3+
1:0 - 3:11 3 STORE_NAME 0 (f)
4+
1:0 - 3:11 5 LOAD_NONE
5+
1:0 - 3:11 6 RETURN_VALUE
6+
7+
Disassembly of f:
8+
Flags: CO_COROUTINE
9+
2:13 - 2:14 0 LOAD_GLOBAL 0 (a)
10+
2:2 - 3:11 2 SETUP_AWITH
11+
2:2 - 3:11 3 GET_AWAITABLE
12+
2:2 - 3:11 4 LOAD_NONE
13+
2:2 - 3:11 >> 5 SEND 10 (to 15)
14+
2:2 - 3:11 >> 7 YIELD_VALUE
15+
2:2 - 3:11 8 RESUME_YIELD
16+
2:2 - 3:11 9 JUMP_BACKWARD 4 (to 5)
17+
2:2 - 3:11 11 THROW 4 (exc handler 8 - 11; stack: 3)
18+
2:2 - 3:11 13 JUMP_BACKWARD 6 (to 7)
19+
2:2 - 3:11 >> 15 POP_TOP
20+
2:2 - 3:11 16 LOAD_NONE
21+
2:2 - 3:11 17 GET_AEXIT_CORO
22+
2:2 - 3:11 18 GET_AWAITABLE
23+
2:2 - 3:11 19 LOAD_NONE
24+
2:2 - 3:11 >> 20 SEND 10 (to 30)
25+
2:2 - 3:11 >> 22 YIELD_VALUE
26+
2:2 - 3:11 23 RESUME_YIELD
27+
2:2 - 3:11 24 JUMP_BACKWARD 4 (to 20)
28+
2:2 - 3:11 26 THROW 4 (exc handler 23 - 26; stack: 2)
29+
2:2 - 3:11 28 JUMP_BACKWARD 6 (to 22)
30+
2:2 - 3:11 >> 30 EXIT_AWITH
31+
2:2 - 3:11 31 LOAD_NONE
32+
2:2 - 3:11 32 RETURN_VALUE
33+
2:2 - 3:11 33 LOAD_NONE
34+
2:2 - 3:11 34 GET_AEXIT_CORO (exc handler 3 - 8; stack: 2 | exc handler 11 - 16; stack: 2)
35+
2:2 - 3:11 35 GET_AWAITABLE
36+
2:2 - 3:11 36 LOAD_NONE
37+
2:2 - 3:11 >> 37 SEND 10 (to 47)
38+
2:2 - 3:11 >> 39 YIELD_VALUE
39+
2:2 - 3:11 40 RESUME_YIELD
40+
2:2 - 3:11 41 JUMP_BACKWARD 4 (to 37)
41+
2:2 - 3:11 43 THROW 4 (exc handler 40 - 43; stack: 2)
42+
2:2 - 3:11 45 JUMP_BACKWARD 6 (to 39)
43+
2:2 - 3:11 >> 47 EXIT_AWITH
44+
2:2 - 3:11 48 LOAD_NONE
45+
2:2 - 3:11 49 RETURN_VALUE
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
Disassembly of <module>:
2+
1:0 - 4:12 0 MAKE_FUNCTION 0 (f)
3+
1:0 - 4:12 3 STORE_NAME 0 (f)
4+
1:0 - 4:12 5 LOAD_NONE
5+
1:0 - 4:12 6 RETURN_VALUE
6+
7+
Disassembly of f:
8+
Flags: CO_COROUTINE
9+
2:13 - 2:14 0 LOAD_GLOBAL 0 (a)
10+
2:2 - 4:12 2 SETUP_AWITH
11+
2:2 - 4:12 3 GET_AWAITABLE
12+
2:2 - 4:12 4 LOAD_NONE
13+
2:2 - 4:12 >> 5 SEND 10 (to 15)
14+
2:2 - 4:12 >> 7 YIELD_VALUE
15+
2:2 - 4:12 8 RESUME_YIELD
16+
2:2 - 4:12 9 JUMP_BACKWARD 4 (to 5)
17+
2:2 - 4:12 11 THROW 4 (exc handler 8 - 11; stack: 3)
18+
2:2 - 4:12 13 JUMP_BACKWARD 6 (to 7)
19+
2:2 - 4:12 >> 15 POP_TOP
20+
3:15 - 3:16 16 LOAD_GLOBAL 1 (b)
21+
3:4 - 4:12 18 SETUP_AWITH
22+
3:4 - 4:12 19 GET_AWAITABLE
23+
3:4 - 4:12 20 LOAD_NONE
24+
3:4 - 4:12 >> 21 SEND 10 (to 31)
25+
3:4 - 4:12 >> 23 YIELD_VALUE
26+
3:4 - 4:12 24 RESUME_YIELD
27+
3:4 - 4:12 25 JUMP_BACKWARD 4 (to 21)
28+
3:4 - 4:12 27 THROW 4 (exc handler 24 - 27; stack: 5)
29+
3:4 - 4:12 29 JUMP_BACKWARD 6 (to 23)
30+
3:4 - 4:12 >> 31 POP_TOP
31+
3:4 - 4:12 32 LOAD_NONE
32+
3:4 - 4:12 33 GET_AEXIT_CORO
33+
3:4 - 4:12 34 GET_AWAITABLE
34+
3:4 - 4:12 35 LOAD_NONE
35+
3:4 - 4:12 >> 36 SEND 10 (to 46)
36+
3:4 - 4:12 >> 38 YIELD_VALUE
37+
3:4 - 4:12 39 RESUME_YIELD
38+
3:4 - 4:12 40 JUMP_BACKWARD 4 (to 36)
39+
3:4 - 4:12 42 THROW 4 (exc handler 39 - 42; stack: 4)
40+
3:4 - 4:12 44 JUMP_BACKWARD 6 (to 38)
41+
3:4 - 4:12 >> 46 EXIT_AWITH
42+
2:2 - 4:12 47 LOAD_NONE
43+
2:2 - 4:12 48 GET_AEXIT_CORO
44+
2:2 - 4:12 49 GET_AWAITABLE
45+
2:2 - 4:12 50 LOAD_NONE
46+
2:2 - 4:12 >> 51 SEND 10 (to 61)
47+
2:2 - 4:12 >> 53 YIELD_VALUE
48+
2:2 - 4:12 54 RESUME_YIELD
49+
2:2 - 4:12 55 JUMP_BACKWARD 4 (to 51)
50+
2:2 - 4:12 57 THROW 4 (exc handler 54 - 57; stack: 2)
51+
2:2 - 4:12 59 JUMP_BACKWARD 6 (to 53)
52+
2:2 - 4:12 >> 61 EXIT_AWITH
53+
2:2 - 4:12 62 LOAD_NONE
54+
2:2 - 4:12 63 RETURN_VALUE
55+
2:2 - 4:12 64 LOAD_NONE
56+
3:4 - 4:12 65 GET_AEXIT_CORO (exc handler 19 - 24; stack: 4 | exc handler 27 - 32; stack: 4)
57+
3:4 - 4:12 66 GET_AWAITABLE
58+
3:4 - 4:12 67 LOAD_NONE
59+
3:4 - 4:12 >> 68 SEND 10 (to 78)
60+
3:4 - 4:12 >> 70 YIELD_VALUE
61+
3:4 - 4:12 71 RESUME_YIELD
62+
3:4 - 4:12 72 JUMP_BACKWARD 4 (to 68)
63+
3:4 - 4:12 74 THROW 4 (exc handler 71 - 74; stack: 4)
64+
3:4 - 4:12 76 JUMP_BACKWARD 6 (to 70)
65+
3:4 - 4:12 >> 78 EXIT_AWITH
66+
3:4 - 4:12 79 LOAD_NONE
67+
2:2 - 4:12 80 GET_AEXIT_CORO (exc handler 3 - 8; stack: 2 | exc handler 11 - 19; stack: 2 | exc handler 32 - 39; stack: 2 | exc handler 42 - 47; stack: 2 | exc handler 65 - 71; stack: 2 | exc handler 74 - 80; stack: 2)
68+
2:2 - 4:12 81 GET_AWAITABLE
69+
2:2 - 4:12 82 LOAD_NONE
70+
2:2 - 4:12 >> 83 SEND 10 (to 93)
71+
2:2 - 4:12 >> 85 YIELD_VALUE
72+
2:2 - 4:12 86 RESUME_YIELD
73+
2:2 - 4:12 87 JUMP_BACKWARD 4 (to 83)
74+
2:2 - 4:12 89 THROW 4 (exc handler 86 - 89; stack: 2)
75+
2:2 - 4:12 91 JUMP_BACKWARD 6 (to 85)
76+
2:2 - 4:12 >> 93 EXIT_AWITH
77+
2:2 - 4:12 94 LOAD_NONE
78+
2:2 - 4:12 95 RETURN_VALUE

graalpython/com.oracle.graal.python.test/testData/goldenFiles/CompilerTests/testYieldFrom.co

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Disassembly of <module>:
77
Disassembly of gen:
88
Flags: CO_GENERATOR
99
2:15 - 2:16 0 LOAD_FAST 0 (a)
10-
2:4 - 2:16 2 GET_ITER
10+
2:4 - 2:16 2 GET_YIELD_FROM_ITER
1111
2:4 - 2:16 3 LOAD_NONE
1212
2:4 - 2:16 >> 4 SEND 10 (to 14)
1313
2:4 - 2:16 >> 6 YIELD_VALUE

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import com.oracle.graal.python.PythonLanguage;
6262
import com.oracle.graal.python.builtins.modules.AbcModuleBuiltins;
6363
import com.oracle.graal.python.builtins.modules.ArrayModuleBuiltins;
64+
import com.oracle.graal.python.builtins.modules.AsyncioModuleBuiltins;
6465
import com.oracle.graal.python.builtins.modules.AtexitModuleBuiltins;
6566
import com.oracle.graal.python.builtins.modules.BinasciiModuleBuiltins;
6667
import com.oracle.graal.python.builtins.modules.BuiltinConstructors;
@@ -266,7 +267,9 @@
266267
import com.oracle.graal.python.builtins.objects.function.PArguments;
267268
import com.oracle.graal.python.builtins.objects.function.PFunction;
268269
import com.oracle.graal.python.builtins.objects.function.WrapperDescriptorBuiltins;
270+
import com.oracle.graal.python.builtins.objects.generator.CommonGeneratorBuiltins;
269271
import com.oracle.graal.python.builtins.objects.generator.CoroutineBuiltins;
272+
import com.oracle.graal.python.builtins.objects.asyncio.CoroutineWrapperBuiltins;
270273
import com.oracle.graal.python.builtins.objects.generator.GeneratorBuiltins;
271274
import com.oracle.graal.python.builtins.objects.getsetdescriptor.DescriptorBuiltins;
272275
import com.oracle.graal.python.builtins.objects.getsetdescriptor.GetSetDescriptorTypeBuiltins;
@@ -498,6 +501,8 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
498501
new ForeignIteratorBuiltins(),
499502
new GeneratorBuiltins(),
500503
new CoroutineBuiltins(),
504+
new CoroutineWrapperBuiltins(),
505+
new CommonGeneratorBuiltins(),
501506
new AbstractFunctionBuiltins(),
502507
new FunctionBuiltins(),
503508
new BuiltinFunctionBuiltins(),
@@ -756,7 +761,11 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
756761

757762
// _hpy_universal and _hpy_debug
758763
new GraalHPyUniversalModuleBuiltins(),
759-
new GraalHPyDebugModuleBuiltins()));
764+
new GraalHPyDebugModuleBuiltins(),
765+
new GraalHPyDebugModuleBuiltins(),
766+
767+
// _asyncio
768+
new AsyncioModuleBuiltins()));
760769
if (hasProfilerTool) {
761770
builtins.add(new LsprofModuleBuiltins());
762771
builtins.add(LsprofModuleBuiltins.newProfilerBuiltins());

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/PythonBuiltinClassType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public enum PythonBuiltinClassType implements TruffleObject {
128128
PFunction("function", Flags.PRIVATE_DERIVED_WDICT),
129129
PGenerator("generator", Flags.PRIVATE_DERIVED_WODICT),
130130
PCoroutine("coroutine", Flags.PRIVATE_DERIVED_WODICT),
131+
PCoroutineWrapper("coroutine_wrapper", Flags.PRIVATE_DERIVED_WODICT),
131132
PAsyncGenerator("async_generator", Flags.PRIVATE_DERIVED_WODICT),
132133
PInt("int", J_BUILTINS),
133134
PList("list", J_BUILTINS),

0 commit comments

Comments
 (0)