55from pprint import pprint
66
77SUBS = [
8- ('from .._backends.auto import AutoBackend' , 'from .._backends.sync import SyncBackend' ),
9- ('import trio as concurrency' , 'from tests import concurrency' ),
10- ('AsyncIterator' , 'Iterator' ),
11- ('Async([A-Z][A-Za-z0-9_]*)' , r'\2' ),
12- ('async def' , 'def' ),
13- ('async with' , 'with' ),
14- ('async for' , 'for' ),
15- ('await ' , '' ),
16- ('handle_async_request' , 'handle_request' ),
17- ('aclose' , 'close' ),
18- ('aiter_stream' , 'iter_stream' ),
19- ('aread' , 'read' ),
20- ('asynccontextmanager' , 'contextmanager' ),
21- ('__aenter__' , '__enter__' ),
22- ('__aexit__' , '__exit__' ),
23- ('__aiter__' , '__iter__' ),
24- ('@pytest.mark.anyio' , '' ),
25- ('@pytest.mark.trio' , '' ),
26- ('AutoBackend' , 'SyncBackend' ),
8+ (
9+ "from .._backends.auto import AutoBackend" ,
10+ "from .._backends.sync import SyncBackend" ,
11+ ),
12+ ("import trio as concurrency" , "from tests import concurrency" ),
13+ ("AsyncIterator" , "Iterator" ),
14+ ("Async([A-Z][A-Za-z0-9_]*)" , r"\2" ),
15+ ("async def" , "def" ),
16+ ("async with" , "with" ),
17+ ("async for" , "for" ),
18+ ("await " , "" ),
19+ ("handle_async_request" , "handle_request" ),
20+ ("aclose" , "close" ),
21+ ("aiter_stream" , "iter_stream" ),
22+ ("aread" , "read" ),
23+ ("asynccontextmanager" , "contextmanager" ),
24+ ("__aenter__" , "__enter__" ),
25+ ("__aexit__" , "__exit__" ),
26+ ("__aiter__" , "__iter__" ),
27+ ("@pytest.mark.anyio" , "" ),
28+ ("@pytest.mark.trio" , "" ),
29+ ("AutoBackend" , "SyncBackend" ),
2730]
2831COMPILED_SUBS = [
29- (re .compile (r'(^|\b)' + regex + r'($|\b)' ), repl )
30- for regex , repl in SUBS
32+ (re .compile (r"(^|\b)" + regex + r"($|\b)" ), repl ) for regex , repl in SUBS
3133]
3234
3335USED_SUBS = set ()
3436
37+
3538def unasync_line (line ):
3639 for index , (regex , repl ) in enumerate (COMPILED_SUBS ):
3740 old_line = line
@@ -55,30 +58,30 @@ def unasync_file_check(in_path, out_path):
5558 for in_line , out_line in zip (in_file .readlines (), out_file .readlines ()):
5659 expected = unasync_line (in_line )
5760 if out_line != expected :
58- print (f' unasync mismatch between { in_path !r} and { out_path !r} ' )
59- print (f' Async code: { in_line !r} ' )
60- print (f' Expected sync code: { expected !r} ' )
61- print (f' Actual sync code: { out_line !r} ' )
61+ print (f" unasync mismatch between { in_path !r} and { out_path !r} " )
62+ print (f" Async code: { in_line !r} " )
63+ print (f" Expected sync code: { expected !r} " )
64+ print (f" Actual sync code: { out_line !r} " )
6265 sys .exit (1 )
6366
6467
6568def unasync_dir (in_dir , out_dir , check_only = False ):
6669 for dirpath , dirnames , filenames in os .walk (in_dir ):
6770 for filename in filenames :
68- if not filename .endswith (' .py' ):
71+ if not filename .endswith (" .py" ):
6972 continue
7073 rel_dir = os .path .relpath (dirpath , in_dir )
7174 in_path = os .path .normpath (os .path .join (in_dir , rel_dir , filename ))
7275 out_path = os .path .normpath (os .path .join (out_dir , rel_dir , filename ))
73- print (in_path , '->' , out_path )
76+ print (in_path , "->" , out_path )
7477 if check_only :
7578 unasync_file_check (in_path , out_path )
7679 else :
7780 unasync_file (in_path , out_path )
7881
7982
8083def main ():
81- check_only = ' --check' in sys .argv
84+ check_only = " --check" in sys .argv
8285 unasync_dir ("httpcore/_async" , "httpcore/_sync" , check_only = check_only )
8386 unasync_dir ("tests/_async" , "tests/_sync" , check_only = check_only )
8487
@@ -87,8 +90,8 @@ def main():
8790
8891 print ("These patterns were not used:" )
8992 pprint (unused_subs )
90- exit (1 )
91-
93+ exit (1 )
94+
9295
93- if __name__ == ' __main__' :
96+ if __name__ == " __main__" :
9497 main ()
0 commit comments