|
17 | 17 |
|
18 | 18 | 1 фаза: |
19 | 19 |
|
20 | | -10. Работает директива nonlocal. |
21 | | -11. Работает директива global. |
| 20 | +
|
22 | 21 | 12. Декоратор @transfunction можно использовать на методах (в т.ч. асинк и генераторных). |
23 | 22 | 14. Все работает с любыми уровнями вложенности (попробовать объявить функцию внутри функции). |
24 | 23 | 15. Сторонние контекстные менеджеры работают, как со скобками, так и без. |
|
47 | 46 | 7. Декоратор @transfunction нельзя использовать дважды на одной функции. |
48 | 47 | 8. Работает чтение из замыканий (в том числе для функций с аргументами). |
49 | 48 | 9. Работает чтение глобальных переменных. |
| 49 | +10. Работает директива nonlocal. |
| 50 | +11. Работает директива global. |
50 | 51 | """ |
51 | 52 |
|
52 | 53 | @transfunction |
@@ -462,3 +463,187 @@ def make(some_number): |
462 | 463 |
|
463 | 464 | assert list(function(1)) == [SOME_GLOBAL + 1] |
464 | 465 | assert list(function(2)) == [SOME_GLOBAL + 2] |
| 466 | + |
| 467 | + |
| 468 | +def test_write_nonlocal_variable_from_usual_function_without_arguments(): |
| 469 | + nonlocal_variable = 1 |
| 470 | + |
| 471 | + @transfunction |
| 472 | + def make(): |
| 473 | + nonlocal nonlocal_variable |
| 474 | + nonlocal_variable += 1 |
| 475 | + |
| 476 | + function = make.get_usual_function() |
| 477 | + function() |
| 478 | + |
| 479 | + assert nonlocal_variable == 2 |
| 480 | + |
| 481 | + |
| 482 | +def test_write_nonlocal_variable_from_usual_function_with_arguments(): |
| 483 | + nonlocal_variable = 1 |
| 484 | + |
| 485 | + @transfunction |
| 486 | + def make(number): |
| 487 | + nonlocal nonlocal_variable |
| 488 | + nonlocal_variable += number |
| 489 | + |
| 490 | + function = make.get_usual_function() |
| 491 | + function(3) |
| 492 | + |
| 493 | + assert nonlocal_variable == 4 |
| 494 | + |
| 495 | + |
| 496 | +def test_write_nonlocal_variable_from_async_function_without_arguments(): |
| 497 | + nonlocal_variable = 1 |
| 498 | + |
| 499 | + @transfunction |
| 500 | + def make(): |
| 501 | + nonlocal nonlocal_variable |
| 502 | + nonlocal_variable += 1 |
| 503 | + |
| 504 | + function = make.get_async_function() |
| 505 | + run(function()) |
| 506 | + |
| 507 | + assert nonlocal_variable == 2 |
| 508 | + |
| 509 | + |
| 510 | +def test_write_nonlocal_variable_from_async_function_with_arguments(): |
| 511 | + nonlocal_variable = 1 |
| 512 | + |
| 513 | + @transfunction |
| 514 | + def make(number): |
| 515 | + nonlocal nonlocal_variable |
| 516 | + nonlocal_variable += number |
| 517 | + |
| 518 | + function = make.get_async_function() |
| 519 | + run(function(3)) |
| 520 | + |
| 521 | + assert nonlocal_variable == 4 |
| 522 | + |
| 523 | + |
| 524 | +def test_write_nonlocal_variable_from_generator_function_without_arguments(): |
| 525 | + nonlocal_variable = 1 |
| 526 | + |
| 527 | + @transfunction |
| 528 | + def make(): |
| 529 | + nonlocal nonlocal_variable |
| 530 | + nonlocal_variable += 1 |
| 531 | + yield nonlocal_variable |
| 532 | + |
| 533 | + function = make.get_generator_function() |
| 534 | + list(function()) |
| 535 | + |
| 536 | + assert nonlocal_variable == 2 |
| 537 | + |
| 538 | + |
| 539 | +def test_write_nonlocal_variable_from_generator_function_with_arguments(): |
| 540 | + nonlocal_variable = 1 |
| 541 | + |
| 542 | + @transfunction |
| 543 | + def make(number): |
| 544 | + nonlocal nonlocal_variable |
| 545 | + nonlocal_variable += number |
| 546 | + yield nonlocal_variable |
| 547 | + |
| 548 | + function = make.get_generator_function() |
| 549 | + list(function(3)) |
| 550 | + |
| 551 | + assert nonlocal_variable == 4 |
| 552 | + |
| 553 | + |
| 554 | +def test_write_global_variable_from_usual_function_without_arguments(): |
| 555 | + @transfunction |
| 556 | + def make(): |
| 557 | + global SOME_GLOBAL |
| 558 | + SOME_GLOBAL += 1 |
| 559 | + |
| 560 | + global SOME_GLOBAL |
| 561 | + SOME_GLOBAL_BEFORE = SOME_GLOBAL |
| 562 | + function = make.get_usual_function() |
| 563 | + function() |
| 564 | + |
| 565 | + assert SOME_GLOBAL == SOME_GLOBAL_BEFORE + 1 |
| 566 | + |
| 567 | + SOME_GLOBAL = SOME_GLOBAL_BEFORE |
| 568 | + |
| 569 | + |
| 570 | +def test_write_global_variable_from_usual_function_with_arguments(): |
| 571 | + @transfunction |
| 572 | + def make(number): |
| 573 | + global SOME_GLOBAL |
| 574 | + SOME_GLOBAL += number |
| 575 | + |
| 576 | + global SOME_GLOBAL |
| 577 | + SOME_GLOBAL_BEFORE = SOME_GLOBAL |
| 578 | + function = make.get_usual_function() |
| 579 | + function(3) |
| 580 | + |
| 581 | + assert SOME_GLOBAL == SOME_GLOBAL_BEFORE + 3 |
| 582 | + |
| 583 | + SOME_GLOBAL = SOME_GLOBAL_BEFORE |
| 584 | + |
| 585 | + |
| 586 | +def test_write_global_variable_from_async_function_without_arguments(): |
| 587 | + @transfunction |
| 588 | + def make(): |
| 589 | + global SOME_GLOBAL |
| 590 | + SOME_GLOBAL += 1 |
| 591 | + |
| 592 | + global SOME_GLOBAL |
| 593 | + SOME_GLOBAL_BEFORE = SOME_GLOBAL |
| 594 | + function = make.get_async_function() |
| 595 | + run(function()) |
| 596 | + |
| 597 | + assert SOME_GLOBAL == SOME_GLOBAL_BEFORE + 1 |
| 598 | + |
| 599 | + SOME_GLOBAL = SOME_GLOBAL_BEFORE |
| 600 | + |
| 601 | + |
| 602 | +def test_write_global_variable_from_async_function_with_arguments(): |
| 603 | + @transfunction |
| 604 | + def make(number): |
| 605 | + global SOME_GLOBAL |
| 606 | + SOME_GLOBAL += number |
| 607 | + |
| 608 | + global SOME_GLOBAL |
| 609 | + SOME_GLOBAL_BEFORE = SOME_GLOBAL |
| 610 | + function = make.get_async_function() |
| 611 | + run(function(3)) |
| 612 | + |
| 613 | + assert SOME_GLOBAL == SOME_GLOBAL_BEFORE + 3 |
| 614 | + |
| 615 | + SOME_GLOBAL = SOME_GLOBAL_BEFORE |
| 616 | + |
| 617 | + |
| 618 | +def test_write_global_variable_from_generator_function_without_arguments(): |
| 619 | + @transfunction |
| 620 | + def make(): |
| 621 | + global SOME_GLOBAL |
| 622 | + SOME_GLOBAL += 1 |
| 623 | + yield None |
| 624 | + |
| 625 | + global SOME_GLOBAL |
| 626 | + SOME_GLOBAL_BEFORE = SOME_GLOBAL |
| 627 | + function = make.get_generator_function() |
| 628 | + list(function()) |
| 629 | + |
| 630 | + assert SOME_GLOBAL == SOME_GLOBAL_BEFORE + 1 |
| 631 | + |
| 632 | + SOME_GLOBAL = SOME_GLOBAL_BEFORE |
| 633 | + |
| 634 | + |
| 635 | +def test_write_global_variable_from_generator_function_with_arguments(): |
| 636 | + @transfunction |
| 637 | + def make(number): |
| 638 | + global SOME_GLOBAL |
| 639 | + SOME_GLOBAL += number |
| 640 | + yield None |
| 641 | + |
| 642 | + global SOME_GLOBAL |
| 643 | + SOME_GLOBAL_BEFORE = SOME_GLOBAL |
| 644 | + function = make.get_generator_function() |
| 645 | + list(function(3)) |
| 646 | + |
| 647 | + assert SOME_GLOBAL == SOME_GLOBAL_BEFORE + 3 |
| 648 | + |
| 649 | + SOME_GLOBAL = SOME_GLOBAL_BEFORE |
0 commit comments