GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — pythonnet/tests/test_method.py at master · pythonnet/pythonnet Expand file tree/ Copy pathtest_method.pyMore file actions 1325 lines (945 loc) · 39.3 KB / Copy pathtest_method.pyFile metadata and controls1325 lines (945 loc) · 39.3 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 # -*- coding: utf-8 -*- """Test CLR method support.""" import System import pytest import sys import gc import tracemalloc from Python.Test import MethodTest @pytest.fixture(scope="function") def memory_usage_tracking(): was_tracing = tracemalloc.is_tracing() if not was_tracing: tracemalloc.start() yield if not was_tracing: tracemalloc.stop() def _get_total_memory_bytes() -> int: """Get total memory consumption combining .NET GC memory and Python tracemalloc.""" dotnet_memory = System.GC.GetTotalMemory(forceFullCollection=False) # Get Python-side memory current, _peak = tracemalloc.get_traced_memory() # Return combined measurement return dotnet_memory + current def test_instance_method_overwritable(): """Test instance method overwriting.""" ob = MethodTest() ob.OverwritableMethod = lambda: "overwritten" assert ob.OverwritableMethod() == "overwritten" def test_public_instance_method(): """Test public instance method visibility.""" ob = MethodTest() assert ob.PublicMethod() == "public" def test_public_static_method(): """Test public static method visibility.""" ob = MethodTest() assert MethodTest.PublicStaticMethod() == "public static" assert ob.PublicStaticMethod() == "public static" def test_protected_instance_method(): """Test protected instance method visibility.""" ob = MethodTest() assert ob.ProtectedMethod() == "protected" def test_protected_static_method(): """Test protected static method visibility.""" ob = MethodTest() result = "protected static" assert MethodTest.ProtectedStaticMethod() == result assert ob.ProtectedStaticMethod() == result def test_internal_method(): """Test internal method visibility.""" with pytest.raises(AttributeError): _ = MethodTest().InternalMethod with pytest.raises(AttributeError): _ = MethodTest.InternalMethod with pytest.raises(AttributeError): _ = MethodTest().InternalStaticMethod with pytest.raises(AttributeError): _ = MethodTest.InternalStaticMethod def test_private_method(): """Test private method visibility.""" with pytest.raises(AttributeError): _ = MethodTest().PrivateMethod with pytest.raises(AttributeError): _ = MethodTest.PrivateMethod with pytest.raises(AttributeError): _ = MethodTest().PrivateStaticMethod with pytest.raises(AttributeError): _ = MethodTest.PrivateStaticMethod def test_unbound_managed_method_call(): """Test calling unbound managed methods.""" from Python.Test import MethodTestSub ob = MethodTest() assert MethodTest.PublicMethod(ob) == "public" with pytest.raises(TypeError): MethodTest.PublicMethod() ob = MethodTestSub() assert MethodTestSub.PublicMethod(ob) == "public" assert MethodTestSub.PublicMethod(ob, "echo") == "echo" with pytest.raises(TypeError): MethodTestSub.PublicMethod("echo") def test_overloaded_method_inheritance(): """Test that overloads are inherited properly.""" from Python.Test import MethodTestSub ob = MethodTest() assert ob.PublicMethod() == "public" with pytest.raises(TypeError): ob = MethodTest() ob.PublicMethod("echo") ob = MethodTestSub() assert ob.PublicMethod() == "public" assert ob.PublicMethod("echo") == "echo" def test_method_descriptor_abuse(): """Test method descriptor abuse.""" desc = MethodTest.__dict__["PublicMethod"] with pytest.raises(TypeError): desc.__get__(0, 0) with pytest.raises(AttributeError): desc.__set__(0, 0) def test_method_docstrings(): """Test standard method docstring generation""" method = MethodTest.GetType value = "System.Type GetType()" assert method.__doc__ == value # ====================================================================== # Tests of specific argument and result conversion scenarios # ====================================================================== def test_method_call_enum_conversion(): """Test enum conversion in method call.""" from System import TypeCode ob = MethodTest() r = ob.TestEnumConversion(TypeCode.Int32) assert r == TypeCode.Int32 def test_method_call_flags_conversion(): """Test flags conversion in method call.""" from System.IO import FileAccess ob = MethodTest() flags = FileAccess.Read | FileAccess.Write r = ob.TestFlagsConversion(flags) assert r == flags def test_method_call_struct_conversion(): """Test struct conversion in method call.""" from System import Guid ob = MethodTest() guid = Guid.NewGuid() temp = guid.ToString() r = ob.TestStructConversion(guid) assert r.ToString() == temp def test_subclass_instance_conversion(): """Test subclass instance conversion in method call.""" class TestSubException(System.Exception): pass ob = MethodTest() instance = TestSubException() result = ob.TestSubclassConversion(instance) assert isinstance(result, System.Exception) def test_null_array_conversion(): """Test null array conversion in method call.""" ob = MethodTest() r = ob.TestNullArrayConversion(None) assert r is None def test_string_params_args(): """Test use of string params.""" result = MethodTest.TestStringParamsArg("one", "two", "three") assert result.Length == 4 assert len(result) == 4, result assert result[0] == "one" assert result[1] == "two" assert result[2] == "three" # ensures params string[] overload takes precedence over params object[] assert result[3] == "tail" result = MethodTest.TestStringParamsArg(["one", "two", "three"]) assert len(result) == 4 assert result[0] == "one" assert result[1] == "two" assert result[2] == "three" assert result[3] == "tail" def test_object_params_args(): """Test use of object params.""" result = MethodTest.TestObjectParamsArg("one", "two", "three") assert len(result) == 3, result assert result[0] == "one" assert result[1] == "two" assert result[2] == "three" result = MethodTest.TestObjectParamsArg(["one", "two", "three"]) assert len(result) == 3, result assert result[0] == "one" assert result[1] == "two" assert result[2] == "three" def test_value_params_args(): """Test use of value type params.""" result = MethodTest.TestValueParamsArg(1, 2, 3) assert len(result) == 3 assert result[0] == 1 assert result[1] == 2 assert result[2] == 3 result = MethodTest.TestValueParamsArg([1, 2, 3]) assert len(result) == 3 assert result[0] == 1 assert result[1] == 2 assert result[2] == 3 def test_non_params_array_in_last_place(): """Test overload resolution with of non-"params" array as last parameter.""" result = MethodTest.TestNonParamsArrayInLastPlace(1, 2, 3) assert result def test_params_methods_with_no_params(): """Tests that passing no arguments to a params method passes an empty array""" result = MethodTest.TestValueParamsArg() assert len(result) == 0 result = MethodTest.TestOneArgWithParams("Some String") assert len(result) == 0 result = MethodTest.TestTwoArgWithParams("Some String", "Some Other String") assert len(result) == 0 def test_params_methods_with_non_lists(): """Tests that passing single parameters to a params method will convert into an array on the .NET side""" result = MethodTest.TestOneArgWithParams("Some String", [1, 2, 3, 4]) assert len(result) == 4 result = MethodTest.TestOneArgWithParams("Some String", 1, 2, 3, 4) assert len(result) == 4 result = MethodTest.TestOneArgWithParams("Some String", [5]) assert len(result) == 1 result = MethodTest.TestOneArgWithParams("Some String", 5) assert len(result) == 1 def test_params_method_with_lists(): """Tests passing multiple lists to a params object[] method""" result = MethodTest.TestObjectParamsArg([], []) assert len(result) == 2 def test_string_out_params(): """Test use of string out-parameters.""" result = MethodTest.TestStringOutParams("hi", "there") assert isinstance(result, tuple) assert len(result) == 2 assert result[0] is True assert result[1] == "output string" result = MethodTest.TestStringOutParams("hi", None) assert isinstance(result, tuple) assert len(result) == 2 assert result[0] is True assert result[1] == "output string" def test_string_out_params_without_passing_string_value(): """Test use of string out-parameters.""" # @eirannejad 2022-01-13 result = MethodTest.TestStringOutParams("hi") assert isinstance(result, tuple) assert len(result) == 2 assert result[0] is True assert result[1] == "output string" def test_string_ref_params(): """Test use of string byref parameters.""" result = MethodTest.TestStringRefParams("hi", "there") assert isinstance(result, tuple) assert len(result) == 2 assert result[0] is True assert result[1] == "output string" result = MethodTest.TestStringRefParams("hi", None) assert isinstance(result, tuple) assert len(result) == 2 assert result[0] is True assert result[1] == "output string" def test_value_out_params(): """Test use of value type out-parameters.""" result = MethodTest.TestValueOutParams("hi", 1) assert isinstance(result, tuple) assert len(result) == 2 assert result[0] is True assert result[1] == 42 # None cannot be converted to a value type like int, long, etc. with pytest.raises(TypeError): MethodTest.TestValueOutParams("hi", None) def test_value_out_params_without_passing_string_value(): """Test use of string out-parameters.""" # @eirannejad 2022-01-13 result = MethodTest.TestValueOutParams("hi") assert isinstance(result, tuple) assert len(result) == 2 assert result[0] is True assert result[1] == 42 def test_value_ref_params(): """Test use of value type byref parameters.""" result = MethodTest.TestValueRefParams("hi", 1) assert isinstance(result, tuple) assert len(result) == 2 assert result[0] is True assert result[1] == 42 # None cannot be converted to a value type like int, long, etc. with pytest.raises(TypeError): MethodTest.TestValueRefParams("hi", None) def test_object_out_params(): """Test use of object out-parameters.""" result = MethodTest.TestObjectOutParams("hi", MethodTest()) assert isinstance(result, tuple) assert len(result) == 2 assert result[0] is True assert isinstance(result[1], System.Exception) result = MethodTest.TestObjectOutParams("hi", None) assert isinstance(result, tuple) assert len(result) == 2 assert result[0] is True assert isinstance(result[1], System.Exception) def test_object_out_params_without_passing_string_value(): """Test use of object out-parameters.""" result = MethodTest.TestObjectOutParams("hi") assert isinstance(result, tuple) assert len(result) == 2 assert result[0] is True assert isinstance(result[1], System.Exception) def test_object_ref_params(): """Test use of object byref parameters.""" result = MethodTest.TestObjectRefParams("hi", MethodTest()) assert isinstance(result, tuple) assert len(result) == 2 assert result[0] is True assert isinstance(result[1], System.Exception) result = MethodTest.TestObjectRefParams("hi", None) assert isinstance(result, tuple) assert len(result) == 2 assert result[0] is True assert isinstance(result[1], System.Exception) def test_struct_out_params(): """Test use of struct out-parameters.""" result = MethodTest.TestStructOutParams("hi", System.Guid.NewGuid()) assert isinstance(result, tuple) assert len(result) == 2 assert result[0] is True assert isinstance(result[1], System.Guid) # None cannot be converted to a value type like a struct with pytest.raises(TypeError): MethodTest.TestValueRefParams("hi", None) def test_struct_out_params_without_passing_string_value(): """Test use of struct out-parameters.""" result = MethodTest.TestStructOutParams("hi") assert isinstance(result, tuple) assert len(result) == 2 assert result[0] is True assert isinstance(result[1], System.Guid) def test_struct_ref_params(): """Test use of struct byref parameters.""" result = MethodTest.TestStructRefParams("hi", System.Guid.NewGuid()) assert isinstance(result, tuple) assert len(result) == 2 assert result[0] is True assert isinstance(result[1], System.Guid) # None cannot be converted to a value type like a struct with pytest.raises(TypeError): MethodTest.TestValueRefParams("hi", None) def test_void_single_out_param(): """Test void method with single out-parameter.""" result = MethodTest.TestVoidSingleOutParam(9) assert result == 42 # None cannot be converted to a value type with pytest.raises(TypeError): MethodTest.TestVoidSingleOutParam(None) def test_void_single_ref_param(): """Test void method with single ref-parameter.""" result = MethodTest.TestVoidSingleRefParam(9) assert result == 42 # None cannot be converted to a value type with pytest.raises(TypeError): MethodTest.TestVoidSingleRefParam(None) def test_single_default_param(): """Test void method with single ref-parameter.""" result = MethodTest.TestSingleDefaultParam() assert result == 5 def test_decimal_default_param(): """Test that decimal default parameters work.""" result = MethodTest.TestDecimalDefaultParam() assert result == System.Decimal(1) def test_one_arg_and_two_default_param(): """Test void method with single ref-parameter.""" result = MethodTest.TestOneArgAndTwoDefaultParam(11) assert result == 22 result = MethodTest.TestOneArgAndTwoDefaultParam(15) assert result == 26 result = MethodTest.TestOneArgAndTwoDefaultParam(20) assert result == 31 def test_two_default_param(): """Test void method with single ref-parameter.""" result = MethodTest.TestTwoDefaultParam() assert result == 11 def test_explicit_selection_with_out_modifier(): """Check explicit overload selection with out modifiers.""" refstr = System.String("").GetType().MakeByRefType() result = MethodTest.TestStringOutParams.__overloads__[str, refstr]("hi", "there") assert isinstance(result, tuple) assert len(result) == 2 assert result[0] is True assert result[1] == "output string" result = MethodTest.TestStringOutParams.__overloads__[str, refstr]("hi", None) assert isinstance(result, tuple) assert len(result) == 2 assert result[0] is True assert result[1] == "output string" def test_explicit_selection_with_ref_modifier(): """Check explicit overload selection with ref modifiers.""" refstr = System.String("").GetType().MakeByRefType() result = MethodTest.TestStringRefParams.__overloads__[str, refstr]("hi", "there") assert isinstance(result, tuple) assert len(result) == 2 assert result[0] is True assert result[1] == "output string" result = MethodTest.TestStringRefParams.__overloads__[str, refstr]("hi", None) assert isinstance(result, tuple) assert len(result) == 2 assert result[0] is True assert result[1] == "output string" def test_explicit_overload_selection(): """Check explicit overload selection using [] syntax.""" from Python.Test import ISayHello1, InterfaceTest, ShortEnum from System import Array inst = InterfaceTest() value = MethodTest.Overloaded.__overloads__[System.Boolean](True) assert value is True value = MethodTest.Overloaded.__overloads__[bool](True) assert value is True value = MethodTest.Overloaded.__overloads__[System.Byte](255) assert value == 255 value = MethodTest.Overloaded.__overloads__[System.SByte](127) assert value == 127 value = MethodTest.Overloaded.__overloads__[System.Char]("A") assert value == "A" value = MethodTest.Overloaded.__overloads__[System.Char](65535) assert value == chr(65535) value = MethodTest.Overloaded.__overloads__[System.Int16](32767) assert value == 32767 value = MethodTest.Overloaded.__overloads__[System.Int32](2147483647) assert value == 2147483647 value = MethodTest.Overloaded.__overloads__[int](2147483647) assert value == 2147483647 value = MethodTest.Overloaded.__overloads__[System.Int64](9223372036854775807) assert value == 9223372036854775807 value = MethodTest.Overloaded.__overloads__[System.UInt16](65000) assert value == 65000 value = MethodTest.Overloaded.__overloads__[System.UInt32](4294967295) assert value == 4294967295 value = MethodTest.Overloaded.__overloads__[System.UInt64](18446744073709551615) assert value == 18446744073709551615 value = MethodTest.Overloaded.__overloads__[System.Single](3.402823e38) assert value == System.Single(3.402823e38) value = MethodTest.Overloaded.__overloads__[System.Double](1.7976931348623157e308) assert value == 1.7976931348623157e308 value = MethodTest.Overloaded.__overloads__[float](1.7976931348623157e308) assert value == 1.7976931348623157e308 value = MethodTest.Overloaded.__overloads__[System.Decimal](System.Decimal.One) assert value == System.Decimal.One value = MethodTest.Overloaded.__overloads__[System.String]("spam") assert value == "spam" value = MethodTest.Overloaded.__overloads__[str]("spam") assert value == "spam" value = MethodTest.Overloaded.__overloads__[ShortEnum](ShortEnum.Zero) assert value == ShortEnum.Zero value = MethodTest.Overloaded.__overloads__[System.Object](inst) assert value.__class__ == inst.__class__ value = MethodTest.Overloaded.__overloads__[InterfaceTest](inst) assert value.__class__ == inst.__class__ iface_class = ISayHello1(InterfaceTest()).__class__ value = MethodTest.Overloaded.__overloads__[ISayHello1](inst) assert value.__class__ != inst.__class__ assert value.__class__ == iface_class atype = Array[System.Object] value = MethodTest.Overloaded.__overloads__[str, int, atype]( "one", 1, atype([1, 2, 3]) ) assert value == 3 value = MethodTest.Overloaded.__overloads__[str, int]("one", 1) assert value == 1 value = MethodTest.Overloaded.__overloads__[int, str](1, "one") assert value == 1 def test_overload_selection_with_array_types(): """Check overload selection using array types.""" from Python.Test import ISayHello1, InterfaceTest, ShortEnum from System import Array inst = InterfaceTest() vtype = Array[System.Boolean] input_ = vtype([True, True]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0] is True assert value[1] is True vtype = Array[bool] input_ = vtype([True, True]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0] is True assert value[1] is True vtype = Array[System.Byte] input_ = vtype([0, 255]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0] == 0 assert value[1] == 255 vtype = Array[System.SByte] input_ = vtype([0, 127]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0] == 0 assert value[1] == 127 vtype = Array[System.Char] input_ = vtype(["A", "Z"]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0] == "A" assert value[1] == "Z" vtype = Array[System.Char] input_ = vtype([0, 65535]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0] == chr(0) assert value[1] == chr(65535) vtype = Array[System.Int16] input_ = vtype([0, 32767]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0] == 0 assert value[1] == 32767 vtype = Array[System.Int32] input_ = vtype([0, 2147483647]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0] == 0 assert value[1] == 2147483647 vtype = Array[int] input_ = vtype([0, 2147483647]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0] == 0 assert value[1] == 2147483647 vtype = Array[System.Int64] input_ = vtype([0, 9223372036854775807]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0] == 0 assert value[1] == 9223372036854775807 vtype = Array[System.UInt16] input_ = vtype([0, 65000]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0] == 0 assert value[1] == 65000 vtype = Array[System.UInt32] input_ = vtype([0, 4294967295]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0] == 0 assert value[1] == 4294967295 vtype = Array[System.UInt64] input_ = vtype([0, 18446744073709551615]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0] == 0 assert value[1] == 18446744073709551615 vtype = Array[System.Single] input_ = vtype([0.0, 3.402823e38]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0] == 0.0 assert value[1] == System.Single(3.402823e38) vtype = Array[System.Double] input_ = vtype([0.0, 1.7976931348623157e308]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0] == 0.0 assert value[1] == 1.7976931348623157e308 vtype = Array[float] input_ = vtype([0.0, 1.7976931348623157e308]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0] == 0.0 assert value[1] == 1.7976931348623157e308 vtype = Array[System.Decimal] input_ = vtype([System.Decimal.Zero, System.Decimal.One]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0] == System.Decimal.Zero assert value[1] == System.Decimal.One vtype = Array[System.String] input_ = vtype(["one", "two"]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0] == "one" assert value[1] == "two" vtype = Array[str] input_ = vtype(["one", "two"]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0] == "one" assert value[1] == "two" vtype = Array[ShortEnum] input_ = vtype([ShortEnum.Zero, ShortEnum.One]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0] == ShortEnum.Zero assert value[1] == ShortEnum.One vtype = Array[System.Object] input_ = vtype([inst, inst]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0].__class__ == inst.__class__ assert value[1].__class__ == inst.__class__ vtype = Array[InterfaceTest] input_ = vtype([inst, inst]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0].__class__ == inst.__class__ assert value[1].__class__ == inst.__class__ iface_class = ISayHello1(inst).__class__ vtype = Array[ISayHello1] input_ = vtype([inst, inst]) value = MethodTest.Overloaded.__overloads__[vtype](input_) assert value[0].__class__ == iface_class assert value[1].__class__ == iface_class def test_explicit_overload_selection_failure(): """Check that overload selection fails correctly.""" with pytest.raises(TypeError): _ = MethodTest.Overloaded.__overloads__[System.Type](True) with pytest.raises(TypeError): _ = MethodTest.Overloaded.__overloads__[int, int](1, 1) with pytest.raises(TypeError): _ = MethodTest.Overloaded.__overloads__[str, int, int]("", 1, 1) with pytest.raises(TypeError): _ = MethodTest.Overloaded.__overloads__[int, int](1) def test_we_can_bind_to_encoding_get_string(): """Check that we can bind to the Encoding.GetString method with variables.""" from System.Text import Encoding from System.IO import MemoryStream my_bytes = Encoding.UTF8.GetBytes("Some testing string") stream = MemoryStream() stream.Write(my_bytes, 0, my_bytes.Length) stream.Position = 0 buff = System.Array.CreateInstance(System.Byte, 3) buff.Initialize() data = [] read = 1 while read > 0: read = stream.Read(buff, 0, buff.Length) temp = Encoding.UTF8.GetString(buff, 0, read) data.append(temp) data = "".join(data) assert data == "Some testing string" def test_wrong_overload(): """Test regression in which implicit conversion caused the wrong types to be used. See #131 for issue. Fixed by #137, #151""" # Used to return `50L` res = System.Math.Abs(50.5) assert res == 50.5 assert type(res) == float res = System.Math.Abs(-50.5) assert res == 50.5 assert type(res) == float res = System.Math.Max(50.5, 50.1) assert res == 50.5 assert type(res) == float res = System.Math.Max(System.Double(10.5), System.Double(50.5)) assert res == 50.5 assert type(res) == float # Should it return a System.Double? res = System.Math.Max(System.Double(50.5), 50.1) assert res == 50.5 assert type(res) == float def test_no_object_in_param(): """Test that fix for #203 doesn't break behavior w/ no object overload""" res = MethodTest.TestOverloadedNoObject(5) assert res == "Got int" res = MethodTest.TestOverloadedNoObject(i=7) assert res == "Got int" with pytest.raises(TypeError): MethodTest.TestOverloadedNoObject("test") with pytest.raises(TypeError): MethodTest.TestOverloadedNoObject(5.5) # Ensure that the top-level error is TypeError even if the inner error is an OverflowError with pytest.raises(TypeError): MethodTest.TestOverloadedNoObject(2147483648) def test_object_in_param(): """Test regression introduced by #151 in which Object method overloads aren't being used. See #203 for issue.""" res = MethodTest.TestOverloadedObject(5) assert res == "Got int" res = MethodTest.TestOverloadedObject(i=7) assert res == "Got int" res = MethodTest.TestOverloadedObject("test") assert res == "Got object" res = MethodTest.TestOverloadedObject(o="test") assert res == "Got object" def test_object_in_multiparam(): """Test method with object multiparams behaves""" res = MethodTest.TestOverloadedObjectTwo(5, 5) assert res == "Got int-int" res = MethodTest.TestOverloadedObjectTwo(5, "foo") assert res == "Got int-string" res = MethodTest.TestOverloadedObjectTwo("foo", 7.24) assert res == "Got string-object" res = MethodTest.TestOverloadedObjectTwo("foo", "bar") assert res == "Got string-string" res = MethodTest.TestOverloadedObjectTwo("foo", 5) assert res == "Got string-int" res = MethodTest.TestOverloadedObjectTwo(7.24, 7.24) assert res == "Got object-object" res = MethodTest.TestOverloadedObjectTwo(a=5, b=5) assert res == "Got int-int" res = MethodTest.TestOverloadedObjectTwo(5, b=5) assert res == "Got int-int" res = MethodTest.TestOverloadedObjectTwo(a=5, b="foo") assert res == "Got int-string" res = MethodTest.TestOverloadedObjectTwo(5, b="foo") assert res == "Got int-string" res = MethodTest.TestOverloadedObjectTwo(a="foo", b=7.24) assert res == "Got string-object" res = MethodTest.TestOverloadedObjectTwo("foo", b=7.24) assert res == "Got string-object" res = MethodTest.TestOverloadedObjectTwo(a="foo", b="bar") assert res == "Got string-string" res = MethodTest.TestOverloadedObjectTwo("foo", b="bar") assert res == "Got string-string" res = MethodTest.TestOverloadedObjectTwo(a="foo", b=5) assert res == "Got string-int" res = MethodTest.TestOverloadedObjectTwo("foo", b=5) assert res == "Got string-int" res = MethodTest.TestOverloadedObjectTwo(a=7.24, b=7.24) assert res == "Got object-object" res = MethodTest.TestOverloadedObjectTwo(7.24, b=7.24) assert res == "Got object-object" def test_object_in_multiparam_exception(): """Test method with object multiparams behaves""" with pytest.raises(TypeError) as excinfo: MethodTest.TestOverloadedObjectThree("foo", "bar") e = excinfo.value c = e.__cause__ assert c.GetType().FullName == "System.AggregateException" assert len(c.InnerExceptions) == 2 def test_case_sensitive(): """Test that case-sensitivity is respected. GH#81""" res = MethodTest.CaseSensitive() assert res == "CaseSensitive" res = MethodTest.Casesensitive() assert res == "Casesensitive" with pytest.raises(AttributeError): MethodTest.casesensitive() def test_getting_generic_method_binding_does_not_leak_ref_count(): """Test that managed object is freed after calling generic method. Issue #691""" from PlainOldNamespace import PlainOldClass refCount = sys.getrefcount(PlainOldClass().GenericMethod[str]) assert refCount == 1 def test_getting_generic_method_binding_does_not_leak_memory(memory_usage_tracking): """Test that managed object is freed after calling generic method. Issue #691""" from PlainOldNamespace import PlainOldClass tracemalloc.start() processBytesBeforeCall = _get_total_memory_bytes() print( "\n\nMemory consumption (bytes) at start of test: " + str(processBytesBeforeCall) ) iterations = 500 for i in range(iterations): PlainOldClass().GenericMethod[str] gc.collect() System.GC.Collect() processBytesAfterCall = _get_total_memory_bytes() print("Memory consumption (bytes) at end of test: " + str(processBytesAfterCall)) processBytesDelta = processBytesAfterCall - processBytesBeforeCall print("Memory delta: " + str(processBytesDelta)) bytesAllocatedPerIteration = pow(2, 20) # 1MB bytesLeakedPerIteration = processBytesDelta / iterations # Tight 10% threshold: with the fix the per-iteration leak is essentially # zero, while the bug retains the bulk of the 1 MB payload (~600 KB/iter # on 3.14 GIL). 100 KB/iter cleanly distinguishes the two states. failThresholdBytesLeakedPerIteration = bytesAllocatedPerIteration * 0.1 assert bytesLeakedPerIteration < failThresholdBytesLeakedPerIteration def test_getting_overloaded_method_binding_does_not_leak_ref_count(): """Test that managed object is freed after calling overloaded method. Issue #691""" from PlainOldNamespace import PlainOldClass refCount = sys.getrefcount(PlainOldClass().OverloadedMethod.Overloads[int]) assert refCount == 1 |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |