Commit 81eaa5d
authored
[mypyc] Wrap async functions with function-like type (#20260)
Currently, introspection functions from the `inspect` module like
`iscoroutinefunction(fn)` don't return the expected result for functions
compiled with mypyc. mypyc defines the functions using `PyMethodDef` in
C for which cpython makes objects with type `PyCFunction_Type` and not
`PyFunction_Type`. The latter corresponds to `types.FunctionType` in
python. So the `isfunction(fn)`
[check](https://github.com/python/cpython/blob/3.14/Lib/inspect.py#L267)
fails.
`iscoroutinefunction(fn)` and some others accept [function-like
objects](https://github.com/python/cpython/blob/3.14/Lib/inspect.py#L2070)
as long as they are callable and have some required attributes to
support compiled functions.
The most important attribute is `__code__` which is a code object that
stores flags for the function. For example for
`iscoroutinefunction(fn)`, the `CO_COROUTINE` flag is
[checked](https://github.com/python/cpython/blob/3.14/Lib/inspect.py#L329).
In this PR, mypyc adds a wrapper `CPyFunction` whose `__call__` calls
the same function that would be defined with `PyMethodDef`. The wrapper
is added to `__dict__` of either the module (for functions) or the
containing class (for methods) with the original name of the function so
the wrapper is returned in place of the `PyCFunction_Type` objects.
For nested functions, mypyc already creates a callable class so the
function-like attributes are added as properties. A static `CPyFunction`
object is created separately for each class that is used in the getter
and setter methods of the callable class.
For now `CPyFunction`s are created only for async functions to support
`iscoroutinefunction(fn)` but the implementation could be extended to
support other introspection functions in future PRs. For non-async
functions the wrapper does not seem necessary for now because the
default behavior makes the function always return false. So the behavior
is as-expected for non-async functions.
This implementation was inspired by
[Cython](https://github.com/cython/cython/blob/master/Cython/Utility/CythonFunction.c)
which generates much richer function wrappers.1 parent 7936e7e commit 81eaa5d
File tree
16 files changed
+837
-126
lines changed- mypyc
- codegen
- irbuild
- ir
- lib-rt
- primitives
- test-data
- driver
- test-data/unit/lib-stub
16 files changed
+837
-126
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| 17 | + | |
16 | 18 | | |
| 19 | + | |
17 | 20 | | |
18 | 21 | | |
19 | 22 | | |
20 | 23 | | |
21 | 24 | | |
22 | | - | |
| 25 | + | |
23 | 26 | | |
24 | 27 | | |
25 | 28 | | |
| |||
169 | 172 | | |
170 | 173 | | |
171 | 174 | | |
| 175 | + | |
172 | 176 | | |
173 | 177 | | |
174 | 178 | | |
175 | 179 | | |
176 | 180 | | |
177 | 181 | | |
178 | 182 | | |
| 183 | + | |
179 | 184 | | |
180 | 185 | | |
181 | 186 | | |
| |||
1207 | 1212 | | |
1208 | 1213 | | |
1209 | 1214 | | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + | |
1210 | 1233 | | |
1211 | 1234 | | |
1212 | 1235 | | |
| |||
1238 | 1261 | | |
1239 | 1262 | | |
1240 | 1263 | | |
| 1264 | + | |
| 1265 | + | |
| 1266 | + | |
| 1267 | + | |
| 1268 | + | |
| 1269 | + | |
| 1270 | + | |
| 1271 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
11 | | - | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
12 | 17 | | |
13 | 18 | | |
14 | 19 | | |
| |||
21 | 26 | | |
22 | 27 | | |
23 | 28 | | |
24 | | - | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
25 | 37 | | |
26 | 38 | | |
27 | 39 | | |
| |||
240 | 252 | | |
241 | 253 | | |
242 | 254 | | |
| 255 | + | |
243 | 256 | | |
244 | 257 | | |
245 | 258 | | |
| |||
347 | 360 | | |
348 | 361 | | |
349 | 362 | | |
| 363 | + | |
| 364 | + | |
350 | 365 | | |
351 | 366 | | |
352 | 367 | | |
| |||
391 | 406 | | |
392 | 407 | | |
393 | 408 | | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
394 | 413 | | |
395 | 414 | | |
396 | 415 | | |
| |||
1254 | 1273 | | |
1255 | 1274 | | |
1256 | 1275 | | |
| 1276 | + | |
| 1277 | + | |
| 1278 | + | |
| 1279 | + | |
| 1280 | + | |
| 1281 | + | |
| 1282 | + | |
| 1283 | + | |
| 1284 | + | |
| 1285 | + | |
| 1286 | + | |
| 1287 | + | |
| 1288 | + | |
| 1289 | + | |
| 1290 | + | |
| 1291 | + | |
| 1292 | + | |
| 1293 | + | |
| 1294 | + | |
| 1295 | + | |
| 1296 | + | |
| 1297 | + | |
| 1298 | + | |
| 1299 | + | |
| 1300 | + | |
| 1301 | + | |
| 1302 | + | |
| 1303 | + | |
| 1304 | + | |
| 1305 | + | |
| 1306 | + | |
| 1307 | + | |
| 1308 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | 8 | | |
10 | 9 | | |
11 | 10 | | |
| |||
18 | 17 | | |
19 | 18 | | |
20 | 19 | | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
| 20 | + | |
29 | 21 | | |
30 | 22 | | |
31 | 23 | | |
| |||
117 | 109 | | |
118 | 110 | | |
119 | 111 | | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | 112 | | |
129 | 113 | | |
130 | 114 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
36 | 37 | | |
37 | | - | |
38 | 38 | | |
| 39 | + | |
| 40 | + | |
39 | 41 | | |
40 | 42 | | |
41 | 43 | | |
| |||
566 | 568 | | |
567 | 569 | | |
568 | 570 | | |
569 | | - | |
| 571 | + | |
570 | 572 | | |
571 | 573 | | |
572 | 574 | | |
| |||
986 | 988 | | |
987 | 989 | | |
988 | 990 | | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
989 | 994 | | |
990 | 995 | | |
991 | 996 | | |
| |||
1024 | 1029 | | |
1025 | 1030 | | |
1026 | 1031 | | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
1027 | 1056 | | |
1028 | 1057 | | |
1029 | 1058 | | |
| |||
1071 | 1100 | | |
1072 | 1101 | | |
1073 | 1102 | | |
| 1103 | + | |
| 1104 | + | |
1074 | 1105 | | |
1075 | 1106 | | |
1076 | 1107 | | |
1077 | 1108 | | |
1078 | 1109 | | |
1079 | 1110 | | |
| 1111 | + | |
1080 | 1112 | | |
1081 | 1113 | | |
1082 | 1114 | | |
1083 | 1115 | | |
1084 | | - | |
| 1116 | + | |
1085 | 1117 | | |
1086 | 1118 | | |
1087 | 1119 | | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
1088 | 1130 | | |
1089 | 1131 | | |
1090 | 1132 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
| 85 | + | |
85 | 86 | | |
86 | 87 | | |
87 | 88 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
223 | 223 | | |
224 | 224 | | |
225 | 225 | | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
226 | 229 | | |
227 | 230 | | |
228 | 231 | | |
| |||
424 | 427 | | |
425 | 428 | | |
426 | 429 | | |
| 430 | + | |
427 | 431 | | |
428 | 432 | | |
429 | 433 | | |
| |||
481 | 485 | | |
482 | 486 | | |
483 | 487 | | |
| 488 | + | |
484 | 489 | | |
485 | 490 | | |
486 | 491 | | |
| |||
0 commit comments