17
17
test_tools .skip_if_missing ('clinic' )
18
18
with test_tools .imports_under_tool ('clinic' ):
19
19
import libclinic
20
- from libclinic .converters import int_converter , str_converter
20
+ from libclinic import ClinicError , unspecified , NULL , fail
21
+ from libclinic .converters import int_converter , str_converter , self_converter
21
22
from libclinic .function import (
23
+ Module , Class , Function , FunctionKind , Parameter ,
22
24
permute_optional_groups , permute_right_option_groups ,
23
25
permute_left_option_groups )
24
26
import clinic
25
- from clinic import DSLParser
27
+ from libclinic .clanguage import CLanguage
28
+ from libclinic .converter import converters , legacy_converters
29
+ from libclinic .return_converters import return_converters , int_return_converter
30
+ from libclinic .block_parser import Block , BlockParser
31
+ from libclinic .codegen import BlockPrinter , Destination
32
+ from libclinic .dsl_parser import DSLParser
33
+ from libclinic .cli import parse_file , Clinic
26
34
27
35
28
36
def _make_clinic (* , filename = 'clinic_tests' , limited_capi = False ):
29
- clang = clinic . CLanguage (filename )
30
- c = clinic . Clinic (clang , filename = filename , limited_capi = limited_capi )
31
- c .block_parser = clinic . BlockParser ('' , clang )
37
+ clang = CLanguage (filename )
38
+ c = Clinic (clang , filename = filename , limited_capi = limited_capi )
39
+ c .block_parser = BlockParser ('' , clang )
32
40
return c
33
41
34
42
@@ -47,7 +55,7 @@ def _expect_failure(tc, parser, code, errmsg, *, filename=None, lineno=None,
47
55
if strip :
48
56
code = code .strip ()
49
57
errmsg = re .escape (errmsg )
50
- with tc .assertRaisesRegex (clinic . ClinicError , errmsg ) as cm :
58
+ with tc .assertRaisesRegex (ClinicError , errmsg ) as cm :
51
59
parser (code )
52
60
if filename is not None :
53
61
tc .assertEqual (cm .exception .filename , filename )
@@ -62,12 +70,12 @@ def restore_dict(converters, old_converters):
62
70
63
71
64
72
def save_restore_converters (testcase ):
65
- testcase .addCleanup (restore_dict , clinic . converters ,
66
- clinic . converters .copy ())
67
- testcase .addCleanup (restore_dict , clinic . legacy_converters ,
68
- clinic . legacy_converters .copy ())
69
- testcase .addCleanup (restore_dict , clinic . return_converters ,
70
- clinic . return_converters .copy ())
73
+ testcase .addCleanup (restore_dict , converters ,
74
+ converters .copy ())
75
+ testcase .addCleanup (restore_dict , legacy_converters ,
76
+ legacy_converters .copy ())
77
+ testcase .addCleanup (restore_dict , return_converters ,
78
+ return_converters .copy ())
71
79
72
80
73
81
class ClinicWholeFileTest (TestCase ):
@@ -140,11 +148,11 @@ def test_whitespace_before_stop_line(self):
140
148
self .expect_failure (raw , err , filename = "test.c" , lineno = 2 )
141
149
142
150
def test_parse_with_body_prefix (self ):
143
- clang = clinic . CLanguage (None )
151
+ clang = CLanguage (None )
144
152
clang .body_prefix = "//"
145
153
clang .start_line = "//[{dsl_name} start]"
146
154
clang .stop_line = "//[{dsl_name} stop]"
147
- cl = clinic . Clinic (clang , filename = "test.c" , limited_capi = False )
155
+ cl = Clinic (clang , filename = "test.c" , limited_capi = False )
148
156
raw = dedent ("""
149
157
//[clinic start]
150
158
//module test
@@ -660,8 +668,8 @@ def expect_parsing_failure(
660
668
self , * , filename , expected_error , verify = True , output = None
661
669
):
662
670
errmsg = re .escape (dedent (expected_error ).strip ())
663
- with self .assertRaisesRegex (clinic . ClinicError , errmsg ):
664
- clinic . parse_file (filename , limited_capi = False )
671
+ with self .assertRaisesRegex (ClinicError , errmsg ):
672
+ parse_file (filename , limited_capi = False )
665
673
666
674
def test_parse_file_no_extension (self ) -> None :
667
675
self .expect_parsing_failure (
@@ -782,13 +790,13 @@ def test_multiline_substitution(self):
782
790
783
791
def test_text_before_block_marker (self ):
784
792
regex = re .escape ("found before '{marker}'" )
785
- with self .assertRaisesRegex (clinic . ClinicError , regex ):
793
+ with self .assertRaisesRegex (ClinicError , regex ):
786
794
libclinic .linear_format ("no text before marker for you! {marker}" ,
787
795
marker = "not allowed!" )
788
796
789
797
def test_text_after_block_marker (self ):
790
798
regex = re .escape ("found after '{marker}'" )
791
- with self .assertRaisesRegex (clinic . ClinicError , regex ):
799
+ with self .assertRaisesRegex (ClinicError , regex ):
792
800
libclinic .linear_format ("{marker} no text after marker for you!" ,
793
801
marker = "not allowed!" )
794
802
@@ -810,10 +818,10 @@ def parse(self, block):
810
818
811
819
class ClinicBlockParserTest (TestCase ):
812
820
def _test (self , input , output ):
813
- language = clinic . CLanguage (None )
821
+ language = CLanguage (None )
814
822
815
- blocks = list (clinic . BlockParser (input , language ))
816
- writer = clinic . BlockPrinter (language )
823
+ blocks = list (BlockParser (input , language ))
824
+ writer = BlockPrinter (language )
817
825
c = _make_clinic ()
818
826
for block in blocks :
819
827
writer .print_block (block , limited_capi = c .limited_capi , header_includes = c .includes )
@@ -841,8 +849,8 @@ def test_round_trip_2(self):
841
849
""" )
842
850
843
851
def _test_clinic (self , input , output ):
844
- language = clinic . CLanguage (None )
845
- c = clinic . Clinic (language , filename = "file" , limited_capi = False )
852
+ language = CLanguage (None )
853
+ c = Clinic (language , filename = "file" , limited_capi = False )
846
854
c .parsers ['inert' ] = InertParser (c )
847
855
c .parsers ['copy' ] = CopyParser (c )
848
856
computed = c .parse (input )
@@ -875,16 +883,16 @@ class ClinicParserTest(TestCase):
875
883
def parse (self , text ):
876
884
c = _make_clinic ()
877
885
parser = DSLParser (c )
878
- block = clinic . Block (text )
886
+ block = Block (text )
879
887
parser .parse (block )
880
888
return block
881
889
882
890
def parse_function (self , text , signatures_in_block = 2 , function_index = 1 ):
883
891
block = self .parse (text )
884
892
s = block .signatures
885
893
self .assertEqual (len (s ), signatures_in_block )
886
- assert isinstance (s [0 ], clinic . Module )
887
- assert isinstance (s [function_index ], clinic . Function )
894
+ assert isinstance (s [0 ], Module )
895
+ assert isinstance (s [function_index ], Function )
888
896
return s [function_index ]
889
897
890
898
def expect_failure (self , block , err , * ,
@@ -899,7 +907,7 @@ def checkDocstring(self, fn, expected):
899
907
900
908
def test_trivial (self ):
901
909
parser = DSLParser (_make_clinic ())
902
- block = clinic . Block ("""
910
+ block = Block ("""
903
911
module os
904
912
os.access
905
913
""" )
@@ -1188,7 +1196,7 @@ def test_base_invalid_syntax(self):
1188
1196
Function 'stat' has an invalid parameter declaration:
1189
1197
\s+'invalid syntax: int = 42'
1190
1198
""" ).strip ()
1191
- with self .assertRaisesRegex (clinic . ClinicError , err ):
1199
+ with self .assertRaisesRegex (ClinicError , err ):
1192
1200
self .parse_function (block )
1193
1201
1194
1202
def test_param_default_invalid_syntax (self ):
@@ -1220,7 +1228,7 @@ def test_return_converter(self):
1220
1228
module os
1221
1229
os.stat -> int
1222
1230
""" )
1223
- self .assertIsInstance (function .return_converter , clinic . int_return_converter )
1231
+ self .assertIsInstance (function .return_converter , int_return_converter )
1224
1232
1225
1233
def test_return_converter_invalid_syntax (self ):
1226
1234
block = """
@@ -2036,7 +2044,7 @@ def test_directive(self):
2036
2044
parser = DSLParser (_make_clinic ())
2037
2045
parser .flag = False
2038
2046
parser .directives ['setflag' ] = lambda : setattr (parser , 'flag' , True )
2039
- block = clinic . Block ("setflag" )
2047
+ block = Block ("setflag" )
2040
2048
parser .parse (block )
2041
2049
self .assertTrue (parser .flag )
2042
2050
@@ -2301,14 +2309,14 @@ def test_unused_param(self):
2301
2309
2302
2310
def test_scaffolding (self ):
2303
2311
# test repr on special values
2304
- self .assertEqual (repr (clinic . unspecified ), '<Unspecified>' )
2305
- self .assertEqual (repr (clinic . NULL ), '<Null>' )
2312
+ self .assertEqual (repr (unspecified ), '<Unspecified>' )
2313
+ self .assertEqual (repr (NULL ), '<Null>' )
2306
2314
2307
2315
# test that fail fails
2308
2316
with support .captured_stdout () as stdout :
2309
2317
errmsg = 'The igloos are melting'
2310
- with self .assertRaisesRegex (clinic . ClinicError , errmsg ) as cm :
2311
- clinic . fail (errmsg , filename = 'clown.txt' , line_number = 69 )
2318
+ with self .assertRaisesRegex (ClinicError , errmsg ) as cm :
2319
+ fail (errmsg , filename = 'clown.txt' , line_number = 69 )
2312
2320
exc = cm .exception
2313
2321
self .assertEqual (exc .filename , 'clown.txt' )
2314
2322
self .assertEqual (exc .lineno , 69 )
@@ -3998,15 +4006,15 @@ def test_suffix_all_lines(self):
3998
4006
3999
4007
class ClinicReprTests (unittest .TestCase ):
4000
4008
def test_Block_repr (self ):
4001
- block = clinic . Block ("foo" )
4009
+ block = Block ("foo" )
4002
4010
expected_repr = "<clinic.Block 'text' input='foo' output=None>"
4003
4011
self .assertEqual (repr (block ), expected_repr )
4004
4012
4005
- block2 = clinic . Block ("bar" , "baz" , [], "eggs" , "spam" )
4013
+ block2 = Block ("bar" , "baz" , [], "eggs" , "spam" )
4006
4014
expected_repr_2 = "<clinic.Block 'baz' input='bar' output='eggs'>"
4007
4015
self .assertEqual (repr (block2 ), expected_repr_2 )
4008
4016
4009
- block3 = clinic . Block (
4017
+ block3 = Block (
4010
4018
input = "longboi_" * 100 ,
4011
4019
dsl_name = "wow_so_long" ,
4012
4020
signatures = [],
@@ -4021,47 +4029,47 @@ def test_Block_repr(self):
4021
4029
def test_Destination_repr (self ):
4022
4030
c = _make_clinic ()
4023
4031
4024
- destination = clinic . Destination (
4032
+ destination = Destination (
4025
4033
"foo" , type = "file" , clinic = c , args = ("eggs" ,)
4026
4034
)
4027
4035
self .assertEqual (
4028
4036
repr (destination ), "<clinic.Destination 'foo' type='file' file='eggs'>"
4029
4037
)
4030
4038
4031
- destination2 = clinic . Destination ("bar" , type = "buffer" , clinic = c )
4039
+ destination2 = Destination ("bar" , type = "buffer" , clinic = c )
4032
4040
self .assertEqual (repr (destination2 ), "<clinic.Destination 'bar' type='buffer'>" )
4033
4041
4034
4042
def test_Module_repr (self ):
4035
- module = clinic . Module ("foo" , _make_clinic ())
4043
+ module = Module ("foo" , _make_clinic ())
4036
4044
self .assertRegex (repr (module ), r"<clinic.Module 'foo' at \d+>" )
4037
4045
4038
4046
def test_Class_repr (self ):
4039
- cls = clinic . Class ("foo" , _make_clinic (), None , 'some_typedef' , 'some_type_object' )
4047
+ cls = Class ("foo" , _make_clinic (), None , 'some_typedef' , 'some_type_object' )
4040
4048
self .assertRegex (repr (cls ), r"<clinic.Class 'foo' at \d+>" )
4041
4049
4042
4050
def test_FunctionKind_repr (self ):
4043
4051
self .assertEqual (
4044
- repr (clinic . FunctionKind .INVALID ), "<clinic.FunctionKind.INVALID>"
4052
+ repr (FunctionKind .INVALID ), "<clinic.FunctionKind.INVALID>"
4045
4053
)
4046
4054
self .assertEqual (
4047
- repr (clinic . FunctionKind .CLASS_METHOD ), "<clinic.FunctionKind.CLASS_METHOD>"
4055
+ repr (FunctionKind .CLASS_METHOD ), "<clinic.FunctionKind.CLASS_METHOD>"
4048
4056
)
4049
4057
4050
4058
def test_Function_and_Parameter_reprs (self ):
4051
- function = clinic . Function (
4059
+ function = Function (
4052
4060
name = 'foo' ,
4053
4061
module = _make_clinic (),
4054
4062
cls = None ,
4055
4063
c_basename = None ,
4056
4064
full_name = 'foofoo' ,
4057
- return_converter = clinic . int_return_converter (),
4058
- kind = clinic . FunctionKind .METHOD_INIT ,
4065
+ return_converter = int_return_converter (),
4066
+ kind = FunctionKind .METHOD_INIT ,
4059
4067
coexist = False
4060
4068
)
4061
4069
self .assertEqual (repr (function ), "<clinic.Function 'foo'>" )
4062
4070
4063
- converter = clinic . self_converter ('bar' , 'bar' , function )
4064
- parameter = clinic . Parameter (
4071
+ converter = self_converter ('bar' , 'bar' , function )
4072
+ parameter = Parameter (
4065
4073
"bar" ,
4066
4074
kind = inspect .Parameter .POSITIONAL_OR_KEYWORD ,
4067
4075
function = function ,
0 commit comments