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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
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
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
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
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
|
# Argument Clinic
# Copyright 2012-2013 by Larry Hastings.
# Licensed to the PSF under a contributor agreement.
from test import support, test_tools
from test.support import import_helper, os_helper
from unittest import TestCase
import collections
import inspect
import os.path
import sys
import unittest
test_tools.skip_if_missing('clinic')
with test_tools.imports_under_tool('clinic'):
import clinic
from clinic import DSLParser
class FakeConverter:
def __init__(self, name, args):
self.name = name
self.args = args
class FakeConverterFactory:
def __init__(self, name):
self.name = name
def __call__(self, name, default, **kwargs):
return FakeConverter(self.name, kwargs)
class FakeConvertersDict:
def __init__(self):
self.used_converters = {}
def get(self, name, default):
return self.used_converters.setdefault(name, FakeConverterFactory(name))
c = clinic.Clinic(language='C', filename = "file")
class FakeClinic:
def __init__(self):
self.converters = FakeConvertersDict()
self.legacy_converters = FakeConvertersDict()
self.language = clinic.CLanguage(None)
self.filename = None
self.destination_buffers = {}
self.block_parser = clinic.BlockParser('', self.language)
self.modules = collections.OrderedDict()
self.classes = collections.OrderedDict()
clinic.clinic = self
self.name = "FakeClinic"
self.line_prefix = self.line_suffix = ''
self.destinations = {}
self.add_destination("block", "buffer")
self.add_destination("file", "buffer")
self.add_destination("suppress", "suppress")
d = self.destinations.get
self.field_destinations = collections.OrderedDict((
('docstring_prototype', d('suppress')),
('docstring_definition', d('block')),
('methoddef_define', d('block')),
('impl_prototype', d('block')),
('parser_prototype', d('suppress')),
('parser_definition', d('block')),
('impl_definition', d('block')),
))
def get_destination(self, name):
d = self.destinations.get(name)
if not d:
sys.exit("Destination does not exist: " + repr(name))
return d
def add_destination(self, name, type, *args):
if name in self.destinations:
sys.exit("Destination already exists: " + repr(name))
self.destinations[name] = clinic.Destination(name, type, self, *args)
def is_directive(self, name):
return name == "module"
def directive(self, name, args):
self.called_directives[name] = args
_module_and_class = clinic.Clinic._module_and_class
class ClinicWholeFileTest(TestCase):
def test_eol(self):
# regression test:
# clinic's block parser didn't recognize
# the "end line" for the block if it
# didn't end in "\n" (as in, the last)
# byte of the file was '/'.
# so it would spit out an end line for you.
# and since you really already had one,
# the last line of the block got corrupted.
c = clinic.Clinic(clinic.CLanguage(None), filename="file")
raw = "/*[clinic]\nfoo\n[clinic]*/"
cooked, _ = c.parse(raw)
lines = cooked.splitlines()
end_line = lines[2].rstrip()
# this test is redundant, it's just here explicitly to catch
# the regression test so we don't forget what it looked like
self.assertNotEqual(end_line, "[clinic]*/[clinic]*/")
self.assertEqual(end_line, "[clinic]*/")
class ClinicGroupPermuterTest(TestCase):
def _test(self, l, m, r, output):
computed = clinic.permute_optional_groups(l, m, r)
self.assertEqual(output, computed)
def test_range(self):
self._test([['start']], ['stop'], [['step']],
(
('stop',),
('start', 'stop',),
('start', 'stop', 'step',),
))
def test_add_window(self):
self._test([['x', 'y']], ['ch'], [['attr']],
(
('ch',),
('ch', 'attr'),
('x', 'y', 'ch',),
('x', 'y', 'ch', 'attr'),
))
def test_ludicrous(self):
self._test([['a1', 'a2', 'a3'], ['b1', 'b2']], ['c1'], [['d1', 'd2'], ['e1', 'e2', 'e3']],
(
('c1',),
('b1', 'b2', 'c1'),
('b1', 'b2', 'c1', 'd1', 'd2'),
('a1', 'a2', 'a3', 'b1', 'b2', 'c1'),
('a1', 'a2', 'a3', 'b1', 'b2', 'c1', 'd1', 'd2'),
('a1', 'a2', 'a3', 'b1', 'b2', 'c1', 'd1', 'd2', 'e1', 'e2', 'e3'),
))
def test_right_only(self):
self._test([], [], [['a'],['b'],['c']],
(
(),
('a',),
('a', 'b'),
('a', 'b', 'c')
))
def test_have_left_options_but_required_is_empty(self):
def fn():
clinic.permute_optional_groups(['a'], [], [])
self.assertRaises(ValueError, fn)
class ClinicLinearFormatTest(TestCase):
def _test(self, input, output, **kwargs):
computed = clinic.linear_format(input, **kwargs)
self.assertEqual(output, computed)
def test_empty_strings(self):
self._test('', '')
def test_solo_newline(self):
self._test('\n', '\n')
def test_no_substitution(self):
self._test("""
abc
""", """
abc
""")
def test_empty_substitution(self):
self._test("""
abc
{name}
def
""", """
abc
def
""", name='')
def test_single_line_substitution(self):
self._test("""
abc
{name}
def
""", """
abc
GARGLE
def
""", name='GARGLE')
def test_multiline_substitution(self):
self._test("""
abc
{name}
def
""", """
abc
bingle
bungle
def
""", name='bingle\nbungle\n')
class InertParser:
def __init__(self, clinic):
pass
def parse(self, block):
pass
class CopyParser:
def __init__(self, clinic):
pass
def parse(self, block):
block.output = block.input
class ClinicBlockParserTest(TestCase):
def _test(self, input, output):
language = clinic.CLanguage(None)
blocks = list(clinic.BlockParser(input, language))
writer = clinic.BlockPrinter(language)
for block in blocks:
writer.print_block(block)
output = writer.f.getvalue()
assert output == input, "output != input!\n\noutput " + repr(output) + "\n\n input " + repr(input)
def round_trip(self, input):
return self._test(input, input)
def test_round_trip_1(self):
self.round_trip("""
verbatim text here
lah dee dah
""")
def test_round_trip_2(self):
self.round_trip("""
verbatim text here
lah dee dah
/*[inert]
abc
[inert]*/
def
/*[inert checksum: 7b18d017f89f61cf17d47f92749ea6930a3f1deb]*/
xyz
""")
def _test_clinic(self, input, output):
language = clinic.CLanguage(None)
c = clinic.Clinic(language, filename="file")
c.parsers['inert'] = InertParser(c)
c.parsers['copy'] = CopyParser(c)
computed, _ = c.parse(input)
self.assertEqual(output, computed)
def test_clinic_1(self):
self._test_clinic("""
verbatim text here
lah dee dah
/*[copy input]
def
[copy start generated code]*/
abc
/*[copy end generated code: output=03cfd743661f0797 input=7b18d017f89f61cf]*/
xyz
""", """
verbatim text here
lah dee dah
/*[copy input]
def
[copy start generated code]*/
def
/*[copy end generated code: output=7b18d017f89f61cf input=7b18d017f89f61cf]*/
xyz
""")
class ClinicParserTest(TestCase):
def test_trivial(self):
parser = DSLParser(FakeClinic())
block = clinic.Block("module os\nos.access")
parser.parse(block)
module, function = block.signatures
self.assertEqual("access", function.name)
self.assertEqual("os", module.name)
def test_ignore_line(self):
block = self.parse("#\nmodule os\nos.access")
module, function = block.signatures
self.assertEqual("access", function.name)
self.assertEqual("os", module.name)
def test_param(self):
function = self.parse_function("module os\nos.access\n path: int")
self.assertEqual("access", function.name)
self.assertEqual(2, len(function.parameters))
p = function.parameters['path']
self.assertEqual('path', p.name)
self.assertIsInstance(p.converter, clinic.int_converter)
def test_param_default(self):
function = self.parse_function("module os\nos.access\n follow_symlinks: bool = True")
p = function.parameters['follow_symlinks']
self.assertEqual(True, p.default)
def test_param_with_continuations(self):
function = self.parse_function("module os\nos.access\n follow_symlinks: \\\n bool \\\n =\\\n True")
p = function.parameters['follow_symlinks']
self.assertEqual(True, p.default)
def test_param_default_expression(self):
function = self.parse_function("module os\nos.access\n follow_symlinks: int(c_default='MAXSIZE') = sys.maxsize")
p = function.parameters['follow_symlinks']
self.assertEqual(sys.maxsize, p.default)
self.assertEqual("MAXSIZE", p.converter.c_default)
s = self.parse_function_should_fail("module os\nos.access\n follow_symlinks: int = sys.maxsize")
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")
def test_param_no_docstring(self):
function = self.parse_function("""
module os
os.access
follow_symlinks: bool = True
something_else: str = ''""")
p = function.parameters['follow_symlinks']
self.assertEqual(3, len(function.parameters))
self.assertIsInstance(function.parameters['something_else'].converter, clinic.str_converter)
def test_param_default_parameters_out_of_order(self):
s = self.parse_function_should_fail("""
module os
os.access
follow_symlinks: bool = True
something_else: str""")
self.assertEqual(s, """Error on line 0:
Can't have a parameter without a default ('something_else')
after a parameter with a default!
""")
def disabled_test_converter_arguments(self):
function = self.parse_function("module os\nos.access\n path: path_t(allow_fd=1)")
p = function.parameters['path']
self.assertEqual(1, p.converter.args['allow_fd'])
def test_function_docstring(self):
function = self.parse_function("""
module os
os.stat as os_stat_fn
path: str
Path to be examined
Perform a stat system call on the given path.""")
self.assertEqual("""
stat($module, /, path)
--
Perform a stat system call on the given path.
path
Path to be examined
""".strip(), function.docstring)
def test_explicit_parameters_in_docstring(self):
function = self.parse_function("""
module foo
foo.bar
x: int
Documentation for x.
y: int
This is the documentation for foo.
Okay, we're done here.
""")
self.assertEqual("""
bar($module, /, x, y)
--
This is the documentation for foo.
x
Documentation for x.
Okay, we're done here.
""".strip(), function.docstring)
def test_parser_regression_special_character_in_parameter_column_of_docstring_first_line(self):
function = self.parse_function("""
module os
os.stat
path: str
This/used to break Clinic!
""")
self.assertEqual("stat($module, /, path)\n--\n\nThis/used to break Clinic!", function.docstring)
def test_c_name(self):
function = self.parse_function("module os\nos.stat as os_stat_fn")
self.assertEqual("os_stat_fn", function.c_basename)
def test_return_converter(self):
function = self.parse_function("module os\nos.stat -> int")
self.assertIsInstance(function.return_converter, clinic.int_return_converter)
def test_star(self):
function = self.parse_function("module os\nos.access\n *\n follow_symlinks: bool = True")
p = function.parameters['follow_symlinks']
self.assertEqual(inspect.Parameter.KEYWORD_ONLY, p.kind)
self.assertEqual(0, p.group)
def test_group(self):
function = self.parse_function("module window\nwindow.border\n [\n ls : int\n ]\n /\n")
p = function.parameters['ls']
self.assertEqual(1, p.group)
def test_left_group(self):
function = self.parse_function("""
module curses
curses.addch
[
y: int
Y-coordinate.
x: int
X-coordinate.
]
ch: char
Character to add.
[
attr: long
Attributes for the character.
]
/
""")
for name, group in (
('y', -1), ('x', -1),
('ch', 0),
('attr', 1),
):
p = function.parameters[name]
self.assertEqual(p.group, group)
self.assertEqual(p.kind, inspect.Parameter.POSITIONAL_ONLY)
self.assertEqual(function.docstring.strip(), """
addch([y, x,] ch, [attr])
y
Y-coordinate.
x
X-coordinate.
ch
Character to add.
attr
Attributes for the character.
""".strip())
def test_nested_groups(self):
function = self.parse_function("""
module curses
curses.imaginary
[
[
y1: int
Y-coordinate.
y2: int
Y-coordinate.
]
x1: int
X-coordinate.
x2: int
X-coordinate.
]
ch: char
Character to add.
[
attr1: long
Attributes for the character.
attr2: long
Attributes for the character.
attr3: long
Attributes for the character.
[
attr4: long
Attributes for the character.
attr5: long
Attributes for the character.
attr6: long
Attributes for the character.
]
]
/
""")
for name, group in (
('y1', -2), ('y2', -2),
('x1', -1), ('x2', -1),
('ch', 0),
('attr1', 1), ('attr2', 1), ('attr3', 1),
('attr4', 2), ('attr5', 2), ('attr6', 2),
):
p = function.parameters[name]
self.assertEqual(p.group, group)
self.assertEqual(p.kind, inspect.Parameter.POSITIONAL_ONLY)
self.assertEqual(function.docstring.strip(), """
imaginary([[y1, y2,] x1, x2,] ch, [attr1, attr2, attr3, [attr4, attr5,
attr6]])
y1
Y-coordinate.
y2
Y-coordinate.
x1
X-coordinate.
x2
X-coordinate.
ch
Character to add.
attr1
Attributes for the character.
attr2
Attributes for the character.
attr3
Attributes for the character.
attr4
Attributes for the character.
attr5
Attributes for the character.
attr6
Attributes for the character.
""".strip())
def parse_function_should_fail(self, s):
with support.captured_stdout() as stdout:
with self.assertRaises(SystemExit):
self.parse_function(s)
return stdout.getvalue()
def test_disallowed_grouping__two_top_groups_on_left(self):
s = self.parse_function_should_fail("""
module foo
foo.two_top_groups_on_left
[
group1 : int
]
[
group2 : int
]
param: int
""")
self.assertEqual(s,
('Error on line 0:\n'
'Function two_top_groups_on_left has an unsupported group configuration. (Unexpected state 2.b)\n'))
def test_disallowed_grouping__two_top_groups_on_right(self):
self.parse_function_should_fail("""
module foo
foo.two_top_groups_on_right
param: int
[
group1 : int
]
[
group2 : int
]
""")
def test_disallowed_grouping__parameter_after_group_on_right(self):
self.parse_function_should_fail("""
module foo
foo.parameter_after_group_on_right
param: int
[
[
group1 : int
]
group2 : int
]
""")
def test_disallowed_grouping__group_after_parameter_on_left(self):
self.parse_function_should_fail("""
module foo
foo.group_after_parameter_on_left
[
group2 : int
[
group1 : int
]
]
param: int
""")
def test_disallowed_grouping__empty_group_on_left(self):
self.parse_function_should_fail("""
module foo
foo.empty_group
[
[
]
group2 : int
]
param: int
""")
def test_disallowed_grouping__empty_group_on_right(self):
self.parse_function_should_fail("""
module foo
foo.empty_group
param: int
[
[
]
group2 : int
]
""")
def test_no_parameters(self):
function = self.parse_function("""
module foo
foo.bar
Docstring
""")
self.assertEqual("bar($module, /)\n--\n\nDocstring", function.docstring)
self.assertEqual(1, len(function.parameters)) # self!
def test_init_with_no_parameters(self):
function = self.parse_function("""
module foo
class foo.Bar "unused" "notneeded"
foo.Bar.__init__
Docstring
""", signatures_in_block=3, function_index=2)
# self is not in the signature
self.assertEqual("Bar()\n--\n\nDocstring", function.docstring)
# but it *is* a parameter
self.assertEqual(1, len(function.parameters))
def test_illegal_module_line(self):
self.parse_function_should_fail("""
module foo
foo.bar => int
/
""")
def test_illegal_c_basename(self):
self.parse_function_should_fail("""
module foo
foo.bar as 935
/
""")
def test_single_star(self):
self.parse_function_should_fail("""
module foo
foo.bar
*
*
""")
def test_parameters_required_after_star_without_initial_parameters_or_docstring(self):
self.parse_function_should_fail("""
module foo
foo.bar
*
""")
def test_parameters_required_after_star_without_initial_parameters_with_docstring(self):
self.parse_function_should_fail("""
module foo
foo.bar
*
Docstring here.
""")
def test_parameters_required_after_star_with_initial_parameters_without_docstring(self):
self.parse_function_should_fail("""
module foo
foo.bar
this: int
*
""")
def test_parameters_required_after_star_with_initial_parameters_and_docstring(self):
self.parse_function_should_fail("""
module foo
foo.bar
this: int
*
Docstring.
""")
def test_single_slash(self):
self.parse_function_should_fail("""
module foo
foo.bar
/
/
""")
def test_mix_star_and_slash(self):
self.parse_function_should_fail("""
module foo
foo.bar
x: int
y: int
*
z: int
/
""")
def test_parameters_not_permitted_after_slash_for_now(self):
self.parse_function_should_fail("""
module foo
foo.bar
/
x: int
""")
def test_parameters_no_more_than_one_vararg(self):
s = self.parse_function_should_fail("""
module foo
foo.bar
*vararg1: object
*vararg2: object
""")
self.assertEqual(s, "Error on line 0:\nToo many var args\n")
def test_function_not_at_column_0(self):
function = self.parse_function("""
module foo
foo.bar
x: int
Nested docstring here, goeth.
*
y: str
Not at column 0!
""")
self.assertEqual("""
bar($module, /, x, *, y)
--
Not at column 0!
x
Nested docstring here, goeth.
""".strip(), function.docstring)
def test_directive(self):
c = FakeClinic()
parser = DSLParser(c)
parser.flag = False
parser.directives['setflag'] = lambda : setattr(parser, 'flag', True)
block = clinic.Block("setflag")
parser.parse(block)
self.assertTrue(parser.flag)
def test_legacy_converters(self):
block = self.parse('module os\nos.access\n path: "s"')
module, function = block.signatures
self.assertIsInstance((function.parameters['path']).converter, clinic.str_converter)
def test_unused_param(self):
block = self.parse("""
module foo
foo.func
fn: object
k: float
i: float(unused=True)
/
*
flag: bool(unused=True) = False
""")
sig = block.signatures[1] # Function index == 1
params = sig.parameters
conv = lambda fn: params[fn].converter
dataset = (
{"name": "fn", "unused": False},
{"name": "k", "unused": False},
{"name": "i", "unused": True},
{"name": "flag", "unused": True},
)
for param in dataset:
name, unused = param.values()
with self.subTest(name=name, unused=unused):
p = conv(name)
# Verify that the unused flag is parsed correctly.
self.assertEqual(unused, p.unused)
# Now, check that we'll produce correct code.
decl = p.simple_declaration(in_parser=False)
if unused:
self.assertIn("Py_UNUSED", decl)
else:
self.assertNotIn("Py_UNUSED", decl)
# Make sure the Py_UNUSED macro is not used in the parser body.
parser_decl = p.simple_declaration(in_parser=True)
self.assertNotIn("Py_UNUSED", parser_decl)
def parse(self, text):
c = FakeClinic()
parser = DSLParser(c)
block = clinic.Block(text)
parser.parse(block)
return block
def parse_function(self, text, signatures_in_block=2, function_index=1):
block = self.parse(text)
s = block.signatures
self.assertEqual(len(s), signatures_in_block)
assert isinstance(s[0], clinic.Module)
assert isinstance(s[function_index], clinic.Function)
return s[function_index]
def test_scaffolding(self):
# test repr on special values
self.assertEqual(repr(clinic.unspecified), '<Unspecified>')
self.assertEqual(repr(clinic.NULL), '<Null>')
# test that fail fails
with support.captured_stdout() as stdout:
with self.assertRaises(SystemExit):
clinic.fail('The igloos are melting!', filename='clown.txt', line_number=69)
self.assertEqual(stdout.getvalue(), 'Error in file "clown.txt" on line 69:\nThe igloos are melting!\n')
class ClinicExternalTest(TestCase):
maxDiff = None
def test_external(self):
# bpo-42398: Test that the destination file is left unchanged if the
# content does not change. Moreover, check also that the file
# modification time does not change in this case.
source = support.findfile('clinic.test')
with open(source, 'r', encoding='utf-8') as f:
orig_contents = f.read()
with os_helper.temp_dir() as tmp_dir:
testfile = os.path.join(tmp_dir, 'clinic.test.c')
with open(testfile, 'w', encoding='utf-8') as f:
f.write(orig_contents)
old_mtime_ns = os.stat(testfile).st_mtime_ns
clinic.parse_file(testfile)
with open(testfile, 'r', encoding='utf-8') as f:
new_contents = f.read()
new_mtime_ns = os.stat(testfile).st_mtime_ns
self.assertEqual(new_contents, orig_contents)
# Don't change the file modification time
# if the content does not change
self.assertEqual(new_mtime_ns, old_mtime_ns)
ac_tester = import_helper.import_module('_testclinic')
class ClinicFunctionalTest(unittest.TestCase):
locals().update((name, getattr(ac_tester, name))
for name in dir(ac_tester) if name.startswith('test_'))
def test_objects_converter(self):
with self.assertRaises(TypeError):
ac_tester.objects_converter()
self.assertEqual(ac_tester.objects_converter(1, 2), (1, 2))
self.assertEqual(ac_tester.objects_converter([], 'whatever class'), ([], 'whatever class'))
self.assertEqual(ac_tester.objects_converter(1), (1, None))
def test_bytes_object_converter(self):
with self.assertRaises(TypeError):
ac_tester.bytes_object_converter(1)
self.assertEqual(ac_tester.bytes_object_converter(b'BytesObject'), (b'BytesObject',))
def test_byte_array_object_converter(self):
with self.assertRaises(TypeError):
ac_tester.byte_array_object_converter(1)
byte_arr = bytearray(b'ByteArrayObject')
self.assertEqual(ac_tester.byte_array_object_converter(byte_arr), (byte_arr,))
def test_unicode_converter(self):
with self.assertRaises(TypeError):
ac_tester.unicode_converter(1)
self.assertEqual(ac_tester.unicode_converter('unicode'), ('unicode',))
def test_bool_converter(self):
with self.assertRaises(TypeError):
ac_tester.bool_converter(False, False, 'not a int')
self.assertEqual(ac_tester.bool_converter(), (True, True, True))
self.assertEqual(ac_tester.bool_converter('', [], 5), (False, False, True))
self.assertEqual(ac_tester.bool_converter(('not empty',), {1: 2}, 0), (True, True, False))
def test_char_converter(self):
with self.assertRaises(TypeError):
ac_tester.char_converter(1)
with self.assertRaises(TypeError):
ac_tester.char_converter(b'ab')
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']
expected = tuple(ord(c) for c in chars)
self.assertEqual(ac_tester.char_converter(), expected)
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']
expected = tuple(ord(c) for c in chars)
self.assertEqual(ac_tester.char_converter(*chars), expected)
def test_unsigned_char_converter(self):
from _testcapi import UCHAR_MAX
with self.assertRaises(OverflowError):
ac_tester.unsigned_char_converter(-1)
with self.assertRaises(OverflowError):
ac_tester.unsigned_char_converter(UCHAR_MAX + 1)
with self.assertRaises(OverflowError):
ac_tester.unsigned_char_converter(0, UCHAR_MAX + 1)
with self.assertRaises(TypeError):
ac_tester.unsigned_char_converter([])
self.assertEqual(ac_tester.unsigned_char_converter(), (12, 34, 56))
self.assertEqual(ac_tester.unsigned_char_converter(0, 0, UCHAR_MAX + 1), (0, 0, 0))
self.assertEqual(ac_tester.unsigned_char_converter(0, 0, (UCHAR_MAX + 1) * 3 + 123), (0, 0, 123))
def test_short_converter(self):
from _testcapi import SHRT_MIN, SHRT_MAX
with self.assertRaises(OverflowError):
ac_tester.short_converter(SHRT_MIN - 1)
with self.assertRaises(OverflowError):
ac_tester.short_converter(SHRT_MAX + 1)
with self.assertRaises(TypeError):
ac_tester.short_converter([])
self.assertEqual(ac_tester.short_converter(-1234), (-1234,))
self.assertEqual(ac_tester.short_converter(4321), (4321,))
def test_unsigned_short_converter(self):
from _testcapi import USHRT_MAX
with self.assertRaises(ValueError):
ac_tester.unsigned_short_converter(-1)
with self.assertRaises(OverflowError):
ac_tester.unsigned_short_converter(USHRT_MAX + 1)
with self.assertRaises(OverflowError):
ac_tester.unsigned_short_converter(0, USHRT_MAX + 1)
with self.assertRaises(TypeError):
ac_tester.unsigned_short_converter([])
self.assertEqual(ac_tester.unsigned_short_converter(), (12, 34, 56))
self.assertEqual(ac_tester.unsigned_short_converter(0, 0, USHRT_MAX + 1), (0, 0, 0))
self.assertEqual(ac_tester.unsigned_short_converter(0, 0, (USHRT_MAX + 1) * 3 + 123), (0, 0, 123))
def test_int_converter(self):
from _testcapi import INT_MIN, INT_MAX
with self.assertRaises(OverflowError):
ac_tester.int_converter(INT_MIN - 1)
with self.assertRaises(OverflowError):
ac_tester.int_converter(INT_MAX + 1)
with self.assertRaises(TypeError):
ac_tester.int_converter(1, 2, 3)
with self.assertRaises(TypeError):
ac_tester.int_converter([])
self.assertEqual(ac_tester.int_converter(), (12, 34, 45))
self.assertEqual(ac_tester.int_converter(1, 2, '3'), (1, 2, ord('3')))
def test_unsigned_int_converter(self):
from _testcapi import UINT_MAX
with self.assertRaises(ValueError):
ac_tester.unsigned_int_converter(-1)
with self.assertRaises(OverflowError):
ac_tester.unsigned_int_converter(UINT_MAX + 1)
with self.assertRaises(OverflowError):
ac_tester.unsigned_int_converter(0, UINT_MAX + 1)
with self.assertRaises(TypeError):
ac_tester.unsigned_int_converter([])
self.assertEqual(ac_tester.unsigned_int_converter(), (12, 34, 56))
self.assertEqual(ac_tester.unsigned_int_converter(0, 0, UINT_MAX + 1), (0, 0, 0))
self.assertEqual(ac_tester.unsigned_int_converter(0, 0, (UINT_MAX + 1) * 3 + 123), (0, 0, 123))
def test_long_converter(self):
from _testcapi import LONG_MIN, LONG_MAX
with self.assertRaises(OverflowError):
ac_tester.long_converter(LONG_MIN - 1)
with self.assertRaises(OverflowError):
ac_tester.long_converter(LONG_MAX + 1)
with self.assertRaises(TypeError):
ac_tester.long_converter([])
self.assertEqual(ac_tester.long_converter(), (12,))
self.assertEqual(ac_tester.long_converter(-1234), (-1234,))
def test_unsigned_long_converter(self):
from _testcapi import ULONG_MAX
with self.assertRaises(ValueError):
ac_tester.unsigned_long_converter(-1)
with self.assertRaises(OverflowError):
ac_tester.unsigned_long_converter(ULONG_MAX + 1)
with self.assertRaises(OverflowError):
ac_tester.unsigned_long_converter(0, ULONG_MAX + 1)
with self.assertRaises(TypeError):
ac_tester.unsigned_long_converter([])
self.assertEqual(ac_tester.unsigned_long_converter(), (12, 34, 56))
self.assertEqual(ac_tester.unsigned_long_converter(0, 0, ULONG_MAX + 1), (0, 0, 0))
self.assertEqual(ac_tester.unsigned_long_converter(0, 0, (ULONG_MAX + 1) * 3 + 123), (0, 0, 123))
def test_long_long_converter(self):
from _testcapi import LLONG_MIN, LLONG_MAX
with self.assertRaises(OverflowError):
ac_tester.long_long_converter(LLONG_MIN - 1)
with self.assertRaises(OverflowError):
ac_tester.long_long_converter(LLONG_MAX + 1)
with self.assertRaises(TypeError):
ac_tester.long_long_converter([])
self.assertEqual(ac_tester.long_long_converter(), (12,))
self.assertEqual(ac_tester.long_long_converter(-1234), (-1234,))
def test_unsigned_long_long_converter(self):
from _testcapi import ULLONG_MAX
with self.assertRaises(ValueError):
ac_tester.unsigned_long_long_converter(-1)
with self.assertRaises(OverflowError):
ac_tester.unsigned_long_long_converter(ULLONG_MAX + 1)
with self.assertRaises(OverflowError):
ac_tester.unsigned_long_long_converter(0, ULLONG_MAX + 1)
with self.assertRaises(TypeError):
ac_tester.unsigned_long_long_converter([])
self.assertEqual(ac_tester.unsigned_long_long_converter(), (12, 34, 56))
self.assertEqual(ac_tester.unsigned_long_long_converter(0, 0, ULLONG_MAX + 1), (0, 0, 0))
self.assertEqual(ac_tester.unsigned_long_long_converter(0, 0, (ULLONG_MAX + 1) * 3 + 123), (0, 0, 123))
def test_py_ssize_t_converter(self):
from _testcapi import PY_SSIZE_T_MIN, PY_SSIZE_T_MAX
with self.assertRaises(OverflowError):
ac_tester.py_ssize_t_converter(PY_SSIZE_T_MIN - 1)
with self.assertRaises(OverflowError):
ac_tester.py_ssize_t_converter(PY_SSIZE_T_MAX + 1)
with self.assertRaises(TypeError):
ac_tester.py_ssize_t_converter([])
self.assertEqual(ac_tester.py_ssize_t_converter(), (12, 34, 56))
self.assertEqual(ac_tester.py_ssize_t_converter(1, 2, None), (1, 2, 56))
def test_slice_index_converter(self):
from _testcapi import PY_SSIZE_T_MIN, PY_SSIZE_T_MAX
with self.assertRaises(TypeError):
ac_tester.slice_index_converter([])
self.assertEqual(ac_tester.slice_index_converter(), (12, 34, 56))
self.assertEqual(ac_tester.slice_index_converter(1, 2, None), (1, 2, 56))
self.assertEqual(ac_tester.slice_index_converter(PY_SSIZE_T_MAX, PY_SSIZE_T_MAX + 1, PY_SSIZE_T_MAX + 1234),
(PY_SSIZE_T_MAX, PY_SSIZE_T_MAX, PY_SSIZE_T_MAX))
self.assertEqual(ac_tester.slice_index_converter(PY_SSIZE_T_MIN, PY_SSIZE_T_MIN - 1, PY_SSIZE_T_MIN - 1234),
(PY_SSIZE_T_MIN, PY_SSIZE_T_MIN, PY_SSIZE_T_MIN))
def test_size_t_converter(self):
with self.assertRaises(ValueError):
ac_tester.size_t_converter(-1)
with self.assertRaises(TypeError):
ac_tester.size_t_converter([])
self.assertEqual(ac_tester.size_t_converter(), (12,))
def test_float_converter(self):
with self.assertRaises(TypeError):
ac_tester.float_converter([])
self.assertEqual(ac_tester.float_converter(), (12.5,))
self.assertEqual(ac_tester.float_converter(-0.5), (-0.5,))
def test_double_converter(self):
with self.assertRaises(TypeError):
ac_tester.double_converter([])
self.assertEqual(ac_tester.double_converter(), (12.5,))
self.assertEqual(ac_tester.double_converter(-0.5), (-0.5,))
def test_py_complex_converter(self):
with self.assertRaises(TypeError):
ac_tester.py_complex_converter([])
self.assertEqual(ac_tester.py_complex_converter(complex(1, 2)), (complex(1, 2),))
self.assertEqual(ac_tester.py_complex_converter(complex('-1-2j')), (complex('-1-2j'),))
self.assertEqual(ac_tester.py_complex_converter(-0.5), (-0.5,))
self.assertEqual(ac_tester.py_complex_converter(10), (10,))
def test_str_converter(self):
with self.assertRaises(TypeError):
ac_tester.str_converter(1)
with self.assertRaises(TypeError):
ac_tester.str_converter('a', 'b', 'c')
with self.assertRaises(ValueError):
ac_tester.str_converter('a', b'b\0b', 'c')
self.assertEqual(ac_tester.str_converter('a', b'b', 'c'), ('a', 'b', 'c'))
self.assertEqual(ac_tester.str_converter('a', b'b', b'c'), ('a', 'b', 'c'))
self.assertEqual(ac_tester.str_converter('a', b'b', 'c\0c'), ('a', 'b', 'c\0c'))
def test_str_converter_encoding(self):
with self.assertRaises(TypeError):
ac_tester.str_converter_encoding(1)
self.assertEqual(ac_tester.str_converter_encoding('a', 'b', 'c'), ('a', 'b', 'c'))
with self.assertRaises(TypeError):
ac_tester.str_converter_encoding('a', b'b\0b', 'c')
self.assertEqual(ac_tester.str_converter_encoding('a', b'b', bytearray([ord('c')])), ('a', 'b', 'c'))
self.assertEqual(ac_tester.str_converter_encoding('a', b'b', bytearray([ord('c'), 0, ord('c')])),
('a', 'b', 'c\x00c'))
self.assertEqual(ac_tester.str_converter_encoding('a', b'b', b'c\x00c'), ('a', 'b', 'c\x00c'))
def test_py_buffer_converter(self):
with self.assertRaises(TypeError):
ac_tester.py_buffer_converter('a', 'b')
self.assertEqual(ac_tester.py_buffer_converter('abc', bytearray([1, 2, 3])), (b'abc', b'\x01\x02\x03'))
def test_keywords(self):
self.assertEqual(ac_tester.keywords(1, 2), (1, 2))
self.assertEqual(ac_tester.keywords(1, b=2), (1, 2))
self.assertEqual(ac_tester.keywords(a=1, b=2), (1, 2))
def test_keywords_kwonly(self):
with self.assertRaises(TypeError):
ac_tester.keywords_kwonly(1, 2)
self.assertEqual(ac_tester.keywords_kwonly(1, b=2), (1, 2))
self.assertEqual(ac_tester.keywords_kwonly(a=1, b=2), (1, 2))
def test_keywords_opt(self):
self.assertEqual(ac_tester.keywords_opt(1), (1, None, None))
self.assertEqual(ac_tester.keywords_opt(1, 2), (1, 2, None))
self.assertEqual(ac_tester.keywords_opt(1, 2, 3), (1, 2, 3))
self.assertEqual(ac_tester.keywords_opt(1, b=2), (1, 2, None))
self.assertEqual(ac_tester.keywords_opt(1, 2, c=3), (1, 2, 3))
self.assertEqual(ac_tester.keywords_opt(a=1, c=3), (1, None, 3))
self.assertEqual(ac_tester.keywords_opt(a=1, b=2, c=3), (1, 2, 3))
def test_keywords_opt_kwonly(self):
self.assertEqual(ac_tester.keywords_opt_kwonly(1), (1, None, None, None))
self.assertEqual(ac_tester.keywords_opt_kwonly(1, 2), (1, 2, None, None))
with self.assertRaises(TypeError):
ac_tester.keywords_opt_kwonly(1, 2, 3)
self.assertEqual(ac_tester.keywords_opt_kwonly(1, b=2), (1, 2, None, None))
self.assertEqual(ac_tester.keywords_opt_kwonly(1, 2, c=3), (1, 2, 3, None))
self.assertEqual(ac_tester.keywords_opt_kwonly(a=1, c=3), (1, None, 3, None))
self.assertEqual(ac_tester.keywords_opt_kwonly(a=1, b=2, c=3, d=4), (1, 2, 3, 4))
def test_keywords_kwonly_opt(self):
self.assertEqual(ac_tester.keywords_kwonly_opt(1), (1, None, None))
with self.assertRaises(TypeError):
ac_tester.keywords_kwonly_opt(1, 2)
self.assertEqual(ac_tester.keywords_kwonly_opt(1, b=2), (1, 2, None))
self.assertEqual(ac_tester.keywords_kwonly_opt(a=1, c=3), (1, None, 3))
self.assertEqual(ac_tester.keywords_kwonly_opt(a=1, b=2, c=3), (1, 2, 3))
def test_posonly_keywords(self):
with self.assertRaises(TypeError):
ac_tester.posonly_keywords(1)
with self.assertRaises(TypeError):
ac_tester.posonly_keywords(a=1, b=2)
self.assertEqual(ac_tester.posonly_keywords(1, 2), (1, 2))
self.assertEqual(ac_tester.posonly_keywords(1, b=2), (1, 2))
def test_posonly_kwonly(self):
with self.assertRaises(TypeError):
ac_tester.posonly_kwonly(1)
with self.assertRaises(TypeError):
ac_tester.posonly_kwonly(1, 2)
with self.assertRaises(TypeError):
ac_tester.posonly_kwonly(a=1, b=2)
self.assertEqual(ac_tester.posonly_kwonly(1, b=2), (1, 2))
def test_posonly_keywords_kwonly(self):
with self.assertRaises(TypeError):
ac_tester.posonly_keywords_kwonly(1)
with self.assertRaises(TypeError):
ac_tester.posonly_keywords_kwonly(1, 2, 3)
with self.assertRaises(TypeError):
ac_tester.posonly_keywords_kwonly(a=1, b=2, c=3)
self.assertEqual(ac_tester.posonly_keywords_kwonly(1, 2, c=3), (1, 2, 3))
self.assertEqual(ac_tester.posonly_keywords_kwonly(1, b=2, c=3), (1, 2, 3))
def test_posonly_keywords_opt(self):
with self.assertRaises(TypeError):
ac_tester.posonly_keywords_opt(1)
self.assertEqual(ac_tester.posonly_keywords_opt(1, 2), (1, 2, None, None))
self.assertEqual(ac_tester.posonly_keywords_opt(1, 2, 3), (1, 2, 3, None))
self.assertEqual(ac_tester.posonly_keywords_opt(1, 2, 3, 4), (1, 2, 3, 4))
self.assertEqual(ac_tester.posonly_keywords_opt(1, b=2), (1, 2, None, None))
self.assertEqual(ac_tester.posonly_keywords_opt(1, 2, c=3), (1, 2, 3, None))
with self.assertRaises(TypeError):
ac_tester.posonly_keywords_opt(a=1, b=2, c=3, d=4)
self.assertEqual(ac_tester.posonly_keywords_opt(1, b=2, c=3, d=4), (1, 2, 3, 4))
def test_posonly_opt_keywords_opt(self):
self.assertEqual(ac_tester.posonly_opt_keywords_opt(1), (1, None, None, None))
self.assertEqual(ac_tester.posonly_opt_keywords_opt(1, 2), (1, 2, None, None))
self.assertEqual(ac_tester.posonly_opt_keywords_opt(1, 2, 3), (1, 2, 3, None))
self.assertEqual(ac_tester.posonly_opt_keywords_opt(1, 2, 3, 4), (1, 2, 3, 4))
with self.assertRaises(TypeError):
ac_tester.posonly_opt_keywords_opt(1, b=2)
self.assertEqual(ac_tester.posonly_opt_keywords_opt(1, 2, c=3), (1, 2, 3, None))
self.assertEqual(ac_tester.posonly_opt_keywords_opt(1, 2, c=3, d=4), (1, 2, 3, 4))
with self.assertRaises(TypeError):
ac_tester.posonly_opt_keywords_opt(a=1, b=2, c=3, d=4)
def test_posonly_kwonly_opt(self):
with self.assertRaises(TypeError):
ac_tester.posonly_kwonly_opt(1)
with self.assertRaises(TypeError):
ac_tester.posonly_kwonly_opt(1, 2)
self.assertEqual(ac_tester.posonly_kwonly_opt(1, b=2), (1, 2, None, None))
self.assertEqual(ac_tester.posonly_kwonly_opt(1, b=2, c=3), (1, 2, 3, None))
self.assertEqual(ac_tester.posonly_kwonly_opt(1, b=2, c=3, d=4), (1, 2, 3, 4))
with self.assertRaises(TypeError):
ac_tester.posonly_kwonly_opt(a=1, b=2, c=3, d=4)
def test_posonly_opt_kwonly_opt(self):
self.assertEqual(ac_tester.posonly_opt_kwonly_opt(1), (1, None, None, None))
self.assertEqual(ac_tester.posonly_opt_kwonly_opt(1, 2), (1, 2, None, None))
with self.assertRaises(TypeError):
ac_tester.posonly_opt_kwonly_opt(1, 2, 3)
with self.assertRaises(TypeError):
ac_tester.posonly_opt_kwonly_opt(1, b=2)
self.assertEqual(ac_tester.posonly_opt_kwonly_opt(1, 2, c=3), (1, 2, 3, None))
self.assertEqual(ac_tester.posonly_opt_kwonly_opt(1, 2, c=3, d=4), (1, 2, 3, 4))
def test_posonly_keywords_kwonly_opt(self):
with self.assertRaises(TypeError):
ac_tester.posonly_keywords_kwonly_opt(1)
with self.assertRaises(TypeError):
ac_tester.posonly_keywords_kwonly_opt(1, 2)
with self.assertRaises(TypeError):
ac_tester.posonly_keywords_kwonly_opt(1, b=2)
with self.assertRaises(TypeError):
ac_tester.posonly_keywords_kwonly_opt(1, 2, 3)
with self.assertRaises(TypeError):
ac_tester.posonly_keywords_kwonly_opt(a=1, b=2, c=3)
self.assertEqual(ac_tester.posonly_keywords_kwonly_opt(1, 2, c=3), (1, 2, 3, None, None))
self.assertEqual(ac_tester.posonly_keywords_kwonly_opt(1, b=2, c=3), (1, 2, 3, None, None))
self.assertEqual(ac_tester.posonly_keywords_kwonly_opt(1, 2, c=3, d=4), (1, 2, 3, 4, None))
self.assertEqual(ac_tester.posonly_keywords_kwonly_opt(1, 2, c=3, d=4, e=5), (1, 2, 3, 4, 5))
def test_posonly_keywords_opt_kwonly_opt(self):
with self.assertRaises(TypeError):
ac_tester.posonly_keywords_opt_kwonly_opt(1)
self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, 2), (1, 2, None, None, None))
self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, b=2), (1, 2, None, None, None))
with self.assertRaises(TypeError):
ac_tester.posonly_keywords_opt_kwonly_opt(1, 2, 3, 4)
with self.assertRaises(TypeError):
ac_tester.posonly_keywords_opt_kwonly_opt(a=1, b=2)
self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, 2, c=3), (1, 2, 3, None, None))
self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, b=2, c=3), (1, 2, 3, None, None))
self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, 2, 3, d=4), (1, 2, 3, 4, None))
self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, 2, c=3, d=4), (1, 2, 3, 4, None))
self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, 2, 3, d=4, e=5), (1, 2, 3, 4, 5))
self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, 2, c=3, d=4, e=5), (1, 2, 3, 4, 5))
def test_posonly_opt_keywords_opt_kwonly_opt(self):
self.assertEqual(ac_tester.posonly_opt_keywords_opt_kwonly_opt(1), (1, None, None, None))
self.assertEqual(ac_tester.posonly_opt_keywords_opt_kwonly_opt(1, 2), (1, 2, None, None))
with self.assertRaises(TypeError):
ac_tester.posonly_opt_keywords_opt_kwonly_opt(1, b=2)
self.assertEqual(ac_tester.posonly_opt_keywords_opt_kwonly_opt(1, 2, 3), (1, 2, 3, None))
self.assertEqual(ac_tester.posonly_opt_keywords_opt_kwonly_opt(1, 2, c=3), (1, 2, 3, None))
self.assertEqual(ac_tester.posonly_opt_keywords_opt_kwonly_opt(1, 2, 3, d=4), (1, 2, 3, 4))
self.assertEqual(ac_tester.posonly_opt_keywords_opt_kwonly_opt(1, 2, c=3, d=4), (1, 2, 3, 4))
with self.assertRaises(TypeError):
ac_tester.posonly_opt_keywords_opt_kwonly_opt(1, 2, 3, 4)
def test_keyword_only_parameter(self):
with self.assertRaises(TypeError):
ac_tester.keyword_only_parameter()
with self.assertRaises(TypeError):
ac_tester.keyword_only_parameter(1)
self.assertEqual(ac_tester.keyword_only_parameter(a=1), (1,))
def test_posonly_vararg(self):
with self.assertRaises(TypeError):
ac_tester.posonly_vararg()
self.assertEqual(ac_tester.posonly_vararg(1, 2), (1, 2, ()))
self.assertEqual(ac_tester.posonly_vararg(1, b=2), (1, 2, ()))
self.assertEqual(ac_tester.posonly_vararg(1, 2, 3, 4), (1, 2, (3, 4)))
def test_vararg_and_posonly(self):
with self.assertRaises(TypeError):
ac_tester.vararg_and_posonly()
with self.assertRaises(TypeError):
ac_tester.vararg_and_posonly(1, b=2)
self.assertEqual(ac_tester.vararg_and_posonly(1, 2, 3, 4), (1, (2, 3, 4)))
def test_vararg(self):
with self.assertRaises(TypeError):
ac_tester.vararg()
with self.assertRaises(TypeError):
ac_tester.vararg(1, b=2)
self.assertEqual(ac_tester.vararg(1, 2, 3, 4), (1, (2, 3, 4)))
def test_vararg_with_default(self):
with self.assertRaises(TypeError):
ac_tester.vararg_with_default()
self.assertEqual(ac_tester.vararg_with_default(1, b=False), (1, (), False))
self.assertEqual(ac_tester.vararg_with_default(1, 2, 3, 4), (1, (2, 3, 4), False))
self.assertEqual(ac_tester.vararg_with_default(1, 2, 3, 4, b=True), (1, (2, 3, 4), True))
def test_vararg_with_only_defaults(self):
self.assertEqual(ac_tester.vararg_with_only_defaults(), ((), None))
self.assertEqual(ac_tester.vararg_with_only_defaults(b=2), ((), 2))
self.assertEqual(ac_tester.vararg_with_only_defaults(1, b=2), ((1, ), 2))
self.assertEqual(ac_tester.vararg_with_only_defaults(1, 2, 3, 4), ((1, 2, 3, 4), None))
self.assertEqual(ac_tester.vararg_with_only_defaults(1, 2, 3, 4, b=5), ((1, 2, 3, 4), 5))
def test_gh_32092_oob(self):
ac_tester.gh_32092_oob(1, 2, 3, 4, kw1=5, kw2=6)
def test_gh_32092_kw_pass(self):
ac_tester.gh_32092_kw_pass(1, 2, 3)
def test_gh_99233_refcount(self):
arg = '*A unique string is not referenced by anywhere else.*'
arg_refcount_origin = sys.getrefcount(arg)
ac_tester.gh_99233_refcount(arg)
arg_refcount_after = sys.getrefcount(arg)
self.assertEqual(arg_refcount_origin, arg_refcount_after)
def test_gh_99240_double_free(self):
expected_error = r'gh_99240_double_free\(\) argument 2 must be encoded string without null bytes, not str'
with self.assertRaisesRegex(TypeError, expected_error):
ac_tester.gh_99240_double_free('a', '\0b')
def test_cloned_func_exception_message(self):
incorrect_arg = -1 # f1() and f2() accept a single str
with self.assertRaisesRegex(TypeError, "clone_f1"):
ac_tester.clone_f1(incorrect_arg)
with self.assertRaisesRegex(TypeError, "clone_f2"):
ac_tester.clone_f2(incorrect_arg)
def test_cloned_func_with_converter_exception_message(self):
for name in "clone_with_conv_f1", "clone_with_conv_f2":
with self.subTest(name=name):
func = getattr(ac_tester, name)
self.assertEqual(func(), name)
if __name__ == "__main__":
unittest.main()
|