1# Argument Clinic 2# Copyright 2012-2013 by Larry Hastings. 3# Licensed to the PSF under a contributor agreement. 4 5from test import support, test_tools 6from test.support import import_helper, os_helper 7from unittest import TestCase 8import collections 9import inspect 10import os.path 11import sys 12import unittest 13 14test_tools.skip_if_missing('clinic') 15with test_tools.imports_under_tool('clinic'): 16 import clinic 17 from clinic import DSLParser 18 19 20class FakeConverter: 21 def __init__(self, name, args): 22 self.name = name 23 self.args = args 24 25 26class FakeConverterFactory: 27 def __init__(self, name): 28 self.name = name 29 30 def __call__(self, name, default, **kwargs): 31 return FakeConverter(self.name, kwargs) 32 33 34class FakeConvertersDict: 35 def __init__(self): 36 self.used_converters = {} 37 38 def get(self, name, default): 39 return self.used_converters.setdefault(name, FakeConverterFactory(name)) 40 41c = clinic.Clinic(language='C', filename = "file") 42 43class FakeClinic: 44 def __init__(self): 45 self.converters = FakeConvertersDict() 46 self.legacy_converters = FakeConvertersDict() 47 self.language = clinic.CLanguage(None) 48 self.filename = None 49 self.destination_buffers = {} 50 self.block_parser = clinic.BlockParser('', self.language) 51 self.modules = collections.OrderedDict() 52 self.classes = collections.OrderedDict() 53 clinic.clinic = self 54 self.name = "FakeClinic" 55 self.line_prefix = self.line_suffix = '' 56 self.destinations = {} 57 self.add_destination("block", "buffer") 58 self.add_destination("file", "buffer") 59 self.add_destination("suppress", "suppress") 60 d = self.destinations.get 61 self.field_destinations = collections.OrderedDict(( 62 ('docstring_prototype', d('suppress')), 63 ('docstring_definition', d('block')), 64 ('methoddef_define', d('block')), 65 ('impl_prototype', d('block')), 66 ('parser_prototype', d('suppress')), 67 ('parser_definition', d('block')), 68 ('impl_definition', d('block')), 69 )) 70 71 def get_destination(self, name): 72 d = self.destinations.get(name) 73 if not d: 74 sys.exit("Destination does not exist: " + repr(name)) 75 return d 76 77 def add_destination(self, name, type, *args): 78 if name in self.destinations: 79 sys.exit("Destination already exists: " + repr(name)) 80 self.destinations[name] = clinic.Destination(name, type, self, *args) 81 82 def is_directive(self, name): 83 return name == "module" 84 85 def directive(self, name, args): 86 self.called_directives[name] = args 87 88 _module_and_class = clinic.Clinic._module_and_class 89 90class ClinicWholeFileTest(TestCase): 91 def test_eol(self): 92 # regression test: 93 # clinic's block parser didn't recognize 94 # the "end line" for the block if it 95 # didn't end in "\n" (as in, the last) 96 # byte of the file was '/'. 97 # so it would spit out an end line for you. 98 # and since you really already had one, 99 # the last line of the block got corrupted. 100 c = clinic.Clinic(clinic.CLanguage(None), filename="file") 101 raw = "/*[clinic]\nfoo\n[clinic]*/" 102 cooked = c.parse(raw).splitlines() 103 end_line = cooked[2].rstrip() 104 # this test is redundant, it's just here explicitly to catch 105 # the regression test so we don't forget what it looked like 106 self.assertNotEqual(end_line, "[clinic]*/[clinic]*/") 107 self.assertEqual(end_line, "[clinic]*/") 108 109 110 111class ClinicGroupPermuterTest(TestCase): 112 def _test(self, l, m, r, output): 113 computed = clinic.permute_optional_groups(l, m, r) 114 self.assertEqual(output, computed) 115 116 def test_range(self): 117 self._test([['start']], ['stop'], [['step']], 118 ( 119 ('stop',), 120 ('start', 'stop',), 121 ('start', 'stop', 'step',), 122 )) 123 124 def test_add_window(self): 125 self._test([['x', 'y']], ['ch'], [['attr']], 126 ( 127 ('ch',), 128 ('ch', 'attr'), 129 ('x', 'y', 'ch',), 130 ('x', 'y', 'ch', 'attr'), 131 )) 132 133 def test_ludicrous(self): 134 self._test([['a1', 'a2', 'a3'], ['b1', 'b2']], ['c1'], [['d1', 'd2'], ['e1', 'e2', 'e3']], 135 ( 136 ('c1',), 137 ('b1', 'b2', 'c1'), 138 ('b1', 'b2', 'c1', 'd1', 'd2'), 139 ('a1', 'a2', 'a3', 'b1', 'b2', 'c1'), 140 ('a1', 'a2', 'a3', 'b1', 'b2', 'c1', 'd1', 'd2'), 141 ('a1', 'a2', 'a3', 'b1', 'b2', 'c1', 'd1', 'd2', 'e1', 'e2', 'e3'), 142 )) 143 144 def test_right_only(self): 145 self._test([], [], [['a'],['b'],['c']], 146 ( 147 (), 148 ('a',), 149 ('a', 'b'), 150 ('a', 'b', 'c') 151 )) 152 153 def test_have_left_options_but_required_is_empty(self): 154 def fn(): 155 clinic.permute_optional_groups(['a'], [], []) 156 self.assertRaises(AssertionError, fn) 157 158 159class ClinicLinearFormatTest(TestCase): 160 def _test(self, input, output, **kwargs): 161 computed = clinic.linear_format(input, **kwargs) 162 self.assertEqual(output, computed) 163 164 def test_empty_strings(self): 165 self._test('', '') 166 167 def test_solo_newline(self): 168 self._test('\n', '\n') 169 170 def test_no_substitution(self): 171 self._test(""" 172 abc 173 """, """ 174 abc 175 """) 176 177 def test_empty_substitution(self): 178 self._test(""" 179 abc 180 {name} 181 def 182 """, """ 183 abc 184 def 185 """, name='') 186 187 def test_single_line_substitution(self): 188 self._test(""" 189 abc 190 {name} 191 def 192 """, """ 193 abc 194 GARGLE 195 def 196 """, name='GARGLE') 197 198 def test_multiline_substitution(self): 199 self._test(""" 200 abc 201 {name} 202 def 203 """, """ 204 abc 205 bingle 206 bungle 207 208 def 209 """, name='bingle\nbungle\n') 210 211class InertParser: 212 def __init__(self, clinic): 213 pass 214 215 def parse(self, block): 216 pass 217 218class CopyParser: 219 def __init__(self, clinic): 220 pass 221 222 def parse(self, block): 223 block.output = block.input 224 225 226class ClinicBlockParserTest(TestCase): 227 def _test(self, input, output): 228 language = clinic.CLanguage(None) 229 230 blocks = list(clinic.BlockParser(input, language)) 231 writer = clinic.BlockPrinter(language) 232 for block in blocks: 233 writer.print_block(block) 234 output = writer.f.getvalue() 235 assert output == input, "output != input!\n\noutput " + repr(output) + "\n\n input " + repr(input) 236 237 def round_trip(self, input): 238 return self._test(input, input) 239 240 def test_round_trip_1(self): 241 self.round_trip(""" 242 verbatim text here 243 lah dee dah 244""") 245 def test_round_trip_2(self): 246 self.round_trip(""" 247 verbatim text here 248 lah dee dah 249/*[inert] 250abc 251[inert]*/ 252def 253/*[inert checksum: 7b18d017f89f61cf17d47f92749ea6930a3f1deb]*/ 254xyz 255""") 256 257 def _test_clinic(self, input, output): 258 language = clinic.CLanguage(None) 259 c = clinic.Clinic(language, filename="file") 260 c.parsers['inert'] = InertParser(c) 261 c.parsers['copy'] = CopyParser(c) 262 computed = c.parse(input) 263 self.assertEqual(output, computed) 264 265 def test_clinic_1(self): 266 self._test_clinic(""" 267 verbatim text here 268 lah dee dah 269/*[copy input] 270def 271[copy start generated code]*/ 272abc 273/*[copy end generated code: output=03cfd743661f0797 input=7b18d017f89f61cf]*/ 274xyz 275""", """ 276 verbatim text here 277 lah dee dah 278/*[copy input] 279def 280[copy start generated code]*/ 281def 282/*[copy end generated code: output=7b18d017f89f61cf input=7b18d017f89f61cf]*/ 283xyz 284""") 285 286 287class ClinicParserTest(TestCase): 288 def test_trivial(self): 289 parser = DSLParser(FakeClinic()) 290 block = clinic.Block("module os\nos.access") 291 parser.parse(block) 292 module, function = block.signatures 293 self.assertEqual("access", function.name) 294 self.assertEqual("os", module.name) 295 296 def test_ignore_line(self): 297 block = self.parse("#\nmodule os\nos.access") 298 module, function = block.signatures 299 self.assertEqual("access", function.name) 300 self.assertEqual("os", module.name) 301 302 def test_param(self): 303 function = self.parse_function("module os\nos.access\n path: int") 304 self.assertEqual("access", function.name) 305 self.assertEqual(2, len(function.parameters)) 306 p = function.parameters['path'] 307 self.assertEqual('path', p.name) 308 self.assertIsInstance(p.converter, clinic.int_converter) 309 310 def test_param_default(self): 311 function = self.parse_function("module os\nos.access\n follow_symlinks: bool = True") 312 p = function.parameters['follow_symlinks'] 313 self.assertEqual(True, p.default) 314 315 def test_param_with_continuations(self): 316 function = self.parse_function("module os\nos.access\n follow_symlinks: \\\n bool \\\n =\\\n True") 317 p = function.parameters['follow_symlinks'] 318 self.assertEqual(True, p.default) 319 320 def test_param_default_expression(self): 321 function = self.parse_function("module os\nos.access\n follow_symlinks: int(c_default='MAXSIZE') = sys.maxsize") 322 p = function.parameters['follow_symlinks'] 323 self.assertEqual(sys.maxsize, p.default) 324 self.assertEqual("MAXSIZE", p.converter.c_default) 325 326 s = self.parse_function_should_fail("module os\nos.access\n follow_symlinks: int = sys.maxsize") 327 self.assertEqual(s, "Error on line 0:\nWhen you specify a named constant ('sys.maxsize') as your default value,\nyou MUST specify a valid c_default.\n") 328 329 def test_param_no_docstring(self): 330 function = self.parse_function(""" 331module os 332os.access 333 follow_symlinks: bool = True 334 something_else: str = ''""") 335 p = function.parameters['follow_symlinks'] 336 self.assertEqual(3, len(function.parameters)) 337 self.assertIsInstance(function.parameters['something_else'].converter, clinic.str_converter) 338 339 def test_param_default_parameters_out_of_order(self): 340 s = self.parse_function_should_fail(""" 341module os 342os.access 343 follow_symlinks: bool = True 344 something_else: str""") 345 self.assertEqual(s, """Error on line 0: 346Can't have a parameter without a default ('something_else') 347after a parameter with a default! 348""") 349 350 def disabled_test_converter_arguments(self): 351 function = self.parse_function("module os\nos.access\n path: path_t(allow_fd=1)") 352 p = function.parameters['path'] 353 self.assertEqual(1, p.converter.args['allow_fd']) 354 355 def test_function_docstring(self): 356 function = self.parse_function(""" 357module os 358os.stat as os_stat_fn 359 360 path: str 361 Path to be examined 362 363Perform a stat system call on the given path.""") 364 self.assertEqual(""" 365stat($module, /, path) 366-- 367 368Perform a stat system call on the given path. 369 370 path 371 Path to be examined 372""".strip(), function.docstring) 373 374 def test_explicit_parameters_in_docstring(self): 375 function = self.parse_function(""" 376module foo 377foo.bar 378 x: int 379 Documentation for x. 380 y: int 381 382This is the documentation for foo. 383 384Okay, we're done here. 385""") 386 self.assertEqual(""" 387bar($module, /, x, y) 388-- 389 390This is the documentation for foo. 391 392 x 393 Documentation for x. 394 395Okay, we're done here. 396""".strip(), function.docstring) 397 398 def test_parser_regression_special_character_in_parameter_column_of_docstring_first_line(self): 399 function = self.parse_function(""" 400module os 401os.stat 402 path: str 403This/used to break Clinic! 404""") 405 self.assertEqual("stat($module, /, path)\n--\n\nThis/used to break Clinic!", function.docstring) 406 407 def test_c_name(self): 408 function = self.parse_function("module os\nos.stat as os_stat_fn") 409 self.assertEqual("os_stat_fn", function.c_basename) 410 411 def test_return_converter(self): 412 function = self.parse_function("module os\nos.stat -> int") 413 self.assertIsInstance(function.return_converter, clinic.int_return_converter) 414 415 def test_star(self): 416 function = self.parse_function("module os\nos.access\n *\n follow_symlinks: bool = True") 417 p = function.parameters['follow_symlinks'] 418 self.assertEqual(inspect.Parameter.KEYWORD_ONLY, p.kind) 419 self.assertEqual(0, p.group) 420 421 def test_group(self): 422 function = self.parse_function("module window\nwindow.border\n [\n ls : int\n ]\n /\n") 423 p = function.parameters['ls'] 424 self.assertEqual(1, p.group) 425 426 def test_left_group(self): 427 function = self.parse_function(""" 428module curses 429curses.addch 430 [ 431 y: int 432 Y-coordinate. 433 x: int 434 X-coordinate. 435 ] 436 ch: char 437 Character to add. 438 [ 439 attr: long 440 Attributes for the character. 441 ] 442 / 443""") 444 for name, group in ( 445 ('y', -1), ('x', -1), 446 ('ch', 0), 447 ('attr', 1), 448 ): 449 p = function.parameters[name] 450 self.assertEqual(p.group, group) 451 self.assertEqual(p.kind, inspect.Parameter.POSITIONAL_ONLY) 452 self.assertEqual(function.docstring.strip(), """ 453addch([y, x,] ch, [attr]) 454 455 456 y 457 Y-coordinate. 458 x 459 X-coordinate. 460 ch 461 Character to add. 462 attr 463 Attributes for the character. 464 """.strip()) 465 466 def test_nested_groups(self): 467 function = self.parse_function(""" 468module curses 469curses.imaginary 470 [ 471 [ 472 y1: int 473 Y-coordinate. 474 y2: int 475 Y-coordinate. 476 ] 477 x1: int 478 X-coordinate. 479 x2: int 480 X-coordinate. 481 ] 482 ch: char 483 Character to add. 484 [ 485 attr1: long 486 Attributes for the character. 487 attr2: long 488 Attributes for the character. 489 attr3: long 490 Attributes for the character. 491 [ 492 attr4: long 493 Attributes for the character. 494 attr5: long 495 Attributes for the character. 496 attr6: long 497 Attributes for the character. 498 ] 499 ] 500 / 501""") 502 for name, group in ( 503 ('y1', -2), ('y2', -2), 504 ('x1', -1), ('x2', -1), 505 ('ch', 0), 506 ('attr1', 1), ('attr2', 1), ('attr3', 1), 507 ('attr4', 2), ('attr5', 2), ('attr6', 2), 508 ): 509 p = function.parameters[name] 510 self.assertEqual(p.group, group) 511 self.assertEqual(p.kind, inspect.Parameter.POSITIONAL_ONLY) 512 513 self.assertEqual(function.docstring.strip(), """ 514imaginary([[y1, y2,] x1, x2,] ch, [attr1, attr2, attr3, [attr4, attr5, 515 attr6]]) 516 517 518 y1 519 Y-coordinate. 520 y2 521 Y-coordinate. 522 x1 523 X-coordinate. 524 x2 525 X-coordinate. 526 ch 527 Character to add. 528 attr1 529 Attributes for the character. 530 attr2 531 Attributes for the character. 532 attr3 533 Attributes for the character. 534 attr4 535 Attributes for the character. 536 attr5 537 Attributes for the character. 538 attr6 539 Attributes for the character. 540 """.strip()) 541 542 def parse_function_should_fail(self, s): 543 with support.captured_stdout() as stdout: 544 with self.assertRaises(SystemExit): 545 self.parse_function(s) 546 return stdout.getvalue() 547 548 def test_disallowed_grouping__two_top_groups_on_left(self): 549 s = self.parse_function_should_fail(""" 550module foo 551foo.two_top_groups_on_left 552 [ 553 group1 : int 554 ] 555 [ 556 group2 : int 557 ] 558 param: int 559 """) 560 self.assertEqual(s, 561 ('Error on line 0:\n' 562 'Function two_top_groups_on_left has an unsupported group configuration. (Unexpected state 2.b)\n')) 563 564 def test_disallowed_grouping__two_top_groups_on_right(self): 565 self.parse_function_should_fail(""" 566module foo 567foo.two_top_groups_on_right 568 param: int 569 [ 570 group1 : int 571 ] 572 [ 573 group2 : int 574 ] 575 """) 576 577 def test_disallowed_grouping__parameter_after_group_on_right(self): 578 self.parse_function_should_fail(""" 579module foo 580foo.parameter_after_group_on_right 581 param: int 582 [ 583 [ 584 group1 : int 585 ] 586 group2 : int 587 ] 588 """) 589 590 def test_disallowed_grouping__group_after_parameter_on_left(self): 591 self.parse_function_should_fail(""" 592module foo 593foo.group_after_parameter_on_left 594 [ 595 group2 : int 596 [ 597 group1 : int 598 ] 599 ] 600 param: int 601 """) 602 603 def test_disallowed_grouping__empty_group_on_left(self): 604 self.parse_function_should_fail(""" 605module foo 606foo.empty_group 607 [ 608 [ 609 ] 610 group2 : int 611 ] 612 param: int 613 """) 614 615 def test_disallowed_grouping__empty_group_on_right(self): 616 self.parse_function_should_fail(""" 617module foo 618foo.empty_group 619 param: int 620 [ 621 [ 622 ] 623 group2 : int 624 ] 625 """) 626 627 def test_no_parameters(self): 628 function = self.parse_function(""" 629module foo 630foo.bar 631 632Docstring 633 634""") 635 self.assertEqual("bar($module, /)\n--\n\nDocstring", function.docstring) 636 self.assertEqual(1, len(function.parameters)) # self! 637 638 def test_init_with_no_parameters(self): 639 function = self.parse_function(""" 640module foo 641class foo.Bar "unused" "notneeded" 642foo.Bar.__init__ 643 644Docstring 645 646""", signatures_in_block=3, function_index=2) 647 # self is not in the signature 648 self.assertEqual("Bar()\n--\n\nDocstring", function.docstring) 649 # but it *is* a parameter 650 self.assertEqual(1, len(function.parameters)) 651 652 def test_illegal_module_line(self): 653 self.parse_function_should_fail(""" 654module foo 655foo.bar => int 656 / 657""") 658 659 def test_illegal_c_basename(self): 660 self.parse_function_should_fail(""" 661module foo 662foo.bar as 935 663 / 664""") 665 666 def test_single_star(self): 667 self.parse_function_should_fail(""" 668module foo 669foo.bar 670 * 671 * 672""") 673 674 def test_parameters_required_after_star_without_initial_parameters_or_docstring(self): 675 self.parse_function_should_fail(""" 676module foo 677foo.bar 678 * 679""") 680 681 def test_parameters_required_after_star_without_initial_parameters_with_docstring(self): 682 self.parse_function_should_fail(""" 683module foo 684foo.bar 685 * 686Docstring here. 687""") 688 689 def test_parameters_required_after_star_with_initial_parameters_without_docstring(self): 690 self.parse_function_should_fail(""" 691module foo 692foo.bar 693 this: int 694 * 695""") 696 697 def test_parameters_required_after_star_with_initial_parameters_and_docstring(self): 698 self.parse_function_should_fail(""" 699module foo 700foo.bar 701 this: int 702 * 703Docstring. 704""") 705 706 def test_single_slash(self): 707 self.parse_function_should_fail(""" 708module foo 709foo.bar 710 / 711 / 712""") 713 714 def test_mix_star_and_slash(self): 715 self.parse_function_should_fail(""" 716module foo 717foo.bar 718 x: int 719 y: int 720 * 721 z: int 722 / 723""") 724 725 def test_parameters_not_permitted_after_slash_for_now(self): 726 self.parse_function_should_fail(""" 727module foo 728foo.bar 729 / 730 x: int 731""") 732 733 def test_parameters_no_more_than_one_vararg(self): 734 s = self.parse_function_should_fail(""" 735module foo 736foo.bar 737 *vararg1: object 738 *vararg2: object 739""") 740 self.assertEqual(s, "Error on line 0:\nToo many var args\n") 741 742 def test_function_not_at_column_0(self): 743 function = self.parse_function(""" 744 module foo 745 foo.bar 746 x: int 747 Nested docstring here, goeth. 748 * 749 y: str 750 Not at column 0! 751""") 752 self.assertEqual(""" 753bar($module, /, x, *, y) 754-- 755 756Not at column 0! 757 758 x 759 Nested docstring here, goeth. 760""".strip(), function.docstring) 761 762 def test_directive(self): 763 c = FakeClinic() 764 parser = DSLParser(c) 765 parser.flag = False 766 parser.directives['setflag'] = lambda : setattr(parser, 'flag', True) 767 block = clinic.Block("setflag") 768 parser.parse(block) 769 self.assertTrue(parser.flag) 770 771 def test_legacy_converters(self): 772 block = self.parse('module os\nos.access\n path: "s"') 773 module, function = block.signatures 774 self.assertIsInstance((function.parameters['path']).converter, clinic.str_converter) 775 776 def test_legacy_converters_non_string_constant_annotation(self): 777 expected_failure_message = """\ 778Error on line 0: 779Annotations must be either a name, a function call, or a string. 780""" 781 782 s = self.parse_function_should_fail('module os\nos.access\n path: 42') 783 self.assertEqual(s, expected_failure_message) 784 785 s = self.parse_function_should_fail('module os\nos.access\n path: 42.42') 786 self.assertEqual(s, expected_failure_message) 787 788 s = self.parse_function_should_fail('module os\nos.access\n path: 42j') 789 self.assertEqual(s, expected_failure_message) 790 791 s = self.parse_function_should_fail('module os\nos.access\n path: b"42"') 792 self.assertEqual(s, expected_failure_message) 793 794 def test_other_bizarre_things_in_annotations_fail(self): 795 expected_failure_message = """\ 796Error on line 0: 797Annotations must be either a name, a function call, or a string. 798""" 799 800 s = self.parse_function_should_fail( 801 'module os\nos.access\n path: {"some": "dictionary"}' 802 ) 803 self.assertEqual(s, expected_failure_message) 804 805 s = self.parse_function_should_fail( 806 'module os\nos.access\n path: ["list", "of", "strings"]' 807 ) 808 self.assertEqual(s, expected_failure_message) 809 810 s = self.parse_function_should_fail( 811 'module os\nos.access\n path: (x for x in range(42))' 812 ) 813 self.assertEqual(s, expected_failure_message) 814 815 def parse(self, text): 816 c = FakeClinic() 817 parser = DSLParser(c) 818 block = clinic.Block(text) 819 parser.parse(block) 820 return block 821 822 def parse_function(self, text, signatures_in_block=2, function_index=1): 823 block = self.parse(text) 824 s = block.signatures 825 self.assertEqual(len(s), signatures_in_block) 826 assert isinstance(s[0], clinic.Module) 827 assert isinstance(s[function_index], clinic.Function) 828 return s[function_index] 829 830 def test_scaffolding(self): 831 # test repr on special values 832 self.assertEqual(repr(clinic.unspecified), '<Unspecified>') 833 self.assertEqual(repr(clinic.NULL), '<Null>') 834 835 # test that fail fails 836 with support.captured_stdout() as stdout: 837 with self.assertRaises(SystemExit): 838 clinic.fail('The igloos are melting!', filename='clown.txt', line_number=69) 839 self.assertEqual(stdout.getvalue(), 'Error in file "clown.txt" on line 69:\nThe igloos are melting!\n') 840 841 842class ClinicExternalTest(TestCase): 843 maxDiff = None 844 845 def test_external(self): 846 # bpo-42398: Test that the destination file is left unchanged if the 847 # content does not change. Moreover, check also that the file 848 # modification time does not change in this case. 849 source = support.findfile('clinic.test') 850 with open(source, 'r', encoding='utf-8') as f: 851 orig_contents = f.read() 852 853 with os_helper.temp_dir() as tmp_dir: 854 testfile = os.path.join(tmp_dir, 'clinic.test.c') 855 with open(testfile, 'w', encoding='utf-8') as f: 856 f.write(orig_contents) 857 old_mtime_ns = os.stat(testfile).st_mtime_ns 858 859 clinic.parse_file(testfile) 860 861 with open(testfile, 'r', encoding='utf-8') as f: 862 new_contents = f.read() 863 new_mtime_ns = os.stat(testfile).st_mtime_ns 864 865 self.assertEqual(new_contents, orig_contents) 866 # Don't change the file modification time 867 # if the content does not change 868 self.assertEqual(new_mtime_ns, old_mtime_ns) 869 870 871try: 872 import _testclinic as ac_tester 873except ImportError: 874 ac_tester = None 875 876@unittest.skipIf(ac_tester is None, "_testclinic is missing") 877class ClinicFunctionalTest(unittest.TestCase): 878 locals().update((name, getattr(ac_tester, name)) 879 for name in dir(ac_tester) if name.startswith('test_')) 880 881 def test_objects_converter(self): 882 with self.assertRaises(TypeError): 883 ac_tester.objects_converter() 884 self.assertEqual(ac_tester.objects_converter(1, 2), (1, 2)) 885 self.assertEqual(ac_tester.objects_converter([], 'whatever class'), ([], 'whatever class')) 886 self.assertEqual(ac_tester.objects_converter(1), (1, None)) 887 888 def test_bytes_object_converter(self): 889 with self.assertRaises(TypeError): 890 ac_tester.bytes_object_converter(1) 891 self.assertEqual(ac_tester.bytes_object_converter(b'BytesObject'), (b'BytesObject',)) 892 893 def test_byte_array_object_converter(self): 894 with self.assertRaises(TypeError): 895 ac_tester.byte_array_object_converter(1) 896 byte_arr = bytearray(b'ByteArrayObject') 897 self.assertEqual(ac_tester.byte_array_object_converter(byte_arr), (byte_arr,)) 898 899 def test_unicode_converter(self): 900 with self.assertRaises(TypeError): 901 ac_tester.unicode_converter(1) 902 self.assertEqual(ac_tester.unicode_converter('unicode'), ('unicode',)) 903 904 def test_bool_converter(self): 905 with self.assertRaises(TypeError): 906 ac_tester.bool_converter(False, False, 'not a int') 907 self.assertEqual(ac_tester.bool_converter(), (True, True, True)) 908 self.assertEqual(ac_tester.bool_converter('', [], 5), (False, False, True)) 909 self.assertEqual(ac_tester.bool_converter(('not empty',), {1: 2}, 0), (True, True, False)) 910 911 def test_char_converter(self): 912 with self.assertRaises(TypeError): 913 ac_tester.char_converter(1) 914 with self.assertRaises(TypeError): 915 ac_tester.char_converter(b'ab') 916 chars = [b'A', b'\a', b'\b', b'\t', b'\n', b'\v', b'\f', b'\r', b'"', b"'", b'?', b'\\', b'\000', b'\377'] 917 expected = tuple(ord(c) for c in chars) 918 self.assertEqual(ac_tester.char_converter(), expected) 919 chars = [b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'0', b'a', b'b', b'c', b'd'] 920 expected = tuple(ord(c) for c in chars) 921 self.assertEqual(ac_tester.char_converter(*chars), expected) 922 923 def test_unsigned_char_converter(self): 924 from _testcapi import UCHAR_MAX 925 with self.assertRaises(OverflowError): 926 ac_tester.unsigned_char_converter(-1) 927 with self.assertRaises(OverflowError): 928 ac_tester.unsigned_char_converter(UCHAR_MAX + 1) 929 with self.assertRaises(OverflowError): 930 ac_tester.unsigned_char_converter(0, UCHAR_MAX + 1) 931 with self.assertRaises(TypeError): 932 ac_tester.unsigned_char_converter([]) 933 self.assertEqual(ac_tester.unsigned_char_converter(), (12, 34, 56)) 934 self.assertEqual(ac_tester.unsigned_char_converter(0, 0, UCHAR_MAX + 1), (0, 0, 0)) 935 self.assertEqual(ac_tester.unsigned_char_converter(0, 0, (UCHAR_MAX + 1) * 3 + 123), (0, 0, 123)) 936 937 def test_short_converter(self): 938 from _testcapi import SHRT_MIN, SHRT_MAX 939 with self.assertRaises(OverflowError): 940 ac_tester.short_converter(SHRT_MIN - 1) 941 with self.assertRaises(OverflowError): 942 ac_tester.short_converter(SHRT_MAX + 1) 943 with self.assertRaises(TypeError): 944 ac_tester.short_converter([]) 945 self.assertEqual(ac_tester.short_converter(-1234), (-1234,)) 946 self.assertEqual(ac_tester.short_converter(4321), (4321,)) 947 948 def test_unsigned_short_converter(self): 949 from _testcapi import USHRT_MAX 950 with self.assertRaises(ValueError): 951 ac_tester.unsigned_short_converter(-1) 952 with self.assertRaises(OverflowError): 953 ac_tester.unsigned_short_converter(USHRT_MAX + 1) 954 with self.assertRaises(OverflowError): 955 ac_tester.unsigned_short_converter(0, USHRT_MAX + 1) 956 with self.assertRaises(TypeError): 957 ac_tester.unsigned_short_converter([]) 958 self.assertEqual(ac_tester.unsigned_short_converter(), (12, 34, 56)) 959 self.assertEqual(ac_tester.unsigned_short_converter(0, 0, USHRT_MAX + 1), (0, 0, 0)) 960 self.assertEqual(ac_tester.unsigned_short_converter(0, 0, (USHRT_MAX + 1) * 3 + 123), (0, 0, 123)) 961 962 def test_int_converter(self): 963 from _testcapi import INT_MIN, INT_MAX 964 with self.assertRaises(OverflowError): 965 ac_tester.int_converter(INT_MIN - 1) 966 with self.assertRaises(OverflowError): 967 ac_tester.int_converter(INT_MAX + 1) 968 with self.assertRaises(TypeError): 969 ac_tester.int_converter(1, 2, 3) 970 with self.assertRaises(TypeError): 971 ac_tester.int_converter([]) 972 self.assertEqual(ac_tester.int_converter(), (12, 34, 45)) 973 self.assertEqual(ac_tester.int_converter(1, 2, '3'), (1, 2, ord('3'))) 974 975 def test_unsigned_int_converter(self): 976 from _testcapi import UINT_MAX 977 with self.assertRaises(ValueError): 978 ac_tester.unsigned_int_converter(-1) 979 with self.assertRaises(OverflowError): 980 ac_tester.unsigned_int_converter(UINT_MAX + 1) 981 with self.assertRaises(OverflowError): 982 ac_tester.unsigned_int_converter(0, UINT_MAX + 1) 983 with self.assertRaises(TypeError): 984 ac_tester.unsigned_int_converter([]) 985 self.assertEqual(ac_tester.unsigned_int_converter(), (12, 34, 56)) 986 self.assertEqual(ac_tester.unsigned_int_converter(0, 0, UINT_MAX + 1), (0, 0, 0)) 987 self.assertEqual(ac_tester.unsigned_int_converter(0, 0, (UINT_MAX + 1) * 3 + 123), (0, 0, 123)) 988 989 def test_long_converter(self): 990 from _testcapi import LONG_MIN, LONG_MAX 991 with self.assertRaises(OverflowError): 992 ac_tester.long_converter(LONG_MIN - 1) 993 with self.assertRaises(OverflowError): 994 ac_tester.long_converter(LONG_MAX + 1) 995 with self.assertRaises(TypeError): 996 ac_tester.long_converter([]) 997 self.assertEqual(ac_tester.long_converter(), (12,)) 998 self.assertEqual(ac_tester.long_converter(-1234), (-1234,)) 999 1000 def test_unsigned_long_converter(self): 1001 from _testcapi import ULONG_MAX 1002 with self.assertRaises(ValueError): 1003 ac_tester.unsigned_long_converter(-1) 1004 with self.assertRaises(OverflowError): 1005 ac_tester.unsigned_long_converter(ULONG_MAX + 1) 1006 with self.assertRaises(OverflowError): 1007 ac_tester.unsigned_long_converter(0, ULONG_MAX + 1) 1008 with self.assertRaises(TypeError): 1009 ac_tester.unsigned_long_converter([]) 1010 self.assertEqual(ac_tester.unsigned_long_converter(), (12, 34, 56)) 1011 self.assertEqual(ac_tester.unsigned_long_converter(0, 0, ULONG_MAX + 1), (0, 0, 0)) 1012 self.assertEqual(ac_tester.unsigned_long_converter(0, 0, (ULONG_MAX + 1) * 3 + 123), (0, 0, 123)) 1013 1014 def test_long_long_converter(self): 1015 from _testcapi import LLONG_MIN, LLONG_MAX 1016 with self.assertRaises(OverflowError): 1017 ac_tester.long_long_converter(LLONG_MIN - 1) 1018 with self.assertRaises(OverflowError): 1019 ac_tester.long_long_converter(LLONG_MAX + 1) 1020 with self.assertRaises(TypeError): 1021 ac_tester.long_long_converter([]) 1022 self.assertEqual(ac_tester.long_long_converter(), (12,)) 1023 self.assertEqual(ac_tester.long_long_converter(-1234), (-1234,)) 1024 1025 def test_unsigned_long_long_converter(self): 1026 from _testcapi import ULLONG_MAX 1027 with self.assertRaises(ValueError): 1028 ac_tester.unsigned_long_long_converter(-1) 1029 with self.assertRaises(OverflowError): 1030 ac_tester.unsigned_long_long_converter(ULLONG_MAX + 1) 1031 with self.assertRaises(OverflowError): 1032 ac_tester.unsigned_long_long_converter(0, ULLONG_MAX + 1) 1033 with self.assertRaises(TypeError): 1034 ac_tester.unsigned_long_long_converter([]) 1035 self.assertEqual(ac_tester.unsigned_long_long_converter(), (12, 34, 56)) 1036 self.assertEqual(ac_tester.unsigned_long_long_converter(0, 0, ULLONG_MAX + 1), (0, 0, 0)) 1037 self.assertEqual(ac_tester.unsigned_long_long_converter(0, 0, (ULLONG_MAX + 1) * 3 + 123), (0, 0, 123)) 1038 1039 def test_py_ssize_t_converter(self): 1040 from _testcapi import PY_SSIZE_T_MIN, PY_SSIZE_T_MAX 1041 with self.assertRaises(OverflowError): 1042 ac_tester.py_ssize_t_converter(PY_SSIZE_T_MIN - 1) 1043 with self.assertRaises(OverflowError): 1044 ac_tester.py_ssize_t_converter(PY_SSIZE_T_MAX + 1) 1045 with self.assertRaises(TypeError): 1046 ac_tester.py_ssize_t_converter([]) 1047 self.assertEqual(ac_tester.py_ssize_t_converter(), (12, 34, 56)) 1048 self.assertEqual(ac_tester.py_ssize_t_converter(1, 2, None), (1, 2, 56)) 1049 1050 def test_slice_index_converter(self): 1051 from _testcapi import PY_SSIZE_T_MIN, PY_SSIZE_T_MAX 1052 with self.assertRaises(TypeError): 1053 ac_tester.slice_index_converter([]) 1054 self.assertEqual(ac_tester.slice_index_converter(), (12, 34, 56)) 1055 self.assertEqual(ac_tester.slice_index_converter(1, 2, None), (1, 2, 56)) 1056 self.assertEqual(ac_tester.slice_index_converter(PY_SSIZE_T_MAX, PY_SSIZE_T_MAX + 1, PY_SSIZE_T_MAX + 1234), 1057 (PY_SSIZE_T_MAX, PY_SSIZE_T_MAX, PY_SSIZE_T_MAX)) 1058 self.assertEqual(ac_tester.slice_index_converter(PY_SSIZE_T_MIN, PY_SSIZE_T_MIN - 1, PY_SSIZE_T_MIN - 1234), 1059 (PY_SSIZE_T_MIN, PY_SSIZE_T_MIN, PY_SSIZE_T_MIN)) 1060 1061 def test_size_t_converter(self): 1062 with self.assertRaises(ValueError): 1063 ac_tester.size_t_converter(-1) 1064 with self.assertRaises(TypeError): 1065 ac_tester.size_t_converter([]) 1066 self.assertEqual(ac_tester.size_t_converter(), (12,)) 1067 1068 def test_float_converter(self): 1069 with self.assertRaises(TypeError): 1070 ac_tester.float_converter([]) 1071 self.assertEqual(ac_tester.float_converter(), (12.5,)) 1072 self.assertEqual(ac_tester.float_converter(-0.5), (-0.5,)) 1073 1074 def test_double_converter(self): 1075 with self.assertRaises(TypeError): 1076 ac_tester.double_converter([]) 1077 self.assertEqual(ac_tester.double_converter(), (12.5,)) 1078 self.assertEqual(ac_tester.double_converter(-0.5), (-0.5,)) 1079 1080 def test_py_complex_converter(self): 1081 with self.assertRaises(TypeError): 1082 ac_tester.py_complex_converter([]) 1083 self.assertEqual(ac_tester.py_complex_converter(complex(1, 2)), (complex(1, 2),)) 1084 self.assertEqual(ac_tester.py_complex_converter(complex('-1-2j')), (complex('-1-2j'),)) 1085 self.assertEqual(ac_tester.py_complex_converter(-0.5), (-0.5,)) 1086 self.assertEqual(ac_tester.py_complex_converter(10), (10,)) 1087 1088 def test_str_converter(self): 1089 with self.assertRaises(TypeError): 1090 ac_tester.str_converter(1) 1091 with self.assertRaises(TypeError): 1092 ac_tester.str_converter('a', 'b', 'c') 1093 with self.assertRaises(ValueError): 1094 ac_tester.str_converter('a', b'b\0b', 'c') 1095 self.assertEqual(ac_tester.str_converter('a', b'b', 'c'), ('a', 'b', 'c')) 1096 self.assertEqual(ac_tester.str_converter('a', b'b', b'c'), ('a', 'b', 'c')) 1097 self.assertEqual(ac_tester.str_converter('a', b'b', 'c\0c'), ('a', 'b', 'c\0c')) 1098 1099 def test_str_converter_encoding(self): 1100 with self.assertRaises(TypeError): 1101 ac_tester.str_converter_encoding(1) 1102 self.assertEqual(ac_tester.str_converter_encoding('a', 'b', 'c'), ('a', 'b', 'c')) 1103 with self.assertRaises(TypeError): 1104 ac_tester.str_converter_encoding('a', b'b\0b', 'c') 1105 self.assertEqual(ac_tester.str_converter_encoding('a', b'b', bytearray([ord('c')])), ('a', 'b', 'c')) 1106 self.assertEqual(ac_tester.str_converter_encoding('a', b'b', bytearray([ord('c'), 0, ord('c')])), 1107 ('a', 'b', 'c\x00c')) 1108 self.assertEqual(ac_tester.str_converter_encoding('a', b'b', b'c\x00c'), ('a', 'b', 'c\x00c')) 1109 1110 def test_py_buffer_converter(self): 1111 with self.assertRaises(TypeError): 1112 ac_tester.py_buffer_converter('a', 'b') 1113 self.assertEqual(ac_tester.py_buffer_converter('abc', bytearray([1, 2, 3])), (b'abc', b'\x01\x02\x03')) 1114 1115 def test_keywords(self): 1116 self.assertEqual(ac_tester.keywords(1, 2), (1, 2)) 1117 self.assertEqual(ac_tester.keywords(1, b=2), (1, 2)) 1118 self.assertEqual(ac_tester.keywords(a=1, b=2), (1, 2)) 1119 1120 def test_keywords_kwonly(self): 1121 with self.assertRaises(TypeError): 1122 ac_tester.keywords_kwonly(1, 2) 1123 self.assertEqual(ac_tester.keywords_kwonly(1, b=2), (1, 2)) 1124 self.assertEqual(ac_tester.keywords_kwonly(a=1, b=2), (1, 2)) 1125 1126 def test_keywords_opt(self): 1127 self.assertEqual(ac_tester.keywords_opt(1), (1, None, None)) 1128 self.assertEqual(ac_tester.keywords_opt(1, 2), (1, 2, None)) 1129 self.assertEqual(ac_tester.keywords_opt(1, 2, 3), (1, 2, 3)) 1130 self.assertEqual(ac_tester.keywords_opt(1, b=2), (1, 2, None)) 1131 self.assertEqual(ac_tester.keywords_opt(1, 2, c=3), (1, 2, 3)) 1132 self.assertEqual(ac_tester.keywords_opt(a=1, c=3), (1, None, 3)) 1133 self.assertEqual(ac_tester.keywords_opt(a=1, b=2, c=3), (1, 2, 3)) 1134 1135 def test_keywords_opt_kwonly(self): 1136 self.assertEqual(ac_tester.keywords_opt_kwonly(1), (1, None, None, None)) 1137 self.assertEqual(ac_tester.keywords_opt_kwonly(1, 2), (1, 2, None, None)) 1138 with self.assertRaises(TypeError): 1139 ac_tester.keywords_opt_kwonly(1, 2, 3) 1140 self.assertEqual(ac_tester.keywords_opt_kwonly(1, b=2), (1, 2, None, None)) 1141 self.assertEqual(ac_tester.keywords_opt_kwonly(1, 2, c=3), (1, 2, 3, None)) 1142 self.assertEqual(ac_tester.keywords_opt_kwonly(a=1, c=3), (1, None, 3, None)) 1143 self.assertEqual(ac_tester.keywords_opt_kwonly(a=1, b=2, c=3, d=4), (1, 2, 3, 4)) 1144 1145 def test_keywords_kwonly_opt(self): 1146 self.assertEqual(ac_tester.keywords_kwonly_opt(1), (1, None, None)) 1147 with self.assertRaises(TypeError): 1148 ac_tester.keywords_kwonly_opt(1, 2) 1149 self.assertEqual(ac_tester.keywords_kwonly_opt(1, b=2), (1, 2, None)) 1150 self.assertEqual(ac_tester.keywords_kwonly_opt(a=1, c=3), (1, None, 3)) 1151 self.assertEqual(ac_tester.keywords_kwonly_opt(a=1, b=2, c=3), (1, 2, 3)) 1152 1153 def test_posonly_keywords(self): 1154 with self.assertRaises(TypeError): 1155 ac_tester.posonly_keywords(1) 1156 with self.assertRaises(TypeError): 1157 ac_tester.posonly_keywords(a=1, b=2) 1158 self.assertEqual(ac_tester.posonly_keywords(1, 2), (1, 2)) 1159 self.assertEqual(ac_tester.posonly_keywords(1, b=2), (1, 2)) 1160 1161 def test_posonly_kwonly(self): 1162 with self.assertRaises(TypeError): 1163 ac_tester.posonly_kwonly(1) 1164 with self.assertRaises(TypeError): 1165 ac_tester.posonly_kwonly(1, 2) 1166 with self.assertRaises(TypeError): 1167 ac_tester.posonly_kwonly(a=1, b=2) 1168 self.assertEqual(ac_tester.posonly_kwonly(1, b=2), (1, 2)) 1169 1170 def test_posonly_keywords_kwonly(self): 1171 with self.assertRaises(TypeError): 1172 ac_tester.posonly_keywords_kwonly(1) 1173 with self.assertRaises(TypeError): 1174 ac_tester.posonly_keywords_kwonly(1, 2, 3) 1175 with self.assertRaises(TypeError): 1176 ac_tester.posonly_keywords_kwonly(a=1, b=2, c=3) 1177 self.assertEqual(ac_tester.posonly_keywords_kwonly(1, 2, c=3), (1, 2, 3)) 1178 self.assertEqual(ac_tester.posonly_keywords_kwonly(1, b=2, c=3), (1, 2, 3)) 1179 1180 def test_posonly_keywords_opt(self): 1181 with self.assertRaises(TypeError): 1182 ac_tester.posonly_keywords_opt(1) 1183 self.assertEqual(ac_tester.posonly_keywords_opt(1, 2), (1, 2, None, None)) 1184 self.assertEqual(ac_tester.posonly_keywords_opt(1, 2, 3), (1, 2, 3, None)) 1185 self.assertEqual(ac_tester.posonly_keywords_opt(1, 2, 3, 4), (1, 2, 3, 4)) 1186 self.assertEqual(ac_tester.posonly_keywords_opt(1, b=2), (1, 2, None, None)) 1187 self.assertEqual(ac_tester.posonly_keywords_opt(1, 2, c=3), (1, 2, 3, None)) 1188 with self.assertRaises(TypeError): 1189 ac_tester.posonly_keywords_opt(a=1, b=2, c=3, d=4) 1190 self.assertEqual(ac_tester.posonly_keywords_opt(1, b=2, c=3, d=4), (1, 2, 3, 4)) 1191 1192 def test_posonly_opt_keywords_opt(self): 1193 self.assertEqual(ac_tester.posonly_opt_keywords_opt(1), (1, None, None, None)) 1194 self.assertEqual(ac_tester.posonly_opt_keywords_opt(1, 2), (1, 2, None, None)) 1195 self.assertEqual(ac_tester.posonly_opt_keywords_opt(1, 2, 3), (1, 2, 3, None)) 1196 self.assertEqual(ac_tester.posonly_opt_keywords_opt(1, 2, 3, 4), (1, 2, 3, 4)) 1197 with self.assertRaises(TypeError): 1198 ac_tester.posonly_opt_keywords_opt(1, b=2) 1199 self.assertEqual(ac_tester.posonly_opt_keywords_opt(1, 2, c=3), (1, 2, 3, None)) 1200 self.assertEqual(ac_tester.posonly_opt_keywords_opt(1, 2, c=3, d=4), (1, 2, 3, 4)) 1201 with self.assertRaises(TypeError): 1202 ac_tester.posonly_opt_keywords_opt(a=1, b=2, c=3, d=4) 1203 1204 def test_posonly_kwonly_opt(self): 1205 with self.assertRaises(TypeError): 1206 ac_tester.posonly_kwonly_opt(1) 1207 with self.assertRaises(TypeError): 1208 ac_tester.posonly_kwonly_opt(1, 2) 1209 self.assertEqual(ac_tester.posonly_kwonly_opt(1, b=2), (1, 2, None, None)) 1210 self.assertEqual(ac_tester.posonly_kwonly_opt(1, b=2, c=3), (1, 2, 3, None)) 1211 self.assertEqual(ac_tester.posonly_kwonly_opt(1, b=2, c=3, d=4), (1, 2, 3, 4)) 1212 with self.assertRaises(TypeError): 1213 ac_tester.posonly_kwonly_opt(a=1, b=2, c=3, d=4) 1214 1215 def test_posonly_opt_kwonly_opt(self): 1216 self.assertEqual(ac_tester.posonly_opt_kwonly_opt(1), (1, None, None, None)) 1217 self.assertEqual(ac_tester.posonly_opt_kwonly_opt(1, 2), (1, 2, None, None)) 1218 with self.assertRaises(TypeError): 1219 ac_tester.posonly_opt_kwonly_opt(1, 2, 3) 1220 with self.assertRaises(TypeError): 1221 ac_tester.posonly_opt_kwonly_opt(1, b=2) 1222 self.assertEqual(ac_tester.posonly_opt_kwonly_opt(1, 2, c=3), (1, 2, 3, None)) 1223 self.assertEqual(ac_tester.posonly_opt_kwonly_opt(1, 2, c=3, d=4), (1, 2, 3, 4)) 1224 1225 def test_posonly_keywords_kwonly_opt(self): 1226 with self.assertRaises(TypeError): 1227 ac_tester.posonly_keywords_kwonly_opt(1) 1228 with self.assertRaises(TypeError): 1229 ac_tester.posonly_keywords_kwonly_opt(1, 2) 1230 with self.assertRaises(TypeError): 1231 ac_tester.posonly_keywords_kwonly_opt(1, b=2) 1232 with self.assertRaises(TypeError): 1233 ac_tester.posonly_keywords_kwonly_opt(1, 2, 3) 1234 with self.assertRaises(TypeError): 1235 ac_tester.posonly_keywords_kwonly_opt(a=1, b=2, c=3) 1236 self.assertEqual(ac_tester.posonly_keywords_kwonly_opt(1, 2, c=3), (1, 2, 3, None, None)) 1237 self.assertEqual(ac_tester.posonly_keywords_kwonly_opt(1, b=2, c=3), (1, 2, 3, None, None)) 1238 self.assertEqual(ac_tester.posonly_keywords_kwonly_opt(1, 2, c=3, d=4), (1, 2, 3, 4, None)) 1239 self.assertEqual(ac_tester.posonly_keywords_kwonly_opt(1, 2, c=3, d=4, e=5), (1, 2, 3, 4, 5)) 1240 1241 def test_posonly_keywords_opt_kwonly_opt(self): 1242 with self.assertRaises(TypeError): 1243 ac_tester.posonly_keywords_opt_kwonly_opt(1) 1244 self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, 2), (1, 2, None, None, None)) 1245 self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, b=2), (1, 2, None, None, None)) 1246 with self.assertRaises(TypeError): 1247 ac_tester.posonly_keywords_opt_kwonly_opt(1, 2, 3, 4) 1248 with self.assertRaises(TypeError): 1249 ac_tester.posonly_keywords_opt_kwonly_opt(a=1, b=2) 1250 self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, 2, c=3), (1, 2, 3, None, None)) 1251 self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, b=2, c=3), (1, 2, 3, None, None)) 1252 self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, 2, 3, d=4), (1, 2, 3, 4, None)) 1253 self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, 2, c=3, d=4), (1, 2, 3, 4, None)) 1254 self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, 2, 3, d=4, e=5), (1, 2, 3, 4, 5)) 1255 self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, 2, c=3, d=4, e=5), (1, 2, 3, 4, 5)) 1256 1257 def test_posonly_opt_keywords_opt_kwonly_opt(self): 1258 self.assertEqual(ac_tester.posonly_opt_keywords_opt_kwonly_opt(1), (1, None, None, None)) 1259 self.assertEqual(ac_tester.posonly_opt_keywords_opt_kwonly_opt(1, 2), (1, 2, None, None)) 1260 with self.assertRaises(TypeError): 1261 ac_tester.posonly_opt_keywords_opt_kwonly_opt(1, b=2) 1262 self.assertEqual(ac_tester.posonly_opt_keywords_opt_kwonly_opt(1, 2, 3), (1, 2, 3, None)) 1263 self.assertEqual(ac_tester.posonly_opt_keywords_opt_kwonly_opt(1, 2, c=3), (1, 2, 3, None)) 1264 self.assertEqual(ac_tester.posonly_opt_keywords_opt_kwonly_opt(1, 2, 3, d=4), (1, 2, 3, 4)) 1265 self.assertEqual(ac_tester.posonly_opt_keywords_opt_kwonly_opt(1, 2, c=3, d=4), (1, 2, 3, 4)) 1266 with self.assertRaises(TypeError): 1267 ac_tester.posonly_opt_keywords_opt_kwonly_opt(1, 2, 3, 4) 1268 1269 def test_keyword_only_parameter(self): 1270 with self.assertRaises(TypeError): 1271 ac_tester.keyword_only_parameter() 1272 with self.assertRaises(TypeError): 1273 ac_tester.keyword_only_parameter(1) 1274 self.assertEqual(ac_tester.keyword_only_parameter(a=1), (1,)) 1275 1276 def test_posonly_vararg(self): 1277 with self.assertRaises(TypeError): 1278 ac_tester.posonly_vararg() 1279 self.assertEqual(ac_tester.posonly_vararg(1, 2), (1, 2, ())) 1280 self.assertEqual(ac_tester.posonly_vararg(1, b=2), (1, 2, ())) 1281 self.assertEqual(ac_tester.posonly_vararg(1, 2, 3, 4), (1, 2, (3, 4))) 1282 1283 def test_vararg_and_posonly(self): 1284 with self.assertRaises(TypeError): 1285 ac_tester.vararg_and_posonly() 1286 with self.assertRaises(TypeError): 1287 ac_tester.vararg_and_posonly(1, b=2) 1288 self.assertEqual(ac_tester.vararg_and_posonly(1, 2, 3, 4), (1, (2, 3, 4))) 1289 1290 def test_vararg(self): 1291 with self.assertRaises(TypeError): 1292 ac_tester.vararg() 1293 with self.assertRaises(TypeError): 1294 ac_tester.vararg(1, b=2) 1295 self.assertEqual(ac_tester.vararg(1, 2, 3, 4), (1, (2, 3, 4))) 1296 1297 def test_vararg_with_default(self): 1298 with self.assertRaises(TypeError): 1299 ac_tester.vararg_with_default() 1300 self.assertEqual(ac_tester.vararg_with_default(1, b=False), (1, (), False)) 1301 self.assertEqual(ac_tester.vararg_with_default(1, 2, 3, 4), (1, (2, 3, 4), False)) 1302 self.assertEqual(ac_tester.vararg_with_default(1, 2, 3, 4, b=True), (1, (2, 3, 4), True)) 1303 1304 def test_vararg_with_only_defaults(self): 1305 self.assertEqual(ac_tester.vararg_with_only_defaults(), ((), None)) 1306 self.assertEqual(ac_tester.vararg_with_only_defaults(b=2), ((), 2)) 1307 self.assertEqual(ac_tester.vararg_with_only_defaults(1, b=2), ((1, ), 2)) 1308 self.assertEqual(ac_tester.vararg_with_only_defaults(1, 2, 3, 4), ((1, 2, 3, 4), None)) 1309 self.assertEqual(ac_tester.vararg_with_only_defaults(1, 2, 3, 4, b=5), ((1, 2, 3, 4), 5)) 1310 1311 def test_gh_32092_oob(self): 1312 ac_tester.gh_32092_oob(1, 2, 3, 4, kw1=5, kw2=6) 1313 1314 def test_gh_32092_kw_pass(self): 1315 ac_tester.gh_32092_kw_pass(1, 2, 3) 1316 1317 def test_gh_99233_refcount(self): 1318 arg = '*A unique string is not referenced by anywhere else.*' 1319 arg_refcount_origin = sys.getrefcount(arg) 1320 ac_tester.gh_99233_refcount(arg) 1321 arg_refcount_after = sys.getrefcount(arg) 1322 self.assertEqual(arg_refcount_origin, arg_refcount_after) 1323 1324 def test_gh_99240_double_free(self): 1325 expected_error = r'gh_99240_double_free\(\) argument 2 must be encoded string without null bytes, not str' 1326 with self.assertRaisesRegex(TypeError, expected_error): 1327 ac_tester.gh_99240_double_free('a', '\0b') 1328 1329 1330if __name__ == "__main__": 1331 unittest.main() 1332