|
15 | 15 |
|
16 | 16 | 1 фаза: |
17 | 17 |
|
18 | | -8. Работают замыкания. |
19 | | -9. Работают чтение глобальных переменных. |
| 18 | +9. Работает чтение глобальных переменных. |
20 | 19 | 10. Работает директива nonlocal. |
21 | 20 | 11. Работает директива global. |
22 | 21 | 12. Декоратор @transfunction можно использовать на методах (в т.ч. асинк и генераторных). |
|
45 | 44 | 13. В декоратор @transfunction нельзя скормить лямбду или число. |
46 | 45 | 17. Нельзя вызывать трансформер напрямую. При попытке это сделать вылетает информативное исключение, причем как при передаче аргументов, так и нет. |
47 | 46 | 7. Декоратор @transfunction нельзя использовать дважды на одной функции. |
| 47 | +8. Работает чтение из замыканий (в том числе для функций с аргументами). |
48 | 48 | """ |
49 | 49 |
|
50 | 50 | @transfunction |
@@ -328,3 +328,71 @@ def make(): |
328 | 328 | function = make.get_usual_function() |
329 | 329 |
|
330 | 330 | assert function() == 1 |
| 331 | + |
| 332 | + |
| 333 | +def test_read_closures_with_usual_function_with_arguments(): |
| 334 | + nonlocal_variable = 1 |
| 335 | + |
| 336 | + @transfunction |
| 337 | + def make(some_number): |
| 338 | + #nonlocal nonlocal_variable |
| 339 | + return nonlocal_variable + some_number |
| 340 | + |
| 341 | + function = make.get_usual_function() |
| 342 | + |
| 343 | + assert function(1) == 2 |
| 344 | + assert function(2) == 3 |
| 345 | + |
| 346 | + |
| 347 | +def test_read_closures_with_async_function(): |
| 348 | + nonlocal_variable = 1 |
| 349 | + |
| 350 | + @transfunction |
| 351 | + def make(): |
| 352 | + #nonlocal nonlocal_variable |
| 353 | + return nonlocal_variable |
| 354 | + |
| 355 | + function = make.get_async_function() |
| 356 | + |
| 357 | + assert run(function()) == 1 |
| 358 | + |
| 359 | + |
| 360 | +def test_read_closures_with_async_function_with_arguments(): |
| 361 | + nonlocal_variable = 1 |
| 362 | + |
| 363 | + @transfunction |
| 364 | + def make(some_number): |
| 365 | + #nonlocal nonlocal_variable |
| 366 | + return nonlocal_variable + some_number |
| 367 | + |
| 368 | + function = make.get_async_function() |
| 369 | + |
| 370 | + assert run(function(1)) == 2 |
| 371 | + assert run(function(2)) == 3 |
| 372 | + |
| 373 | + |
| 374 | +def test_read_closures_with_generator_function(): |
| 375 | + nonlocal_variable = 1 |
| 376 | + |
| 377 | + @transfunction |
| 378 | + def make(): |
| 379 | + #nonlocal nonlocal_variable |
| 380 | + yield nonlocal_variable |
| 381 | + |
| 382 | + function = make.get_generator_function() |
| 383 | + |
| 384 | + assert list(function()) == [1] |
| 385 | + |
| 386 | + |
| 387 | +def test_read_closures_with_generator_function_with_arguments(): |
| 388 | + nonlocal_variable = 1 |
| 389 | + |
| 390 | + @transfunction |
| 391 | + def make(some_number): |
| 392 | + #nonlocal nonlocal_variable |
| 393 | + yield nonlocal_variable + some_number |
| 394 | + |
| 395 | + function = make.get_generator_function() |
| 396 | + |
| 397 | + assert list(function(1)) == [2] |
| 398 | + assert list(function(2)) == [3] |
0 commit comments