ZTWHHH commited on
Commit
732d54f
·
verified ·
1 Parent(s): c01fda1

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +2 -0
  2. evalkit_llava/lib/libncursesw.so.6.4 +3 -0
  3. evalkit_llava/lib/libquadmath.so.0.0.0 +3 -0
  4. evalkit_llava/lib/python3.10/asyncio/__main__.py +127 -0
  5. evalkit_llava/lib/python3.10/asyncio/__pycache__/__init__.cpython-310.pyc +0 -0
  6. evalkit_llava/lib/python3.10/asyncio/__pycache__/__main__.cpython-310.pyc +0 -0
  7. evalkit_llava/lib/python3.10/asyncio/__pycache__/base_events.cpython-310.pyc +0 -0
  8. evalkit_llava/lib/python3.10/asyncio/__pycache__/base_futures.cpython-310.pyc +0 -0
  9. evalkit_llava/lib/python3.10/asyncio/__pycache__/base_subprocess.cpython-310.pyc +0 -0
  10. evalkit_llava/lib/python3.10/asyncio/__pycache__/base_tasks.cpython-310.pyc +0 -0
  11. evalkit_llava/lib/python3.10/asyncio/__pycache__/constants.cpython-310.pyc +0 -0
  12. evalkit_llava/lib/python3.10/asyncio/__pycache__/coroutines.cpython-310.pyc +0 -0
  13. evalkit_llava/lib/python3.10/asyncio/__pycache__/events.cpython-310.pyc +0 -0
  14. evalkit_llava/lib/python3.10/asyncio/__pycache__/exceptions.cpython-310.pyc +0 -0
  15. evalkit_llava/lib/python3.10/asyncio/__pycache__/format_helpers.cpython-310.pyc +0 -0
  16. evalkit_llava/lib/python3.10/asyncio/__pycache__/futures.cpython-310.pyc +0 -0
  17. evalkit_llava/lib/python3.10/asyncio/__pycache__/locks.cpython-310.pyc +0 -0
  18. evalkit_llava/lib/python3.10/asyncio/__pycache__/log.cpython-310.pyc +0 -0
  19. evalkit_llava/lib/python3.10/asyncio/__pycache__/mixins.cpython-310.pyc +0 -0
  20. evalkit_llava/lib/python3.10/asyncio/__pycache__/proactor_events.cpython-310.pyc +0 -0
  21. evalkit_llava/lib/python3.10/asyncio/__pycache__/protocols.cpython-310.pyc +0 -0
  22. evalkit_llava/lib/python3.10/asyncio/__pycache__/queues.cpython-310.pyc +0 -0
  23. evalkit_llava/lib/python3.10/asyncio/__pycache__/runners.cpython-310.pyc +0 -0
  24. evalkit_llava/lib/python3.10/asyncio/__pycache__/selector_events.cpython-310.pyc +0 -0
  25. evalkit_llava/lib/python3.10/asyncio/__pycache__/sslproto.cpython-310.pyc +0 -0
  26. evalkit_llava/lib/python3.10/asyncio/__pycache__/staggered.cpython-310.pyc +0 -0
  27. evalkit_llava/lib/python3.10/asyncio/__pycache__/streams.cpython-310.pyc +0 -0
  28. evalkit_llava/lib/python3.10/asyncio/__pycache__/subprocess.cpython-310.pyc +0 -0
  29. evalkit_llava/lib/python3.10/asyncio/__pycache__/tasks.cpython-310.pyc +0 -0
  30. evalkit_llava/lib/python3.10/asyncio/__pycache__/threads.cpython-310.pyc +0 -0
  31. evalkit_llava/lib/python3.10/asyncio/__pycache__/transports.cpython-310.pyc +0 -0
  32. evalkit_llava/lib/python3.10/asyncio/__pycache__/trsock.cpython-310.pyc +0 -0
  33. evalkit_llava/lib/python3.10/asyncio/__pycache__/unix_events.cpython-310.pyc +0 -0
  34. evalkit_llava/lib/python3.10/asyncio/__pycache__/windows_events.cpython-310.pyc +0 -0
  35. evalkit_llava/lib/python3.10/asyncio/__pycache__/windows_utils.cpython-310.pyc +0 -0
  36. evalkit_llava/lib/python3.10/asyncio/base_events.py +1934 -0
  37. evalkit_llava/lib/python3.10/asyncio/base_futures.py +80 -0
  38. evalkit_llava/lib/python3.10/asyncio/base_subprocess.py +285 -0
  39. evalkit_llava/lib/python3.10/asyncio/constants.py +27 -0
  40. evalkit_llava/lib/python3.10/asyncio/coroutines.py +269 -0
  41. evalkit_llava/lib/python3.10/asyncio/events.py +819 -0
  42. evalkit_llava/lib/python3.10/asyncio/futures.py +426 -0
  43. evalkit_llava/lib/python3.10/asyncio/locks.py +438 -0
  44. evalkit_llava/lib/python3.10/asyncio/log.py +7 -0
  45. evalkit_llava/lib/python3.10/asyncio/proactor_events.py +875 -0
  46. evalkit_llava/lib/python3.10/asyncio/queues.py +245 -0
  47. evalkit_llava/lib/python3.10/asyncio/sslproto.py +739 -0
  48. evalkit_llava/lib/python3.10/asyncio/staggered.py +149 -0
  49. evalkit_llava/lib/python3.10/asyncio/streams.py +726 -0
  50. evalkit_llava/lib/python3.10/asyncio/subprocess.py +223 -0
.gitattributes CHANGED
@@ -105,3 +105,5 @@ evalkit_llava/lib/liblzma.so.5 filter=lfs diff=lfs merge=lfs -text
105
  evalkit_llava/bin/bzcat filter=lfs diff=lfs merge=lfs -text
106
  evalkit_llava/lib/libbz2.so filter=lfs diff=lfs merge=lfs -text
107
  evalkit_llava/lib/liblzma.so.5.6.4 filter=lfs diff=lfs merge=lfs -text
 
 
 
105
  evalkit_llava/bin/bzcat filter=lfs diff=lfs merge=lfs -text
106
  evalkit_llava/lib/libbz2.so filter=lfs diff=lfs merge=lfs -text
107
  evalkit_llava/lib/liblzma.so.5.6.4 filter=lfs diff=lfs merge=lfs -text
108
+ evalkit_llava/lib/libncursesw.so.6.4 filter=lfs diff=lfs merge=lfs -text
109
+ evalkit_llava/lib/libquadmath.so.0.0.0 filter=lfs diff=lfs merge=lfs -text
evalkit_llava/lib/libncursesw.so.6.4 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7fa4e5e93804d78660b0eef727cdb4211209e1742e4ad3669348022668d90962
3
+ size 271304
evalkit_llava/lib/libquadmath.so.0.0.0 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:10c6fadba4c2f6d77e836a50aadbd92e95b137a85eb01b1ca183b50d8f39a2c6
3
+ size 1009408
evalkit_llava/lib/python3.10/asyncio/__main__.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ast
2
+ import asyncio
3
+ import code
4
+ import concurrent.futures
5
+ import inspect
6
+ import sys
7
+ import threading
8
+ import types
9
+ import warnings
10
+
11
+ from . import futures
12
+
13
+
14
+ class AsyncIOInteractiveConsole(code.InteractiveConsole):
15
+
16
+ def __init__(self, locals, loop):
17
+ super().__init__(locals)
18
+ self.compile.compiler.flags |= ast.PyCF_ALLOW_TOP_LEVEL_AWAIT
19
+
20
+ self.loop = loop
21
+
22
+ def runcode(self, code):
23
+ future = concurrent.futures.Future()
24
+
25
+ def callback():
26
+ global repl_future
27
+ global repl_future_interrupted
28
+
29
+ repl_future = None
30
+ repl_future_interrupted = False
31
+
32
+ func = types.FunctionType(code, self.locals)
33
+ try:
34
+ coro = func()
35
+ except SystemExit:
36
+ raise
37
+ except KeyboardInterrupt as ex:
38
+ repl_future_interrupted = True
39
+ future.set_exception(ex)
40
+ return
41
+ except BaseException as ex:
42
+ future.set_exception(ex)
43
+ return
44
+
45
+ if not inspect.iscoroutine(coro):
46
+ future.set_result(coro)
47
+ return
48
+
49
+ try:
50
+ repl_future = self.loop.create_task(coro)
51
+ futures._chain_future(repl_future, future)
52
+ except BaseException as exc:
53
+ future.set_exception(exc)
54
+
55
+ loop.call_soon_threadsafe(callback)
56
+
57
+ try:
58
+ return future.result()
59
+ except SystemExit:
60
+ raise
61
+ except BaseException:
62
+ if repl_future_interrupted:
63
+ self.write("\nKeyboardInterrupt\n")
64
+ else:
65
+ self.showtraceback()
66
+
67
+
68
+ class REPLThread(threading.Thread):
69
+
70
+ def run(self):
71
+ try:
72
+ banner = (
73
+ f'asyncio REPL {sys.version} on {sys.platform}\n'
74
+ f'Use "await" directly instead of "asyncio.run()".\n'
75
+ f'Type "help", "copyright", "credits" or "license" '
76
+ f'for more information.\n'
77
+ f'{getattr(sys, "ps1", ">>> ")}import asyncio'
78
+ )
79
+
80
+ console.interact(
81
+ banner=banner,
82
+ exitmsg='exiting asyncio REPL...')
83
+ finally:
84
+ warnings.filterwarnings(
85
+ 'ignore',
86
+ message=r'^coroutine .* was never awaited$',
87
+ category=RuntimeWarning)
88
+
89
+ loop.call_soon_threadsafe(loop.stop)
90
+
91
+
92
+ if __name__ == '__main__':
93
+ sys.audit("cpython.run_stdin")
94
+
95
+ loop = asyncio.new_event_loop()
96
+ asyncio.set_event_loop(loop)
97
+
98
+ repl_locals = {'asyncio': asyncio}
99
+ for key in {'__name__', '__package__',
100
+ '__loader__', '__spec__',
101
+ '__builtins__', '__file__'}:
102
+ repl_locals[key] = locals()[key]
103
+
104
+ console = AsyncIOInteractiveConsole(repl_locals, loop)
105
+
106
+ repl_future = None
107
+ repl_future_interrupted = False
108
+
109
+ try:
110
+ import readline # NoQA
111
+ except ImportError:
112
+ pass
113
+
114
+ repl_thread = REPLThread()
115
+ repl_thread.daemon = True
116
+ repl_thread.start()
117
+
118
+ while True:
119
+ try:
120
+ loop.run_forever()
121
+ except KeyboardInterrupt:
122
+ if repl_future and not repl_future.done():
123
+ repl_future.cancel()
124
+ repl_future_interrupted = True
125
+ continue
126
+ else:
127
+ break
evalkit_llava/lib/python3.10/asyncio/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (739 Bytes). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/__main__.cpython-310.pyc ADDED
Binary file (3.46 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/base_events.cpython-310.pyc ADDED
Binary file (52.1 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/base_futures.cpython-310.pyc ADDED
Binary file (2.17 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/base_subprocess.cpython-310.pyc ADDED
Binary file (9.38 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/base_tasks.cpython-310.pyc ADDED
Binary file (2.24 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/constants.cpython-310.pyc ADDED
Binary file (840 Bytes). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/coroutines.cpython-310.pyc ADDED
Binary file (6.62 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/events.cpython-310.pyc ADDED
Binary file (28.6 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/exceptions.cpython-310.pyc ADDED
Binary file (2.72 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/format_helpers.cpython-310.pyc ADDED
Binary file (2.59 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/futures.cpython-310.pyc ADDED
Binary file (12.1 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/locks.cpython-310.pyc ADDED
Binary file (14.4 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/log.cpython-310.pyc ADDED
Binary file (483 Bytes). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/mixins.cpython-310.pyc ADDED
Binary file (1.07 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/proactor_events.cpython-310.pyc ADDED
Binary file (24.7 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/protocols.cpython-310.pyc ADDED
Binary file (8.29 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/queues.cpython-310.pyc ADDED
Binary file (8.3 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/runners.cpython-310.pyc ADDED
Binary file (2.09 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/selector_events.cpython-310.pyc ADDED
Binary file (29.8 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/sslproto.cpython-310.pyc ADDED
Binary file (22.2 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/staggered.cpython-310.pyc ADDED
Binary file (4.44 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/streams.cpython-310.pyc ADDED
Binary file (20.4 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/subprocess.cpython-310.pyc ADDED
Binary file (7.32 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/tasks.cpython-310.pyc ADDED
Binary file (24.2 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/threads.cpython-310.pyc ADDED
Binary file (1.24 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/transports.cpython-310.pyc ADDED
Binary file (12.6 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/trsock.cpython-310.pyc ADDED
Binary file (7.84 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/unix_events.cpython-310.pyc ADDED
Binary file (41.9 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/windows_events.cpython-310.pyc ADDED
Binary file (24.4 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/__pycache__/windows_utils.cpython-310.pyc ADDED
Binary file (4.75 kB). View file
 
evalkit_llava/lib/python3.10/asyncio/base_events.py ADDED
@@ -0,0 +1,1934 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Base implementation of event loop.
2
+
3
+ The event loop can be broken up into a multiplexer (the part
4
+ responsible for notifying us of I/O events) and the event loop proper,
5
+ which wraps a multiplexer with functionality for scheduling callbacks,
6
+ immediately or at a given time in the future.
7
+
8
+ Whenever a public API takes a callback, subsequent positional
9
+ arguments will be passed to the callback if/when it is called. This
10
+ avoids the proliferation of trivial lambdas implementing closures.
11
+ Keyword arguments for the callback are not supported; this is a
12
+ conscious design decision, leaving the door open for keyword arguments
13
+ to modify the meaning of the API call itself.
14
+ """
15
+
16
+ import collections
17
+ import collections.abc
18
+ import concurrent.futures
19
+ import functools
20
+ import heapq
21
+ import itertools
22
+ import os
23
+ import socket
24
+ import stat
25
+ import subprocess
26
+ import threading
27
+ import time
28
+ import traceback
29
+ import sys
30
+ import warnings
31
+ import weakref
32
+
33
+ try:
34
+ import ssl
35
+ except ImportError: # pragma: no cover
36
+ ssl = None
37
+
38
+ from . import constants
39
+ from . import coroutines
40
+ from . import events
41
+ from . import exceptions
42
+ from . import futures
43
+ from . import protocols
44
+ from . import sslproto
45
+ from . import staggered
46
+ from . import tasks
47
+ from . import transports
48
+ from . import trsock
49
+ from .log import logger
50
+
51
+
52
+ __all__ = 'BaseEventLoop','Server',
53
+
54
+
55
+ # Minimum number of _scheduled timer handles before cleanup of
56
+ # cancelled handles is performed.
57
+ _MIN_SCHEDULED_TIMER_HANDLES = 100
58
+
59
+ # Minimum fraction of _scheduled timer handles that are cancelled
60
+ # before cleanup of cancelled handles is performed.
61
+ _MIN_CANCELLED_TIMER_HANDLES_FRACTION = 0.5
62
+
63
+
64
+ _HAS_IPv6 = hasattr(socket, 'AF_INET6')
65
+
66
+ # Maximum timeout passed to select to avoid OS limitations
67
+ MAXIMUM_SELECT_TIMEOUT = 24 * 3600
68
+
69
+ # Used for deprecation and removal of `loop.create_datagram_endpoint()`'s
70
+ # *reuse_address* parameter
71
+ _unset = object()
72
+
73
+
74
+ def _format_handle(handle):
75
+ cb = handle._callback
76
+ if isinstance(getattr(cb, '__self__', None), tasks.Task):
77
+ # format the task
78
+ return repr(cb.__self__)
79
+ else:
80
+ return str(handle)
81
+
82
+
83
+ def _format_pipe(fd):
84
+ if fd == subprocess.PIPE:
85
+ return '<pipe>'
86
+ elif fd == subprocess.STDOUT:
87
+ return '<stdout>'
88
+ else:
89
+ return repr(fd)
90
+
91
+
92
+ def _set_reuseport(sock):
93
+ if not hasattr(socket, 'SO_REUSEPORT'):
94
+ raise ValueError('reuse_port not supported by socket module')
95
+ else:
96
+ try:
97
+ sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
98
+ except OSError:
99
+ raise ValueError('reuse_port not supported by socket module, '
100
+ 'SO_REUSEPORT defined but not implemented.')
101
+
102
+
103
+ def _ipaddr_info(host, port, family, type, proto, flowinfo=0, scopeid=0):
104
+ # Try to skip getaddrinfo if "host" is already an IP. Users might have
105
+ # handled name resolution in their own code and pass in resolved IPs.
106
+ if not hasattr(socket, 'inet_pton'):
107
+ return
108
+
109
+ if proto not in {0, socket.IPPROTO_TCP, socket.IPPROTO_UDP} or \
110
+ host is None:
111
+ return None
112
+
113
+ if type == socket.SOCK_STREAM:
114
+ proto = socket.IPPROTO_TCP
115
+ elif type == socket.SOCK_DGRAM:
116
+ proto = socket.IPPROTO_UDP
117
+ else:
118
+ return None
119
+
120
+ if port is None:
121
+ port = 0
122
+ elif isinstance(port, bytes) and port == b'':
123
+ port = 0
124
+ elif isinstance(port, str) and port == '':
125
+ port = 0
126
+ else:
127
+ # If port's a service name like "http", don't skip getaddrinfo.
128
+ try:
129
+ port = int(port)
130
+ except (TypeError, ValueError):
131
+ return None
132
+
133
+ if family == socket.AF_UNSPEC:
134
+ afs = [socket.AF_INET]
135
+ if _HAS_IPv6:
136
+ afs.append(socket.AF_INET6)
137
+ else:
138
+ afs = [family]
139
+
140
+ if isinstance(host, bytes):
141
+ host = host.decode('idna')
142
+ if '%' in host:
143
+ # Linux's inet_pton doesn't accept an IPv6 zone index after host,
144
+ # like '::1%lo0'.
145
+ return None
146
+
147
+ for af in afs:
148
+ try:
149
+ socket.inet_pton(af, host)
150
+ # The host has already been resolved.
151
+ if _HAS_IPv6 and af == socket.AF_INET6:
152
+ return af, type, proto, '', (host, port, flowinfo, scopeid)
153
+ else:
154
+ return af, type, proto, '', (host, port)
155
+ except OSError:
156
+ pass
157
+
158
+ # "host" is not an IP address.
159
+ return None
160
+
161
+
162
+ def _interleave_addrinfos(addrinfos, first_address_family_count=1):
163
+ """Interleave list of addrinfo tuples by family."""
164
+ # Group addresses by family
165
+ addrinfos_by_family = collections.OrderedDict()
166
+ for addr in addrinfos:
167
+ family = addr[0]
168
+ if family not in addrinfos_by_family:
169
+ addrinfos_by_family[family] = []
170
+ addrinfos_by_family[family].append(addr)
171
+ addrinfos_lists = list(addrinfos_by_family.values())
172
+
173
+ reordered = []
174
+ if first_address_family_count > 1:
175
+ reordered.extend(addrinfos_lists[0][:first_address_family_count - 1])
176
+ del addrinfos_lists[0][:first_address_family_count - 1]
177
+ reordered.extend(
178
+ a for a in itertools.chain.from_iterable(
179
+ itertools.zip_longest(*addrinfos_lists)
180
+ ) if a is not None)
181
+ return reordered
182
+
183
+
184
+ def _run_until_complete_cb(fut):
185
+ if not fut.cancelled():
186
+ exc = fut.exception()
187
+ if isinstance(exc, (SystemExit, KeyboardInterrupt)):
188
+ # Issue #22429: run_forever() already finished, no need to
189
+ # stop it.
190
+ return
191
+ futures._get_loop(fut).stop()
192
+
193
+
194
+ if hasattr(socket, 'TCP_NODELAY'):
195
+ def _set_nodelay(sock):
196
+ if (sock.family in {socket.AF_INET, socket.AF_INET6} and
197
+ sock.type == socket.SOCK_STREAM and
198
+ sock.proto == socket.IPPROTO_TCP):
199
+ sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
200
+ else:
201
+ def _set_nodelay(sock):
202
+ pass
203
+
204
+
205
+ def _check_ssl_socket(sock):
206
+ if ssl is not None and isinstance(sock, ssl.SSLSocket):
207
+ raise TypeError("Socket cannot be of type SSLSocket")
208
+
209
+
210
+ class _SendfileFallbackProtocol(protocols.Protocol):
211
+ def __init__(self, transp):
212
+ if not isinstance(transp, transports._FlowControlMixin):
213
+ raise TypeError("transport should be _FlowControlMixin instance")
214
+ self._transport = transp
215
+ self._proto = transp.get_protocol()
216
+ self._should_resume_reading = transp.is_reading()
217
+ self._should_resume_writing = transp._protocol_paused
218
+ transp.pause_reading()
219
+ transp.set_protocol(self)
220
+ if self._should_resume_writing:
221
+ self._write_ready_fut = self._transport._loop.create_future()
222
+ else:
223
+ self._write_ready_fut = None
224
+
225
+ async def drain(self):
226
+ if self._transport.is_closing():
227
+ raise ConnectionError("Connection closed by peer")
228
+ fut = self._write_ready_fut
229
+ if fut is None:
230
+ return
231
+ await fut
232
+
233
+ def connection_made(self, transport):
234
+ raise RuntimeError("Invalid state: "
235
+ "connection should have been established already.")
236
+
237
+ def connection_lost(self, exc):
238
+ if self._write_ready_fut is not None:
239
+ # Never happens if peer disconnects after sending the whole content
240
+ # Thus disconnection is always an exception from user perspective
241
+ if exc is None:
242
+ self._write_ready_fut.set_exception(
243
+ ConnectionError("Connection is closed by peer"))
244
+ else:
245
+ self._write_ready_fut.set_exception(exc)
246
+ self._proto.connection_lost(exc)
247
+
248
+ def pause_writing(self):
249
+ if self._write_ready_fut is not None:
250
+ return
251
+ self._write_ready_fut = self._transport._loop.create_future()
252
+
253
+ def resume_writing(self):
254
+ if self._write_ready_fut is None:
255
+ return
256
+ self._write_ready_fut.set_result(False)
257
+ self._write_ready_fut = None
258
+
259
+ def data_received(self, data):
260
+ raise RuntimeError("Invalid state: reading should be paused")
261
+
262
+ def eof_received(self):
263
+ raise RuntimeError("Invalid state: reading should be paused")
264
+
265
+ async def restore(self):
266
+ self._transport.set_protocol(self._proto)
267
+ if self._should_resume_reading:
268
+ self._transport.resume_reading()
269
+ if self._write_ready_fut is not None:
270
+ # Cancel the future.
271
+ # Basically it has no effect because protocol is switched back,
272
+ # no code should wait for it anymore.
273
+ self._write_ready_fut.cancel()
274
+ if self._should_resume_writing:
275
+ self._proto.resume_writing()
276
+
277
+
278
+ class Server(events.AbstractServer):
279
+
280
+ def __init__(self, loop, sockets, protocol_factory, ssl_context, backlog,
281
+ ssl_handshake_timeout):
282
+ self._loop = loop
283
+ self._sockets = sockets
284
+ self._active_count = 0
285
+ self._waiters = []
286
+ self._protocol_factory = protocol_factory
287
+ self._backlog = backlog
288
+ self._ssl_context = ssl_context
289
+ self._ssl_handshake_timeout = ssl_handshake_timeout
290
+ self._serving = False
291
+ self._serving_forever_fut = None
292
+
293
+ def __repr__(self):
294
+ return f'<{self.__class__.__name__} sockets={self.sockets!r}>'
295
+
296
+ def _attach(self):
297
+ assert self._sockets is not None
298
+ self._active_count += 1
299
+
300
+ def _detach(self):
301
+ assert self._active_count > 0
302
+ self._active_count -= 1
303
+ if self._active_count == 0 and self._sockets is None:
304
+ self._wakeup()
305
+
306
+ def _wakeup(self):
307
+ waiters = self._waiters
308
+ self._waiters = None
309
+ for waiter in waiters:
310
+ if not waiter.done():
311
+ waiter.set_result(waiter)
312
+
313
+ def _start_serving(self):
314
+ if self._serving:
315
+ return
316
+ self._serving = True
317
+ for sock in self._sockets:
318
+ sock.listen(self._backlog)
319
+ self._loop._start_serving(
320
+ self._protocol_factory, sock, self._ssl_context,
321
+ self, self._backlog, self._ssl_handshake_timeout)
322
+
323
+ def get_loop(self):
324
+ return self._loop
325
+
326
+ def is_serving(self):
327
+ return self._serving
328
+
329
+ @property
330
+ def sockets(self):
331
+ if self._sockets is None:
332
+ return ()
333
+ return tuple(trsock.TransportSocket(s) for s in self._sockets)
334
+
335
+ def close(self):
336
+ sockets = self._sockets
337
+ if sockets is None:
338
+ return
339
+ self._sockets = None
340
+
341
+ for sock in sockets:
342
+ self._loop._stop_serving(sock)
343
+
344
+ self._serving = False
345
+
346
+ if (self._serving_forever_fut is not None and
347
+ not self._serving_forever_fut.done()):
348
+ self._serving_forever_fut.cancel()
349
+ self._serving_forever_fut = None
350
+
351
+ if self._active_count == 0:
352
+ self._wakeup()
353
+
354
+ async def start_serving(self):
355
+ self._start_serving()
356
+ # Skip one loop iteration so that all 'loop.add_reader'
357
+ # go through.
358
+ await tasks.sleep(0)
359
+
360
+ async def serve_forever(self):
361
+ if self._serving_forever_fut is not None:
362
+ raise RuntimeError(
363
+ f'server {self!r} is already being awaited on serve_forever()')
364
+ if self._sockets is None:
365
+ raise RuntimeError(f'server {self!r} is closed')
366
+
367
+ self._start_serving()
368
+ self._serving_forever_fut = self._loop.create_future()
369
+
370
+ try:
371
+ await self._serving_forever_fut
372
+ except exceptions.CancelledError:
373
+ try:
374
+ self.close()
375
+ await self.wait_closed()
376
+ finally:
377
+ raise
378
+ finally:
379
+ self._serving_forever_fut = None
380
+
381
+ async def wait_closed(self):
382
+ if self._sockets is None or self._waiters is None:
383
+ return
384
+ waiter = self._loop.create_future()
385
+ self._waiters.append(waiter)
386
+ await waiter
387
+
388
+
389
+ class BaseEventLoop(events.AbstractEventLoop):
390
+
391
+ def __init__(self):
392
+ self._timer_cancelled_count = 0
393
+ self._closed = False
394
+ self._stopping = False
395
+ self._ready = collections.deque()
396
+ self._scheduled = []
397
+ self._default_executor = None
398
+ self._internal_fds = 0
399
+ # Identifier of the thread running the event loop, or None if the
400
+ # event loop is not running
401
+ self._thread_id = None
402
+ self._clock_resolution = time.get_clock_info('monotonic').resolution
403
+ self._exception_handler = None
404
+ self.set_debug(coroutines._is_debug_mode())
405
+ # In debug mode, if the execution of a callback or a step of a task
406
+ # exceed this duration in seconds, the slow callback/task is logged.
407
+ self.slow_callback_duration = 0.1
408
+ self._current_handle = None
409
+ self._task_factory = None
410
+ self._coroutine_origin_tracking_enabled = False
411
+ self._coroutine_origin_tracking_saved_depth = None
412
+
413
+ # A weak set of all asynchronous generators that are
414
+ # being iterated by the loop.
415
+ self._asyncgens = weakref.WeakSet()
416
+ # Set to True when `loop.shutdown_asyncgens` is called.
417
+ self._asyncgens_shutdown_called = False
418
+ # Set to True when `loop.shutdown_default_executor` is called.
419
+ self._executor_shutdown_called = False
420
+
421
+ def __repr__(self):
422
+ return (
423
+ f'<{self.__class__.__name__} running={self.is_running()} '
424
+ f'closed={self.is_closed()} debug={self.get_debug()}>'
425
+ )
426
+
427
+ def create_future(self):
428
+ """Create a Future object attached to the loop."""
429
+ return futures.Future(loop=self)
430
+
431
+ def create_task(self, coro, *, name=None):
432
+ """Schedule a coroutine object.
433
+
434
+ Return a task object.
435
+ """
436
+ self._check_closed()
437
+ if self._task_factory is None:
438
+ task = tasks.Task(coro, loop=self, name=name)
439
+ if task._source_traceback:
440
+ del task._source_traceback[-1]
441
+ else:
442
+ task = self._task_factory(self, coro)
443
+ tasks._set_task_name(task, name)
444
+
445
+ return task
446
+
447
+ def set_task_factory(self, factory):
448
+ """Set a task factory that will be used by loop.create_task().
449
+
450
+ If factory is None the default task factory will be set.
451
+
452
+ If factory is a callable, it should have a signature matching
453
+ '(loop, coro)', where 'loop' will be a reference to the active
454
+ event loop, 'coro' will be a coroutine object. The callable
455
+ must return a Future.
456
+ """
457
+ if factory is not None and not callable(factory):
458
+ raise TypeError('task factory must be a callable or None')
459
+ self._task_factory = factory
460
+
461
+ def get_task_factory(self):
462
+ """Return a task factory, or None if the default one is in use."""
463
+ return self._task_factory
464
+
465
+ def _make_socket_transport(self, sock, protocol, waiter=None, *,
466
+ extra=None, server=None):
467
+ """Create socket transport."""
468
+ raise NotImplementedError
469
+
470
+ def _make_ssl_transport(
471
+ self, rawsock, protocol, sslcontext, waiter=None,
472
+ *, server_side=False, server_hostname=None,
473
+ extra=None, server=None,
474
+ ssl_handshake_timeout=None,
475
+ call_connection_made=True):
476
+ """Create SSL transport."""
477
+ raise NotImplementedError
478
+
479
+ def _make_datagram_transport(self, sock, protocol,
480
+ address=None, waiter=None, extra=None):
481
+ """Create datagram transport."""
482
+ raise NotImplementedError
483
+
484
+ def _make_read_pipe_transport(self, pipe, protocol, waiter=None,
485
+ extra=None):
486
+ """Create read pipe transport."""
487
+ raise NotImplementedError
488
+
489
+ def _make_write_pipe_transport(self, pipe, protocol, waiter=None,
490
+ extra=None):
491
+ """Create write pipe transport."""
492
+ raise NotImplementedError
493
+
494
+ async def _make_subprocess_transport(self, protocol, args, shell,
495
+ stdin, stdout, stderr, bufsize,
496
+ extra=None, **kwargs):
497
+ """Create subprocess transport."""
498
+ raise NotImplementedError
499
+
500
+ def _write_to_self(self):
501
+ """Write a byte to self-pipe, to wake up the event loop.
502
+
503
+ This may be called from a different thread.
504
+
505
+ The subclass is responsible for implementing the self-pipe.
506
+ """
507
+ raise NotImplementedError
508
+
509
+ def _process_events(self, event_list):
510
+ """Process selector events."""
511
+ raise NotImplementedError
512
+
513
+ def _check_closed(self):
514
+ if self._closed:
515
+ raise RuntimeError('Event loop is closed')
516
+
517
+ def _check_default_executor(self):
518
+ if self._executor_shutdown_called:
519
+ raise RuntimeError('Executor shutdown has been called')
520
+
521
+ def _asyncgen_finalizer_hook(self, agen):
522
+ self._asyncgens.discard(agen)
523
+ if not self.is_closed():
524
+ self.call_soon_threadsafe(self.create_task, agen.aclose())
525
+
526
+ def _asyncgen_firstiter_hook(self, agen):
527
+ if self._asyncgens_shutdown_called:
528
+ warnings.warn(
529
+ f"asynchronous generator {agen!r} was scheduled after "
530
+ f"loop.shutdown_asyncgens() call",
531
+ ResourceWarning, source=self)
532
+
533
+ self._asyncgens.add(agen)
534
+
535
+ async def shutdown_asyncgens(self):
536
+ """Shutdown all active asynchronous generators."""
537
+ self._asyncgens_shutdown_called = True
538
+
539
+ if not len(self._asyncgens):
540
+ # If Python version is <3.6 or we don't have any asynchronous
541
+ # generators alive.
542
+ return
543
+
544
+ closing_agens = list(self._asyncgens)
545
+ self._asyncgens.clear()
546
+
547
+ results = await tasks.gather(
548
+ *[ag.aclose() for ag in closing_agens],
549
+ return_exceptions=True)
550
+
551
+ for result, agen in zip(results, closing_agens):
552
+ if isinstance(result, Exception):
553
+ self.call_exception_handler({
554
+ 'message': f'an error occurred during closing of '
555
+ f'asynchronous generator {agen!r}',
556
+ 'exception': result,
557
+ 'asyncgen': agen
558
+ })
559
+
560
+ async def shutdown_default_executor(self):
561
+ """Schedule the shutdown of the default executor."""
562
+ self._executor_shutdown_called = True
563
+ if self._default_executor is None:
564
+ return
565
+ future = self.create_future()
566
+ thread = threading.Thread(target=self._do_shutdown, args=(future,))
567
+ thread.start()
568
+ try:
569
+ await future
570
+ finally:
571
+ thread.join()
572
+
573
+ def _do_shutdown(self, future):
574
+ try:
575
+ self._default_executor.shutdown(wait=True)
576
+ if not self.is_closed():
577
+ self.call_soon_threadsafe(future.set_result, None)
578
+ except Exception as ex:
579
+ if not self.is_closed():
580
+ self.call_soon_threadsafe(future.set_exception, ex)
581
+
582
+ def _check_running(self):
583
+ if self.is_running():
584
+ raise RuntimeError('This event loop is already running')
585
+ if events._get_running_loop() is not None:
586
+ raise RuntimeError(
587
+ 'Cannot run the event loop while another loop is running')
588
+
589
+ def run_forever(self):
590
+ """Run until stop() is called."""
591
+ self._check_closed()
592
+ self._check_running()
593
+ self._set_coroutine_origin_tracking(self._debug)
594
+
595
+ old_agen_hooks = sys.get_asyncgen_hooks()
596
+ try:
597
+ self._thread_id = threading.get_ident()
598
+ sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter_hook,
599
+ finalizer=self._asyncgen_finalizer_hook)
600
+
601
+ events._set_running_loop(self)
602
+ while True:
603
+ self._run_once()
604
+ if self._stopping:
605
+ break
606
+ finally:
607
+ self._stopping = False
608
+ self._thread_id = None
609
+ events._set_running_loop(None)
610
+ self._set_coroutine_origin_tracking(False)
611
+ sys.set_asyncgen_hooks(*old_agen_hooks)
612
+
613
+ def run_until_complete(self, future):
614
+ """Run until the Future is done.
615
+
616
+ If the argument is a coroutine, it is wrapped in a Task.
617
+
618
+ WARNING: It would be disastrous to call run_until_complete()
619
+ with the same coroutine twice -- it would wrap it in two
620
+ different Tasks and that can't be good.
621
+
622
+ Return the Future's result, or raise its exception.
623
+ """
624
+ self._check_closed()
625
+ self._check_running()
626
+
627
+ new_task = not futures.isfuture(future)
628
+ future = tasks.ensure_future(future, loop=self)
629
+ if new_task:
630
+ # An exception is raised if the future didn't complete, so there
631
+ # is no need to log the "destroy pending task" message
632
+ future._log_destroy_pending = False
633
+
634
+ future.add_done_callback(_run_until_complete_cb)
635
+ try:
636
+ self.run_forever()
637
+ except:
638
+ if new_task and future.done() and not future.cancelled():
639
+ # The coroutine raised a BaseException. Consume the exception
640
+ # to not log a warning, the caller doesn't have access to the
641
+ # local task.
642
+ future.exception()
643
+ raise
644
+ finally:
645
+ future.remove_done_callback(_run_until_complete_cb)
646
+ if not future.done():
647
+ raise RuntimeError('Event loop stopped before Future completed.')
648
+
649
+ return future.result()
650
+
651
+ def stop(self):
652
+ """Stop running the event loop.
653
+
654
+ Every callback already scheduled will still run. This simply informs
655
+ run_forever to stop looping after a complete iteration.
656
+ """
657
+ self._stopping = True
658
+
659
+ def close(self):
660
+ """Close the event loop.
661
+
662
+ This clears the queues and shuts down the executor,
663
+ but does not wait for the executor to finish.
664
+
665
+ The event loop must not be running.
666
+ """
667
+ if self.is_running():
668
+ raise RuntimeError("Cannot close a running event loop")
669
+ if self._closed:
670
+ return
671
+ if self._debug:
672
+ logger.debug("Close %r", self)
673
+ self._closed = True
674
+ self._ready.clear()
675
+ self._scheduled.clear()
676
+ self._executor_shutdown_called = True
677
+ executor = self._default_executor
678
+ if executor is not None:
679
+ self._default_executor = None
680
+ executor.shutdown(wait=False)
681
+
682
+ def is_closed(self):
683
+ """Returns True if the event loop was closed."""
684
+ return self._closed
685
+
686
+ def __del__(self, _warn=warnings.warn):
687
+ if not self.is_closed():
688
+ _warn(f"unclosed event loop {self!r}", ResourceWarning, source=self)
689
+ if not self.is_running():
690
+ self.close()
691
+
692
+ def is_running(self):
693
+ """Returns True if the event loop is running."""
694
+ return (self._thread_id is not None)
695
+
696
+ def time(self):
697
+ """Return the time according to the event loop's clock.
698
+
699
+ This is a float expressed in seconds since an epoch, but the
700
+ epoch, precision, accuracy and drift are unspecified and may
701
+ differ per event loop.
702
+ """
703
+ return time.monotonic()
704
+
705
+ def call_later(self, delay, callback, *args, context=None):
706
+ """Arrange for a callback to be called at a given time.
707
+
708
+ Return a Handle: an opaque object with a cancel() method that
709
+ can be used to cancel the call.
710
+
711
+ The delay can be an int or float, expressed in seconds. It is
712
+ always relative to the current time.
713
+
714
+ Each callback will be called exactly once. If two callbacks
715
+ are scheduled for exactly the same time, it undefined which
716
+ will be called first.
717
+
718
+ Any positional arguments after the callback will be passed to
719
+ the callback when it is called.
720
+ """
721
+ timer = self.call_at(self.time() + delay, callback, *args,
722
+ context=context)
723
+ if timer._source_traceback:
724
+ del timer._source_traceback[-1]
725
+ return timer
726
+
727
+ def call_at(self, when, callback, *args, context=None):
728
+ """Like call_later(), but uses an absolute time.
729
+
730
+ Absolute time corresponds to the event loop's time() method.
731
+ """
732
+ self._check_closed()
733
+ if self._debug:
734
+ self._check_thread()
735
+ self._check_callback(callback, 'call_at')
736
+ timer = events.TimerHandle(when, callback, args, self, context)
737
+ if timer._source_traceback:
738
+ del timer._source_traceback[-1]
739
+ heapq.heappush(self._scheduled, timer)
740
+ timer._scheduled = True
741
+ return timer
742
+
743
+ def call_soon(self, callback, *args, context=None):
744
+ """Arrange for a callback to be called as soon as possible.
745
+
746
+ This operates as a FIFO queue: callbacks are called in the
747
+ order in which they are registered. Each callback will be
748
+ called exactly once.
749
+
750
+ Any positional arguments after the callback will be passed to
751
+ the callback when it is called.
752
+ """
753
+ self._check_closed()
754
+ if self._debug:
755
+ self._check_thread()
756
+ self._check_callback(callback, 'call_soon')
757
+ handle = self._call_soon(callback, args, context)
758
+ if handle._source_traceback:
759
+ del handle._source_traceback[-1]
760
+ return handle
761
+
762
+ def _check_callback(self, callback, method):
763
+ if (coroutines.iscoroutine(callback) or
764
+ coroutines.iscoroutinefunction(callback)):
765
+ raise TypeError(
766
+ f"coroutines cannot be used with {method}()")
767
+ if not callable(callback):
768
+ raise TypeError(
769
+ f'a callable object was expected by {method}(), '
770
+ f'got {callback!r}')
771
+
772
+ def _call_soon(self, callback, args, context):
773
+ handle = events.Handle(callback, args, self, context)
774
+ if handle._source_traceback:
775
+ del handle._source_traceback[-1]
776
+ self._ready.append(handle)
777
+ return handle
778
+
779
+ def _check_thread(self):
780
+ """Check that the current thread is the thread running the event loop.
781
+
782
+ Non-thread-safe methods of this class make this assumption and will
783
+ likely behave incorrectly when the assumption is violated.
784
+
785
+ Should only be called when (self._debug == True). The caller is
786
+ responsible for checking this condition for performance reasons.
787
+ """
788
+ if self._thread_id is None:
789
+ return
790
+ thread_id = threading.get_ident()
791
+ if thread_id != self._thread_id:
792
+ raise RuntimeError(
793
+ "Non-thread-safe operation invoked on an event loop other "
794
+ "than the current one")
795
+
796
+ def call_soon_threadsafe(self, callback, *args, context=None):
797
+ """Like call_soon(), but thread-safe."""
798
+ self._check_closed()
799
+ if self._debug:
800
+ self._check_callback(callback, 'call_soon_threadsafe')
801
+ handle = self._call_soon(callback, args, context)
802
+ if handle._source_traceback:
803
+ del handle._source_traceback[-1]
804
+ self._write_to_self()
805
+ return handle
806
+
807
+ def run_in_executor(self, executor, func, *args):
808
+ self._check_closed()
809
+ if self._debug:
810
+ self._check_callback(func, 'run_in_executor')
811
+ if executor is None:
812
+ executor = self._default_executor
813
+ # Only check when the default executor is being used
814
+ self._check_default_executor()
815
+ if executor is None:
816
+ executor = concurrent.futures.ThreadPoolExecutor(
817
+ thread_name_prefix='asyncio'
818
+ )
819
+ self._default_executor = executor
820
+ return futures.wrap_future(
821
+ executor.submit(func, *args), loop=self)
822
+
823
+ def set_default_executor(self, executor):
824
+ if not isinstance(executor, concurrent.futures.ThreadPoolExecutor):
825
+ warnings.warn(
826
+ 'Using the default executor that is not an instance of '
827
+ 'ThreadPoolExecutor is deprecated and will be prohibited '
828
+ 'in Python 3.9',
829
+ DeprecationWarning, 2)
830
+ self._default_executor = executor
831
+
832
+ def _getaddrinfo_debug(self, host, port, family, type, proto, flags):
833
+ msg = [f"{host}:{port!r}"]
834
+ if family:
835
+ msg.append(f'family={family!r}')
836
+ if type:
837
+ msg.append(f'type={type!r}')
838
+ if proto:
839
+ msg.append(f'proto={proto!r}')
840
+ if flags:
841
+ msg.append(f'flags={flags!r}')
842
+ msg = ', '.join(msg)
843
+ logger.debug('Get address info %s', msg)
844
+
845
+ t0 = self.time()
846
+ addrinfo = socket.getaddrinfo(host, port, family, type, proto, flags)
847
+ dt = self.time() - t0
848
+
849
+ msg = f'Getting address info {msg} took {dt * 1e3:.3f}ms: {addrinfo!r}'
850
+ if dt >= self.slow_callback_duration:
851
+ logger.info(msg)
852
+ else:
853
+ logger.debug(msg)
854
+ return addrinfo
855
+
856
+ async def getaddrinfo(self, host, port, *,
857
+ family=0, type=0, proto=0, flags=0):
858
+ if self._debug:
859
+ getaddr_func = self._getaddrinfo_debug
860
+ else:
861
+ getaddr_func = socket.getaddrinfo
862
+
863
+ return await self.run_in_executor(
864
+ None, getaddr_func, host, port, family, type, proto, flags)
865
+
866
+ async def getnameinfo(self, sockaddr, flags=0):
867
+ return await self.run_in_executor(
868
+ None, socket.getnameinfo, sockaddr, flags)
869
+
870
+ async def sock_sendfile(self, sock, file, offset=0, count=None,
871
+ *, fallback=True):
872
+ if self._debug and sock.gettimeout() != 0:
873
+ raise ValueError("the socket must be non-blocking")
874
+ _check_ssl_socket(sock)
875
+ self._check_sendfile_params(sock, file, offset, count)
876
+ try:
877
+ return await self._sock_sendfile_native(sock, file,
878
+ offset, count)
879
+ except exceptions.SendfileNotAvailableError as exc:
880
+ if not fallback:
881
+ raise
882
+ return await self._sock_sendfile_fallback(sock, file,
883
+ offset, count)
884
+
885
+ async def _sock_sendfile_native(self, sock, file, offset, count):
886
+ # NB: sendfile syscall is not supported for SSL sockets and
887
+ # non-mmap files even if sendfile is supported by OS
888
+ raise exceptions.SendfileNotAvailableError(
889
+ f"syscall sendfile is not available for socket {sock!r} "
890
+ f"and file {file!r} combination")
891
+
892
+ async def _sock_sendfile_fallback(self, sock, file, offset, count):
893
+ if offset:
894
+ file.seek(offset)
895
+ blocksize = (
896
+ min(count, constants.SENDFILE_FALLBACK_READBUFFER_SIZE)
897
+ if count else constants.SENDFILE_FALLBACK_READBUFFER_SIZE
898
+ )
899
+ buf = bytearray(blocksize)
900
+ total_sent = 0
901
+ try:
902
+ while True:
903
+ if count:
904
+ blocksize = min(count - total_sent, blocksize)
905
+ if blocksize <= 0:
906
+ break
907
+ view = memoryview(buf)[:blocksize]
908
+ read = await self.run_in_executor(None, file.readinto, view)
909
+ if not read:
910
+ break # EOF
911
+ await self.sock_sendall(sock, view[:read])
912
+ total_sent += read
913
+ return total_sent
914
+ finally:
915
+ if total_sent > 0 and hasattr(file, 'seek'):
916
+ file.seek(offset + total_sent)
917
+
918
+ def _check_sendfile_params(self, sock, file, offset, count):
919
+ if 'b' not in getattr(file, 'mode', 'b'):
920
+ raise ValueError("file should be opened in binary mode")
921
+ if not sock.type == socket.SOCK_STREAM:
922
+ raise ValueError("only SOCK_STREAM type sockets are supported")
923
+ if count is not None:
924
+ if not isinstance(count, int):
925
+ raise TypeError(
926
+ "count must be a positive integer (got {!r})".format(count))
927
+ if count <= 0:
928
+ raise ValueError(
929
+ "count must be a positive integer (got {!r})".format(count))
930
+ if not isinstance(offset, int):
931
+ raise TypeError(
932
+ "offset must be a non-negative integer (got {!r})".format(
933
+ offset))
934
+ if offset < 0:
935
+ raise ValueError(
936
+ "offset must be a non-negative integer (got {!r})".format(
937
+ offset))
938
+
939
+ async def _connect_sock(self, exceptions, addr_info, local_addr_infos=None):
940
+ """Create, bind and connect one socket."""
941
+ my_exceptions = []
942
+ exceptions.append(my_exceptions)
943
+ family, type_, proto, _, address = addr_info
944
+ sock = None
945
+ try:
946
+ sock = socket.socket(family=family, type=type_, proto=proto)
947
+ sock.setblocking(False)
948
+ if local_addr_infos is not None:
949
+ for lfamily, _, _, _, laddr in local_addr_infos:
950
+ # skip local addresses of different family
951
+ if lfamily != family:
952
+ continue
953
+ try:
954
+ sock.bind(laddr)
955
+ break
956
+ except OSError as exc:
957
+ msg = (
958
+ f'error while attempting to bind on '
959
+ f'address {laddr!r}: '
960
+ f'{exc.strerror.lower()}'
961
+ )
962
+ exc = OSError(exc.errno, msg)
963
+ my_exceptions.append(exc)
964
+ else: # all bind attempts failed
965
+ if my_exceptions:
966
+ raise my_exceptions.pop()
967
+ else:
968
+ raise OSError(f"no matching local address with {family=} found")
969
+ await self.sock_connect(sock, address)
970
+ return sock
971
+ except OSError as exc:
972
+ my_exceptions.append(exc)
973
+ if sock is not None:
974
+ sock.close()
975
+ raise
976
+ except:
977
+ if sock is not None:
978
+ sock.close()
979
+ raise
980
+ finally:
981
+ exceptions = my_exceptions = None
982
+
983
+ async def create_connection(
984
+ self, protocol_factory, host=None, port=None,
985
+ *, ssl=None, family=0,
986
+ proto=0, flags=0, sock=None,
987
+ local_addr=None, server_hostname=None,
988
+ ssl_handshake_timeout=None,
989
+ happy_eyeballs_delay=None, interleave=None):
990
+ """Connect to a TCP server.
991
+
992
+ Create a streaming transport connection to a given internet host and
993
+ port: socket family AF_INET or socket.AF_INET6 depending on host (or
994
+ family if specified), socket type SOCK_STREAM. protocol_factory must be
995
+ a callable returning a protocol instance.
996
+
997
+ This method is a coroutine which will try to establish the connection
998
+ in the background. When successful, the coroutine returns a
999
+ (transport, protocol) pair.
1000
+ """
1001
+ if server_hostname is not None and not ssl:
1002
+ raise ValueError('server_hostname is only meaningful with ssl')
1003
+
1004
+ if server_hostname is None and ssl:
1005
+ # Use host as default for server_hostname. It is an error
1006
+ # if host is empty or not set, e.g. when an
1007
+ # already-connected socket was passed or when only a port
1008
+ # is given. To avoid this error, you can pass
1009
+ # server_hostname='' -- this will bypass the hostname
1010
+ # check. (This also means that if host is a numeric
1011
+ # IP/IPv6 address, we will attempt to verify that exact
1012
+ # address; this will probably fail, but it is possible to
1013
+ # create a certificate for a specific IP address, so we
1014
+ # don't judge it here.)
1015
+ if not host:
1016
+ raise ValueError('You must set server_hostname '
1017
+ 'when using ssl without a host')
1018
+ server_hostname = host
1019
+
1020
+ if ssl_handshake_timeout is not None and not ssl:
1021
+ raise ValueError(
1022
+ 'ssl_handshake_timeout is only meaningful with ssl')
1023
+
1024
+ if sock is not None:
1025
+ _check_ssl_socket(sock)
1026
+
1027
+ if happy_eyeballs_delay is not None and interleave is None:
1028
+ # If using happy eyeballs, default to interleave addresses by family
1029
+ interleave = 1
1030
+
1031
+ if host is not None or port is not None:
1032
+ if sock is not None:
1033
+ raise ValueError(
1034
+ 'host/port and sock can not be specified at the same time')
1035
+
1036
+ infos = await self._ensure_resolved(
1037
+ (host, port), family=family,
1038
+ type=socket.SOCK_STREAM, proto=proto, flags=flags, loop=self)
1039
+ if not infos:
1040
+ raise OSError('getaddrinfo() returned empty list')
1041
+
1042
+ if local_addr is not None:
1043
+ laddr_infos = await self._ensure_resolved(
1044
+ local_addr, family=family,
1045
+ type=socket.SOCK_STREAM, proto=proto,
1046
+ flags=flags, loop=self)
1047
+ if not laddr_infos:
1048
+ raise OSError('getaddrinfo() returned empty list')
1049
+ else:
1050
+ laddr_infos = None
1051
+
1052
+ if interleave:
1053
+ infos = _interleave_addrinfos(infos, interleave)
1054
+
1055
+ exceptions = []
1056
+ if happy_eyeballs_delay is None:
1057
+ # not using happy eyeballs
1058
+ for addrinfo in infos:
1059
+ try:
1060
+ sock = await self._connect_sock(
1061
+ exceptions, addrinfo, laddr_infos)
1062
+ break
1063
+ except OSError:
1064
+ continue
1065
+ else: # using happy eyeballs
1066
+ sock, _, _ = await staggered.staggered_race(
1067
+ (functools.partial(self._connect_sock,
1068
+ exceptions, addrinfo, laddr_infos)
1069
+ for addrinfo in infos),
1070
+ happy_eyeballs_delay, loop=self)
1071
+
1072
+ if sock is None:
1073
+ exceptions = [exc for sub in exceptions for exc in sub]
1074
+ try:
1075
+ if len(exceptions) == 1:
1076
+ raise exceptions[0]
1077
+ else:
1078
+ # If they all have the same str(), raise one.
1079
+ model = str(exceptions[0])
1080
+ if all(str(exc) == model for exc in exceptions):
1081
+ raise exceptions[0]
1082
+ # Raise a combined exception so the user can see all
1083
+ # the various error messages.
1084
+ raise OSError('Multiple exceptions: {}'.format(
1085
+ ', '.join(str(exc) for exc in exceptions)))
1086
+ finally:
1087
+ exceptions = None
1088
+
1089
+ else:
1090
+ if sock is None:
1091
+ raise ValueError(
1092
+ 'host and port was not specified and no sock specified')
1093
+ if sock.type != socket.SOCK_STREAM:
1094
+ # We allow AF_INET, AF_INET6, AF_UNIX as long as they
1095
+ # are SOCK_STREAM.
1096
+ # We support passing AF_UNIX sockets even though we have
1097
+ # a dedicated API for that: create_unix_connection.
1098
+ # Disallowing AF_UNIX in this method, breaks backwards
1099
+ # compatibility.
1100
+ raise ValueError(
1101
+ f'A Stream Socket was expected, got {sock!r}')
1102
+
1103
+ transport, protocol = await self._create_connection_transport(
1104
+ sock, protocol_factory, ssl, server_hostname,
1105
+ ssl_handshake_timeout=ssl_handshake_timeout)
1106
+ if self._debug:
1107
+ # Get the socket from the transport because SSL transport closes
1108
+ # the old socket and creates a new SSL socket
1109
+ sock = transport.get_extra_info('socket')
1110
+ logger.debug("%r connected to %s:%r: (%r, %r)",
1111
+ sock, host, port, transport, protocol)
1112
+ return transport, protocol
1113
+
1114
+ async def _create_connection_transport(
1115
+ self, sock, protocol_factory, ssl,
1116
+ server_hostname, server_side=False,
1117
+ ssl_handshake_timeout=None):
1118
+
1119
+ sock.setblocking(False)
1120
+
1121
+ protocol = protocol_factory()
1122
+ waiter = self.create_future()
1123
+ if ssl:
1124
+ sslcontext = None if isinstance(ssl, bool) else ssl
1125
+ transport = self._make_ssl_transport(
1126
+ sock, protocol, sslcontext, waiter,
1127
+ server_side=server_side, server_hostname=server_hostname,
1128
+ ssl_handshake_timeout=ssl_handshake_timeout)
1129
+ else:
1130
+ transport = self._make_socket_transport(sock, protocol, waiter)
1131
+
1132
+ try:
1133
+ await waiter
1134
+ except:
1135
+ transport.close()
1136
+ raise
1137
+
1138
+ return transport, protocol
1139
+
1140
+ async def sendfile(self, transport, file, offset=0, count=None,
1141
+ *, fallback=True):
1142
+ """Send a file to transport.
1143
+
1144
+ Return the total number of bytes which were sent.
1145
+
1146
+ The method uses high-performance os.sendfile if available.
1147
+
1148
+ file must be a regular file object opened in binary mode.
1149
+
1150
+ offset tells from where to start reading the file. If specified,
1151
+ count is the total number of bytes to transmit as opposed to
1152
+ sending the file until EOF is reached. File position is updated on
1153
+ return or also in case of error in which case file.tell()
1154
+ can be used to figure out the number of bytes
1155
+ which were sent.
1156
+
1157
+ fallback set to True makes asyncio to manually read and send
1158
+ the file when the platform does not support the sendfile syscall
1159
+ (e.g. Windows or SSL socket on Unix).
1160
+
1161
+ Raise SendfileNotAvailableError if the system does not support
1162
+ sendfile syscall and fallback is False.
1163
+ """
1164
+ if transport.is_closing():
1165
+ raise RuntimeError("Transport is closing")
1166
+ mode = getattr(transport, '_sendfile_compatible',
1167
+ constants._SendfileMode.UNSUPPORTED)
1168
+ if mode is constants._SendfileMode.UNSUPPORTED:
1169
+ raise RuntimeError(
1170
+ f"sendfile is not supported for transport {transport!r}")
1171
+ if mode is constants._SendfileMode.TRY_NATIVE:
1172
+ try:
1173
+ return await self._sendfile_native(transport, file,
1174
+ offset, count)
1175
+ except exceptions.SendfileNotAvailableError as exc:
1176
+ if not fallback:
1177
+ raise
1178
+
1179
+ if not fallback:
1180
+ raise RuntimeError(
1181
+ f"fallback is disabled and native sendfile is not "
1182
+ f"supported for transport {transport!r}")
1183
+
1184
+ return await self._sendfile_fallback(transport, file,
1185
+ offset, count)
1186
+
1187
+ async def _sendfile_native(self, transp, file, offset, count):
1188
+ raise exceptions.SendfileNotAvailableError(
1189
+ "sendfile syscall is not supported")
1190
+
1191
+ async def _sendfile_fallback(self, transp, file, offset, count):
1192
+ if offset:
1193
+ file.seek(offset)
1194
+ blocksize = min(count, 16384) if count else 16384
1195
+ buf = bytearray(blocksize)
1196
+ total_sent = 0
1197
+ proto = _SendfileFallbackProtocol(transp)
1198
+ try:
1199
+ while True:
1200
+ if count:
1201
+ blocksize = min(count - total_sent, blocksize)
1202
+ if blocksize <= 0:
1203
+ return total_sent
1204
+ view = memoryview(buf)[:blocksize]
1205
+ read = await self.run_in_executor(None, file.readinto, view)
1206
+ if not read:
1207
+ return total_sent # EOF
1208
+ await proto.drain()
1209
+ transp.write(view[:read])
1210
+ total_sent += read
1211
+ finally:
1212
+ if total_sent > 0 and hasattr(file, 'seek'):
1213
+ file.seek(offset + total_sent)
1214
+ await proto.restore()
1215
+
1216
+ async def start_tls(self, transport, protocol, sslcontext, *,
1217
+ server_side=False,
1218
+ server_hostname=None,
1219
+ ssl_handshake_timeout=None):
1220
+ """Upgrade transport to TLS.
1221
+
1222
+ Return a new transport that *protocol* should start using
1223
+ immediately.
1224
+ """
1225
+ if ssl is None:
1226
+ raise RuntimeError('Python ssl module is not available')
1227
+
1228
+ if not isinstance(sslcontext, ssl.SSLContext):
1229
+ raise TypeError(
1230
+ f'sslcontext is expected to be an instance of ssl.SSLContext, '
1231
+ f'got {sslcontext!r}')
1232
+
1233
+ if not getattr(transport, '_start_tls_compatible', False):
1234
+ raise TypeError(
1235
+ f'transport {transport!r} is not supported by start_tls()')
1236
+
1237
+ waiter = self.create_future()
1238
+ ssl_protocol = sslproto.SSLProtocol(
1239
+ self, protocol, sslcontext, waiter,
1240
+ server_side, server_hostname,
1241
+ ssl_handshake_timeout=ssl_handshake_timeout,
1242
+ call_connection_made=False)
1243
+
1244
+ # Pause early so that "ssl_protocol.data_received()" doesn't
1245
+ # have a chance to get called before "ssl_protocol.connection_made()".
1246
+ transport.pause_reading()
1247
+
1248
+ transport.set_protocol(ssl_protocol)
1249
+ conmade_cb = self.call_soon(ssl_protocol.connection_made, transport)
1250
+ resume_cb = self.call_soon(transport.resume_reading)
1251
+
1252
+ try:
1253
+ await waiter
1254
+ except BaseException:
1255
+ transport.close()
1256
+ conmade_cb.cancel()
1257
+ resume_cb.cancel()
1258
+ raise
1259
+
1260
+ return ssl_protocol._app_transport
1261
+
1262
+ async def create_datagram_endpoint(self, protocol_factory,
1263
+ local_addr=None, remote_addr=None, *,
1264
+ family=0, proto=0, flags=0,
1265
+ reuse_address=_unset, reuse_port=None,
1266
+ allow_broadcast=None, sock=None):
1267
+ """Create datagram connection."""
1268
+ if sock is not None:
1269
+ if sock.type != socket.SOCK_DGRAM:
1270
+ raise ValueError(
1271
+ f'A UDP Socket was expected, got {sock!r}')
1272
+ if (local_addr or remote_addr or
1273
+ family or proto or flags or
1274
+ reuse_port or allow_broadcast):
1275
+ # show the problematic kwargs in exception msg
1276
+ opts = dict(local_addr=local_addr, remote_addr=remote_addr,
1277
+ family=family, proto=proto, flags=flags,
1278
+ reuse_address=reuse_address, reuse_port=reuse_port,
1279
+ allow_broadcast=allow_broadcast)
1280
+ problems = ', '.join(f'{k}={v}' for k, v in opts.items() if v)
1281
+ raise ValueError(
1282
+ f'socket modifier keyword arguments can not be used '
1283
+ f'when sock is specified. ({problems})')
1284
+ sock.setblocking(False)
1285
+ r_addr = None
1286
+ else:
1287
+ if not (local_addr or remote_addr):
1288
+ if family == 0:
1289
+ raise ValueError('unexpected address family')
1290
+ addr_pairs_info = (((family, proto), (None, None)),)
1291
+ elif hasattr(socket, 'AF_UNIX') and family == socket.AF_UNIX:
1292
+ for addr in (local_addr, remote_addr):
1293
+ if addr is not None and not isinstance(addr, str):
1294
+ raise TypeError('string is expected')
1295
+
1296
+ if local_addr and local_addr[0] not in (0, '\x00'):
1297
+ try:
1298
+ if stat.S_ISSOCK(os.stat(local_addr).st_mode):
1299
+ os.remove(local_addr)
1300
+ except FileNotFoundError:
1301
+ pass
1302
+ except OSError as err:
1303
+ # Directory may have permissions only to create socket.
1304
+ logger.error('Unable to check or remove stale UNIX '
1305
+ 'socket %r: %r',
1306
+ local_addr, err)
1307
+
1308
+ addr_pairs_info = (((family, proto),
1309
+ (local_addr, remote_addr)), )
1310
+ else:
1311
+ # join address by (family, protocol)
1312
+ addr_infos = {} # Using order preserving dict
1313
+ for idx, addr in ((0, local_addr), (1, remote_addr)):
1314
+ if addr is not None:
1315
+ assert isinstance(addr, tuple) and len(addr) == 2, (
1316
+ '2-tuple is expected')
1317
+
1318
+ infos = await self._ensure_resolved(
1319
+ addr, family=family, type=socket.SOCK_DGRAM,
1320
+ proto=proto, flags=flags, loop=self)
1321
+ if not infos:
1322
+ raise OSError('getaddrinfo() returned empty list')
1323
+
1324
+ for fam, _, pro, _, address in infos:
1325
+ key = (fam, pro)
1326
+ if key not in addr_infos:
1327
+ addr_infos[key] = [None, None]
1328
+ addr_infos[key][idx] = address
1329
+
1330
+ # each addr has to have info for each (family, proto) pair
1331
+ addr_pairs_info = [
1332
+ (key, addr_pair) for key, addr_pair in addr_infos.items()
1333
+ if not ((local_addr and addr_pair[0] is None) or
1334
+ (remote_addr and addr_pair[1] is None))]
1335
+
1336
+ if not addr_pairs_info:
1337
+ raise ValueError('can not get address information')
1338
+
1339
+ exceptions = []
1340
+
1341
+ # bpo-37228
1342
+ if reuse_address is not _unset:
1343
+ if reuse_address:
1344
+ raise ValueError("Passing `reuse_address=True` is no "
1345
+ "longer supported, as the usage of "
1346
+ "SO_REUSEPORT in UDP poses a significant "
1347
+ "security concern.")
1348
+ else:
1349
+ warnings.warn("The *reuse_address* parameter has been "
1350
+ "deprecated as of 3.5.10 and is scheduled "
1351
+ "for removal in 3.11.", DeprecationWarning,
1352
+ stacklevel=2)
1353
+
1354
+ for ((family, proto),
1355
+ (local_address, remote_address)) in addr_pairs_info:
1356
+ sock = None
1357
+ r_addr = None
1358
+ try:
1359
+ sock = socket.socket(
1360
+ family=family, type=socket.SOCK_DGRAM, proto=proto)
1361
+ if reuse_port:
1362
+ _set_reuseport(sock)
1363
+ if allow_broadcast:
1364
+ sock.setsockopt(
1365
+ socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
1366
+ sock.setblocking(False)
1367
+
1368
+ if local_addr:
1369
+ sock.bind(local_address)
1370
+ if remote_addr:
1371
+ if not allow_broadcast:
1372
+ await self.sock_connect(sock, remote_address)
1373
+ r_addr = remote_address
1374
+ except OSError as exc:
1375
+ if sock is not None:
1376
+ sock.close()
1377
+ exceptions.append(exc)
1378
+ except:
1379
+ if sock is not None:
1380
+ sock.close()
1381
+ raise
1382
+ else:
1383
+ break
1384
+ else:
1385
+ raise exceptions[0]
1386
+
1387
+ protocol = protocol_factory()
1388
+ waiter = self.create_future()
1389
+ transport = self._make_datagram_transport(
1390
+ sock, protocol, r_addr, waiter)
1391
+ if self._debug:
1392
+ if local_addr:
1393
+ logger.info("Datagram endpoint local_addr=%r remote_addr=%r "
1394
+ "created: (%r, %r)",
1395
+ local_addr, remote_addr, transport, protocol)
1396
+ else:
1397
+ logger.debug("Datagram endpoint remote_addr=%r created: "
1398
+ "(%r, %r)",
1399
+ remote_addr, transport, protocol)
1400
+
1401
+ try:
1402
+ await waiter
1403
+ except:
1404
+ transport.close()
1405
+ raise
1406
+
1407
+ return transport, protocol
1408
+
1409
+ async def _ensure_resolved(self, address, *,
1410
+ family=0, type=socket.SOCK_STREAM,
1411
+ proto=0, flags=0, loop):
1412
+ host, port = address[:2]
1413
+ info = _ipaddr_info(host, port, family, type, proto, *address[2:])
1414
+ if info is not None:
1415
+ # "host" is already a resolved IP.
1416
+ return [info]
1417
+ else:
1418
+ return await loop.getaddrinfo(host, port, family=family, type=type,
1419
+ proto=proto, flags=flags)
1420
+
1421
+ async def _create_server_getaddrinfo(self, host, port, family, flags):
1422
+ infos = await self._ensure_resolved((host, port), family=family,
1423
+ type=socket.SOCK_STREAM,
1424
+ flags=flags, loop=self)
1425
+ if not infos:
1426
+ raise OSError(f'getaddrinfo({host!r}) returned empty list')
1427
+ return infos
1428
+
1429
+ async def create_server(
1430
+ self, protocol_factory, host=None, port=None,
1431
+ *,
1432
+ family=socket.AF_UNSPEC,
1433
+ flags=socket.AI_PASSIVE,
1434
+ sock=None,
1435
+ backlog=100,
1436
+ ssl=None,
1437
+ reuse_address=None,
1438
+ reuse_port=None,
1439
+ ssl_handshake_timeout=None,
1440
+ start_serving=True):
1441
+ """Create a TCP server.
1442
+
1443
+ The host parameter can be a string, in that case the TCP server is
1444
+ bound to host and port.
1445
+
1446
+ The host parameter can also be a sequence of strings and in that case
1447
+ the TCP server is bound to all hosts of the sequence. If a host
1448
+ appears multiple times (possibly indirectly e.g. when hostnames
1449
+ resolve to the same IP address), the server is only bound once to that
1450
+ host.
1451
+
1452
+ Return a Server object which can be used to stop the service.
1453
+
1454
+ This method is a coroutine.
1455
+ """
1456
+ if isinstance(ssl, bool):
1457
+ raise TypeError('ssl argument must be an SSLContext or None')
1458
+
1459
+ if ssl_handshake_timeout is not None and ssl is None:
1460
+ raise ValueError(
1461
+ 'ssl_handshake_timeout is only meaningful with ssl')
1462
+
1463
+ if sock is not None:
1464
+ _check_ssl_socket(sock)
1465
+
1466
+ if host is not None or port is not None:
1467
+ if sock is not None:
1468
+ raise ValueError(
1469
+ 'host/port and sock can not be specified at the same time')
1470
+
1471
+ if reuse_address is None:
1472
+ reuse_address = os.name == 'posix' and sys.platform != 'cygwin'
1473
+ sockets = []
1474
+ if host == '':
1475
+ hosts = [None]
1476
+ elif (isinstance(host, str) or
1477
+ not isinstance(host, collections.abc.Iterable)):
1478
+ hosts = [host]
1479
+ else:
1480
+ hosts = host
1481
+
1482
+ fs = [self._create_server_getaddrinfo(host, port, family=family,
1483
+ flags=flags)
1484
+ for host in hosts]
1485
+ infos = await tasks.gather(*fs)
1486
+ infos = set(itertools.chain.from_iterable(infos))
1487
+
1488
+ completed = False
1489
+ try:
1490
+ for res in infos:
1491
+ af, socktype, proto, canonname, sa = res
1492
+ try:
1493
+ sock = socket.socket(af, socktype, proto)
1494
+ except socket.error:
1495
+ # Assume it's a bad family/type/protocol combination.
1496
+ if self._debug:
1497
+ logger.warning('create_server() failed to create '
1498
+ 'socket.socket(%r, %r, %r)',
1499
+ af, socktype, proto, exc_info=True)
1500
+ continue
1501
+ sockets.append(sock)
1502
+ if reuse_address:
1503
+ sock.setsockopt(
1504
+ socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
1505
+ if reuse_port:
1506
+ _set_reuseport(sock)
1507
+ # Disable IPv4/IPv6 dual stack support (enabled by
1508
+ # default on Linux) which makes a single socket
1509
+ # listen on both address families.
1510
+ if (_HAS_IPv6 and
1511
+ af == socket.AF_INET6 and
1512
+ hasattr(socket, 'IPPROTO_IPV6')):
1513
+ sock.setsockopt(socket.IPPROTO_IPV6,
1514
+ socket.IPV6_V6ONLY,
1515
+ True)
1516
+ try:
1517
+ sock.bind(sa)
1518
+ except OSError as err:
1519
+ raise OSError(err.errno, 'error while attempting '
1520
+ 'to bind on address %r: %s'
1521
+ % (sa, err.strerror.lower())) from None
1522
+ completed = True
1523
+ finally:
1524
+ if not completed:
1525
+ for sock in sockets:
1526
+ sock.close()
1527
+ else:
1528
+ if sock is None:
1529
+ raise ValueError('Neither host/port nor sock were specified')
1530
+ if sock.type != socket.SOCK_STREAM:
1531
+ raise ValueError(f'A Stream Socket was expected, got {sock!r}')
1532
+ sockets = [sock]
1533
+
1534
+ for sock in sockets:
1535
+ sock.setblocking(False)
1536
+
1537
+ server = Server(self, sockets, protocol_factory,
1538
+ ssl, backlog, ssl_handshake_timeout)
1539
+ if start_serving:
1540
+ server._start_serving()
1541
+ # Skip one loop iteration so that all 'loop.add_reader'
1542
+ # go through.
1543
+ await tasks.sleep(0)
1544
+
1545
+ if self._debug:
1546
+ logger.info("%r is serving", server)
1547
+ return server
1548
+
1549
+ async def connect_accepted_socket(
1550
+ self, protocol_factory, sock,
1551
+ *, ssl=None,
1552
+ ssl_handshake_timeout=None):
1553
+ if sock.type != socket.SOCK_STREAM:
1554
+ raise ValueError(f'A Stream Socket was expected, got {sock!r}')
1555
+
1556
+ if ssl_handshake_timeout is not None and not ssl:
1557
+ raise ValueError(
1558
+ 'ssl_handshake_timeout is only meaningful with ssl')
1559
+
1560
+ if sock is not None:
1561
+ _check_ssl_socket(sock)
1562
+
1563
+ transport, protocol = await self._create_connection_transport(
1564
+ sock, protocol_factory, ssl, '', server_side=True,
1565
+ ssl_handshake_timeout=ssl_handshake_timeout)
1566
+ if self._debug:
1567
+ # Get the socket from the transport because SSL transport closes
1568
+ # the old socket and creates a new SSL socket
1569
+ sock = transport.get_extra_info('socket')
1570
+ logger.debug("%r handled: (%r, %r)", sock, transport, protocol)
1571
+ return transport, protocol
1572
+
1573
+ async def connect_read_pipe(self, protocol_factory, pipe):
1574
+ protocol = protocol_factory()
1575
+ waiter = self.create_future()
1576
+ transport = self._make_read_pipe_transport(pipe, protocol, waiter)
1577
+
1578
+ try:
1579
+ await waiter
1580
+ except:
1581
+ transport.close()
1582
+ raise
1583
+
1584
+ if self._debug:
1585
+ logger.debug('Read pipe %r connected: (%r, %r)',
1586
+ pipe.fileno(), transport, protocol)
1587
+ return transport, protocol
1588
+
1589
+ async def connect_write_pipe(self, protocol_factory, pipe):
1590
+ protocol = protocol_factory()
1591
+ waiter = self.create_future()
1592
+ transport = self._make_write_pipe_transport(pipe, protocol, waiter)
1593
+
1594
+ try:
1595
+ await waiter
1596
+ except:
1597
+ transport.close()
1598
+ raise
1599
+
1600
+ if self._debug:
1601
+ logger.debug('Write pipe %r connected: (%r, %r)',
1602
+ pipe.fileno(), transport, protocol)
1603
+ return transport, protocol
1604
+
1605
+ def _log_subprocess(self, msg, stdin, stdout, stderr):
1606
+ info = [msg]
1607
+ if stdin is not None:
1608
+ info.append(f'stdin={_format_pipe(stdin)}')
1609
+ if stdout is not None and stderr == subprocess.STDOUT:
1610
+ info.append(f'stdout=stderr={_format_pipe(stdout)}')
1611
+ else:
1612
+ if stdout is not None:
1613
+ info.append(f'stdout={_format_pipe(stdout)}')
1614
+ if stderr is not None:
1615
+ info.append(f'stderr={_format_pipe(stderr)}')
1616
+ logger.debug(' '.join(info))
1617
+
1618
+ async def subprocess_shell(self, protocol_factory, cmd, *,
1619
+ stdin=subprocess.PIPE,
1620
+ stdout=subprocess.PIPE,
1621
+ stderr=subprocess.PIPE,
1622
+ universal_newlines=False,
1623
+ shell=True, bufsize=0,
1624
+ encoding=None, errors=None, text=None,
1625
+ **kwargs):
1626
+ if not isinstance(cmd, (bytes, str)):
1627
+ raise ValueError("cmd must be a string")
1628
+ if universal_newlines:
1629
+ raise ValueError("universal_newlines must be False")
1630
+ if not shell:
1631
+ raise ValueError("shell must be True")
1632
+ if bufsize != 0:
1633
+ raise ValueError("bufsize must be 0")
1634
+ if text:
1635
+ raise ValueError("text must be False")
1636
+ if encoding is not None:
1637
+ raise ValueError("encoding must be None")
1638
+ if errors is not None:
1639
+ raise ValueError("errors must be None")
1640
+
1641
+ protocol = protocol_factory()
1642
+ debug_log = None
1643
+ if self._debug:
1644
+ # don't log parameters: they may contain sensitive information
1645
+ # (password) and may be too long
1646
+ debug_log = 'run shell command %r' % cmd
1647
+ self._log_subprocess(debug_log, stdin, stdout, stderr)
1648
+ transport = await self._make_subprocess_transport(
1649
+ protocol, cmd, True, stdin, stdout, stderr, bufsize, **kwargs)
1650
+ if self._debug and debug_log is not None:
1651
+ logger.info('%s: %r', debug_log, transport)
1652
+ return transport, protocol
1653
+
1654
+ async def subprocess_exec(self, protocol_factory, program, *args,
1655
+ stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1656
+ stderr=subprocess.PIPE, universal_newlines=False,
1657
+ shell=False, bufsize=0,
1658
+ encoding=None, errors=None, text=None,
1659
+ **kwargs):
1660
+ if universal_newlines:
1661
+ raise ValueError("universal_newlines must be False")
1662
+ if shell:
1663
+ raise ValueError("shell must be False")
1664
+ if bufsize != 0:
1665
+ raise ValueError("bufsize must be 0")
1666
+ if text:
1667
+ raise ValueError("text must be False")
1668
+ if encoding is not None:
1669
+ raise ValueError("encoding must be None")
1670
+ if errors is not None:
1671
+ raise ValueError("errors must be None")
1672
+
1673
+ popen_args = (program,) + args
1674
+ protocol = protocol_factory()
1675
+ debug_log = None
1676
+ if self._debug:
1677
+ # don't log parameters: they may contain sensitive information
1678
+ # (password) and may be too long
1679
+ debug_log = f'execute program {program!r}'
1680
+ self._log_subprocess(debug_log, stdin, stdout, stderr)
1681
+ transport = await self._make_subprocess_transport(
1682
+ protocol, popen_args, False, stdin, stdout, stderr,
1683
+ bufsize, **kwargs)
1684
+ if self._debug and debug_log is not None:
1685
+ logger.info('%s: %r', debug_log, transport)
1686
+ return transport, protocol
1687
+
1688
+ def get_exception_handler(self):
1689
+ """Return an exception handler, or None if the default one is in use.
1690
+ """
1691
+ return self._exception_handler
1692
+
1693
+ def set_exception_handler(self, handler):
1694
+ """Set handler as the new event loop exception handler.
1695
+
1696
+ If handler is None, the default exception handler will
1697
+ be set.
1698
+
1699
+ If handler is a callable object, it should have a
1700
+ signature matching '(loop, context)', where 'loop'
1701
+ will be a reference to the active event loop, 'context'
1702
+ will be a dict object (see `call_exception_handler()`
1703
+ documentation for details about context).
1704
+ """
1705
+ if handler is not None and not callable(handler):
1706
+ raise TypeError(f'A callable object or None is expected, '
1707
+ f'got {handler!r}')
1708
+ self._exception_handler = handler
1709
+
1710
+ def default_exception_handler(self, context):
1711
+ """Default exception handler.
1712
+
1713
+ This is called when an exception occurs and no exception
1714
+ handler is set, and can be called by a custom exception
1715
+ handler that wants to defer to the default behavior.
1716
+
1717
+ This default handler logs the error message and other
1718
+ context-dependent information. In debug mode, a truncated
1719
+ stack trace is also appended showing where the given object
1720
+ (e.g. a handle or future or task) was created, if any.
1721
+
1722
+ The context parameter has the same meaning as in
1723
+ `call_exception_handler()`.
1724
+ """
1725
+ message = context.get('message')
1726
+ if not message:
1727
+ message = 'Unhandled exception in event loop'
1728
+
1729
+ exception = context.get('exception')
1730
+ if exception is not None:
1731
+ exc_info = (type(exception), exception, exception.__traceback__)
1732
+ else:
1733
+ exc_info = False
1734
+
1735
+ if ('source_traceback' not in context and
1736
+ self._current_handle is not None and
1737
+ self._current_handle._source_traceback):
1738
+ context['handle_traceback'] = \
1739
+ self._current_handle._source_traceback
1740
+
1741
+ log_lines = [message]
1742
+ for key in sorted(context):
1743
+ if key in {'message', 'exception'}:
1744
+ continue
1745
+ value = context[key]
1746
+ if key == 'source_traceback':
1747
+ tb = ''.join(traceback.format_list(value))
1748
+ value = 'Object created at (most recent call last):\n'
1749
+ value += tb.rstrip()
1750
+ elif key == 'handle_traceback':
1751
+ tb = ''.join(traceback.format_list(value))
1752
+ value = 'Handle created at (most recent call last):\n'
1753
+ value += tb.rstrip()
1754
+ else:
1755
+ value = repr(value)
1756
+ log_lines.append(f'{key}: {value}')
1757
+
1758
+ logger.error('\n'.join(log_lines), exc_info=exc_info)
1759
+
1760
+ def call_exception_handler(self, context):
1761
+ """Call the current event loop's exception handler.
1762
+
1763
+ The context argument is a dict containing the following keys:
1764
+
1765
+ - 'message': Error message;
1766
+ - 'exception' (optional): Exception object;
1767
+ - 'future' (optional): Future instance;
1768
+ - 'task' (optional): Task instance;
1769
+ - 'handle' (optional): Handle instance;
1770
+ - 'protocol' (optional): Protocol instance;
1771
+ - 'transport' (optional): Transport instance;
1772
+ - 'socket' (optional): Socket instance;
1773
+ - 'asyncgen' (optional): Asynchronous generator that caused
1774
+ the exception.
1775
+
1776
+ New keys maybe introduced in the future.
1777
+
1778
+ Note: do not overload this method in an event loop subclass.
1779
+ For custom exception handling, use the
1780
+ `set_exception_handler()` method.
1781
+ """
1782
+ if self._exception_handler is None:
1783
+ try:
1784
+ self.default_exception_handler(context)
1785
+ except (SystemExit, KeyboardInterrupt):
1786
+ raise
1787
+ except BaseException:
1788
+ # Second protection layer for unexpected errors
1789
+ # in the default implementation, as well as for subclassed
1790
+ # event loops with overloaded "default_exception_handler".
1791
+ logger.error('Exception in default exception handler',
1792
+ exc_info=True)
1793
+ else:
1794
+ try:
1795
+ self._exception_handler(self, context)
1796
+ except (SystemExit, KeyboardInterrupt):
1797
+ raise
1798
+ except BaseException as exc:
1799
+ # Exception in the user set custom exception handler.
1800
+ try:
1801
+ # Let's try default handler.
1802
+ self.default_exception_handler({
1803
+ 'message': 'Unhandled error in exception handler',
1804
+ 'exception': exc,
1805
+ 'context': context,
1806
+ })
1807
+ except (SystemExit, KeyboardInterrupt):
1808
+ raise
1809
+ except BaseException:
1810
+ # Guard 'default_exception_handler' in case it is
1811
+ # overloaded.
1812
+ logger.error('Exception in default exception handler '
1813
+ 'while handling an unexpected error '
1814
+ 'in custom exception handler',
1815
+ exc_info=True)
1816
+
1817
+ def _add_callback(self, handle):
1818
+ """Add a Handle to _ready."""
1819
+ if not handle._cancelled:
1820
+ self._ready.append(handle)
1821
+
1822
+ def _add_callback_signalsafe(self, handle):
1823
+ """Like _add_callback() but called from a signal handler."""
1824
+ self._add_callback(handle)
1825
+ self._write_to_self()
1826
+
1827
+ def _timer_handle_cancelled(self, handle):
1828
+ """Notification that a TimerHandle has been cancelled."""
1829
+ if handle._scheduled:
1830
+ self._timer_cancelled_count += 1
1831
+
1832
+ def _run_once(self):
1833
+ """Run one full iteration of the event loop.
1834
+
1835
+ This calls all currently ready callbacks, polls for I/O,
1836
+ schedules the resulting callbacks, and finally schedules
1837
+ 'call_later' callbacks.
1838
+ """
1839
+
1840
+ sched_count = len(self._scheduled)
1841
+ if (sched_count > _MIN_SCHEDULED_TIMER_HANDLES and
1842
+ self._timer_cancelled_count / sched_count >
1843
+ _MIN_CANCELLED_TIMER_HANDLES_FRACTION):
1844
+ # Remove delayed calls that were cancelled if their number
1845
+ # is too high
1846
+ new_scheduled = []
1847
+ for handle in self._scheduled:
1848
+ if handle._cancelled:
1849
+ handle._scheduled = False
1850
+ else:
1851
+ new_scheduled.append(handle)
1852
+
1853
+ heapq.heapify(new_scheduled)
1854
+ self._scheduled = new_scheduled
1855
+ self._timer_cancelled_count = 0
1856
+ else:
1857
+ # Remove delayed calls that were cancelled from head of queue.
1858
+ while self._scheduled and self._scheduled[0]._cancelled:
1859
+ self._timer_cancelled_count -= 1
1860
+ handle = heapq.heappop(self._scheduled)
1861
+ handle._scheduled = False
1862
+
1863
+ timeout = None
1864
+ if self._ready or self._stopping:
1865
+ timeout = 0
1866
+ elif self._scheduled:
1867
+ # Compute the desired timeout.
1868
+ when = self._scheduled[0]._when
1869
+ timeout = min(max(0, when - self.time()), MAXIMUM_SELECT_TIMEOUT)
1870
+
1871
+ event_list = self._selector.select(timeout)
1872
+ self._process_events(event_list)
1873
+ # Needed to break cycles when an exception occurs.
1874
+ event_list = None
1875
+
1876
+ # Handle 'later' callbacks that are ready.
1877
+ end_time = self.time() + self._clock_resolution
1878
+ while self._scheduled:
1879
+ handle = self._scheduled[0]
1880
+ if handle._when >= end_time:
1881
+ break
1882
+ handle = heapq.heappop(self._scheduled)
1883
+ handle._scheduled = False
1884
+ self._ready.append(handle)
1885
+
1886
+ # This is the only place where callbacks are actually *called*.
1887
+ # All other places just add them to ready.
1888
+ # Note: We run all currently scheduled callbacks, but not any
1889
+ # callbacks scheduled by callbacks run this time around --
1890
+ # they will be run the next time (after another I/O poll).
1891
+ # Use an idiom that is thread-safe without using locks.
1892
+ ntodo = len(self._ready)
1893
+ for i in range(ntodo):
1894
+ handle = self._ready.popleft()
1895
+ if handle._cancelled:
1896
+ continue
1897
+ if self._debug:
1898
+ try:
1899
+ self._current_handle = handle
1900
+ t0 = self.time()
1901
+ handle._run()
1902
+ dt = self.time() - t0
1903
+ if dt >= self.slow_callback_duration:
1904
+ logger.warning('Executing %s took %.3f seconds',
1905
+ _format_handle(handle), dt)
1906
+ finally:
1907
+ self._current_handle = None
1908
+ else:
1909
+ handle._run()
1910
+ handle = None # Needed to break cycles when an exception occurs.
1911
+
1912
+ def _set_coroutine_origin_tracking(self, enabled):
1913
+ if bool(enabled) == bool(self._coroutine_origin_tracking_enabled):
1914
+ return
1915
+
1916
+ if enabled:
1917
+ self._coroutine_origin_tracking_saved_depth = (
1918
+ sys.get_coroutine_origin_tracking_depth())
1919
+ sys.set_coroutine_origin_tracking_depth(
1920
+ constants.DEBUG_STACK_DEPTH)
1921
+ else:
1922
+ sys.set_coroutine_origin_tracking_depth(
1923
+ self._coroutine_origin_tracking_saved_depth)
1924
+
1925
+ self._coroutine_origin_tracking_enabled = enabled
1926
+
1927
+ def get_debug(self):
1928
+ return self._debug
1929
+
1930
+ def set_debug(self, enabled):
1931
+ self._debug = enabled
1932
+
1933
+ if self.is_running():
1934
+ self.call_soon_threadsafe(self._set_coroutine_origin_tracking, enabled)
evalkit_llava/lib/python3.10/asyncio/base_futures.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ __all__ = ()
2
+
3
+ import reprlib
4
+ from _thread import get_ident
5
+
6
+ from . import format_helpers
7
+
8
+ # States for Future.
9
+ _PENDING = 'PENDING'
10
+ _CANCELLED = 'CANCELLED'
11
+ _FINISHED = 'FINISHED'
12
+
13
+
14
+ def isfuture(obj):
15
+ """Check for a Future.
16
+
17
+ This returns True when obj is a Future instance or is advertising
18
+ itself as duck-type compatible by setting _asyncio_future_blocking.
19
+ See comment in Future for more details.
20
+ """
21
+ return (hasattr(obj.__class__, '_asyncio_future_blocking') and
22
+ obj._asyncio_future_blocking is not None)
23
+
24
+
25
+ def _format_callbacks(cb):
26
+ """helper function for Future.__repr__"""
27
+ size = len(cb)
28
+ if not size:
29
+ cb = ''
30
+
31
+ def format_cb(callback):
32
+ return format_helpers._format_callback_source(callback, ())
33
+
34
+ if size == 1:
35
+ cb = format_cb(cb[0][0])
36
+ elif size == 2:
37
+ cb = '{}, {}'.format(format_cb(cb[0][0]), format_cb(cb[1][0]))
38
+ elif size > 2:
39
+ cb = '{}, <{} more>, {}'.format(format_cb(cb[0][0]),
40
+ size - 2,
41
+ format_cb(cb[-1][0]))
42
+ return f'cb=[{cb}]'
43
+
44
+
45
+ # bpo-42183: _repr_running is needed for repr protection
46
+ # when a Future or Task result contains itself directly or indirectly.
47
+ # The logic is borrowed from @reprlib.recursive_repr decorator.
48
+ # Unfortunately, the direct decorator usage is impossible because of
49
+ # AttributeError: '_asyncio.Task' object has no attribute '__module__' error.
50
+ #
51
+ # After fixing this thing we can return to the decorator based approach.
52
+ _repr_running = set()
53
+
54
+
55
+ def _future_repr_info(future):
56
+ # (Future) -> str
57
+ """helper function for Future.__repr__"""
58
+ info = [future._state.lower()]
59
+ if future._state == _FINISHED:
60
+ if future._exception is not None:
61
+ info.append(f'exception={future._exception!r}')
62
+ else:
63
+ key = id(future), get_ident()
64
+ if key in _repr_running:
65
+ result = '...'
66
+ else:
67
+ _repr_running.add(key)
68
+ try:
69
+ # use reprlib to limit the length of the output, especially
70
+ # for very long strings
71
+ result = reprlib.repr(future._result)
72
+ finally:
73
+ _repr_running.discard(key)
74
+ info.append(f'result={result}')
75
+ if future._callbacks:
76
+ info.append(_format_callbacks(future._callbacks))
77
+ if future._source_traceback:
78
+ frame = future._source_traceback[-1]
79
+ info.append(f'created at {frame[0]}:{frame[1]}')
80
+ return info
evalkit_llava/lib/python3.10/asyncio/base_subprocess.py ADDED
@@ -0,0 +1,285 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import collections
2
+ import subprocess
3
+ import warnings
4
+
5
+ from . import protocols
6
+ from . import transports
7
+ from .log import logger
8
+
9
+
10
+ class BaseSubprocessTransport(transports.SubprocessTransport):
11
+
12
+ def __init__(self, loop, protocol, args, shell,
13
+ stdin, stdout, stderr, bufsize,
14
+ waiter=None, extra=None, **kwargs):
15
+ super().__init__(extra)
16
+ self._closed = False
17
+ self._protocol = protocol
18
+ self._loop = loop
19
+ self._proc = None
20
+ self._pid = None
21
+ self._returncode = None
22
+ self._exit_waiters = []
23
+ self._pending_calls = collections.deque()
24
+ self._pipes = {}
25
+ self._finished = False
26
+
27
+ if stdin == subprocess.PIPE:
28
+ self._pipes[0] = None
29
+ if stdout == subprocess.PIPE:
30
+ self._pipes[1] = None
31
+ if stderr == subprocess.PIPE:
32
+ self._pipes[2] = None
33
+
34
+ # Create the child process: set the _proc attribute
35
+ try:
36
+ self._start(args=args, shell=shell, stdin=stdin, stdout=stdout,
37
+ stderr=stderr, bufsize=bufsize, **kwargs)
38
+ except:
39
+ self.close()
40
+ raise
41
+
42
+ self._pid = self._proc.pid
43
+ self._extra['subprocess'] = self._proc
44
+
45
+ if self._loop.get_debug():
46
+ if isinstance(args, (bytes, str)):
47
+ program = args
48
+ else:
49
+ program = args[0]
50
+ logger.debug('process %r created: pid %s',
51
+ program, self._pid)
52
+
53
+ self._loop.create_task(self._connect_pipes(waiter))
54
+
55
+ def __repr__(self):
56
+ info = [self.__class__.__name__]
57
+ if self._closed:
58
+ info.append('closed')
59
+ if self._pid is not None:
60
+ info.append(f'pid={self._pid}')
61
+ if self._returncode is not None:
62
+ info.append(f'returncode={self._returncode}')
63
+ elif self._pid is not None:
64
+ info.append('running')
65
+ else:
66
+ info.append('not started')
67
+
68
+ stdin = self._pipes.get(0)
69
+ if stdin is not None:
70
+ info.append(f'stdin={stdin.pipe}')
71
+
72
+ stdout = self._pipes.get(1)
73
+ stderr = self._pipes.get(2)
74
+ if stdout is not None and stderr is stdout:
75
+ info.append(f'stdout=stderr={stdout.pipe}')
76
+ else:
77
+ if stdout is not None:
78
+ info.append(f'stdout={stdout.pipe}')
79
+ if stderr is not None:
80
+ info.append(f'stderr={stderr.pipe}')
81
+
82
+ return '<{}>'.format(' '.join(info))
83
+
84
+ def _start(self, args, shell, stdin, stdout, stderr, bufsize, **kwargs):
85
+ raise NotImplementedError
86
+
87
+ def set_protocol(self, protocol):
88
+ self._protocol = protocol
89
+
90
+ def get_protocol(self):
91
+ return self._protocol
92
+
93
+ def is_closing(self):
94
+ return self._closed
95
+
96
+ def close(self):
97
+ if self._closed:
98
+ return
99
+ self._closed = True
100
+
101
+ for proto in self._pipes.values():
102
+ if proto is None:
103
+ continue
104
+ proto.pipe.close()
105
+
106
+ if (self._proc is not None and
107
+ # has the child process finished?
108
+ self._returncode is None and
109
+ # the child process has finished, but the
110
+ # transport hasn't been notified yet?
111
+ self._proc.poll() is None):
112
+
113
+ if self._loop.get_debug():
114
+ logger.warning('Close running child process: kill %r', self)
115
+
116
+ try:
117
+ self._proc.kill()
118
+ except ProcessLookupError:
119
+ pass
120
+
121
+ # Don't clear the _proc reference yet: _post_init() may still run
122
+
123
+ def __del__(self, _warn=warnings.warn):
124
+ if not self._closed:
125
+ _warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
126
+ self.close()
127
+
128
+ def get_pid(self):
129
+ return self._pid
130
+
131
+ def get_returncode(self):
132
+ return self._returncode
133
+
134
+ def get_pipe_transport(self, fd):
135
+ if fd in self._pipes:
136
+ return self._pipes[fd].pipe
137
+ else:
138
+ return None
139
+
140
+ def _check_proc(self):
141
+ if self._proc is None:
142
+ raise ProcessLookupError()
143
+
144
+ def send_signal(self, signal):
145
+ self._check_proc()
146
+ self._proc.send_signal(signal)
147
+
148
+ def terminate(self):
149
+ self._check_proc()
150
+ self._proc.terminate()
151
+
152
+ def kill(self):
153
+ self._check_proc()
154
+ self._proc.kill()
155
+
156
+ async def _connect_pipes(self, waiter):
157
+ try:
158
+ proc = self._proc
159
+ loop = self._loop
160
+
161
+ if proc.stdin is not None:
162
+ _, pipe = await loop.connect_write_pipe(
163
+ lambda: WriteSubprocessPipeProto(self, 0),
164
+ proc.stdin)
165
+ self._pipes[0] = pipe
166
+
167
+ if proc.stdout is not None:
168
+ _, pipe = await loop.connect_read_pipe(
169
+ lambda: ReadSubprocessPipeProto(self, 1),
170
+ proc.stdout)
171
+ self._pipes[1] = pipe
172
+
173
+ if proc.stderr is not None:
174
+ _, pipe = await loop.connect_read_pipe(
175
+ lambda: ReadSubprocessPipeProto(self, 2),
176
+ proc.stderr)
177
+ self._pipes[2] = pipe
178
+
179
+ assert self._pending_calls is not None
180
+
181
+ loop.call_soon(self._protocol.connection_made, self)
182
+ for callback, data in self._pending_calls:
183
+ loop.call_soon(callback, *data)
184
+ self._pending_calls = None
185
+ except (SystemExit, KeyboardInterrupt):
186
+ raise
187
+ except BaseException as exc:
188
+ if waiter is not None and not waiter.cancelled():
189
+ waiter.set_exception(exc)
190
+ else:
191
+ if waiter is not None and not waiter.cancelled():
192
+ waiter.set_result(None)
193
+
194
+ def _call(self, cb, *data):
195
+ if self._pending_calls is not None:
196
+ self._pending_calls.append((cb, data))
197
+ else:
198
+ self._loop.call_soon(cb, *data)
199
+
200
+ def _pipe_connection_lost(self, fd, exc):
201
+ self._call(self._protocol.pipe_connection_lost, fd, exc)
202
+ self._try_finish()
203
+
204
+ def _pipe_data_received(self, fd, data):
205
+ self._call(self._protocol.pipe_data_received, fd, data)
206
+
207
+ def _process_exited(self, returncode):
208
+ assert returncode is not None, returncode
209
+ assert self._returncode is None, self._returncode
210
+ if self._loop.get_debug():
211
+ logger.info('%r exited with return code %r', self, returncode)
212
+ self._returncode = returncode
213
+ if self._proc.returncode is None:
214
+ # asyncio uses a child watcher: copy the status into the Popen
215
+ # object. On Python 3.6, it is required to avoid a ResourceWarning.
216
+ self._proc.returncode = returncode
217
+ self._call(self._protocol.process_exited)
218
+ self._try_finish()
219
+
220
+ # wake up futures waiting for wait()
221
+ for waiter in self._exit_waiters:
222
+ if not waiter.cancelled():
223
+ waiter.set_result(returncode)
224
+ self._exit_waiters = None
225
+
226
+ async def _wait(self):
227
+ """Wait until the process exit and return the process return code.
228
+
229
+ This method is a coroutine."""
230
+ if self._returncode is not None:
231
+ return self._returncode
232
+
233
+ waiter = self._loop.create_future()
234
+ self._exit_waiters.append(waiter)
235
+ return await waiter
236
+
237
+ def _try_finish(self):
238
+ assert not self._finished
239
+ if self._returncode is None:
240
+ return
241
+ if all(p is not None and p.disconnected
242
+ for p in self._pipes.values()):
243
+ self._finished = True
244
+ self._call(self._call_connection_lost, None)
245
+
246
+ def _call_connection_lost(self, exc):
247
+ try:
248
+ self._protocol.connection_lost(exc)
249
+ finally:
250
+ self._loop = None
251
+ self._proc = None
252
+ self._protocol = None
253
+
254
+
255
+ class WriteSubprocessPipeProto(protocols.BaseProtocol):
256
+
257
+ def __init__(self, proc, fd):
258
+ self.proc = proc
259
+ self.fd = fd
260
+ self.pipe = None
261
+ self.disconnected = False
262
+
263
+ def connection_made(self, transport):
264
+ self.pipe = transport
265
+
266
+ def __repr__(self):
267
+ return f'<{self.__class__.__name__} fd={self.fd} pipe={self.pipe!r}>'
268
+
269
+ def connection_lost(self, exc):
270
+ self.disconnected = True
271
+ self.proc._pipe_connection_lost(self.fd, exc)
272
+ self.proc = None
273
+
274
+ def pause_writing(self):
275
+ self.proc._protocol.pause_writing()
276
+
277
+ def resume_writing(self):
278
+ self.proc._protocol.resume_writing()
279
+
280
+
281
+ class ReadSubprocessPipeProto(WriteSubprocessPipeProto,
282
+ protocols.Protocol):
283
+
284
+ def data_received(self, data):
285
+ self.proc._pipe_data_received(self.fd, data)
evalkit_llava/lib/python3.10/asyncio/constants.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import enum
2
+
3
+ # After the connection is lost, log warnings after this many write()s.
4
+ LOG_THRESHOLD_FOR_CONNLOST_WRITES = 5
5
+
6
+ # Seconds to wait before retrying accept().
7
+ ACCEPT_RETRY_DELAY = 1
8
+
9
+ # Number of stack entries to capture in debug mode.
10
+ # The larger the number, the slower the operation in debug mode
11
+ # (see extract_stack() in format_helpers.py).
12
+ DEBUG_STACK_DEPTH = 10
13
+
14
+ # Number of seconds to wait for SSL handshake to complete
15
+ # The default timeout matches that of Nginx.
16
+ SSL_HANDSHAKE_TIMEOUT = 60.0
17
+
18
+ # Used in sendfile fallback code. We use fallback for platforms
19
+ # that don't support sendfile, or for TLS connections.
20
+ SENDFILE_FALLBACK_READBUFFER_SIZE = 1024 * 256
21
+
22
+ # The enum should be here to break circular dependencies between
23
+ # base_events and sslproto
24
+ class _SendfileMode(enum.Enum):
25
+ UNSUPPORTED = enum.auto()
26
+ TRY_NATIVE = enum.auto()
27
+ FALLBACK = enum.auto()
evalkit_llava/lib/python3.10/asyncio/coroutines.py ADDED
@@ -0,0 +1,269 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ __all__ = 'coroutine', 'iscoroutinefunction', 'iscoroutine'
2
+
3
+ import collections.abc
4
+ import functools
5
+ import inspect
6
+ import os
7
+ import sys
8
+ import traceback
9
+ import types
10
+ import warnings
11
+
12
+ from . import base_futures
13
+ from . import constants
14
+ from . import format_helpers
15
+ from .log import logger
16
+
17
+
18
+ def _is_debug_mode():
19
+ # If you set _DEBUG to true, @coroutine will wrap the resulting
20
+ # generator objects in a CoroWrapper instance (defined below). That
21
+ # instance will log a message when the generator is never iterated
22
+ # over, which may happen when you forget to use "await" or "yield from"
23
+ # with a coroutine call.
24
+ # Note that the value of the _DEBUG flag is taken
25
+ # when the decorator is used, so to be of any use it must be set
26
+ # before you define your coroutines. A downside of using this feature
27
+ # is that tracebacks show entries for the CoroWrapper.__next__ method
28
+ # when _DEBUG is true.
29
+ return sys.flags.dev_mode or (not sys.flags.ignore_environment and
30
+ bool(os.environ.get('PYTHONASYNCIODEBUG')))
31
+
32
+
33
+ _DEBUG = _is_debug_mode()
34
+
35
+
36
+ class CoroWrapper:
37
+ # Wrapper for coroutine object in _DEBUG mode.
38
+
39
+ def __init__(self, gen, func=None):
40
+ assert inspect.isgenerator(gen) or inspect.iscoroutine(gen), gen
41
+ self.gen = gen
42
+ self.func = func # Used to unwrap @coroutine decorator
43
+ self._source_traceback = format_helpers.extract_stack(sys._getframe(1))
44
+ self.__name__ = getattr(gen, '__name__', None)
45
+ self.__qualname__ = getattr(gen, '__qualname__', None)
46
+
47
+ def __repr__(self):
48
+ coro_repr = _format_coroutine(self)
49
+ if self._source_traceback:
50
+ frame = self._source_traceback[-1]
51
+ coro_repr += f', created at {frame[0]}:{frame[1]}'
52
+
53
+ return f'<{self.__class__.__name__} {coro_repr}>'
54
+
55
+ def __iter__(self):
56
+ return self
57
+
58
+ def __next__(self):
59
+ return self.gen.send(None)
60
+
61
+ def send(self, value):
62
+ return self.gen.send(value)
63
+
64
+ def throw(self, type, value=None, traceback=None):
65
+ return self.gen.throw(type, value, traceback)
66
+
67
+ def close(self):
68
+ return self.gen.close()
69
+
70
+ @property
71
+ def gi_frame(self):
72
+ return self.gen.gi_frame
73
+
74
+ @property
75
+ def gi_running(self):
76
+ return self.gen.gi_running
77
+
78
+ @property
79
+ def gi_code(self):
80
+ return self.gen.gi_code
81
+
82
+ def __await__(self):
83
+ return self
84
+
85
+ @property
86
+ def gi_yieldfrom(self):
87
+ return self.gen.gi_yieldfrom
88
+
89
+ def __del__(self):
90
+ # Be careful accessing self.gen.frame -- self.gen might not exist.
91
+ gen = getattr(self, 'gen', None)
92
+ frame = getattr(gen, 'gi_frame', None)
93
+ if frame is not None and frame.f_lasti == -1:
94
+ msg = f'{self!r} was never yielded from'
95
+ tb = getattr(self, '_source_traceback', ())
96
+ if tb:
97
+ tb = ''.join(traceback.format_list(tb))
98
+ msg += (f'\nCoroutine object created at '
99
+ f'(most recent call last, truncated to '
100
+ f'{constants.DEBUG_STACK_DEPTH} last lines):\n')
101
+ msg += tb.rstrip()
102
+ logger.error(msg)
103
+
104
+
105
+ def coroutine(func):
106
+ """Decorator to mark coroutines.
107
+
108
+ If the coroutine is not yielded from before it is destroyed,
109
+ an error message is logged.
110
+ """
111
+ warnings.warn('"@coroutine" decorator is deprecated since Python 3.8, use "async def" instead',
112
+ DeprecationWarning,
113
+ stacklevel=2)
114
+ if inspect.iscoroutinefunction(func):
115
+ # In Python 3.5 that's all we need to do for coroutines
116
+ # defined with "async def".
117
+ return func
118
+
119
+ if inspect.isgeneratorfunction(func):
120
+ coro = func
121
+ else:
122
+ @functools.wraps(func)
123
+ def coro(*args, **kw):
124
+ res = func(*args, **kw)
125
+ if (base_futures.isfuture(res) or inspect.isgenerator(res) or
126
+ isinstance(res, CoroWrapper)):
127
+ res = yield from res
128
+ else:
129
+ # If 'res' is an awaitable, run it.
130
+ try:
131
+ await_meth = res.__await__
132
+ except AttributeError:
133
+ pass
134
+ else:
135
+ if isinstance(res, collections.abc.Awaitable):
136
+ res = yield from await_meth()
137
+ return res
138
+
139
+ coro = types.coroutine(coro)
140
+ if not _DEBUG:
141
+ wrapper = coro
142
+ else:
143
+ @functools.wraps(func)
144
+ def wrapper(*args, **kwds):
145
+ w = CoroWrapper(coro(*args, **kwds), func=func)
146
+ if w._source_traceback:
147
+ del w._source_traceback[-1]
148
+ # Python < 3.5 does not implement __qualname__
149
+ # on generator objects, so we set it manually.
150
+ # We use getattr as some callables (such as
151
+ # functools.partial may lack __qualname__).
152
+ w.__name__ = getattr(func, '__name__', None)
153
+ w.__qualname__ = getattr(func, '__qualname__', None)
154
+ return w
155
+
156
+ wrapper._is_coroutine = _is_coroutine # For iscoroutinefunction().
157
+ return wrapper
158
+
159
+
160
+ # A marker for iscoroutinefunction.
161
+ _is_coroutine = object()
162
+
163
+
164
+ def iscoroutinefunction(func):
165
+ """Return True if func is a decorated coroutine function."""
166
+ return (inspect.iscoroutinefunction(func) or
167
+ getattr(func, '_is_coroutine', None) is _is_coroutine)
168
+
169
+
170
+ # Prioritize native coroutine check to speed-up
171
+ # asyncio.iscoroutine.
172
+ _COROUTINE_TYPES = (types.CoroutineType, types.GeneratorType,
173
+ collections.abc.Coroutine, CoroWrapper)
174
+ _iscoroutine_typecache = set()
175
+
176
+
177
+ def iscoroutine(obj):
178
+ """Return True if obj is a coroutine object."""
179
+ if type(obj) in _iscoroutine_typecache:
180
+ return True
181
+
182
+ if isinstance(obj, _COROUTINE_TYPES):
183
+ # Just in case we don't want to cache more than 100
184
+ # positive types. That shouldn't ever happen, unless
185
+ # someone stressing the system on purpose.
186
+ if len(_iscoroutine_typecache) < 100:
187
+ _iscoroutine_typecache.add(type(obj))
188
+ return True
189
+ else:
190
+ return False
191
+
192
+
193
+ def _format_coroutine(coro):
194
+ assert iscoroutine(coro)
195
+
196
+ is_corowrapper = isinstance(coro, CoroWrapper)
197
+
198
+ def get_name(coro):
199
+ # Coroutines compiled with Cython sometimes don't have
200
+ # proper __qualname__ or __name__. While that is a bug
201
+ # in Cython, asyncio shouldn't crash with an AttributeError
202
+ # in its __repr__ functions.
203
+ if is_corowrapper:
204
+ return format_helpers._format_callback(coro.func, (), {})
205
+
206
+ if hasattr(coro, '__qualname__') and coro.__qualname__:
207
+ coro_name = coro.__qualname__
208
+ elif hasattr(coro, '__name__') and coro.__name__:
209
+ coro_name = coro.__name__
210
+ else:
211
+ # Stop masking Cython bugs, expose them in a friendly way.
212
+ coro_name = f'<{type(coro).__name__} without __name__>'
213
+ return f'{coro_name}()'
214
+
215
+ def is_running(coro):
216
+ try:
217
+ return coro.cr_running
218
+ except AttributeError:
219
+ try:
220
+ return coro.gi_running
221
+ except AttributeError:
222
+ return False
223
+
224
+ coro_code = None
225
+ if hasattr(coro, 'cr_code') and coro.cr_code:
226
+ coro_code = coro.cr_code
227
+ elif hasattr(coro, 'gi_code') and coro.gi_code:
228
+ coro_code = coro.gi_code
229
+
230
+ coro_name = get_name(coro)
231
+
232
+ if not coro_code:
233
+ # Built-in types might not have __qualname__ or __name__.
234
+ if is_running(coro):
235
+ return f'{coro_name} running'
236
+ else:
237
+ return coro_name
238
+
239
+ coro_frame = None
240
+ if hasattr(coro, 'gi_frame') and coro.gi_frame:
241
+ coro_frame = coro.gi_frame
242
+ elif hasattr(coro, 'cr_frame') and coro.cr_frame:
243
+ coro_frame = coro.cr_frame
244
+
245
+ # If Cython's coroutine has a fake code object without proper
246
+ # co_filename -- expose that.
247
+ filename = coro_code.co_filename or '<empty co_filename>'
248
+
249
+ lineno = 0
250
+ if (is_corowrapper and
251
+ coro.func is not None and
252
+ not inspect.isgeneratorfunction(coro.func)):
253
+ source = format_helpers._get_function_source(coro.func)
254
+ if source is not None:
255
+ filename, lineno = source
256
+ if coro_frame is None:
257
+ coro_repr = f'{coro_name} done, defined at {filename}:{lineno}'
258
+ else:
259
+ coro_repr = f'{coro_name} running, defined at {filename}:{lineno}'
260
+
261
+ elif coro_frame is not None:
262
+ lineno = coro_frame.f_lineno
263
+ coro_repr = f'{coro_name} running at {filename}:{lineno}'
264
+
265
+ else:
266
+ lineno = coro_code.co_firstlineno
267
+ coro_repr = f'{coro_name} done, defined at {filename}:{lineno}'
268
+
269
+ return coro_repr
evalkit_llava/lib/python3.10/asyncio/events.py ADDED
@@ -0,0 +1,819 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Event loop and event loop policy."""
2
+
3
+ __all__ = (
4
+ 'AbstractEventLoopPolicy',
5
+ 'AbstractEventLoop', 'AbstractServer',
6
+ 'Handle', 'TimerHandle',
7
+ 'get_event_loop_policy', 'set_event_loop_policy',
8
+ 'get_event_loop', 'set_event_loop', 'new_event_loop',
9
+ 'get_child_watcher', 'set_child_watcher',
10
+ '_set_running_loop', 'get_running_loop',
11
+ '_get_running_loop',
12
+ )
13
+
14
+ import contextvars
15
+ import os
16
+ import socket
17
+ import subprocess
18
+ import sys
19
+ import threading
20
+
21
+ from . import format_helpers
22
+
23
+
24
+ class Handle:
25
+ """Object returned by callback registration methods."""
26
+
27
+ __slots__ = ('_callback', '_args', '_cancelled', '_loop',
28
+ '_source_traceback', '_repr', '__weakref__',
29
+ '_context')
30
+
31
+ def __init__(self, callback, args, loop, context=None):
32
+ if context is None:
33
+ context = contextvars.copy_context()
34
+ self._context = context
35
+ self._loop = loop
36
+ self._callback = callback
37
+ self._args = args
38
+ self._cancelled = False
39
+ self._repr = None
40
+ if self._loop.get_debug():
41
+ self._source_traceback = format_helpers.extract_stack(
42
+ sys._getframe(1))
43
+ else:
44
+ self._source_traceback = None
45
+
46
+ def _repr_info(self):
47
+ info = [self.__class__.__name__]
48
+ if self._cancelled:
49
+ info.append('cancelled')
50
+ if self._callback is not None:
51
+ info.append(format_helpers._format_callback_source(
52
+ self._callback, self._args))
53
+ if self._source_traceback:
54
+ frame = self._source_traceback[-1]
55
+ info.append(f'created at {frame[0]}:{frame[1]}')
56
+ return info
57
+
58
+ def __repr__(self):
59
+ if self._repr is not None:
60
+ return self._repr
61
+ info = self._repr_info()
62
+ return '<{}>'.format(' '.join(info))
63
+
64
+ def cancel(self):
65
+ if not self._cancelled:
66
+ self._cancelled = True
67
+ if self._loop.get_debug():
68
+ # Keep a representation in debug mode to keep callback and
69
+ # parameters. For example, to log the warning
70
+ # "Executing <Handle...> took 2.5 second"
71
+ self._repr = repr(self)
72
+ self._callback = None
73
+ self._args = None
74
+
75
+ def cancelled(self):
76
+ return self._cancelled
77
+
78
+ def _run(self):
79
+ try:
80
+ self._context.run(self._callback, *self._args)
81
+ except (SystemExit, KeyboardInterrupt):
82
+ raise
83
+ except BaseException as exc:
84
+ cb = format_helpers._format_callback_source(
85
+ self._callback, self._args)
86
+ msg = f'Exception in callback {cb}'
87
+ context = {
88
+ 'message': msg,
89
+ 'exception': exc,
90
+ 'handle': self,
91
+ }
92
+ if self._source_traceback:
93
+ context['source_traceback'] = self._source_traceback
94
+ self._loop.call_exception_handler(context)
95
+ self = None # Needed to break cycles when an exception occurs.
96
+
97
+
98
+ class TimerHandle(Handle):
99
+ """Object returned by timed callback registration methods."""
100
+
101
+ __slots__ = ['_scheduled', '_when']
102
+
103
+ def __init__(self, when, callback, args, loop, context=None):
104
+ assert when is not None
105
+ super().__init__(callback, args, loop, context)
106
+ if self._source_traceback:
107
+ del self._source_traceback[-1]
108
+ self._when = when
109
+ self._scheduled = False
110
+
111
+ def _repr_info(self):
112
+ info = super()._repr_info()
113
+ pos = 2 if self._cancelled else 1
114
+ info.insert(pos, f'when={self._when}')
115
+ return info
116
+
117
+ def __hash__(self):
118
+ return hash(self._when)
119
+
120
+ def __lt__(self, other):
121
+ if isinstance(other, TimerHandle):
122
+ return self._when < other._when
123
+ return NotImplemented
124
+
125
+ def __le__(self, other):
126
+ if isinstance(other, TimerHandle):
127
+ return self._when < other._when or self.__eq__(other)
128
+ return NotImplemented
129
+
130
+ def __gt__(self, other):
131
+ if isinstance(other, TimerHandle):
132
+ return self._when > other._when
133
+ return NotImplemented
134
+
135
+ def __ge__(self, other):
136
+ if isinstance(other, TimerHandle):
137
+ return self._when > other._when or self.__eq__(other)
138
+ return NotImplemented
139
+
140
+ def __eq__(self, other):
141
+ if isinstance(other, TimerHandle):
142
+ return (self._when == other._when and
143
+ self._callback == other._callback and
144
+ self._args == other._args and
145
+ self._cancelled == other._cancelled)
146
+ return NotImplemented
147
+
148
+ def cancel(self):
149
+ if not self._cancelled:
150
+ self._loop._timer_handle_cancelled(self)
151
+ super().cancel()
152
+
153
+ def when(self):
154
+ """Return a scheduled callback time.
155
+
156
+ The time is an absolute timestamp, using the same time
157
+ reference as loop.time().
158
+ """
159
+ return self._when
160
+
161
+
162
+ class AbstractServer:
163
+ """Abstract server returned by create_server()."""
164
+
165
+ def close(self):
166
+ """Stop serving. This leaves existing connections open."""
167
+ raise NotImplementedError
168
+
169
+ def get_loop(self):
170
+ """Get the event loop the Server object is attached to."""
171
+ raise NotImplementedError
172
+
173
+ def is_serving(self):
174
+ """Return True if the server is accepting connections."""
175
+ raise NotImplementedError
176
+
177
+ async def start_serving(self):
178
+ """Start accepting connections.
179
+
180
+ This method is idempotent, so it can be called when
181
+ the server is already being serving.
182
+ """
183
+ raise NotImplementedError
184
+
185
+ async def serve_forever(self):
186
+ """Start accepting connections until the coroutine is cancelled.
187
+
188
+ The server is closed when the coroutine is cancelled.
189
+ """
190
+ raise NotImplementedError
191
+
192
+ async def wait_closed(self):
193
+ """Coroutine to wait until service is closed."""
194
+ raise NotImplementedError
195
+
196
+ async def __aenter__(self):
197
+ return self
198
+
199
+ async def __aexit__(self, *exc):
200
+ self.close()
201
+ await self.wait_closed()
202
+
203
+
204
+ class AbstractEventLoop:
205
+ """Abstract event loop."""
206
+
207
+ # Running and stopping the event loop.
208
+
209
+ def run_forever(self):
210
+ """Run the event loop until stop() is called."""
211
+ raise NotImplementedError
212
+
213
+ def run_until_complete(self, future):
214
+ """Run the event loop until a Future is done.
215
+
216
+ Return the Future's result, or raise its exception.
217
+ """
218
+ raise NotImplementedError
219
+
220
+ def stop(self):
221
+ """Stop the event loop as soon as reasonable.
222
+
223
+ Exactly how soon that is may depend on the implementation, but
224
+ no more I/O callbacks should be scheduled.
225
+ """
226
+ raise NotImplementedError
227
+
228
+ def is_running(self):
229
+ """Return whether the event loop is currently running."""
230
+ raise NotImplementedError
231
+
232
+ def is_closed(self):
233
+ """Returns True if the event loop was closed."""
234
+ raise NotImplementedError
235
+
236
+ def close(self):
237
+ """Close the loop.
238
+
239
+ The loop should not be running.
240
+
241
+ This is idempotent and irreversible.
242
+
243
+ No other methods should be called after this one.
244
+ """
245
+ raise NotImplementedError
246
+
247
+ async def shutdown_asyncgens(self):
248
+ """Shutdown all active asynchronous generators."""
249
+ raise NotImplementedError
250
+
251
+ async def shutdown_default_executor(self):
252
+ """Schedule the shutdown of the default executor."""
253
+ raise NotImplementedError
254
+
255
+ # Methods scheduling callbacks. All these return Handles.
256
+
257
+ def _timer_handle_cancelled(self, handle):
258
+ """Notification that a TimerHandle has been cancelled."""
259
+ raise NotImplementedError
260
+
261
+ def call_soon(self, callback, *args, context=None):
262
+ return self.call_later(0, callback, *args, context=context)
263
+
264
+ def call_later(self, delay, callback, *args, context=None):
265
+ raise NotImplementedError
266
+
267
+ def call_at(self, when, callback, *args, context=None):
268
+ raise NotImplementedError
269
+
270
+ def time(self):
271
+ raise NotImplementedError
272
+
273
+ def create_future(self):
274
+ raise NotImplementedError
275
+
276
+ # Method scheduling a coroutine object: create a task.
277
+
278
+ def create_task(self, coro, *, name=None):
279
+ raise NotImplementedError
280
+
281
+ # Methods for interacting with threads.
282
+
283
+ def call_soon_threadsafe(self, callback, *args, context=None):
284
+ raise NotImplementedError
285
+
286
+ def run_in_executor(self, executor, func, *args):
287
+ raise NotImplementedError
288
+
289
+ def set_default_executor(self, executor):
290
+ raise NotImplementedError
291
+
292
+ # Network I/O methods returning Futures.
293
+
294
+ async def getaddrinfo(self, host, port, *,
295
+ family=0, type=0, proto=0, flags=0):
296
+ raise NotImplementedError
297
+
298
+ async def getnameinfo(self, sockaddr, flags=0):
299
+ raise NotImplementedError
300
+
301
+ async def create_connection(
302
+ self, protocol_factory, host=None, port=None,
303
+ *, ssl=None, family=0, proto=0,
304
+ flags=0, sock=None, local_addr=None,
305
+ server_hostname=None,
306
+ ssl_handshake_timeout=None,
307
+ happy_eyeballs_delay=None, interleave=None):
308
+ raise NotImplementedError
309
+
310
+ async def create_server(
311
+ self, protocol_factory, host=None, port=None,
312
+ *, family=socket.AF_UNSPEC,
313
+ flags=socket.AI_PASSIVE, sock=None, backlog=100,
314
+ ssl=None, reuse_address=None, reuse_port=None,
315
+ ssl_handshake_timeout=None,
316
+ start_serving=True):
317
+ """A coroutine which creates a TCP server bound to host and port.
318
+
319
+ The return value is a Server object which can be used to stop
320
+ the service.
321
+
322
+ If host is an empty string or None all interfaces are assumed
323
+ and a list of multiple sockets will be returned (most likely
324
+ one for IPv4 and another one for IPv6). The host parameter can also be
325
+ a sequence (e.g. list) of hosts to bind to.
326
+
327
+ family can be set to either AF_INET or AF_INET6 to force the
328
+ socket to use IPv4 or IPv6. If not set it will be determined
329
+ from host (defaults to AF_UNSPEC).
330
+
331
+ flags is a bitmask for getaddrinfo().
332
+
333
+ sock can optionally be specified in order to use a preexisting
334
+ socket object.
335
+
336
+ backlog is the maximum number of queued connections passed to
337
+ listen() (defaults to 100).
338
+
339
+ ssl can be set to an SSLContext to enable SSL over the
340
+ accepted connections.
341
+
342
+ reuse_address tells the kernel to reuse a local socket in
343
+ TIME_WAIT state, without waiting for its natural timeout to
344
+ expire. If not specified will automatically be set to True on
345
+ UNIX.
346
+
347
+ reuse_port tells the kernel to allow this endpoint to be bound to
348
+ the same port as other existing endpoints are bound to, so long as
349
+ they all set this flag when being created. This option is not
350
+ supported on Windows.
351
+
352
+ ssl_handshake_timeout is the time in seconds that an SSL server
353
+ will wait for completion of the SSL handshake before aborting the
354
+ connection. Default is 60s.
355
+
356
+ start_serving set to True (default) causes the created server
357
+ to start accepting connections immediately. When set to False,
358
+ the user should await Server.start_serving() or Server.serve_forever()
359
+ to make the server to start accepting connections.
360
+ """
361
+ raise NotImplementedError
362
+
363
+ async def sendfile(self, transport, file, offset=0, count=None,
364
+ *, fallback=True):
365
+ """Send a file through a transport.
366
+
367
+ Return an amount of sent bytes.
368
+ """
369
+ raise NotImplementedError
370
+
371
+ async def start_tls(self, transport, protocol, sslcontext, *,
372
+ server_side=False,
373
+ server_hostname=None,
374
+ ssl_handshake_timeout=None):
375
+ """Upgrade a transport to TLS.
376
+
377
+ Return a new transport that *protocol* should start using
378
+ immediately.
379
+ """
380
+ raise NotImplementedError
381
+
382
+ async def create_unix_connection(
383
+ self, protocol_factory, path=None, *,
384
+ ssl=None, sock=None,
385
+ server_hostname=None,
386
+ ssl_handshake_timeout=None):
387
+ raise NotImplementedError
388
+
389
+ async def create_unix_server(
390
+ self, protocol_factory, path=None, *,
391
+ sock=None, backlog=100, ssl=None,
392
+ ssl_handshake_timeout=None,
393
+ start_serving=True):
394
+ """A coroutine which creates a UNIX Domain Socket server.
395
+
396
+ The return value is a Server object, which can be used to stop
397
+ the service.
398
+
399
+ path is a str, representing a file system path to bind the
400
+ server socket to.
401
+
402
+ sock can optionally be specified in order to use a preexisting
403
+ socket object.
404
+
405
+ backlog is the maximum number of queued connections passed to
406
+ listen() (defaults to 100).
407
+
408
+ ssl can be set to an SSLContext to enable SSL over the
409
+ accepted connections.
410
+
411
+ ssl_handshake_timeout is the time in seconds that an SSL server
412
+ will wait for the SSL handshake to complete (defaults to 60s).
413
+
414
+ start_serving set to True (default) causes the created server
415
+ to start accepting connections immediately. When set to False,
416
+ the user should await Server.start_serving() or Server.serve_forever()
417
+ to make the server to start accepting connections.
418
+ """
419
+ raise NotImplementedError
420
+
421
+ async def connect_accepted_socket(
422
+ self, protocol_factory, sock,
423
+ *, ssl=None,
424
+ ssl_handshake_timeout=None):
425
+ """Handle an accepted connection.
426
+
427
+ This is used by servers that accept connections outside of
428
+ asyncio, but use asyncio to handle connections.
429
+
430
+ This method is a coroutine. When completed, the coroutine
431
+ returns a (transport, protocol) pair.
432
+ """
433
+ raise NotImplementedError
434
+
435
+ async def create_datagram_endpoint(self, protocol_factory,
436
+ local_addr=None, remote_addr=None, *,
437
+ family=0, proto=0, flags=0,
438
+ reuse_address=None, reuse_port=None,
439
+ allow_broadcast=None, sock=None):
440
+ """A coroutine which creates a datagram endpoint.
441
+
442
+ This method will try to establish the endpoint in the background.
443
+ When successful, the coroutine returns a (transport, protocol) pair.
444
+
445
+ protocol_factory must be a callable returning a protocol instance.
446
+
447
+ socket family AF_INET, socket.AF_INET6 or socket.AF_UNIX depending on
448
+ host (or family if specified), socket type SOCK_DGRAM.
449
+
450
+ reuse_address tells the kernel to reuse a local socket in
451
+ TIME_WAIT state, without waiting for its natural timeout to
452
+ expire. If not specified it will automatically be set to True on
453
+ UNIX.
454
+
455
+ reuse_port tells the kernel to allow this endpoint to be bound to
456
+ the same port as other existing endpoints are bound to, so long as
457
+ they all set this flag when being created. This option is not
458
+ supported on Windows and some UNIX's. If the
459
+ :py:data:`~socket.SO_REUSEPORT` constant is not defined then this
460
+ capability is unsupported.
461
+
462
+ allow_broadcast tells the kernel to allow this endpoint to send
463
+ messages to the broadcast address.
464
+
465
+ sock can optionally be specified in order to use a preexisting
466
+ socket object.
467
+ """
468
+ raise NotImplementedError
469
+
470
+ # Pipes and subprocesses.
471
+
472
+ async def connect_read_pipe(self, protocol_factory, pipe):
473
+ """Register read pipe in event loop. Set the pipe to non-blocking mode.
474
+
475
+ protocol_factory should instantiate object with Protocol interface.
476
+ pipe is a file-like object.
477
+ Return pair (transport, protocol), where transport supports the
478
+ ReadTransport interface."""
479
+ # The reason to accept file-like object instead of just file descriptor
480
+ # is: we need to own pipe and close it at transport finishing
481
+ # Can got complicated errors if pass f.fileno(),
482
+ # close fd in pipe transport then close f and vice versa.
483
+ raise NotImplementedError
484
+
485
+ async def connect_write_pipe(self, protocol_factory, pipe):
486
+ """Register write pipe in event loop.
487
+
488
+ protocol_factory should instantiate object with BaseProtocol interface.
489
+ Pipe is file-like object already switched to nonblocking.
490
+ Return pair (transport, protocol), where transport support
491
+ WriteTransport interface."""
492
+ # The reason to accept file-like object instead of just file descriptor
493
+ # is: we need to own pipe and close it at transport finishing
494
+ # Can got complicated errors if pass f.fileno(),
495
+ # close fd in pipe transport then close f and vice versa.
496
+ raise NotImplementedError
497
+
498
+ async def subprocess_shell(self, protocol_factory, cmd, *,
499
+ stdin=subprocess.PIPE,
500
+ stdout=subprocess.PIPE,
501
+ stderr=subprocess.PIPE,
502
+ **kwargs):
503
+ raise NotImplementedError
504
+
505
+ async def subprocess_exec(self, protocol_factory, *args,
506
+ stdin=subprocess.PIPE,
507
+ stdout=subprocess.PIPE,
508
+ stderr=subprocess.PIPE,
509
+ **kwargs):
510
+ raise NotImplementedError
511
+
512
+ # Ready-based callback registration methods.
513
+ # The add_*() methods return None.
514
+ # The remove_*() methods return True if something was removed,
515
+ # False if there was nothing to delete.
516
+
517
+ def add_reader(self, fd, callback, *args):
518
+ raise NotImplementedError
519
+
520
+ def remove_reader(self, fd):
521
+ raise NotImplementedError
522
+
523
+ def add_writer(self, fd, callback, *args):
524
+ raise NotImplementedError
525
+
526
+ def remove_writer(self, fd):
527
+ raise NotImplementedError
528
+
529
+ # Completion based I/O methods returning Futures.
530
+
531
+ async def sock_recv(self, sock, nbytes):
532
+ raise NotImplementedError
533
+
534
+ async def sock_recv_into(self, sock, buf):
535
+ raise NotImplementedError
536
+
537
+ async def sock_sendall(self, sock, data):
538
+ raise NotImplementedError
539
+
540
+ async def sock_connect(self, sock, address):
541
+ raise NotImplementedError
542
+
543
+ async def sock_accept(self, sock):
544
+ raise NotImplementedError
545
+
546
+ async def sock_sendfile(self, sock, file, offset=0, count=None,
547
+ *, fallback=None):
548
+ raise NotImplementedError
549
+
550
+ # Signal handling.
551
+
552
+ def add_signal_handler(self, sig, callback, *args):
553
+ raise NotImplementedError
554
+
555
+ def remove_signal_handler(self, sig):
556
+ raise NotImplementedError
557
+
558
+ # Task factory.
559
+
560
+ def set_task_factory(self, factory):
561
+ raise NotImplementedError
562
+
563
+ def get_task_factory(self):
564
+ raise NotImplementedError
565
+
566
+ # Error handlers.
567
+
568
+ def get_exception_handler(self):
569
+ raise NotImplementedError
570
+
571
+ def set_exception_handler(self, handler):
572
+ raise NotImplementedError
573
+
574
+ def default_exception_handler(self, context):
575
+ raise NotImplementedError
576
+
577
+ def call_exception_handler(self, context):
578
+ raise NotImplementedError
579
+
580
+ # Debug flag management.
581
+
582
+ def get_debug(self):
583
+ raise NotImplementedError
584
+
585
+ def set_debug(self, enabled):
586
+ raise NotImplementedError
587
+
588
+
589
+ class AbstractEventLoopPolicy:
590
+ """Abstract policy for accessing the event loop."""
591
+
592
+ def get_event_loop(self):
593
+ """Get the event loop for the current context.
594
+
595
+ Returns an event loop object implementing the BaseEventLoop interface,
596
+ or raises an exception in case no event loop has been set for the
597
+ current context and the current policy does not specify to create one.
598
+
599
+ It should never return None."""
600
+ raise NotImplementedError
601
+
602
+ def set_event_loop(self, loop):
603
+ """Set the event loop for the current context to loop."""
604
+ raise NotImplementedError
605
+
606
+ def new_event_loop(self):
607
+ """Create and return a new event loop object according to this
608
+ policy's rules. If there's need to set this loop as the event loop for
609
+ the current context, set_event_loop must be called explicitly."""
610
+ raise NotImplementedError
611
+
612
+ # Child processes handling (Unix only).
613
+
614
+ def get_child_watcher(self):
615
+ "Get the watcher for child processes."
616
+ raise NotImplementedError
617
+
618
+ def set_child_watcher(self, watcher):
619
+ """Set the watcher for child processes."""
620
+ raise NotImplementedError
621
+
622
+
623
+ class BaseDefaultEventLoopPolicy(AbstractEventLoopPolicy):
624
+ """Default policy implementation for accessing the event loop.
625
+
626
+ In this policy, each thread has its own event loop. However, we
627
+ only automatically create an event loop by default for the main
628
+ thread; other threads by default have no event loop.
629
+
630
+ Other policies may have different rules (e.g. a single global
631
+ event loop, or automatically creating an event loop per thread, or
632
+ using some other notion of context to which an event loop is
633
+ associated).
634
+ """
635
+
636
+ _loop_factory = None
637
+
638
+ class _Local(threading.local):
639
+ _loop = None
640
+ _set_called = False
641
+
642
+ def __init__(self):
643
+ self._local = self._Local()
644
+
645
+ def get_event_loop(self):
646
+ """Get the event loop for the current context.
647
+
648
+ Returns an instance of EventLoop or raises an exception.
649
+ """
650
+ if (self._local._loop is None and
651
+ not self._local._set_called and
652
+ threading.current_thread() is threading.main_thread()):
653
+ self.set_event_loop(self.new_event_loop())
654
+
655
+ if self._local._loop is None:
656
+ raise RuntimeError('There is no current event loop in thread %r.'
657
+ % threading.current_thread().name)
658
+
659
+ return self._local._loop
660
+
661
+ def set_event_loop(self, loop):
662
+ """Set the event loop."""
663
+ self._local._set_called = True
664
+ assert loop is None or isinstance(loop, AbstractEventLoop)
665
+ self._local._loop = loop
666
+
667
+ def new_event_loop(self):
668
+ """Create a new event loop.
669
+
670
+ You must call set_event_loop() to make this the current event
671
+ loop.
672
+ """
673
+ return self._loop_factory()
674
+
675
+
676
+ # Event loop policy. The policy itself is always global, even if the
677
+ # policy's rules say that there is an event loop per thread (or other
678
+ # notion of context). The default policy is installed by the first
679
+ # call to get_event_loop_policy().
680
+ _event_loop_policy = None
681
+
682
+ # Lock for protecting the on-the-fly creation of the event loop policy.
683
+ _lock = threading.Lock()
684
+
685
+
686
+ # A TLS for the running event loop, used by _get_running_loop.
687
+ class _RunningLoop(threading.local):
688
+ loop_pid = (None, None)
689
+
690
+
691
+ _running_loop = _RunningLoop()
692
+
693
+
694
+ def get_running_loop():
695
+ """Return the running event loop. Raise a RuntimeError if there is none.
696
+
697
+ This function is thread-specific.
698
+ """
699
+ # NOTE: this function is implemented in C (see _asynciomodule.c)
700
+ loop = _get_running_loop()
701
+ if loop is None:
702
+ raise RuntimeError('no running event loop')
703
+ return loop
704
+
705
+
706
+ def _get_running_loop():
707
+ """Return the running event loop or None.
708
+
709
+ This is a low-level function intended to be used by event loops.
710
+ This function is thread-specific.
711
+ """
712
+ # NOTE: this function is implemented in C (see _asynciomodule.c)
713
+ running_loop, pid = _running_loop.loop_pid
714
+ if running_loop is not None and pid == os.getpid():
715
+ return running_loop
716
+
717
+
718
+ def _set_running_loop(loop):
719
+ """Set the running event loop.
720
+
721
+ This is a low-level function intended to be used by event loops.
722
+ This function is thread-specific.
723
+ """
724
+ # NOTE: this function is implemented in C (see _asynciomodule.c)
725
+ _running_loop.loop_pid = (loop, os.getpid())
726
+
727
+
728
+ def _init_event_loop_policy():
729
+ global _event_loop_policy
730
+ with _lock:
731
+ if _event_loop_policy is None: # pragma: no branch
732
+ from . import DefaultEventLoopPolicy
733
+ _event_loop_policy = DefaultEventLoopPolicy()
734
+
735
+
736
+ def get_event_loop_policy():
737
+ """Get the current event loop policy."""
738
+ if _event_loop_policy is None:
739
+ _init_event_loop_policy()
740
+ return _event_loop_policy
741
+
742
+
743
+ def set_event_loop_policy(policy):
744
+ """Set the current event loop policy.
745
+
746
+ If policy is None, the default policy is restored."""
747
+ global _event_loop_policy
748
+ assert policy is None or isinstance(policy, AbstractEventLoopPolicy)
749
+ _event_loop_policy = policy
750
+
751
+
752
+ def get_event_loop():
753
+ """Return an asyncio event loop.
754
+
755
+ When called from a coroutine or a callback (e.g. scheduled with call_soon
756
+ or similar API), this function will always return the running event loop.
757
+
758
+ If there is no running event loop set, the function will return
759
+ the result of `get_event_loop_policy().get_event_loop()` call.
760
+ """
761
+ # NOTE: this function is implemented in C (see _asynciomodule.c)
762
+ return _py__get_event_loop()
763
+
764
+
765
+ def _get_event_loop(stacklevel=3):
766
+ # This internal method is going away in Python 3.12, left here only for
767
+ # backwards compatibility with 3.10.0 - 3.10.8 and 3.11.0.
768
+ # Similarly, this method's C equivalent in _asyncio is going away as well.
769
+ # See GH-99949 for more details.
770
+ current_loop = _get_running_loop()
771
+ if current_loop is not None:
772
+ return current_loop
773
+ return get_event_loop_policy().get_event_loop()
774
+
775
+
776
+ def set_event_loop(loop):
777
+ """Equivalent to calling get_event_loop_policy().set_event_loop(loop)."""
778
+ get_event_loop_policy().set_event_loop(loop)
779
+
780
+
781
+ def new_event_loop():
782
+ """Equivalent to calling get_event_loop_policy().new_event_loop()."""
783
+ return get_event_loop_policy().new_event_loop()
784
+
785
+
786
+ def get_child_watcher():
787
+ """Equivalent to calling get_event_loop_policy().get_child_watcher()."""
788
+ return get_event_loop_policy().get_child_watcher()
789
+
790
+
791
+ def set_child_watcher(watcher):
792
+ """Equivalent to calling
793
+ get_event_loop_policy().set_child_watcher(watcher)."""
794
+ return get_event_loop_policy().set_child_watcher(watcher)
795
+
796
+
797
+ # Alias pure-Python implementations for testing purposes.
798
+ _py__get_running_loop = _get_running_loop
799
+ _py__set_running_loop = _set_running_loop
800
+ _py_get_running_loop = get_running_loop
801
+ _py_get_event_loop = get_event_loop
802
+ _py__get_event_loop = _get_event_loop
803
+
804
+
805
+ try:
806
+ # get_event_loop() is one of the most frequently called
807
+ # functions in asyncio. Pure Python implementation is
808
+ # about 4 times slower than C-accelerated.
809
+ from _asyncio import (_get_running_loop, _set_running_loop,
810
+ get_running_loop, get_event_loop, _get_event_loop)
811
+ except ImportError:
812
+ pass
813
+ else:
814
+ # Alias C implementations for testing purposes.
815
+ _c__get_running_loop = _get_running_loop
816
+ _c__set_running_loop = _set_running_loop
817
+ _c_get_running_loop = get_running_loop
818
+ _c_get_event_loop = get_event_loop
819
+ _c__get_event_loop = _get_event_loop
evalkit_llava/lib/python3.10/asyncio/futures.py ADDED
@@ -0,0 +1,426 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """A Future class similar to the one in PEP 3148."""
2
+
3
+ __all__ = (
4
+ 'Future', 'wrap_future', 'isfuture',
5
+ )
6
+
7
+ import concurrent.futures
8
+ import contextvars
9
+ import logging
10
+ import sys
11
+ from types import GenericAlias
12
+
13
+ from . import base_futures
14
+ from . import events
15
+ from . import exceptions
16
+ from . import format_helpers
17
+
18
+
19
+ isfuture = base_futures.isfuture
20
+
21
+
22
+ _PENDING = base_futures._PENDING
23
+ _CANCELLED = base_futures._CANCELLED
24
+ _FINISHED = base_futures._FINISHED
25
+
26
+
27
+ STACK_DEBUG = logging.DEBUG - 1 # heavy-duty debugging
28
+
29
+
30
+ class Future:
31
+ """This class is *almost* compatible with concurrent.futures.Future.
32
+
33
+ Differences:
34
+
35
+ - This class is not thread-safe.
36
+
37
+ - result() and exception() do not take a timeout argument and
38
+ raise an exception when the future isn't done yet.
39
+
40
+ - Callbacks registered with add_done_callback() are always called
41
+ via the event loop's call_soon().
42
+
43
+ - This class is not compatible with the wait() and as_completed()
44
+ methods in the concurrent.futures package.
45
+
46
+ (In Python 3.4 or later we may be able to unify the implementations.)
47
+ """
48
+
49
+ # Class variables serving as defaults for instance variables.
50
+ _state = _PENDING
51
+ _result = None
52
+ _exception = None
53
+ _loop = None
54
+ _source_traceback = None
55
+ _cancel_message = None
56
+ # A saved CancelledError for later chaining as an exception context.
57
+ _cancelled_exc = None
58
+
59
+ # This field is used for a dual purpose:
60
+ # - Its presence is a marker to declare that a class implements
61
+ # the Future protocol (i.e. is intended to be duck-type compatible).
62
+ # The value must also be not-None, to enable a subclass to declare
63
+ # that it is not compatible by setting this to None.
64
+ # - It is set by __iter__() below so that Task._step() can tell
65
+ # the difference between
66
+ # `await Future()` or`yield from Future()` (correct) vs.
67
+ # `yield Future()` (incorrect).
68
+ _asyncio_future_blocking = False
69
+
70
+ __log_traceback = False
71
+
72
+ def __init__(self, *, loop=None):
73
+ """Initialize the future.
74
+
75
+ The optional event_loop argument allows explicitly setting the event
76
+ loop object used by the future. If it's not provided, the future uses
77
+ the default event loop.
78
+ """
79
+ if loop is None:
80
+ self._loop = events._get_event_loop()
81
+ else:
82
+ self._loop = loop
83
+ self._callbacks = []
84
+ if self._loop.get_debug():
85
+ self._source_traceback = format_helpers.extract_stack(
86
+ sys._getframe(1))
87
+
88
+ _repr_info = base_futures._future_repr_info
89
+
90
+ def __repr__(self):
91
+ return '<{} {}>'.format(self.__class__.__name__,
92
+ ' '.join(self._repr_info()))
93
+
94
+ def __del__(self):
95
+ if not self.__log_traceback:
96
+ # set_exception() was not called, or result() or exception()
97
+ # has consumed the exception
98
+ return
99
+ exc = self._exception
100
+ context = {
101
+ 'message':
102
+ f'{self.__class__.__name__} exception was never retrieved',
103
+ 'exception': exc,
104
+ 'future': self,
105
+ }
106
+ if self._source_traceback:
107
+ context['source_traceback'] = self._source_traceback
108
+ self._loop.call_exception_handler(context)
109
+
110
+ __class_getitem__ = classmethod(GenericAlias)
111
+
112
+ @property
113
+ def _log_traceback(self):
114
+ return self.__log_traceback
115
+
116
+ @_log_traceback.setter
117
+ def _log_traceback(self, val):
118
+ if val:
119
+ raise ValueError('_log_traceback can only be set to False')
120
+ self.__log_traceback = False
121
+
122
+ def get_loop(self):
123
+ """Return the event loop the Future is bound to."""
124
+ loop = self._loop
125
+ if loop is None:
126
+ raise RuntimeError("Future object is not initialized.")
127
+ return loop
128
+
129
+ def _make_cancelled_error(self):
130
+ """Create the CancelledError to raise if the Future is cancelled.
131
+
132
+ This should only be called once when handling a cancellation since
133
+ it erases the saved context exception value.
134
+ """
135
+ if self._cancel_message is None:
136
+ exc = exceptions.CancelledError()
137
+ else:
138
+ exc = exceptions.CancelledError(self._cancel_message)
139
+ exc.__context__ = self._cancelled_exc
140
+ # Remove the reference since we don't need this anymore.
141
+ self._cancelled_exc = None
142
+ return exc
143
+
144
+ def cancel(self, msg=None):
145
+ """Cancel the future and schedule callbacks.
146
+
147
+ If the future is already done or cancelled, return False. Otherwise,
148
+ change the future's state to cancelled, schedule the callbacks and
149
+ return True.
150
+ """
151
+ self.__log_traceback = False
152
+ if self._state != _PENDING:
153
+ return False
154
+ self._state = _CANCELLED
155
+ self._cancel_message = msg
156
+ self.__schedule_callbacks()
157
+ return True
158
+
159
+ def __schedule_callbacks(self):
160
+ """Internal: Ask the event loop to call all callbacks.
161
+
162
+ The callbacks are scheduled to be called as soon as possible. Also
163
+ clears the callback list.
164
+ """
165
+ callbacks = self._callbacks[:]
166
+ if not callbacks:
167
+ return
168
+
169
+ self._callbacks[:] = []
170
+ for callback, ctx in callbacks:
171
+ self._loop.call_soon(callback, self, context=ctx)
172
+
173
+ def cancelled(self):
174
+ """Return True if the future was cancelled."""
175
+ return self._state == _CANCELLED
176
+
177
+ # Don't implement running(); see http://bugs.python.org/issue18699
178
+
179
+ def done(self):
180
+ """Return True if the future is done.
181
+
182
+ Done means either that a result / exception are available, or that the
183
+ future was cancelled.
184
+ """
185
+ return self._state != _PENDING
186
+
187
+ def result(self):
188
+ """Return the result this future represents.
189
+
190
+ If the future has been cancelled, raises CancelledError. If the
191
+ future's result isn't yet available, raises InvalidStateError. If
192
+ the future is done and has an exception set, this exception is raised.
193
+ """
194
+ if self._state == _CANCELLED:
195
+ exc = self._make_cancelled_error()
196
+ raise exc
197
+ if self._state != _FINISHED:
198
+ raise exceptions.InvalidStateError('Result is not ready.')
199
+ self.__log_traceback = False
200
+ if self._exception is not None:
201
+ raise self._exception.with_traceback(self._exception_tb)
202
+ return self._result
203
+
204
+ def exception(self):
205
+ """Return the exception that was set on this future.
206
+
207
+ The exception (or None if no exception was set) is returned only if
208
+ the future is done. If the future has been cancelled, raises
209
+ CancelledError. If the future isn't done yet, raises
210
+ InvalidStateError.
211
+ """
212
+ if self._state == _CANCELLED:
213
+ exc = self._make_cancelled_error()
214
+ raise exc
215
+ if self._state != _FINISHED:
216
+ raise exceptions.InvalidStateError('Exception is not set.')
217
+ self.__log_traceback = False
218
+ return self._exception
219
+
220
+ def add_done_callback(self, fn, *, context=None):
221
+ """Add a callback to be run when the future becomes done.
222
+
223
+ The callback is called with a single argument - the future object. If
224
+ the future is already done when this is called, the callback is
225
+ scheduled with call_soon.
226
+ """
227
+ if self._state != _PENDING:
228
+ self._loop.call_soon(fn, self, context=context)
229
+ else:
230
+ if context is None:
231
+ context = contextvars.copy_context()
232
+ self._callbacks.append((fn, context))
233
+
234
+ # New method not in PEP 3148.
235
+
236
+ def remove_done_callback(self, fn):
237
+ """Remove all instances of a callback from the "call when done" list.
238
+
239
+ Returns the number of callbacks removed.
240
+ """
241
+ filtered_callbacks = [(f, ctx)
242
+ for (f, ctx) in self._callbacks
243
+ if f != fn]
244
+ removed_count = len(self._callbacks) - len(filtered_callbacks)
245
+ if removed_count:
246
+ self._callbacks[:] = filtered_callbacks
247
+ return removed_count
248
+
249
+ # So-called internal methods (note: no set_running_or_notify_cancel()).
250
+
251
+ def set_result(self, result):
252
+ """Mark the future done and set its result.
253
+
254
+ If the future is already done when this method is called, raises
255
+ InvalidStateError.
256
+ """
257
+ if self._state != _PENDING:
258
+ raise exceptions.InvalidStateError(f'{self._state}: {self!r}')
259
+ self._result = result
260
+ self._state = _FINISHED
261
+ self.__schedule_callbacks()
262
+
263
+ def set_exception(self, exception):
264
+ """Mark the future done and set an exception.
265
+
266
+ If the future is already done when this method is called, raises
267
+ InvalidStateError.
268
+ """
269
+ if self._state != _PENDING:
270
+ raise exceptions.InvalidStateError(f'{self._state}: {self!r}')
271
+ if isinstance(exception, type):
272
+ exception = exception()
273
+ if type(exception) is StopIteration:
274
+ raise TypeError("StopIteration interacts badly with generators "
275
+ "and cannot be raised into a Future")
276
+ self._exception = exception
277
+ self._exception_tb = exception.__traceback__
278
+ self._state = _FINISHED
279
+ self.__schedule_callbacks()
280
+ self.__log_traceback = True
281
+
282
+ def __await__(self):
283
+ if not self.done():
284
+ self._asyncio_future_blocking = True
285
+ yield self # This tells Task to wait for completion.
286
+ if not self.done():
287
+ raise RuntimeError("await wasn't used with future")
288
+ return self.result() # May raise too.
289
+
290
+ __iter__ = __await__ # make compatible with 'yield from'.
291
+
292
+
293
+ # Needed for testing purposes.
294
+ _PyFuture = Future
295
+
296
+
297
+ def _get_loop(fut):
298
+ # Tries to call Future.get_loop() if it's available.
299
+ # Otherwise fallbacks to using the old '_loop' property.
300
+ try:
301
+ get_loop = fut.get_loop
302
+ except AttributeError:
303
+ pass
304
+ else:
305
+ return get_loop()
306
+ return fut._loop
307
+
308
+
309
+ def _set_result_unless_cancelled(fut, result):
310
+ """Helper setting the result only if the future was not cancelled."""
311
+ if fut.cancelled():
312
+ return
313
+ fut.set_result(result)
314
+
315
+
316
+ def _convert_future_exc(exc):
317
+ exc_class = type(exc)
318
+ if exc_class is concurrent.futures.CancelledError:
319
+ return exceptions.CancelledError(*exc.args)
320
+ elif exc_class is concurrent.futures.TimeoutError:
321
+ return exceptions.TimeoutError(*exc.args)
322
+ elif exc_class is concurrent.futures.InvalidStateError:
323
+ return exceptions.InvalidStateError(*exc.args)
324
+ else:
325
+ return exc
326
+
327
+
328
+ def _set_concurrent_future_state(concurrent, source):
329
+ """Copy state from a future to a concurrent.futures.Future."""
330
+ assert source.done()
331
+ if source.cancelled():
332
+ concurrent.cancel()
333
+ if not concurrent.set_running_or_notify_cancel():
334
+ return
335
+ exception = source.exception()
336
+ if exception is not None:
337
+ concurrent.set_exception(_convert_future_exc(exception))
338
+ else:
339
+ result = source.result()
340
+ concurrent.set_result(result)
341
+
342
+
343
+ def _copy_future_state(source, dest):
344
+ """Internal helper to copy state from another Future.
345
+
346
+ The other Future may be a concurrent.futures.Future.
347
+ """
348
+ assert source.done()
349
+ if dest.cancelled():
350
+ return
351
+ assert not dest.done()
352
+ if source.cancelled():
353
+ dest.cancel()
354
+ else:
355
+ exception = source.exception()
356
+ if exception is not None:
357
+ dest.set_exception(_convert_future_exc(exception))
358
+ else:
359
+ result = source.result()
360
+ dest.set_result(result)
361
+
362
+
363
+ def _chain_future(source, destination):
364
+ """Chain two futures so that when one completes, so does the other.
365
+
366
+ The result (or exception) of source will be copied to destination.
367
+ If destination is cancelled, source gets cancelled too.
368
+ Compatible with both asyncio.Future and concurrent.futures.Future.
369
+ """
370
+ if not isfuture(source) and not isinstance(source,
371
+ concurrent.futures.Future):
372
+ raise TypeError('A future is required for source argument')
373
+ if not isfuture(destination) and not isinstance(destination,
374
+ concurrent.futures.Future):
375
+ raise TypeError('A future is required for destination argument')
376
+ source_loop = _get_loop(source) if isfuture(source) else None
377
+ dest_loop = _get_loop(destination) if isfuture(destination) else None
378
+
379
+ def _set_state(future, other):
380
+ if isfuture(future):
381
+ _copy_future_state(other, future)
382
+ else:
383
+ _set_concurrent_future_state(future, other)
384
+
385
+ def _call_check_cancel(destination):
386
+ if destination.cancelled():
387
+ if source_loop is None or source_loop is dest_loop:
388
+ source.cancel()
389
+ else:
390
+ source_loop.call_soon_threadsafe(source.cancel)
391
+
392
+ def _call_set_state(source):
393
+ if (destination.cancelled() and
394
+ dest_loop is not None and dest_loop.is_closed()):
395
+ return
396
+ if dest_loop is None or dest_loop is source_loop:
397
+ _set_state(destination, source)
398
+ else:
399
+ if dest_loop.is_closed():
400
+ return
401
+ dest_loop.call_soon_threadsafe(_set_state, destination, source)
402
+
403
+ destination.add_done_callback(_call_check_cancel)
404
+ source.add_done_callback(_call_set_state)
405
+
406
+
407
+ def wrap_future(future, *, loop=None):
408
+ """Wrap concurrent.futures.Future object."""
409
+ if isfuture(future):
410
+ return future
411
+ assert isinstance(future, concurrent.futures.Future), \
412
+ f'concurrent.futures.Future is expected, got {future!r}'
413
+ if loop is None:
414
+ loop = events._get_event_loop()
415
+ new_future = loop.create_future()
416
+ _chain_future(future, new_future)
417
+ return new_future
418
+
419
+
420
+ try:
421
+ import _asyncio
422
+ except ImportError:
423
+ pass
424
+ else:
425
+ # _CFuture is needed for tests.
426
+ Future = _CFuture = _asyncio.Future
evalkit_llava/lib/python3.10/asyncio/locks.py ADDED
@@ -0,0 +1,438 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Synchronization primitives."""
2
+
3
+ __all__ = ('Lock', 'Event', 'Condition', 'Semaphore', 'BoundedSemaphore')
4
+
5
+ import collections
6
+
7
+ from . import exceptions
8
+ from . import mixins
9
+ from . import tasks
10
+
11
+
12
+ class _ContextManagerMixin:
13
+ async def __aenter__(self):
14
+ await self.acquire()
15
+ # We have no use for the "as ..." clause in the with
16
+ # statement for locks.
17
+ return None
18
+
19
+ async def __aexit__(self, exc_type, exc, tb):
20
+ self.release()
21
+
22
+
23
+ class Lock(_ContextManagerMixin, mixins._LoopBoundMixin):
24
+ """Primitive lock objects.
25
+
26
+ A primitive lock is a synchronization primitive that is not owned
27
+ by a particular coroutine when locked. A primitive lock is in one
28
+ of two states, 'locked' or 'unlocked'.
29
+
30
+ It is created in the unlocked state. It has two basic methods,
31
+ acquire() and release(). When the state is unlocked, acquire()
32
+ changes the state to locked and returns immediately. When the
33
+ state is locked, acquire() blocks until a call to release() in
34
+ another coroutine changes it to unlocked, then the acquire() call
35
+ resets it to locked and returns. The release() method should only
36
+ be called in the locked state; it changes the state to unlocked
37
+ and returns immediately. If an attempt is made to release an
38
+ unlocked lock, a RuntimeError will be raised.
39
+
40
+ When more than one coroutine is blocked in acquire() waiting for
41
+ the state to turn to unlocked, only one coroutine proceeds when a
42
+ release() call resets the state to unlocked; first coroutine which
43
+ is blocked in acquire() is being processed.
44
+
45
+ acquire() is a coroutine and should be called with 'await'.
46
+
47
+ Locks also support the asynchronous context management protocol.
48
+ 'async with lock' statement should be used.
49
+
50
+ Usage:
51
+
52
+ lock = Lock()
53
+ ...
54
+ await lock.acquire()
55
+ try:
56
+ ...
57
+ finally:
58
+ lock.release()
59
+
60
+ Context manager usage:
61
+
62
+ lock = Lock()
63
+ ...
64
+ async with lock:
65
+ ...
66
+
67
+ Lock objects can be tested for locking state:
68
+
69
+ if not lock.locked():
70
+ await lock.acquire()
71
+ else:
72
+ # lock is acquired
73
+ ...
74
+
75
+ """
76
+
77
+ def __init__(self, *, loop=mixins._marker):
78
+ super().__init__(loop=loop)
79
+ self._waiters = None
80
+ self._locked = False
81
+
82
+ def __repr__(self):
83
+ res = super().__repr__()
84
+ extra = 'locked' if self._locked else 'unlocked'
85
+ if self._waiters:
86
+ extra = f'{extra}, waiters:{len(self._waiters)}'
87
+ return f'<{res[1:-1]} [{extra}]>'
88
+
89
+ def locked(self):
90
+ """Return True if lock is acquired."""
91
+ return self._locked
92
+
93
+ async def acquire(self):
94
+ """Acquire a lock.
95
+
96
+ This method blocks until the lock is unlocked, then sets it to
97
+ locked and returns True.
98
+ """
99
+ if (not self._locked and (self._waiters is None or
100
+ all(w.cancelled() for w in self._waiters))):
101
+ self._locked = True
102
+ return True
103
+
104
+ if self._waiters is None:
105
+ self._waiters = collections.deque()
106
+ fut = self._get_loop().create_future()
107
+ self._waiters.append(fut)
108
+
109
+ # Finally block should be called before the CancelledError
110
+ # handling as we don't want CancelledError to call
111
+ # _wake_up_first() and attempt to wake up itself.
112
+ try:
113
+ try:
114
+ await fut
115
+ finally:
116
+ self._waiters.remove(fut)
117
+ except exceptions.CancelledError:
118
+ if not self._locked:
119
+ self._wake_up_first()
120
+ raise
121
+
122
+ self._locked = True
123
+ return True
124
+
125
+ def release(self):
126
+ """Release a lock.
127
+
128
+ When the lock is locked, reset it to unlocked, and return.
129
+ If any other coroutines are blocked waiting for the lock to become
130
+ unlocked, allow exactly one of them to proceed.
131
+
132
+ When invoked on an unlocked lock, a RuntimeError is raised.
133
+
134
+ There is no return value.
135
+ """
136
+ if self._locked:
137
+ self._locked = False
138
+ self._wake_up_first()
139
+ else:
140
+ raise RuntimeError('Lock is not acquired.')
141
+
142
+ def _wake_up_first(self):
143
+ """Wake up the first waiter if it isn't done."""
144
+ if not self._waiters:
145
+ return
146
+ try:
147
+ fut = next(iter(self._waiters))
148
+ except StopIteration:
149
+ return
150
+
151
+ # .done() necessarily means that a waiter will wake up later on and
152
+ # either take the lock, or, if it was cancelled and lock wasn't
153
+ # taken already, will hit this again and wake up a new waiter.
154
+ if not fut.done():
155
+ fut.set_result(True)
156
+
157
+
158
+ class Event(mixins._LoopBoundMixin):
159
+ """Asynchronous equivalent to threading.Event.
160
+
161
+ Class implementing event objects. An event manages a flag that can be set
162
+ to true with the set() method and reset to false with the clear() method.
163
+ The wait() method blocks until the flag is true. The flag is initially
164
+ false.
165
+ """
166
+
167
+ def __init__(self, *, loop=mixins._marker):
168
+ super().__init__(loop=loop)
169
+ self._waiters = collections.deque()
170
+ self._value = False
171
+
172
+ def __repr__(self):
173
+ res = super().__repr__()
174
+ extra = 'set' if self._value else 'unset'
175
+ if self._waiters:
176
+ extra = f'{extra}, waiters:{len(self._waiters)}'
177
+ return f'<{res[1:-1]} [{extra}]>'
178
+
179
+ def is_set(self):
180
+ """Return True if and only if the internal flag is true."""
181
+ return self._value
182
+
183
+ def set(self):
184
+ """Set the internal flag to true. All coroutines waiting for it to
185
+ become true are awakened. Coroutine that call wait() once the flag is
186
+ true will not block at all.
187
+ """
188
+ if not self._value:
189
+ self._value = True
190
+
191
+ for fut in self._waiters:
192
+ if not fut.done():
193
+ fut.set_result(True)
194
+
195
+ def clear(self):
196
+ """Reset the internal flag to false. Subsequently, coroutines calling
197
+ wait() will block until set() is called to set the internal flag
198
+ to true again."""
199
+ self._value = False
200
+
201
+ async def wait(self):
202
+ """Block until the internal flag is true.
203
+
204
+ If the internal flag is true on entry, return True
205
+ immediately. Otherwise, block until another coroutine calls
206
+ set() to set the flag to true, then return True.
207
+ """
208
+ if self._value:
209
+ return True
210
+
211
+ fut = self._get_loop().create_future()
212
+ self._waiters.append(fut)
213
+ try:
214
+ await fut
215
+ return True
216
+ finally:
217
+ self._waiters.remove(fut)
218
+
219
+
220
+ class Condition(_ContextManagerMixin, mixins._LoopBoundMixin):
221
+ """Asynchronous equivalent to threading.Condition.
222
+
223
+ This class implements condition variable objects. A condition variable
224
+ allows one or more coroutines to wait until they are notified by another
225
+ coroutine.
226
+
227
+ A new Lock object is created and used as the underlying lock.
228
+ """
229
+
230
+ def __init__(self, lock=None, *, loop=mixins._marker):
231
+ super().__init__(loop=loop)
232
+ if lock is None:
233
+ lock = Lock()
234
+
235
+ self._lock = lock
236
+ # Export the lock's locked(), acquire() and release() methods.
237
+ self.locked = lock.locked
238
+ self.acquire = lock.acquire
239
+ self.release = lock.release
240
+
241
+ self._waiters = collections.deque()
242
+
243
+ def __repr__(self):
244
+ res = super().__repr__()
245
+ extra = 'locked' if self.locked() else 'unlocked'
246
+ if self._waiters:
247
+ extra = f'{extra}, waiters:{len(self._waiters)}'
248
+ return f'<{res[1:-1]} [{extra}]>'
249
+
250
+ async def wait(self):
251
+ """Wait until notified.
252
+
253
+ If the calling coroutine has not acquired the lock when this
254
+ method is called, a RuntimeError is raised.
255
+
256
+ This method releases the underlying lock, and then blocks
257
+ until it is awakened by a notify() or notify_all() call for
258
+ the same condition variable in another coroutine. Once
259
+ awakened, it re-acquires the lock and returns True.
260
+ """
261
+ if not self.locked():
262
+ raise RuntimeError('cannot wait on un-acquired lock')
263
+
264
+ self.release()
265
+ try:
266
+ fut = self._get_loop().create_future()
267
+ self._waiters.append(fut)
268
+ try:
269
+ await fut
270
+ return True
271
+ finally:
272
+ self._waiters.remove(fut)
273
+
274
+ finally:
275
+ # Must reacquire lock even if wait is cancelled
276
+ cancelled = False
277
+ while True:
278
+ try:
279
+ await self.acquire()
280
+ break
281
+ except exceptions.CancelledError:
282
+ cancelled = True
283
+
284
+ if cancelled:
285
+ raise exceptions.CancelledError
286
+
287
+ async def wait_for(self, predicate):
288
+ """Wait until a predicate becomes true.
289
+
290
+ The predicate should be a callable which result will be
291
+ interpreted as a boolean value. The final predicate value is
292
+ the return value.
293
+ """
294
+ result = predicate()
295
+ while not result:
296
+ await self.wait()
297
+ result = predicate()
298
+ return result
299
+
300
+ def notify(self, n=1):
301
+ """By default, wake up one coroutine waiting on this condition, if any.
302
+ If the calling coroutine has not acquired the lock when this method
303
+ is called, a RuntimeError is raised.
304
+
305
+ This method wakes up at most n of the coroutines waiting for the
306
+ condition variable; it is a no-op if no coroutines are waiting.
307
+
308
+ Note: an awakened coroutine does not actually return from its
309
+ wait() call until it can reacquire the lock. Since notify() does
310
+ not release the lock, its caller should.
311
+ """
312
+ if not self.locked():
313
+ raise RuntimeError('cannot notify on un-acquired lock')
314
+
315
+ idx = 0
316
+ for fut in self._waiters:
317
+ if idx >= n:
318
+ break
319
+
320
+ if not fut.done():
321
+ idx += 1
322
+ fut.set_result(False)
323
+
324
+ def notify_all(self):
325
+ """Wake up all threads waiting on this condition. This method acts
326
+ like notify(), but wakes up all waiting threads instead of one. If the
327
+ calling thread has not acquired the lock when this method is called,
328
+ a RuntimeError is raised.
329
+ """
330
+ self.notify(len(self._waiters))
331
+
332
+
333
+ class Semaphore(_ContextManagerMixin, mixins._LoopBoundMixin):
334
+ """A Semaphore implementation.
335
+
336
+ A semaphore manages an internal counter which is decremented by each
337
+ acquire() call and incremented by each release() call. The counter
338
+ can never go below zero; when acquire() finds that it is zero, it blocks,
339
+ waiting until some other thread calls release().
340
+
341
+ Semaphores also support the context management protocol.
342
+
343
+ The optional argument gives the initial value for the internal
344
+ counter; it defaults to 1. If the value given is less than 0,
345
+ ValueError is raised.
346
+ """
347
+
348
+ def __init__(self, value=1, *, loop=mixins._marker):
349
+ super().__init__(loop=loop)
350
+ if value < 0:
351
+ raise ValueError("Semaphore initial value must be >= 0")
352
+ self._waiters = None
353
+ self._value = value
354
+
355
+ def __repr__(self):
356
+ res = super().__repr__()
357
+ extra = 'locked' if self.locked() else f'unlocked, value:{self._value}'
358
+ if self._waiters:
359
+ extra = f'{extra}, waiters:{len(self._waiters)}'
360
+ return f'<{res[1:-1]} [{extra}]>'
361
+
362
+ def locked(self):
363
+ """Returns True if semaphore cannot be acquired immediately."""
364
+ return self._value == 0 or (
365
+ any(not w.cancelled() for w in (self._waiters or ())))
366
+
367
+ async def acquire(self):
368
+ """Acquire a semaphore.
369
+
370
+ If the internal counter is larger than zero on entry,
371
+ decrement it by one and return True immediately. If it is
372
+ zero on entry, block, waiting until some other coroutine has
373
+ called release() to make it larger than 0, and then return
374
+ True.
375
+ """
376
+ if not self.locked():
377
+ self._value -= 1
378
+ return True
379
+
380
+ if self._waiters is None:
381
+ self._waiters = collections.deque()
382
+ fut = self._get_loop().create_future()
383
+ self._waiters.append(fut)
384
+
385
+ # Finally block should be called before the CancelledError
386
+ # handling as we don't want CancelledError to call
387
+ # _wake_up_first() and attempt to wake up itself.
388
+ try:
389
+ try:
390
+ await fut
391
+ finally:
392
+ self._waiters.remove(fut)
393
+ except exceptions.CancelledError:
394
+ if not fut.cancelled():
395
+ self._value += 1
396
+ self._wake_up_next()
397
+ raise
398
+
399
+ if self._value > 0:
400
+ self._wake_up_next()
401
+ return True
402
+
403
+ def release(self):
404
+ """Release a semaphore, incrementing the internal counter by one.
405
+
406
+ When it was zero on entry and another coroutine is waiting for it to
407
+ become larger than zero again, wake up that coroutine.
408
+ """
409
+ self._value += 1
410
+ self._wake_up_next()
411
+
412
+ def _wake_up_next(self):
413
+ """Wake up the first waiter that isn't done."""
414
+ if not self._waiters:
415
+ return
416
+
417
+ for fut in self._waiters:
418
+ if not fut.done():
419
+ self._value -= 1
420
+ fut.set_result(True)
421
+ return
422
+
423
+
424
+ class BoundedSemaphore(Semaphore):
425
+ """A bounded semaphore implementation.
426
+
427
+ This raises ValueError in release() if it would increase the value
428
+ above the initial value.
429
+ """
430
+
431
+ def __init__(self, value=1, *, loop=mixins._marker):
432
+ self._bound_value = value
433
+ super().__init__(value, loop=loop)
434
+
435
+ def release(self):
436
+ if self._value >= self._bound_value:
437
+ raise ValueError('BoundedSemaphore released too many times')
438
+ super().release()
evalkit_llava/lib/python3.10/asyncio/log.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ """Logging configuration."""
2
+
3
+ import logging
4
+
5
+
6
+ # Name the logger after the package.
7
+ logger = logging.getLogger(__package__)
evalkit_llava/lib/python3.10/asyncio/proactor_events.py ADDED
@@ -0,0 +1,875 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Event loop using a proactor and related classes.
2
+
3
+ A proactor is a "notify-on-completion" multiplexer. Currently a
4
+ proactor is only implemented on Windows with IOCP.
5
+ """
6
+
7
+ __all__ = 'BaseProactorEventLoop',
8
+
9
+ import io
10
+ import os
11
+ import socket
12
+ import warnings
13
+ import signal
14
+ import threading
15
+ import collections
16
+
17
+ from . import base_events
18
+ from . import constants
19
+ from . import futures
20
+ from . import exceptions
21
+ from . import protocols
22
+ from . import sslproto
23
+ from . import transports
24
+ from . import trsock
25
+ from .log import logger
26
+
27
+
28
+ def _set_socket_extra(transport, sock):
29
+ transport._extra['socket'] = trsock.TransportSocket(sock)
30
+
31
+ try:
32
+ transport._extra['sockname'] = sock.getsockname()
33
+ except socket.error:
34
+ if transport._loop.get_debug():
35
+ logger.warning(
36
+ "getsockname() failed on %r", sock, exc_info=True)
37
+
38
+ if 'peername' not in transport._extra:
39
+ try:
40
+ transport._extra['peername'] = sock.getpeername()
41
+ except socket.error:
42
+ # UDP sockets may not have a peer name
43
+ transport._extra['peername'] = None
44
+
45
+
46
+ class _ProactorBasePipeTransport(transports._FlowControlMixin,
47
+ transports.BaseTransport):
48
+ """Base class for pipe and socket transports."""
49
+
50
+ def __init__(self, loop, sock, protocol, waiter=None,
51
+ extra=None, server=None):
52
+ super().__init__(extra, loop)
53
+ self._set_extra(sock)
54
+ self._sock = sock
55
+ self.set_protocol(protocol)
56
+ self._server = server
57
+ self._buffer = None # None or bytearray.
58
+ self._read_fut = None
59
+ self._write_fut = None
60
+ self._pending_write = 0
61
+ self._conn_lost = 0
62
+ self._closing = False # Set when close() called.
63
+ self._called_connection_lost = False
64
+ self._eof_written = False
65
+ if self._server is not None:
66
+ self._server._attach()
67
+ self._loop.call_soon(self._protocol.connection_made, self)
68
+ if waiter is not None:
69
+ # only wake up the waiter when connection_made() has been called
70
+ self._loop.call_soon(futures._set_result_unless_cancelled,
71
+ waiter, None)
72
+
73
+ def __repr__(self):
74
+ info = [self.__class__.__name__]
75
+ if self._sock is None:
76
+ info.append('closed')
77
+ elif self._closing:
78
+ info.append('closing')
79
+ if self._sock is not None:
80
+ info.append(f'fd={self._sock.fileno()}')
81
+ if self._read_fut is not None:
82
+ info.append(f'read={self._read_fut!r}')
83
+ if self._write_fut is not None:
84
+ info.append(f'write={self._write_fut!r}')
85
+ if self._buffer:
86
+ info.append(f'write_bufsize={len(self._buffer)}')
87
+ if self._eof_written:
88
+ info.append('EOF written')
89
+ return '<{}>'.format(' '.join(info))
90
+
91
+ def _set_extra(self, sock):
92
+ self._extra['pipe'] = sock
93
+
94
+ def set_protocol(self, protocol):
95
+ self._protocol = protocol
96
+
97
+ def get_protocol(self):
98
+ return self._protocol
99
+
100
+ def is_closing(self):
101
+ return self._closing
102
+
103
+ def close(self):
104
+ if self._closing:
105
+ return
106
+ self._closing = True
107
+ self._conn_lost += 1
108
+ if not self._buffer and self._write_fut is None:
109
+ self._loop.call_soon(self._call_connection_lost, None)
110
+ if self._read_fut is not None:
111
+ self._read_fut.cancel()
112
+ self._read_fut = None
113
+
114
+ def __del__(self, _warn=warnings.warn):
115
+ if self._sock is not None:
116
+ _warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
117
+ self._sock.close()
118
+
119
+ def _fatal_error(self, exc, message='Fatal error on pipe transport'):
120
+ try:
121
+ if isinstance(exc, OSError):
122
+ if self._loop.get_debug():
123
+ logger.debug("%r: %s", self, message, exc_info=True)
124
+ else:
125
+ self._loop.call_exception_handler({
126
+ 'message': message,
127
+ 'exception': exc,
128
+ 'transport': self,
129
+ 'protocol': self._protocol,
130
+ })
131
+ finally:
132
+ self._force_close(exc)
133
+
134
+ def _force_close(self, exc):
135
+ if self._empty_waiter is not None and not self._empty_waiter.done():
136
+ if exc is None:
137
+ self._empty_waiter.set_result(None)
138
+ else:
139
+ self._empty_waiter.set_exception(exc)
140
+ if self._closing and self._called_connection_lost:
141
+ return
142
+ self._closing = True
143
+ self._conn_lost += 1
144
+ if self._write_fut:
145
+ self._write_fut.cancel()
146
+ self._write_fut = None
147
+ if self._read_fut:
148
+ self._read_fut.cancel()
149
+ self._read_fut = None
150
+ self._pending_write = 0
151
+ self._buffer = None
152
+ self._loop.call_soon(self._call_connection_lost, exc)
153
+
154
+ def _call_connection_lost(self, exc):
155
+ if self._called_connection_lost:
156
+ return
157
+ try:
158
+ self._protocol.connection_lost(exc)
159
+ finally:
160
+ # XXX If there is a pending overlapped read on the other
161
+ # end then it may fail with ERROR_NETNAME_DELETED if we
162
+ # just close our end. First calling shutdown() seems to
163
+ # cure it, but maybe using DisconnectEx() would be better.
164
+ if hasattr(self._sock, 'shutdown') and self._sock.fileno() != -1:
165
+ self._sock.shutdown(socket.SHUT_RDWR)
166
+ self._sock.close()
167
+ self._sock = None
168
+ server = self._server
169
+ if server is not None:
170
+ server._detach()
171
+ self._server = None
172
+ self._called_connection_lost = True
173
+
174
+ def get_write_buffer_size(self):
175
+ size = self._pending_write
176
+ if self._buffer is not None:
177
+ size += len(self._buffer)
178
+ return size
179
+
180
+
181
+ class _ProactorReadPipeTransport(_ProactorBasePipeTransport,
182
+ transports.ReadTransport):
183
+ """Transport for read pipes."""
184
+
185
+ def __init__(self, loop, sock, protocol, waiter=None,
186
+ extra=None, server=None, buffer_size=65536):
187
+ self._pending_data_length = -1
188
+ self._paused = True
189
+ super().__init__(loop, sock, protocol, waiter, extra, server)
190
+
191
+ self._data = bytearray(buffer_size)
192
+ self._loop.call_soon(self._loop_reading)
193
+ self._paused = False
194
+
195
+ def is_reading(self):
196
+ return not self._paused and not self._closing
197
+
198
+ def pause_reading(self):
199
+ if self._closing or self._paused:
200
+ return
201
+ self._paused = True
202
+
203
+ # bpo-33694: Don't cancel self._read_fut because cancelling an
204
+ # overlapped WSASend() loss silently data with the current proactor
205
+ # implementation.
206
+ #
207
+ # If CancelIoEx() fails with ERROR_NOT_FOUND, it means that WSASend()
208
+ # completed (even if HasOverlappedIoCompleted() returns 0), but
209
+ # Overlapped.cancel() currently silently ignores the ERROR_NOT_FOUND
210
+ # error. Once the overlapped is ignored, the IOCP loop will ignores the
211
+ # completion I/O event and so not read the result of the overlapped
212
+ # WSARecv().
213
+
214
+ if self._loop.get_debug():
215
+ logger.debug("%r pauses reading", self)
216
+
217
+ def resume_reading(self):
218
+ if self._closing or not self._paused:
219
+ return
220
+
221
+ self._paused = False
222
+ if self._read_fut is None:
223
+ self._loop.call_soon(self._loop_reading, None)
224
+
225
+ length = self._pending_data_length
226
+ self._pending_data_length = -1
227
+ if length > -1:
228
+ # Call the protocol methode after calling _loop_reading(),
229
+ # since the protocol can decide to pause reading again.
230
+ self._loop.call_soon(self._data_received, self._data[:length], length)
231
+
232
+ if self._loop.get_debug():
233
+ logger.debug("%r resumes reading", self)
234
+
235
+ def _eof_received(self):
236
+ if self._loop.get_debug():
237
+ logger.debug("%r received EOF", self)
238
+
239
+ try:
240
+ keep_open = self._protocol.eof_received()
241
+ except (SystemExit, KeyboardInterrupt):
242
+ raise
243
+ except BaseException as exc:
244
+ self._fatal_error(
245
+ exc, 'Fatal error: protocol.eof_received() call failed.')
246
+ return
247
+
248
+ if not keep_open:
249
+ self.close()
250
+
251
+ def _data_received(self, data, length):
252
+ if self._paused:
253
+ # Don't call any protocol method while reading is paused.
254
+ # The protocol will be called on resume_reading().
255
+ assert self._pending_data_length == -1
256
+ self._pending_data_length = length
257
+ return
258
+
259
+ if length == 0:
260
+ self._eof_received()
261
+ return
262
+
263
+ if isinstance(self._protocol, protocols.BufferedProtocol):
264
+ try:
265
+ protocols._feed_data_to_buffered_proto(self._protocol, data)
266
+ except (SystemExit, KeyboardInterrupt):
267
+ raise
268
+ except BaseException as exc:
269
+ self._fatal_error(exc,
270
+ 'Fatal error: protocol.buffer_updated() '
271
+ 'call failed.')
272
+ return
273
+ else:
274
+ self._protocol.data_received(data)
275
+
276
+ def _loop_reading(self, fut=None):
277
+ length = -1
278
+ data = None
279
+ try:
280
+ if fut is not None:
281
+ assert self._read_fut is fut or (self._read_fut is None and
282
+ self._closing)
283
+ self._read_fut = None
284
+ if fut.done():
285
+ # deliver data later in "finally" clause
286
+ length = fut.result()
287
+ if length == 0:
288
+ # we got end-of-file so no need to reschedule a new read
289
+ return
290
+
291
+ data = self._data[:length]
292
+ else:
293
+ # the future will be replaced by next proactor.recv call
294
+ fut.cancel()
295
+
296
+ if self._closing:
297
+ # since close() has been called we ignore any read data
298
+ return
299
+
300
+ # bpo-33694: buffer_updated() has currently no fast path because of
301
+ # a data loss issue caused by overlapped WSASend() cancellation.
302
+
303
+ if not self._paused:
304
+ # reschedule a new read
305
+ self._read_fut = self._loop._proactor.recv_into(self._sock, self._data)
306
+ except ConnectionAbortedError as exc:
307
+ if not self._closing:
308
+ self._fatal_error(exc, 'Fatal read error on pipe transport')
309
+ elif self._loop.get_debug():
310
+ logger.debug("Read error on pipe transport while closing",
311
+ exc_info=True)
312
+ except ConnectionResetError as exc:
313
+ self._force_close(exc)
314
+ except OSError as exc:
315
+ self._fatal_error(exc, 'Fatal read error on pipe transport')
316
+ except exceptions.CancelledError:
317
+ if not self._closing:
318
+ raise
319
+ else:
320
+ if not self._paused:
321
+ self._read_fut.add_done_callback(self._loop_reading)
322
+ finally:
323
+ if length > -1:
324
+ self._data_received(data, length)
325
+
326
+
327
+ class _ProactorBaseWritePipeTransport(_ProactorBasePipeTransport,
328
+ transports.WriteTransport):
329
+ """Transport for write pipes."""
330
+
331
+ _start_tls_compatible = True
332
+
333
+ def __init__(self, *args, **kw):
334
+ super().__init__(*args, **kw)
335
+ self._empty_waiter = None
336
+
337
+ def write(self, data):
338
+ if not isinstance(data, (bytes, bytearray, memoryview)):
339
+ raise TypeError(
340
+ f"data argument must be a bytes-like object, "
341
+ f"not {type(data).__name__}")
342
+ if self._eof_written:
343
+ raise RuntimeError('write_eof() already called')
344
+ if self._empty_waiter is not None:
345
+ raise RuntimeError('unable to write; sendfile is in progress')
346
+
347
+ if not data:
348
+ return
349
+
350
+ if self._conn_lost:
351
+ if self._conn_lost >= constants.LOG_THRESHOLD_FOR_CONNLOST_WRITES:
352
+ logger.warning('socket.send() raised exception.')
353
+ self._conn_lost += 1
354
+ return
355
+
356
+ # Observable states:
357
+ # 1. IDLE: _write_fut and _buffer both None
358
+ # 2. WRITING: _write_fut set; _buffer None
359
+ # 3. BACKED UP: _write_fut set; _buffer a bytearray
360
+ # We always copy the data, so the caller can't modify it
361
+ # while we're still waiting for the I/O to happen.
362
+ if self._write_fut is None: # IDLE -> WRITING
363
+ assert self._buffer is None
364
+ # Pass a copy, except if it's already immutable.
365
+ self._loop_writing(data=bytes(data))
366
+ elif not self._buffer: # WRITING -> BACKED UP
367
+ # Make a mutable copy which we can extend.
368
+ self._buffer = bytearray(data)
369
+ self._maybe_pause_protocol()
370
+ else: # BACKED UP
371
+ # Append to buffer (also copies).
372
+ self._buffer.extend(data)
373
+ self._maybe_pause_protocol()
374
+
375
+ def _loop_writing(self, f=None, data=None):
376
+ try:
377
+ if f is not None and self._write_fut is None and self._closing:
378
+ # XXX most likely self._force_close() has been called, and
379
+ # it has set self._write_fut to None.
380
+ return
381
+ assert f is self._write_fut
382
+ self._write_fut = None
383
+ self._pending_write = 0
384
+ if f:
385
+ f.result()
386
+ if data is None:
387
+ data = self._buffer
388
+ self._buffer = None
389
+ if not data:
390
+ if self._closing:
391
+ self._loop.call_soon(self._call_connection_lost, None)
392
+ if self._eof_written:
393
+ self._sock.shutdown(socket.SHUT_WR)
394
+ # Now that we've reduced the buffer size, tell the
395
+ # protocol to resume writing if it was paused. Note that
396
+ # we do this last since the callback is called immediately
397
+ # and it may add more data to the buffer (even causing the
398
+ # protocol to be paused again).
399
+ self._maybe_resume_protocol()
400
+ else:
401
+ self._write_fut = self._loop._proactor.send(self._sock, data)
402
+ if not self._write_fut.done():
403
+ assert self._pending_write == 0
404
+ self._pending_write = len(data)
405
+ self._write_fut.add_done_callback(self._loop_writing)
406
+ self._maybe_pause_protocol()
407
+ else:
408
+ self._write_fut.add_done_callback(self._loop_writing)
409
+ if self._empty_waiter is not None and self._write_fut is None:
410
+ self._empty_waiter.set_result(None)
411
+ except ConnectionResetError as exc:
412
+ self._force_close(exc)
413
+ except OSError as exc:
414
+ self._fatal_error(exc, 'Fatal write error on pipe transport')
415
+
416
+ def can_write_eof(self):
417
+ return True
418
+
419
+ def write_eof(self):
420
+ self.close()
421
+
422
+ def abort(self):
423
+ self._force_close(None)
424
+
425
+ def _make_empty_waiter(self):
426
+ if self._empty_waiter is not None:
427
+ raise RuntimeError("Empty waiter is already set")
428
+ self._empty_waiter = self._loop.create_future()
429
+ if self._write_fut is None:
430
+ self._empty_waiter.set_result(None)
431
+ return self._empty_waiter
432
+
433
+ def _reset_empty_waiter(self):
434
+ self._empty_waiter = None
435
+
436
+
437
+ class _ProactorWritePipeTransport(_ProactorBaseWritePipeTransport):
438
+ def __init__(self, *args, **kw):
439
+ super().__init__(*args, **kw)
440
+ self._read_fut = self._loop._proactor.recv(self._sock, 16)
441
+ self._read_fut.add_done_callback(self._pipe_closed)
442
+
443
+ def _pipe_closed(self, fut):
444
+ if fut.cancelled():
445
+ # the transport has been closed
446
+ return
447
+ assert fut.result() == b''
448
+ if self._closing:
449
+ assert self._read_fut is None
450
+ return
451
+ assert fut is self._read_fut, (fut, self._read_fut)
452
+ self._read_fut = None
453
+ if self._write_fut is not None:
454
+ self._force_close(BrokenPipeError())
455
+ else:
456
+ self.close()
457
+
458
+
459
+ class _ProactorDatagramTransport(_ProactorBasePipeTransport,
460
+ transports.DatagramTransport):
461
+ max_size = 256 * 1024
462
+ def __init__(self, loop, sock, protocol, address=None,
463
+ waiter=None, extra=None):
464
+ self._address = address
465
+ self._empty_waiter = None
466
+ # We don't need to call _protocol.connection_made() since our base
467
+ # constructor does it for us.
468
+ super().__init__(loop, sock, protocol, waiter=waiter, extra=extra)
469
+
470
+ # The base constructor sets _buffer = None, so we set it here
471
+ self._buffer = collections.deque()
472
+ self._loop.call_soon(self._loop_reading)
473
+
474
+ def _set_extra(self, sock):
475
+ _set_socket_extra(self, sock)
476
+
477
+ def get_write_buffer_size(self):
478
+ return sum(len(data) for data, _ in self._buffer)
479
+
480
+ def abort(self):
481
+ self._force_close(None)
482
+
483
+ def sendto(self, data, addr=None):
484
+ if not isinstance(data, (bytes, bytearray, memoryview)):
485
+ raise TypeError('data argument must be bytes-like object (%r)',
486
+ type(data))
487
+
488
+ if not data:
489
+ return
490
+
491
+ if self._address is not None and addr not in (None, self._address):
492
+ raise ValueError(
493
+ f'Invalid address: must be None or {self._address}')
494
+
495
+ if self._conn_lost and self._address:
496
+ if self._conn_lost >= constants.LOG_THRESHOLD_FOR_CONNLOST_WRITES:
497
+ logger.warning('socket.sendto() raised exception.')
498
+ self._conn_lost += 1
499
+ return
500
+
501
+ # Ensure that what we buffer is immutable.
502
+ self._buffer.append((bytes(data), addr))
503
+
504
+ if self._write_fut is None:
505
+ # No current write operations are active, kick one off
506
+ self._loop_writing()
507
+ # else: A write operation is already kicked off
508
+
509
+ self._maybe_pause_protocol()
510
+
511
+ def _loop_writing(self, fut=None):
512
+ try:
513
+ if self._conn_lost:
514
+ return
515
+
516
+ assert fut is self._write_fut
517
+ self._write_fut = None
518
+ if fut:
519
+ # We are in a _loop_writing() done callback, get the result
520
+ fut.result()
521
+
522
+ if not self._buffer or (self._conn_lost and self._address):
523
+ # The connection has been closed
524
+ if self._closing:
525
+ self._loop.call_soon(self._call_connection_lost, None)
526
+ return
527
+
528
+ data, addr = self._buffer.popleft()
529
+ if self._address is not None:
530
+ self._write_fut = self._loop._proactor.send(self._sock,
531
+ data)
532
+ else:
533
+ self._write_fut = self._loop._proactor.sendto(self._sock,
534
+ data,
535
+ addr=addr)
536
+ except OSError as exc:
537
+ self._protocol.error_received(exc)
538
+ except Exception as exc:
539
+ self._fatal_error(exc, 'Fatal write error on datagram transport')
540
+ else:
541
+ self._write_fut.add_done_callback(self._loop_writing)
542
+ self._maybe_resume_protocol()
543
+
544
+ def _loop_reading(self, fut=None):
545
+ data = None
546
+ try:
547
+ if self._conn_lost:
548
+ return
549
+
550
+ assert self._read_fut is fut or (self._read_fut is None and
551
+ self._closing)
552
+
553
+ self._read_fut = None
554
+ if fut is not None:
555
+ res = fut.result()
556
+
557
+ if self._closing:
558
+ # since close() has been called we ignore any read data
559
+ data = None
560
+ return
561
+
562
+ if self._address is not None:
563
+ data, addr = res, self._address
564
+ else:
565
+ data, addr = res
566
+
567
+ if self._conn_lost:
568
+ return
569
+ if self._address is not None:
570
+ self._read_fut = self._loop._proactor.recv(self._sock,
571
+ self.max_size)
572
+ else:
573
+ self._read_fut = self._loop._proactor.recvfrom(self._sock,
574
+ self.max_size)
575
+ except OSError as exc:
576
+ self._protocol.error_received(exc)
577
+ except exceptions.CancelledError:
578
+ if not self._closing:
579
+ raise
580
+ else:
581
+ if self._read_fut is not None:
582
+ self._read_fut.add_done_callback(self._loop_reading)
583
+ finally:
584
+ if data:
585
+ self._protocol.datagram_received(data, addr)
586
+
587
+
588
+ class _ProactorDuplexPipeTransport(_ProactorReadPipeTransport,
589
+ _ProactorBaseWritePipeTransport,
590
+ transports.Transport):
591
+ """Transport for duplex pipes."""
592
+
593
+ def can_write_eof(self):
594
+ return False
595
+
596
+ def write_eof(self):
597
+ raise NotImplementedError
598
+
599
+
600
+ class _ProactorSocketTransport(_ProactorReadPipeTransport,
601
+ _ProactorBaseWritePipeTransport,
602
+ transports.Transport):
603
+ """Transport for connected sockets."""
604
+
605
+ _sendfile_compatible = constants._SendfileMode.TRY_NATIVE
606
+
607
+ def __init__(self, loop, sock, protocol, waiter=None,
608
+ extra=None, server=None):
609
+ super().__init__(loop, sock, protocol, waiter, extra, server)
610
+ base_events._set_nodelay(sock)
611
+
612
+ def _set_extra(self, sock):
613
+ _set_socket_extra(self, sock)
614
+
615
+ def can_write_eof(self):
616
+ return True
617
+
618
+ def write_eof(self):
619
+ if self._closing or self._eof_written:
620
+ return
621
+ self._eof_written = True
622
+ if self._write_fut is None:
623
+ self._sock.shutdown(socket.SHUT_WR)
624
+
625
+
626
+ class BaseProactorEventLoop(base_events.BaseEventLoop):
627
+
628
+ def __init__(self, proactor):
629
+ super().__init__()
630
+ logger.debug('Using proactor: %s', proactor.__class__.__name__)
631
+ self._proactor = proactor
632
+ self._selector = proactor # convenient alias
633
+ self._self_reading_future = None
634
+ self._accept_futures = {} # socket file descriptor => Future
635
+ proactor.set_loop(self)
636
+ self._make_self_pipe()
637
+ if threading.current_thread() is threading.main_thread():
638
+ # wakeup fd can only be installed to a file descriptor from the main thread
639
+ signal.set_wakeup_fd(self._csock.fileno())
640
+
641
+ def _make_socket_transport(self, sock, protocol, waiter=None,
642
+ extra=None, server=None):
643
+ return _ProactorSocketTransport(self, sock, protocol, waiter,
644
+ extra, server)
645
+
646
+ def _make_ssl_transport(
647
+ self, rawsock, protocol, sslcontext, waiter=None,
648
+ *, server_side=False, server_hostname=None,
649
+ extra=None, server=None,
650
+ ssl_handshake_timeout=None):
651
+ ssl_protocol = sslproto.SSLProtocol(
652
+ self, protocol, sslcontext, waiter,
653
+ server_side, server_hostname,
654
+ ssl_handshake_timeout=ssl_handshake_timeout)
655
+ _ProactorSocketTransport(self, rawsock, ssl_protocol,
656
+ extra=extra, server=server)
657
+ return ssl_protocol._app_transport
658
+
659
+ def _make_datagram_transport(self, sock, protocol,
660
+ address=None, waiter=None, extra=None):
661
+ return _ProactorDatagramTransport(self, sock, protocol, address,
662
+ waiter, extra)
663
+
664
+ def _make_duplex_pipe_transport(self, sock, protocol, waiter=None,
665
+ extra=None):
666
+ return _ProactorDuplexPipeTransport(self,
667
+ sock, protocol, waiter, extra)
668
+
669
+ def _make_read_pipe_transport(self, sock, protocol, waiter=None,
670
+ extra=None):
671
+ return _ProactorReadPipeTransport(self, sock, protocol, waiter, extra)
672
+
673
+ def _make_write_pipe_transport(self, sock, protocol, waiter=None,
674
+ extra=None):
675
+ # We want connection_lost() to be called when other end closes
676
+ return _ProactorWritePipeTransport(self,
677
+ sock, protocol, waiter, extra)
678
+
679
+ def close(self):
680
+ if self.is_running():
681
+ raise RuntimeError("Cannot close a running event loop")
682
+ if self.is_closed():
683
+ return
684
+
685
+ if threading.current_thread() is threading.main_thread():
686
+ signal.set_wakeup_fd(-1)
687
+ # Call these methods before closing the event loop (before calling
688
+ # BaseEventLoop.close), because they can schedule callbacks with
689
+ # call_soon(), which is forbidden when the event loop is closed.
690
+ self._stop_accept_futures()
691
+ self._close_self_pipe()
692
+ self._proactor.close()
693
+ self._proactor = None
694
+ self._selector = None
695
+
696
+ # Close the event loop
697
+ super().close()
698
+
699
+ async def sock_recv(self, sock, n):
700
+ return await self._proactor.recv(sock, n)
701
+
702
+ async def sock_recv_into(self, sock, buf):
703
+ return await self._proactor.recv_into(sock, buf)
704
+
705
+ async def sock_sendall(self, sock, data):
706
+ return await self._proactor.send(sock, data)
707
+
708
+ async def sock_connect(self, sock, address):
709
+ return await self._proactor.connect(sock, address)
710
+
711
+ async def sock_accept(self, sock):
712
+ return await self._proactor.accept(sock)
713
+
714
+ async def _sock_sendfile_native(self, sock, file, offset, count):
715
+ try:
716
+ fileno = file.fileno()
717
+ except (AttributeError, io.UnsupportedOperation) as err:
718
+ raise exceptions.SendfileNotAvailableError("not a regular file")
719
+ try:
720
+ fsize = os.fstat(fileno).st_size
721
+ except OSError:
722
+ raise exceptions.SendfileNotAvailableError("not a regular file")
723
+ blocksize = count if count else fsize
724
+ if not blocksize:
725
+ return 0 # empty file
726
+
727
+ blocksize = min(blocksize, 0xffff_ffff)
728
+ end_pos = min(offset + count, fsize) if count else fsize
729
+ offset = min(offset, fsize)
730
+ total_sent = 0
731
+ try:
732
+ while True:
733
+ blocksize = min(end_pos - offset, blocksize)
734
+ if blocksize <= 0:
735
+ return total_sent
736
+ await self._proactor.sendfile(sock, file, offset, blocksize)
737
+ offset += blocksize
738
+ total_sent += blocksize
739
+ finally:
740
+ if total_sent > 0:
741
+ file.seek(offset)
742
+
743
+ async def _sendfile_native(self, transp, file, offset, count):
744
+ resume_reading = transp.is_reading()
745
+ transp.pause_reading()
746
+ await transp._make_empty_waiter()
747
+ try:
748
+ return await self.sock_sendfile(transp._sock, file, offset, count,
749
+ fallback=False)
750
+ finally:
751
+ transp._reset_empty_waiter()
752
+ if resume_reading:
753
+ transp.resume_reading()
754
+
755
+ def _close_self_pipe(self):
756
+ if self._self_reading_future is not None:
757
+ self._self_reading_future.cancel()
758
+ self._self_reading_future = None
759
+ self._ssock.close()
760
+ self._ssock = None
761
+ self._csock.close()
762
+ self._csock = None
763
+ self._internal_fds -= 1
764
+
765
+ def _make_self_pipe(self):
766
+ # A self-socket, really. :-)
767
+ self._ssock, self._csock = socket.socketpair()
768
+ self._ssock.setblocking(False)
769
+ self._csock.setblocking(False)
770
+ self._internal_fds += 1
771
+
772
+ def _loop_self_reading(self, f=None):
773
+ try:
774
+ if f is not None:
775
+ f.result() # may raise
776
+ if self._self_reading_future is not f:
777
+ # When we scheduled this Future, we assigned it to
778
+ # _self_reading_future. If it's not there now, something has
779
+ # tried to cancel the loop while this callback was still in the
780
+ # queue (see windows_events.ProactorEventLoop.run_forever). In
781
+ # that case stop here instead of continuing to schedule a new
782
+ # iteration.
783
+ return
784
+ f = self._proactor.recv(self._ssock, 4096)
785
+ except exceptions.CancelledError:
786
+ # _close_self_pipe() has been called, stop waiting for data
787
+ return
788
+ except (SystemExit, KeyboardInterrupt):
789
+ raise
790
+ except BaseException as exc:
791
+ self.call_exception_handler({
792
+ 'message': 'Error on reading from the event loop self pipe',
793
+ 'exception': exc,
794
+ 'loop': self,
795
+ })
796
+ else:
797
+ self._self_reading_future = f
798
+ f.add_done_callback(self._loop_self_reading)
799
+
800
+ def _write_to_self(self):
801
+ # This may be called from a different thread, possibly after
802
+ # _close_self_pipe() has been called or even while it is
803
+ # running. Guard for self._csock being None or closed. When
804
+ # a socket is closed, send() raises OSError (with errno set to
805
+ # EBADF, but let's not rely on the exact error code).
806
+ csock = self._csock
807
+ if csock is None:
808
+ return
809
+
810
+ try:
811
+ csock.send(b'\0')
812
+ except OSError:
813
+ if self._debug:
814
+ logger.debug("Fail to write a null byte into the "
815
+ "self-pipe socket",
816
+ exc_info=True)
817
+
818
+ def _start_serving(self, protocol_factory, sock,
819
+ sslcontext=None, server=None, backlog=100,
820
+ ssl_handshake_timeout=None):
821
+
822
+ def loop(f=None):
823
+ try:
824
+ if f is not None:
825
+ conn, addr = f.result()
826
+ if self._debug:
827
+ logger.debug("%r got a new connection from %r: %r",
828
+ server, addr, conn)
829
+ protocol = protocol_factory()
830
+ if sslcontext is not None:
831
+ self._make_ssl_transport(
832
+ conn, protocol, sslcontext, server_side=True,
833
+ extra={'peername': addr}, server=server,
834
+ ssl_handshake_timeout=ssl_handshake_timeout)
835
+ else:
836
+ self._make_socket_transport(
837
+ conn, protocol,
838
+ extra={'peername': addr}, server=server)
839
+ if self.is_closed():
840
+ return
841
+ f = self._proactor.accept(sock)
842
+ except OSError as exc:
843
+ if sock.fileno() != -1:
844
+ self.call_exception_handler({
845
+ 'message': 'Accept failed on a socket',
846
+ 'exception': exc,
847
+ 'socket': trsock.TransportSocket(sock),
848
+ })
849
+ sock.close()
850
+ elif self._debug:
851
+ logger.debug("Accept failed on socket %r",
852
+ sock, exc_info=True)
853
+ except exceptions.CancelledError:
854
+ sock.close()
855
+ else:
856
+ self._accept_futures[sock.fileno()] = f
857
+ f.add_done_callback(loop)
858
+
859
+ self.call_soon(loop)
860
+
861
+ def _process_events(self, event_list):
862
+ # Events are processed in the IocpProactor._poll() method
863
+ pass
864
+
865
+ def _stop_accept_futures(self):
866
+ for future in self._accept_futures.values():
867
+ future.cancel()
868
+ self._accept_futures.clear()
869
+
870
+ def _stop_serving(self, sock):
871
+ future = self._accept_futures.pop(sock.fileno(), None)
872
+ if future:
873
+ future.cancel()
874
+ self._proactor._stop_serving(sock)
875
+ sock.close()
evalkit_llava/lib/python3.10/asyncio/queues.py ADDED
@@ -0,0 +1,245 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ __all__ = ('Queue', 'PriorityQueue', 'LifoQueue', 'QueueFull', 'QueueEmpty')
2
+
3
+ import collections
4
+ import heapq
5
+ from types import GenericAlias
6
+
7
+ from . import locks
8
+ from . import mixins
9
+
10
+
11
+ class QueueEmpty(Exception):
12
+ """Raised when Queue.get_nowait() is called on an empty Queue."""
13
+ pass
14
+
15
+
16
+ class QueueFull(Exception):
17
+ """Raised when the Queue.put_nowait() method is called on a full Queue."""
18
+ pass
19
+
20
+
21
+ class Queue(mixins._LoopBoundMixin):
22
+ """A queue, useful for coordinating producer and consumer coroutines.
23
+
24
+ If maxsize is less than or equal to zero, the queue size is infinite. If it
25
+ is an integer greater than 0, then "await put()" will block when the
26
+ queue reaches maxsize, until an item is removed by get().
27
+
28
+ Unlike the standard library Queue, you can reliably know this Queue's size
29
+ with qsize(), since your single-threaded asyncio application won't be
30
+ interrupted between calling qsize() and doing an operation on the Queue.
31
+ """
32
+
33
+ def __init__(self, maxsize=0, *, loop=mixins._marker):
34
+ super().__init__(loop=loop)
35
+ self._maxsize = maxsize
36
+
37
+ # Futures.
38
+ self._getters = collections.deque()
39
+ # Futures.
40
+ self._putters = collections.deque()
41
+ self._unfinished_tasks = 0
42
+ self._finished = locks.Event()
43
+ self._finished.set()
44
+ self._init(maxsize)
45
+
46
+ # These three are overridable in subclasses.
47
+
48
+ def _init(self, maxsize):
49
+ self._queue = collections.deque()
50
+
51
+ def _get(self):
52
+ return self._queue.popleft()
53
+
54
+ def _put(self, item):
55
+ self._queue.append(item)
56
+
57
+ # End of the overridable methods.
58
+
59
+ def _wakeup_next(self, waiters):
60
+ # Wake up the next waiter (if any) that isn't cancelled.
61
+ while waiters:
62
+ waiter = waiters.popleft()
63
+ if not waiter.done():
64
+ waiter.set_result(None)
65
+ break
66
+
67
+ def __repr__(self):
68
+ return f'<{type(self).__name__} at {id(self):#x} {self._format()}>'
69
+
70
+ def __str__(self):
71
+ return f'<{type(self).__name__} {self._format()}>'
72
+
73
+ __class_getitem__ = classmethod(GenericAlias)
74
+
75
+ def _format(self):
76
+ result = f'maxsize={self._maxsize!r}'
77
+ if getattr(self, '_queue', None):
78
+ result += f' _queue={list(self._queue)!r}'
79
+ if self._getters:
80
+ result += f' _getters[{len(self._getters)}]'
81
+ if self._putters:
82
+ result += f' _putters[{len(self._putters)}]'
83
+ if self._unfinished_tasks:
84
+ result += f' tasks={self._unfinished_tasks}'
85
+ return result
86
+
87
+ def qsize(self):
88
+ """Number of items in the queue."""
89
+ return len(self._queue)
90
+
91
+ @property
92
+ def maxsize(self):
93
+ """Number of items allowed in the queue."""
94
+ return self._maxsize
95
+
96
+ def empty(self):
97
+ """Return True if the queue is empty, False otherwise."""
98
+ return not self._queue
99
+
100
+ def full(self):
101
+ """Return True if there are maxsize items in the queue.
102
+
103
+ Note: if the Queue was initialized with maxsize=0 (the default),
104
+ then full() is never True.
105
+ """
106
+ if self._maxsize <= 0:
107
+ return False
108
+ else:
109
+ return self.qsize() >= self._maxsize
110
+
111
+ async def put(self, item):
112
+ """Put an item into the queue.
113
+
114
+ Put an item into the queue. If the queue is full, wait until a free
115
+ slot is available before adding item.
116
+ """
117
+ while self.full():
118
+ putter = self._get_loop().create_future()
119
+ self._putters.append(putter)
120
+ try:
121
+ await putter
122
+ except:
123
+ putter.cancel() # Just in case putter is not done yet.
124
+ try:
125
+ # Clean self._putters from canceled putters.
126
+ self._putters.remove(putter)
127
+ except ValueError:
128
+ # The putter could be removed from self._putters by a
129
+ # previous get_nowait call.
130
+ pass
131
+ if not self.full() and not putter.cancelled():
132
+ # We were woken up by get_nowait(), but can't take
133
+ # the call. Wake up the next in line.
134
+ self._wakeup_next(self._putters)
135
+ raise
136
+ return self.put_nowait(item)
137
+
138
+ def put_nowait(self, item):
139
+ """Put an item into the queue without blocking.
140
+
141
+ If no free slot is immediately available, raise QueueFull.
142
+ """
143
+ if self.full():
144
+ raise QueueFull
145
+ self._put(item)
146
+ self._unfinished_tasks += 1
147
+ self._finished.clear()
148
+ self._wakeup_next(self._getters)
149
+
150
+ async def get(self):
151
+ """Remove and return an item from the queue.
152
+
153
+ If queue is empty, wait until an item is available.
154
+ """
155
+ while self.empty():
156
+ getter = self._get_loop().create_future()
157
+ self._getters.append(getter)
158
+ try:
159
+ await getter
160
+ except:
161
+ getter.cancel() # Just in case getter is not done yet.
162
+ try:
163
+ # Clean self._getters from canceled getters.
164
+ self._getters.remove(getter)
165
+ except ValueError:
166
+ # The getter could be removed from self._getters by a
167
+ # previous put_nowait call.
168
+ pass
169
+ if not self.empty() and not getter.cancelled():
170
+ # We were woken up by put_nowait(), but can't take
171
+ # the call. Wake up the next in line.
172
+ self._wakeup_next(self._getters)
173
+ raise
174
+ return self.get_nowait()
175
+
176
+ def get_nowait(self):
177
+ """Remove and return an item from the queue.
178
+
179
+ Return an item if one is immediately available, else raise QueueEmpty.
180
+ """
181
+ if self.empty():
182
+ raise QueueEmpty
183
+ item = self._get()
184
+ self._wakeup_next(self._putters)
185
+ return item
186
+
187
+ def task_done(self):
188
+ """Indicate that a formerly enqueued task is complete.
189
+
190
+ Used by queue consumers. For each get() used to fetch a task,
191
+ a subsequent call to task_done() tells the queue that the processing
192
+ on the task is complete.
193
+
194
+ If a join() is currently blocking, it will resume when all items have
195
+ been processed (meaning that a task_done() call was received for every
196
+ item that had been put() into the queue).
197
+
198
+ Raises ValueError if called more times than there were items placed in
199
+ the queue.
200
+ """
201
+ if self._unfinished_tasks <= 0:
202
+ raise ValueError('task_done() called too many times')
203
+ self._unfinished_tasks -= 1
204
+ if self._unfinished_tasks == 0:
205
+ self._finished.set()
206
+
207
+ async def join(self):
208
+ """Block until all items in the queue have been gotten and processed.
209
+
210
+ The count of unfinished tasks goes up whenever an item is added to the
211
+ queue. The count goes down whenever a consumer calls task_done() to
212
+ indicate that the item was retrieved and all work on it is complete.
213
+ When the count of unfinished tasks drops to zero, join() unblocks.
214
+ """
215
+ if self._unfinished_tasks > 0:
216
+ await self._finished.wait()
217
+
218
+
219
+ class PriorityQueue(Queue):
220
+ """A subclass of Queue; retrieves entries in priority order (lowest first).
221
+
222
+ Entries are typically tuples of the form: (priority number, data).
223
+ """
224
+
225
+ def _init(self, maxsize):
226
+ self._queue = []
227
+
228
+ def _put(self, item, heappush=heapq.heappush):
229
+ heappush(self._queue, item)
230
+
231
+ def _get(self, heappop=heapq.heappop):
232
+ return heappop(self._queue)
233
+
234
+
235
+ class LifoQueue(Queue):
236
+ """A subclass of Queue that retrieves most recently added entries first."""
237
+
238
+ def _init(self, maxsize):
239
+ self._queue = []
240
+
241
+ def _put(self, item):
242
+ self._queue.append(item)
243
+
244
+ def _get(self):
245
+ return self._queue.pop()
evalkit_llava/lib/python3.10/asyncio/sslproto.py ADDED
@@ -0,0 +1,739 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import collections
2
+ import warnings
3
+ try:
4
+ import ssl
5
+ except ImportError: # pragma: no cover
6
+ ssl = None
7
+
8
+ from . import constants
9
+ from . import protocols
10
+ from . import transports
11
+ from .log import logger
12
+
13
+
14
+ def _create_transport_context(server_side, server_hostname):
15
+ if server_side:
16
+ raise ValueError('Server side SSL needs a valid SSLContext')
17
+
18
+ # Client side may pass ssl=True to use a default
19
+ # context; in that case the sslcontext passed is None.
20
+ # The default is secure for client connections.
21
+ # Python 3.4+: use up-to-date strong settings.
22
+ sslcontext = ssl.create_default_context()
23
+ if not server_hostname:
24
+ sslcontext.check_hostname = False
25
+ return sslcontext
26
+
27
+
28
+ # States of an _SSLPipe.
29
+ _UNWRAPPED = "UNWRAPPED"
30
+ _DO_HANDSHAKE = "DO_HANDSHAKE"
31
+ _WRAPPED = "WRAPPED"
32
+ _SHUTDOWN = "SHUTDOWN"
33
+
34
+
35
+ class _SSLPipe(object):
36
+ """An SSL "Pipe".
37
+
38
+ An SSL pipe allows you to communicate with an SSL/TLS protocol instance
39
+ through memory buffers. It can be used to implement a security layer for an
40
+ existing connection where you don't have access to the connection's file
41
+ descriptor, or for some reason you don't want to use it.
42
+
43
+ An SSL pipe can be in "wrapped" and "unwrapped" mode. In unwrapped mode,
44
+ data is passed through untransformed. In wrapped mode, application level
45
+ data is encrypted to SSL record level data and vice versa. The SSL record
46
+ level is the lowest level in the SSL protocol suite and is what travels
47
+ as-is over the wire.
48
+
49
+ An SslPipe initially is in "unwrapped" mode. To start SSL, call
50
+ do_handshake(). To shutdown SSL again, call unwrap().
51
+ """
52
+
53
+ max_size = 256 * 1024 # Buffer size passed to read()
54
+
55
+ def __init__(self, context, server_side, server_hostname=None):
56
+ """
57
+ The *context* argument specifies the ssl.SSLContext to use.
58
+
59
+ The *server_side* argument indicates whether this is a server side or
60
+ client side transport.
61
+
62
+ The optional *server_hostname* argument can be used to specify the
63
+ hostname you are connecting to. You may only specify this parameter if
64
+ the _ssl module supports Server Name Indication (SNI).
65
+ """
66
+ self._context = context
67
+ self._server_side = server_side
68
+ self._server_hostname = server_hostname
69
+ self._state = _UNWRAPPED
70
+ self._incoming = ssl.MemoryBIO()
71
+ self._outgoing = ssl.MemoryBIO()
72
+ self._sslobj = None
73
+ self._need_ssldata = False
74
+ self._handshake_cb = None
75
+ self._shutdown_cb = None
76
+
77
+ @property
78
+ def context(self):
79
+ """The SSL context passed to the constructor."""
80
+ return self._context
81
+
82
+ @property
83
+ def ssl_object(self):
84
+ """The internal ssl.SSLObject instance.
85
+
86
+ Return None if the pipe is not wrapped.
87
+ """
88
+ return self._sslobj
89
+
90
+ @property
91
+ def need_ssldata(self):
92
+ """Whether more record level data is needed to complete a handshake
93
+ that is currently in progress."""
94
+ return self._need_ssldata
95
+
96
+ @property
97
+ def wrapped(self):
98
+ """
99
+ Whether a security layer is currently in effect.
100
+
101
+ Return False during handshake.
102
+ """
103
+ return self._state == _WRAPPED
104
+
105
+ def do_handshake(self, callback=None):
106
+ """Start the SSL handshake.
107
+
108
+ Return a list of ssldata. A ssldata element is a list of buffers
109
+
110
+ The optional *callback* argument can be used to install a callback that
111
+ will be called when the handshake is complete. The callback will be
112
+ called with None if successful, else an exception instance.
113
+ """
114
+ if self._state != _UNWRAPPED:
115
+ raise RuntimeError('handshake in progress or completed')
116
+ self._sslobj = self._context.wrap_bio(
117
+ self._incoming, self._outgoing,
118
+ server_side=self._server_side,
119
+ server_hostname=self._server_hostname)
120
+ self._state = _DO_HANDSHAKE
121
+ self._handshake_cb = callback
122
+ ssldata, appdata = self.feed_ssldata(b'', only_handshake=True)
123
+ assert len(appdata) == 0
124
+ return ssldata
125
+
126
+ def shutdown(self, callback=None):
127
+ """Start the SSL shutdown sequence.
128
+
129
+ Return a list of ssldata. A ssldata element is a list of buffers
130
+
131
+ The optional *callback* argument can be used to install a callback that
132
+ will be called when the shutdown is complete. The callback will be
133
+ called without arguments.
134
+ """
135
+ if self._state == _UNWRAPPED:
136
+ raise RuntimeError('no security layer present')
137
+ if self._state == _SHUTDOWN:
138
+ raise RuntimeError('shutdown in progress')
139
+ assert self._state in (_WRAPPED, _DO_HANDSHAKE)
140
+ self._state = _SHUTDOWN
141
+ self._shutdown_cb = callback
142
+ ssldata, appdata = self.feed_ssldata(b'')
143
+ assert appdata == [] or appdata == [b'']
144
+ return ssldata
145
+
146
+ def feed_eof(self):
147
+ """Send a potentially "ragged" EOF.
148
+
149
+ This method will raise an SSL_ERROR_EOF exception if the EOF is
150
+ unexpected.
151
+ """
152
+ self._incoming.write_eof()
153
+ ssldata, appdata = self.feed_ssldata(b'')
154
+ assert appdata == [] or appdata == [b'']
155
+
156
+ def feed_ssldata(self, data, only_handshake=False):
157
+ """Feed SSL record level data into the pipe.
158
+
159
+ The data must be a bytes instance. It is OK to send an empty bytes
160
+ instance. This can be used to get ssldata for a handshake initiated by
161
+ this endpoint.
162
+
163
+ Return a (ssldata, appdata) tuple. The ssldata element is a list of
164
+ buffers containing SSL data that needs to be sent to the remote SSL.
165
+
166
+ The appdata element is a list of buffers containing plaintext data that
167
+ needs to be forwarded to the application. The appdata list may contain
168
+ an empty buffer indicating an SSL "close_notify" alert. This alert must
169
+ be acknowledged by calling shutdown().
170
+ """
171
+ if self._state == _UNWRAPPED:
172
+ # If unwrapped, pass plaintext data straight through.
173
+ if data:
174
+ appdata = [data]
175
+ else:
176
+ appdata = []
177
+ return ([], appdata)
178
+
179
+ self._need_ssldata = False
180
+ if data:
181
+ self._incoming.write(data)
182
+
183
+ ssldata = []
184
+ appdata = []
185
+ try:
186
+ if self._state == _DO_HANDSHAKE:
187
+ # Call do_handshake() until it doesn't raise anymore.
188
+ self._sslobj.do_handshake()
189
+ self._state = _WRAPPED
190
+ if self._handshake_cb:
191
+ self._handshake_cb(None)
192
+ if only_handshake:
193
+ return (ssldata, appdata)
194
+ # Handshake done: execute the wrapped block
195
+
196
+ if self._state == _WRAPPED:
197
+ # Main state: read data from SSL until close_notify
198
+ while True:
199
+ chunk = self._sslobj.read(self.max_size)
200
+ appdata.append(chunk)
201
+ if not chunk: # close_notify
202
+ break
203
+
204
+ elif self._state == _SHUTDOWN:
205
+ # Call shutdown() until it doesn't raise anymore.
206
+ self._sslobj.unwrap()
207
+ self._sslobj = None
208
+ self._state = _UNWRAPPED
209
+ if self._shutdown_cb:
210
+ self._shutdown_cb()
211
+
212
+ elif self._state == _UNWRAPPED:
213
+ # Drain possible plaintext data after close_notify.
214
+ appdata.append(self._incoming.read())
215
+ except (ssl.SSLError, ssl.CertificateError) as exc:
216
+ exc_errno = getattr(exc, 'errno', None)
217
+ if exc_errno not in (
218
+ ssl.SSL_ERROR_WANT_READ, ssl.SSL_ERROR_WANT_WRITE,
219
+ ssl.SSL_ERROR_SYSCALL):
220
+ if self._state == _DO_HANDSHAKE and self._handshake_cb:
221
+ self._handshake_cb(exc)
222
+ raise
223
+ self._need_ssldata = (exc_errno == ssl.SSL_ERROR_WANT_READ)
224
+
225
+ # Check for record level data that needs to be sent back.
226
+ # Happens for the initial handshake and renegotiations.
227
+ if self._outgoing.pending:
228
+ ssldata.append(self._outgoing.read())
229
+ return (ssldata, appdata)
230
+
231
+ def feed_appdata(self, data, offset=0):
232
+ """Feed plaintext data into the pipe.
233
+
234
+ Return an (ssldata, offset) tuple. The ssldata element is a list of
235
+ buffers containing record level data that needs to be sent to the
236
+ remote SSL instance. The offset is the number of plaintext bytes that
237
+ were processed, which may be less than the length of data.
238
+
239
+ NOTE: In case of short writes, this call MUST be retried with the SAME
240
+ buffer passed into the *data* argument (i.e. the id() must be the
241
+ same). This is an OpenSSL requirement. A further particularity is that
242
+ a short write will always have offset == 0, because the _ssl module
243
+ does not enable partial writes. And even though the offset is zero,
244
+ there will still be encrypted data in ssldata.
245
+ """
246
+ assert 0 <= offset <= len(data)
247
+ if self._state == _UNWRAPPED:
248
+ # pass through data in unwrapped mode
249
+ if offset < len(data):
250
+ ssldata = [data[offset:]]
251
+ else:
252
+ ssldata = []
253
+ return (ssldata, len(data))
254
+
255
+ ssldata = []
256
+ view = memoryview(data)
257
+ while True:
258
+ self._need_ssldata = False
259
+ try:
260
+ if offset < len(view):
261
+ offset += self._sslobj.write(view[offset:])
262
+ except ssl.SSLError as exc:
263
+ # It is not allowed to call write() after unwrap() until the
264
+ # close_notify is acknowledged. We return the condition to the
265
+ # caller as a short write.
266
+ exc_errno = getattr(exc, 'errno', None)
267
+ if exc.reason == 'PROTOCOL_IS_SHUTDOWN':
268
+ exc_errno = exc.errno = ssl.SSL_ERROR_WANT_READ
269
+ if exc_errno not in (ssl.SSL_ERROR_WANT_READ,
270
+ ssl.SSL_ERROR_WANT_WRITE,
271
+ ssl.SSL_ERROR_SYSCALL):
272
+ raise
273
+ self._need_ssldata = (exc_errno == ssl.SSL_ERROR_WANT_READ)
274
+
275
+ # See if there's any record level data back for us.
276
+ if self._outgoing.pending:
277
+ ssldata.append(self._outgoing.read())
278
+ if offset == len(view) or self._need_ssldata:
279
+ break
280
+ return (ssldata, offset)
281
+
282
+
283
+ class _SSLProtocolTransport(transports._FlowControlMixin,
284
+ transports.Transport):
285
+
286
+ _sendfile_compatible = constants._SendfileMode.FALLBACK
287
+
288
+ def __init__(self, loop, ssl_protocol):
289
+ self._loop = loop
290
+ # SSLProtocol instance
291
+ self._ssl_protocol = ssl_protocol
292
+ self._closed = False
293
+
294
+ def get_extra_info(self, name, default=None):
295
+ """Get optional transport information."""
296
+ return self._ssl_protocol._get_extra_info(name, default)
297
+
298
+ def set_protocol(self, protocol):
299
+ self._ssl_protocol._set_app_protocol(protocol)
300
+
301
+ def get_protocol(self):
302
+ return self._ssl_protocol._app_protocol
303
+
304
+ def is_closing(self):
305
+ return self._closed
306
+
307
+ def close(self):
308
+ """Close the transport.
309
+
310
+ Buffered data will be flushed asynchronously. No more data
311
+ will be received. After all buffered data is flushed, the
312
+ protocol's connection_lost() method will (eventually) called
313
+ with None as its argument.
314
+ """
315
+ self._closed = True
316
+ self._ssl_protocol._start_shutdown()
317
+
318
+ def __del__(self, _warn=warnings.warn):
319
+ if not self._closed:
320
+ _warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
321
+ self.close()
322
+
323
+ def is_reading(self):
324
+ tr = self._ssl_protocol._transport
325
+ if tr is None:
326
+ raise RuntimeError('SSL transport has not been initialized yet')
327
+ return tr.is_reading()
328
+
329
+ def pause_reading(self):
330
+ """Pause the receiving end.
331
+
332
+ No data will be passed to the protocol's data_received()
333
+ method until resume_reading() is called.
334
+ """
335
+ self._ssl_protocol._transport.pause_reading()
336
+
337
+ def resume_reading(self):
338
+ """Resume the receiving end.
339
+
340
+ Data received will once again be passed to the protocol's
341
+ data_received() method.
342
+ """
343
+ self._ssl_protocol._transport.resume_reading()
344
+
345
+ def set_write_buffer_limits(self, high=None, low=None):
346
+ """Set the high- and low-water limits for write flow control.
347
+
348
+ These two values control when to call the protocol's
349
+ pause_writing() and resume_writing() methods. If specified,
350
+ the low-water limit must be less than or equal to the
351
+ high-water limit. Neither value can be negative.
352
+
353
+ The defaults are implementation-specific. If only the
354
+ high-water limit is given, the low-water limit defaults to an
355
+ implementation-specific value less than or equal to the
356
+ high-water limit. Setting high to zero forces low to zero as
357
+ well, and causes pause_writing() to be called whenever the
358
+ buffer becomes non-empty. Setting low to zero causes
359
+ resume_writing() to be called only once the buffer is empty.
360
+ Use of zero for either limit is generally sub-optimal as it
361
+ reduces opportunities for doing I/O and computation
362
+ concurrently.
363
+ """
364
+ self._ssl_protocol._transport.set_write_buffer_limits(high, low)
365
+
366
+ def get_write_buffer_size(self):
367
+ """Return the current size of the write buffer."""
368
+ return self._ssl_protocol._transport.get_write_buffer_size()
369
+
370
+ def get_write_buffer_limits(self):
371
+ """Get the high and low watermarks for write flow control.
372
+ Return a tuple (low, high) where low and high are
373
+ positive number of bytes."""
374
+ return self._ssl_protocol._transport.get_write_buffer_limits()
375
+
376
+ @property
377
+ def _protocol_paused(self):
378
+ # Required for sendfile fallback pause_writing/resume_writing logic
379
+ return self._ssl_protocol._transport._protocol_paused
380
+
381
+ def write(self, data):
382
+ """Write some data bytes to the transport.
383
+
384
+ This does not block; it buffers the data and arranges for it
385
+ to be sent out asynchronously.
386
+ """
387
+ if not isinstance(data, (bytes, bytearray, memoryview)):
388
+ raise TypeError(f"data: expecting a bytes-like instance, "
389
+ f"got {type(data).__name__}")
390
+ if not data:
391
+ return
392
+ self._ssl_protocol._write_appdata(data)
393
+
394
+ def can_write_eof(self):
395
+ """Return True if this transport supports write_eof(), False if not."""
396
+ return False
397
+
398
+ def abort(self):
399
+ """Close the transport immediately.
400
+
401
+ Buffered data will be lost. No more data will be received.
402
+ The protocol's connection_lost() method will (eventually) be
403
+ called with None as its argument.
404
+ """
405
+ self._ssl_protocol._abort()
406
+ self._closed = True
407
+
408
+
409
+ class SSLProtocol(protocols.Protocol):
410
+ """SSL protocol.
411
+
412
+ Implementation of SSL on top of a socket using incoming and outgoing
413
+ buffers which are ssl.MemoryBIO objects.
414
+ """
415
+
416
+ def __init__(self, loop, app_protocol, sslcontext, waiter,
417
+ server_side=False, server_hostname=None,
418
+ call_connection_made=True,
419
+ ssl_handshake_timeout=None):
420
+ if ssl is None:
421
+ raise RuntimeError('stdlib ssl module not available')
422
+
423
+ if ssl_handshake_timeout is None:
424
+ ssl_handshake_timeout = constants.SSL_HANDSHAKE_TIMEOUT
425
+ elif ssl_handshake_timeout <= 0:
426
+ raise ValueError(
427
+ f"ssl_handshake_timeout should be a positive number, "
428
+ f"got {ssl_handshake_timeout}")
429
+
430
+ if not sslcontext:
431
+ sslcontext = _create_transport_context(
432
+ server_side, server_hostname)
433
+
434
+ self._server_side = server_side
435
+ if server_hostname and not server_side:
436
+ self._server_hostname = server_hostname
437
+ else:
438
+ self._server_hostname = None
439
+ self._sslcontext = sslcontext
440
+ # SSL-specific extra info. More info are set when the handshake
441
+ # completes.
442
+ self._extra = dict(sslcontext=sslcontext)
443
+
444
+ # App data write buffering
445
+ self._write_backlog = collections.deque()
446
+ self._write_buffer_size = 0
447
+
448
+ self._waiter = waiter
449
+ self._loop = loop
450
+ self._set_app_protocol(app_protocol)
451
+ self._app_transport = _SSLProtocolTransport(self._loop, self)
452
+ # _SSLPipe instance (None until the connection is made)
453
+ self._sslpipe = None
454
+ self._session_established = False
455
+ self._in_handshake = False
456
+ self._in_shutdown = False
457
+ # transport, ex: SelectorSocketTransport
458
+ self._transport = None
459
+ self._call_connection_made = call_connection_made
460
+ self._ssl_handshake_timeout = ssl_handshake_timeout
461
+
462
+ def _set_app_protocol(self, app_protocol):
463
+ self._app_protocol = app_protocol
464
+ self._app_protocol_is_buffer = \
465
+ isinstance(app_protocol, protocols.BufferedProtocol)
466
+
467
+ def _wakeup_waiter(self, exc=None):
468
+ if self._waiter is None:
469
+ return
470
+ if not self._waiter.cancelled():
471
+ if exc is not None:
472
+ self._waiter.set_exception(exc)
473
+ else:
474
+ self._waiter.set_result(None)
475
+ self._waiter = None
476
+
477
+ def connection_made(self, transport):
478
+ """Called when the low-level connection is made.
479
+
480
+ Start the SSL handshake.
481
+ """
482
+ self._transport = transport
483
+ self._sslpipe = _SSLPipe(self._sslcontext,
484
+ self._server_side,
485
+ self._server_hostname)
486
+ self._start_handshake()
487
+
488
+ def connection_lost(self, exc):
489
+ """Called when the low-level connection is lost or closed.
490
+
491
+ The argument is an exception object or None (the latter
492
+ meaning a regular EOF is received or the connection was
493
+ aborted or closed).
494
+ """
495
+ if self._session_established:
496
+ self._session_established = False
497
+ self._loop.call_soon(self._app_protocol.connection_lost, exc)
498
+ else:
499
+ # Most likely an exception occurred while in SSL handshake.
500
+ # Just mark the app transport as closed so that its __del__
501
+ # doesn't complain.
502
+ if self._app_transport is not None:
503
+ self._app_transport._closed = True
504
+ self._transport = None
505
+ self._app_transport = None
506
+ if getattr(self, '_handshake_timeout_handle', None):
507
+ self._handshake_timeout_handle.cancel()
508
+ self._wakeup_waiter(exc)
509
+ self._app_protocol = None
510
+ self._sslpipe = None
511
+
512
+ def pause_writing(self):
513
+ """Called when the low-level transport's buffer goes over
514
+ the high-water mark.
515
+ """
516
+ self._app_protocol.pause_writing()
517
+
518
+ def resume_writing(self):
519
+ """Called when the low-level transport's buffer drains below
520
+ the low-water mark.
521
+ """
522
+ self._app_protocol.resume_writing()
523
+
524
+ def data_received(self, data):
525
+ """Called when some SSL data is received.
526
+
527
+ The argument is a bytes object.
528
+ """
529
+ if self._sslpipe is None:
530
+ # transport closing, sslpipe is destroyed
531
+ return
532
+
533
+ try:
534
+ ssldata, appdata = self._sslpipe.feed_ssldata(data)
535
+ except (SystemExit, KeyboardInterrupt):
536
+ raise
537
+ except BaseException as e:
538
+ self._fatal_error(e, 'SSL error in data received')
539
+ return
540
+
541
+ for chunk in ssldata:
542
+ self._transport.write(chunk)
543
+
544
+ for chunk in appdata:
545
+ if chunk:
546
+ try:
547
+ if self._app_protocol_is_buffer:
548
+ protocols._feed_data_to_buffered_proto(
549
+ self._app_protocol, chunk)
550
+ else:
551
+ self._app_protocol.data_received(chunk)
552
+ except (SystemExit, KeyboardInterrupt):
553
+ raise
554
+ except BaseException as ex:
555
+ self._fatal_error(
556
+ ex, 'application protocol failed to receive SSL data')
557
+ return
558
+ else:
559
+ self._start_shutdown()
560
+ break
561
+
562
+ def eof_received(self):
563
+ """Called when the other end of the low-level stream
564
+ is half-closed.
565
+
566
+ If this returns a false value (including None), the transport
567
+ will close itself. If it returns a true value, closing the
568
+ transport is up to the protocol.
569
+ """
570
+ try:
571
+ if self._loop.get_debug():
572
+ logger.debug("%r received EOF", self)
573
+
574
+ self._wakeup_waiter(ConnectionResetError)
575
+
576
+ if not self._in_handshake:
577
+ keep_open = self._app_protocol.eof_received()
578
+ if keep_open:
579
+ logger.warning('returning true from eof_received() '
580
+ 'has no effect when using ssl')
581
+ finally:
582
+ self._transport.close()
583
+
584
+ def _get_extra_info(self, name, default=None):
585
+ if name in self._extra:
586
+ return self._extra[name]
587
+ elif self._transport is not None:
588
+ return self._transport.get_extra_info(name, default)
589
+ else:
590
+ return default
591
+
592
+ def _start_shutdown(self):
593
+ if self._in_shutdown:
594
+ return
595
+ if self._in_handshake:
596
+ self._abort()
597
+ else:
598
+ self._in_shutdown = True
599
+ self._write_appdata(b'')
600
+
601
+ def _write_appdata(self, data):
602
+ self._write_backlog.append((data, 0))
603
+ self._write_buffer_size += len(data)
604
+ self._process_write_backlog()
605
+
606
+ def _start_handshake(self):
607
+ if self._loop.get_debug():
608
+ logger.debug("%r starts SSL handshake", self)
609
+ self._handshake_start_time = self._loop.time()
610
+ else:
611
+ self._handshake_start_time = None
612
+ self._in_handshake = True
613
+ # (b'', 1) is a special value in _process_write_backlog() to do
614
+ # the SSL handshake
615
+ self._write_backlog.append((b'', 1))
616
+ self._handshake_timeout_handle = \
617
+ self._loop.call_later(self._ssl_handshake_timeout,
618
+ self._check_handshake_timeout)
619
+ self._process_write_backlog()
620
+
621
+ def _check_handshake_timeout(self):
622
+ if self._in_handshake is True:
623
+ msg = (
624
+ f"SSL handshake is taking longer than "
625
+ f"{self._ssl_handshake_timeout} seconds: "
626
+ f"aborting the connection"
627
+ )
628
+ self._fatal_error(ConnectionAbortedError(msg))
629
+
630
+ def _on_handshake_complete(self, handshake_exc):
631
+ self._in_handshake = False
632
+ self._handshake_timeout_handle.cancel()
633
+
634
+ sslobj = self._sslpipe.ssl_object
635
+ try:
636
+ if handshake_exc is not None:
637
+ raise handshake_exc
638
+
639
+ peercert = sslobj.getpeercert()
640
+ except (SystemExit, KeyboardInterrupt):
641
+ raise
642
+ except BaseException as exc:
643
+ if isinstance(exc, ssl.CertificateError):
644
+ msg = 'SSL handshake failed on verifying the certificate'
645
+ else:
646
+ msg = 'SSL handshake failed'
647
+ self._fatal_error(exc, msg)
648
+ return
649
+
650
+ if self._loop.get_debug():
651
+ dt = self._loop.time() - self._handshake_start_time
652
+ logger.debug("%r: SSL handshake took %.1f ms", self, dt * 1e3)
653
+
654
+ # Add extra info that becomes available after handshake.
655
+ self._extra.update(peercert=peercert,
656
+ cipher=sslobj.cipher(),
657
+ compression=sslobj.compression(),
658
+ ssl_object=sslobj,
659
+ )
660
+ if self._call_connection_made:
661
+ self._app_protocol.connection_made(self._app_transport)
662
+ self._wakeup_waiter()
663
+ self._session_established = True
664
+ # In case transport.write() was already called. Don't call
665
+ # immediately _process_write_backlog(), but schedule it:
666
+ # _on_handshake_complete() can be called indirectly from
667
+ # _process_write_backlog(), and _process_write_backlog() is not
668
+ # reentrant.
669
+ self._loop.call_soon(self._process_write_backlog)
670
+
671
+ def _process_write_backlog(self):
672
+ # Try to make progress on the write backlog.
673
+ if self._transport is None or self._sslpipe is None:
674
+ return
675
+
676
+ try:
677
+ for i in range(len(self._write_backlog)):
678
+ data, offset = self._write_backlog[0]
679
+ if data:
680
+ ssldata, offset = self._sslpipe.feed_appdata(data, offset)
681
+ elif offset:
682
+ ssldata = self._sslpipe.do_handshake(
683
+ self._on_handshake_complete)
684
+ offset = 1
685
+ else:
686
+ ssldata = self._sslpipe.shutdown(self._finalize)
687
+ offset = 1
688
+
689
+ for chunk in ssldata:
690
+ self._transport.write(chunk)
691
+
692
+ if offset < len(data):
693
+ self._write_backlog[0] = (data, offset)
694
+ # A short write means that a write is blocked on a read
695
+ # We need to enable reading if it is paused!
696
+ assert self._sslpipe.need_ssldata
697
+ if self._transport._paused:
698
+ self._transport.resume_reading()
699
+ break
700
+
701
+ # An entire chunk from the backlog was processed. We can
702
+ # delete it and reduce the outstanding buffer size.
703
+ del self._write_backlog[0]
704
+ self._write_buffer_size -= len(data)
705
+ except (SystemExit, KeyboardInterrupt):
706
+ raise
707
+ except BaseException as exc:
708
+ if self._in_handshake:
709
+ # Exceptions will be re-raised in _on_handshake_complete.
710
+ self._on_handshake_complete(exc)
711
+ else:
712
+ self._fatal_error(exc, 'Fatal error on SSL transport')
713
+
714
+ def _fatal_error(self, exc, message='Fatal error on transport'):
715
+ if isinstance(exc, OSError):
716
+ if self._loop.get_debug():
717
+ logger.debug("%r: %s", self, message, exc_info=True)
718
+ else:
719
+ self._loop.call_exception_handler({
720
+ 'message': message,
721
+ 'exception': exc,
722
+ 'transport': self._transport,
723
+ 'protocol': self,
724
+ })
725
+ if self._transport:
726
+ self._transport._force_close(exc)
727
+
728
+ def _finalize(self):
729
+ self._sslpipe = None
730
+
731
+ if self._transport is not None:
732
+ self._transport.close()
733
+
734
+ def _abort(self):
735
+ try:
736
+ if self._transport is not None:
737
+ self._transport.abort()
738
+ finally:
739
+ self._finalize()
evalkit_llava/lib/python3.10/asyncio/staggered.py ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Support for running coroutines in parallel with staggered start times."""
2
+
3
+ __all__ = 'staggered_race',
4
+
5
+ import contextlib
6
+ import typing
7
+
8
+ from . import events
9
+ from . import exceptions as exceptions_mod
10
+ from . import locks
11
+ from . import tasks
12
+
13
+
14
+ async def staggered_race(
15
+ coro_fns: typing.Iterable[typing.Callable[[], typing.Awaitable]],
16
+ delay: typing.Optional[float],
17
+ *,
18
+ loop: events.AbstractEventLoop = None,
19
+ ) -> typing.Tuple[
20
+ typing.Any,
21
+ typing.Optional[int],
22
+ typing.List[typing.Optional[Exception]]
23
+ ]:
24
+ """Run coroutines with staggered start times and take the first to finish.
25
+
26
+ This method takes an iterable of coroutine functions. The first one is
27
+ started immediately. From then on, whenever the immediately preceding one
28
+ fails (raises an exception), or when *delay* seconds has passed, the next
29
+ coroutine is started. This continues until one of the coroutines complete
30
+ successfully, in which case all others are cancelled, or until all
31
+ coroutines fail.
32
+
33
+ The coroutines provided should be well-behaved in the following way:
34
+
35
+ * They should only ``return`` if completed successfully.
36
+
37
+ * They should always raise an exception if they did not complete
38
+ successfully. In particular, if they handle cancellation, they should
39
+ probably reraise, like this::
40
+
41
+ try:
42
+ # do work
43
+ except asyncio.CancelledError:
44
+ # undo partially completed work
45
+ raise
46
+
47
+ Args:
48
+ coro_fns: an iterable of coroutine functions, i.e. callables that
49
+ return a coroutine object when called. Use ``functools.partial`` or
50
+ lambdas to pass arguments.
51
+
52
+ delay: amount of time, in seconds, between starting coroutines. If
53
+ ``None``, the coroutines will run sequentially.
54
+
55
+ loop: the event loop to use.
56
+
57
+ Returns:
58
+ tuple *(winner_result, winner_index, exceptions)* where
59
+
60
+ - *winner_result*: the result of the winning coroutine, or ``None``
61
+ if no coroutines won.
62
+
63
+ - *winner_index*: the index of the winning coroutine in
64
+ ``coro_fns``, or ``None`` if no coroutines won. If the winning
65
+ coroutine may return None on success, *winner_index* can be used
66
+ to definitively determine whether any coroutine won.
67
+
68
+ - *exceptions*: list of exceptions returned by the coroutines.
69
+ ``len(exceptions)`` is equal to the number of coroutines actually
70
+ started, and the order is the same as in ``coro_fns``. The winning
71
+ coroutine's entry is ``None``.
72
+
73
+ """
74
+ # TODO: when we have aiter() and anext(), allow async iterables in coro_fns.
75
+ loop = loop or events.get_running_loop()
76
+ enum_coro_fns = enumerate(coro_fns)
77
+ winner_result = None
78
+ winner_index = None
79
+ exceptions = []
80
+ running_tasks = []
81
+
82
+ async def run_one_coro(
83
+ previous_failed: typing.Optional[locks.Event]) -> None:
84
+ # Wait for the previous task to finish, or for delay seconds
85
+ if previous_failed is not None:
86
+ with contextlib.suppress(exceptions_mod.TimeoutError):
87
+ # Use asyncio.wait_for() instead of asyncio.wait() here, so
88
+ # that if we get cancelled at this point, Event.wait() is also
89
+ # cancelled, otherwise there will be a "Task destroyed but it is
90
+ # pending" later.
91
+ await tasks.wait_for(previous_failed.wait(), delay)
92
+ # Get the next coroutine to run
93
+ try:
94
+ this_index, coro_fn = next(enum_coro_fns)
95
+ except StopIteration:
96
+ return
97
+ # Start task that will run the next coroutine
98
+ this_failed = locks.Event()
99
+ next_task = loop.create_task(run_one_coro(this_failed))
100
+ running_tasks.append(next_task)
101
+ assert len(running_tasks) == this_index + 2
102
+ # Prepare place to put this coroutine's exceptions if not won
103
+ exceptions.append(None)
104
+ assert len(exceptions) == this_index + 1
105
+
106
+ try:
107
+ result = await coro_fn()
108
+ except (SystemExit, KeyboardInterrupt):
109
+ raise
110
+ except BaseException as e:
111
+ exceptions[this_index] = e
112
+ this_failed.set() # Kickstart the next coroutine
113
+ else:
114
+ # Store winner's results
115
+ nonlocal winner_index, winner_result
116
+ assert winner_index is None
117
+ winner_index = this_index
118
+ winner_result = result
119
+ # Cancel all other tasks. We take care to not cancel the current
120
+ # task as well. If we do so, then since there is no `await` after
121
+ # here and CancelledError are usually thrown at one, we will
122
+ # encounter a curious corner case where the current task will end
123
+ # up as done() == True, cancelled() == False, exception() ==
124
+ # asyncio.CancelledError. This behavior is specified in
125
+ # https://bugs.python.org/issue30048
126
+ for i, t in enumerate(running_tasks):
127
+ if i != this_index:
128
+ t.cancel()
129
+
130
+ first_task = loop.create_task(run_one_coro(None))
131
+ running_tasks.append(first_task)
132
+ try:
133
+ # Wait for a growing list of tasks to all finish: poor man's version of
134
+ # curio's TaskGroup or trio's nursery
135
+ done_count = 0
136
+ while done_count != len(running_tasks):
137
+ done, _ = await tasks.wait(running_tasks)
138
+ done_count = len(done)
139
+ # If run_one_coro raises an unhandled exception, it's probably a
140
+ # programming error, and I want to see it.
141
+ if __debug__:
142
+ for d in done:
143
+ if d.done() and not d.cancelled() and d.exception():
144
+ raise d.exception()
145
+ return winner_result, winner_index, exceptions
146
+ finally:
147
+ # Make sure no tasks are left running if we leave this function
148
+ for t in running_tasks:
149
+ t.cancel()
evalkit_llava/lib/python3.10/asyncio/streams.py ADDED
@@ -0,0 +1,726 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ __all__ = (
2
+ 'StreamReader', 'StreamWriter', 'StreamReaderProtocol',
3
+ 'open_connection', 'start_server')
4
+
5
+ import collections
6
+ import socket
7
+ import sys
8
+ import warnings
9
+ import weakref
10
+
11
+ if hasattr(socket, 'AF_UNIX'):
12
+ __all__ += ('open_unix_connection', 'start_unix_server')
13
+
14
+ from . import coroutines
15
+ from . import events
16
+ from . import exceptions
17
+ from . import format_helpers
18
+ from . import protocols
19
+ from .log import logger
20
+ from .tasks import sleep
21
+
22
+
23
+ _DEFAULT_LIMIT = 2 ** 16 # 64 KiB
24
+
25
+
26
+ async def open_connection(host=None, port=None, *,
27
+ limit=_DEFAULT_LIMIT, **kwds):
28
+ """A wrapper for create_connection() returning a (reader, writer) pair.
29
+
30
+ The reader returned is a StreamReader instance; the writer is a
31
+ StreamWriter instance.
32
+
33
+ The arguments are all the usual arguments to create_connection()
34
+ except protocol_factory; most common are positional host and port,
35
+ with various optional keyword arguments following.
36
+
37
+ Additional optional keyword arguments are loop (to set the event loop
38
+ instance to use) and limit (to set the buffer limit passed to the
39
+ StreamReader).
40
+
41
+ (If you want to customize the StreamReader and/or
42
+ StreamReaderProtocol classes, just copy the code -- there's
43
+ really nothing special here except some convenience.)
44
+ """
45
+ loop = events.get_running_loop()
46
+ reader = StreamReader(limit=limit, loop=loop)
47
+ protocol = StreamReaderProtocol(reader, loop=loop)
48
+ transport, _ = await loop.create_connection(
49
+ lambda: protocol, host, port, **kwds)
50
+ writer = StreamWriter(transport, protocol, reader, loop)
51
+ return reader, writer
52
+
53
+
54
+ async def start_server(client_connected_cb, host=None, port=None, *,
55
+ limit=_DEFAULT_LIMIT, **kwds):
56
+ """Start a socket server, call back for each client connected.
57
+
58
+ The first parameter, `client_connected_cb`, takes two parameters:
59
+ client_reader, client_writer. client_reader is a StreamReader
60
+ object, while client_writer is a StreamWriter object. This
61
+ parameter can either be a plain callback function or a coroutine;
62
+ if it is a coroutine, it will be automatically converted into a
63
+ Task.
64
+
65
+ The rest of the arguments are all the usual arguments to
66
+ loop.create_server() except protocol_factory; most common are
67
+ positional host and port, with various optional keyword arguments
68
+ following. The return value is the same as loop.create_server().
69
+
70
+ Additional optional keyword arguments are loop (to set the event loop
71
+ instance to use) and limit (to set the buffer limit passed to the
72
+ StreamReader).
73
+
74
+ The return value is the same as loop.create_server(), i.e. a
75
+ Server object which can be used to stop the service.
76
+ """
77
+ loop = events.get_running_loop()
78
+
79
+ def factory():
80
+ reader = StreamReader(limit=limit, loop=loop)
81
+ protocol = StreamReaderProtocol(reader, client_connected_cb,
82
+ loop=loop)
83
+ return protocol
84
+
85
+ return await loop.create_server(factory, host, port, **kwds)
86
+
87
+
88
+ if hasattr(socket, 'AF_UNIX'):
89
+ # UNIX Domain Sockets are supported on this platform
90
+
91
+ async def open_unix_connection(path=None, *,
92
+ limit=_DEFAULT_LIMIT, **kwds):
93
+ """Similar to `open_connection` but works with UNIX Domain Sockets."""
94
+ loop = events.get_running_loop()
95
+
96
+ reader = StreamReader(limit=limit, loop=loop)
97
+ protocol = StreamReaderProtocol(reader, loop=loop)
98
+ transport, _ = await loop.create_unix_connection(
99
+ lambda: protocol, path, **kwds)
100
+ writer = StreamWriter(transport, protocol, reader, loop)
101
+ return reader, writer
102
+
103
+ async def start_unix_server(client_connected_cb, path=None, *,
104
+ limit=_DEFAULT_LIMIT, **kwds):
105
+ """Similar to `start_server` but works with UNIX Domain Sockets."""
106
+ loop = events.get_running_loop()
107
+
108
+ def factory():
109
+ reader = StreamReader(limit=limit, loop=loop)
110
+ protocol = StreamReaderProtocol(reader, client_connected_cb,
111
+ loop=loop)
112
+ return protocol
113
+
114
+ return await loop.create_unix_server(factory, path, **kwds)
115
+
116
+
117
+ class FlowControlMixin(protocols.Protocol):
118
+ """Reusable flow control logic for StreamWriter.drain().
119
+
120
+ This implements the protocol methods pause_writing(),
121
+ resume_writing() and connection_lost(). If the subclass overrides
122
+ these it must call the super methods.
123
+
124
+ StreamWriter.drain() must wait for _drain_helper() coroutine.
125
+ """
126
+
127
+ def __init__(self, loop=None):
128
+ if loop is None:
129
+ self._loop = events._get_event_loop(stacklevel=4)
130
+ else:
131
+ self._loop = loop
132
+ self._paused = False
133
+ self._drain_waiters = collections.deque()
134
+ self._connection_lost = False
135
+
136
+ def pause_writing(self):
137
+ assert not self._paused
138
+ self._paused = True
139
+ if self._loop.get_debug():
140
+ logger.debug("%r pauses writing", self)
141
+
142
+ def resume_writing(self):
143
+ assert self._paused
144
+ self._paused = False
145
+ if self._loop.get_debug():
146
+ logger.debug("%r resumes writing", self)
147
+
148
+ for waiter in self._drain_waiters:
149
+ if not waiter.done():
150
+ waiter.set_result(None)
151
+
152
+ def connection_lost(self, exc):
153
+ self._connection_lost = True
154
+ # Wake up the writer(s) if currently paused.
155
+ if not self._paused:
156
+ return
157
+
158
+ for waiter in self._drain_waiters:
159
+ if not waiter.done():
160
+ if exc is None:
161
+ waiter.set_result(None)
162
+ else:
163
+ waiter.set_exception(exc)
164
+
165
+ async def _drain_helper(self):
166
+ if self._connection_lost:
167
+ raise ConnectionResetError('Connection lost')
168
+ if not self._paused:
169
+ return
170
+ waiter = self._loop.create_future()
171
+ self._drain_waiters.append(waiter)
172
+ try:
173
+ await waiter
174
+ finally:
175
+ self._drain_waiters.remove(waiter)
176
+
177
+ def _get_close_waiter(self, stream):
178
+ raise NotImplementedError
179
+
180
+
181
+ class StreamReaderProtocol(FlowControlMixin, protocols.Protocol):
182
+ """Helper class to adapt between Protocol and StreamReader.
183
+
184
+ (This is a helper class instead of making StreamReader itself a
185
+ Protocol subclass, because the StreamReader has other potential
186
+ uses, and to prevent the user of the StreamReader to accidentally
187
+ call inappropriate methods of the protocol.)
188
+ """
189
+
190
+ _source_traceback = None
191
+
192
+ def __init__(self, stream_reader, client_connected_cb=None, loop=None):
193
+ super().__init__(loop=loop)
194
+ if stream_reader is not None:
195
+ self._stream_reader_wr = weakref.ref(stream_reader)
196
+ self._source_traceback = stream_reader._source_traceback
197
+ else:
198
+ self._stream_reader_wr = None
199
+ if client_connected_cb is not None:
200
+ # This is a stream created by the `create_server()` function.
201
+ # Keep a strong reference to the reader until a connection
202
+ # is established.
203
+ self._strong_reader = stream_reader
204
+ self._reject_connection = False
205
+ self._stream_writer = None
206
+ self._task = None
207
+ self._transport = None
208
+ self._client_connected_cb = client_connected_cb
209
+ self._over_ssl = False
210
+ self._closed = self._loop.create_future()
211
+
212
+ @property
213
+ def _stream_reader(self):
214
+ if self._stream_reader_wr is None:
215
+ return None
216
+ return self._stream_reader_wr()
217
+
218
+ def connection_made(self, transport):
219
+ if self._reject_connection:
220
+ context = {
221
+ 'message': ('An open stream was garbage collected prior to '
222
+ 'establishing network connection; '
223
+ 'call "stream.close()" explicitly.')
224
+ }
225
+ if self._source_traceback:
226
+ context['source_traceback'] = self._source_traceback
227
+ self._loop.call_exception_handler(context)
228
+ transport.abort()
229
+ return
230
+ self._transport = transport
231
+ reader = self._stream_reader
232
+ if reader is not None:
233
+ reader.set_transport(transport)
234
+ self._over_ssl = transport.get_extra_info('sslcontext') is not None
235
+ if self._client_connected_cb is not None:
236
+ self._stream_writer = StreamWriter(transport, self,
237
+ reader,
238
+ self._loop)
239
+ res = self._client_connected_cb(reader,
240
+ self._stream_writer)
241
+ if coroutines.iscoroutine(res):
242
+ self._task = self._loop.create_task(res)
243
+ self._strong_reader = None
244
+
245
+ def connection_lost(self, exc):
246
+ reader = self._stream_reader
247
+ if reader is not None:
248
+ if exc is None:
249
+ reader.feed_eof()
250
+ else:
251
+ reader.set_exception(exc)
252
+ if not self._closed.done():
253
+ if exc is None:
254
+ self._closed.set_result(None)
255
+ else:
256
+ self._closed.set_exception(exc)
257
+ super().connection_lost(exc)
258
+ self._stream_reader_wr = None
259
+ self._stream_writer = None
260
+ self._task = None
261
+ self._transport = None
262
+
263
+ def data_received(self, data):
264
+ reader = self._stream_reader
265
+ if reader is not None:
266
+ reader.feed_data(data)
267
+
268
+ def eof_received(self):
269
+ reader = self._stream_reader
270
+ if reader is not None:
271
+ reader.feed_eof()
272
+ if self._over_ssl:
273
+ # Prevent a warning in SSLProtocol.eof_received:
274
+ # "returning true from eof_received()
275
+ # has no effect when using ssl"
276
+ return False
277
+ return True
278
+
279
+ def _get_close_waiter(self, stream):
280
+ return self._closed
281
+
282
+ def __del__(self):
283
+ # Prevent reports about unhandled exceptions.
284
+ # Better than self._closed._log_traceback = False hack
285
+ try:
286
+ closed = self._closed
287
+ except AttributeError:
288
+ pass # failed constructor
289
+ else:
290
+ if closed.done() and not closed.cancelled():
291
+ closed.exception()
292
+
293
+
294
+ class StreamWriter:
295
+ """Wraps a Transport.
296
+
297
+ This exposes write(), writelines(), [can_]write_eof(),
298
+ get_extra_info() and close(). It adds drain() which returns an
299
+ optional Future on which you can wait for flow control. It also
300
+ adds a transport property which references the Transport
301
+ directly.
302
+ """
303
+
304
+ def __init__(self, transport, protocol, reader, loop):
305
+ self._transport = transport
306
+ self._protocol = protocol
307
+ # drain() expects that the reader has an exception() method
308
+ assert reader is None or isinstance(reader, StreamReader)
309
+ self._reader = reader
310
+ self._loop = loop
311
+ self._complete_fut = self._loop.create_future()
312
+ self._complete_fut.set_result(None)
313
+
314
+ def __repr__(self):
315
+ info = [self.__class__.__name__, f'transport={self._transport!r}']
316
+ if self._reader is not None:
317
+ info.append(f'reader={self._reader!r}')
318
+ return '<{}>'.format(' '.join(info))
319
+
320
+ @property
321
+ def transport(self):
322
+ return self._transport
323
+
324
+ def write(self, data):
325
+ self._transport.write(data)
326
+
327
+ def writelines(self, data):
328
+ self._transport.writelines(data)
329
+
330
+ def write_eof(self):
331
+ return self._transport.write_eof()
332
+
333
+ def can_write_eof(self):
334
+ return self._transport.can_write_eof()
335
+
336
+ def close(self):
337
+ return self._transport.close()
338
+
339
+ def is_closing(self):
340
+ return self._transport.is_closing()
341
+
342
+ async def wait_closed(self):
343
+ await self._protocol._get_close_waiter(self)
344
+
345
+ def get_extra_info(self, name, default=None):
346
+ return self._transport.get_extra_info(name, default)
347
+
348
+ async def drain(self):
349
+ """Flush the write buffer.
350
+
351
+ The intended use is to write
352
+
353
+ w.write(data)
354
+ await w.drain()
355
+ """
356
+ if self._reader is not None:
357
+ exc = self._reader.exception()
358
+ if exc is not None:
359
+ raise exc
360
+ if self._transport.is_closing():
361
+ # Wait for protocol.connection_lost() call
362
+ # Raise connection closing error if any,
363
+ # ConnectionResetError otherwise
364
+ # Yield to the event loop so connection_lost() may be
365
+ # called. Without this, _drain_helper() would return
366
+ # immediately, and code that calls
367
+ # write(...); await drain()
368
+ # in a loop would never call connection_lost(), so it
369
+ # would not see an error when the socket is closed.
370
+ await sleep(0)
371
+ await self._protocol._drain_helper()
372
+
373
+
374
+ class StreamReader:
375
+
376
+ _source_traceback = None
377
+
378
+ def __init__(self, limit=_DEFAULT_LIMIT, loop=None):
379
+ # The line length limit is a security feature;
380
+ # it also doubles as half the buffer limit.
381
+
382
+ if limit <= 0:
383
+ raise ValueError('Limit cannot be <= 0')
384
+
385
+ self._limit = limit
386
+ if loop is None:
387
+ self._loop = events._get_event_loop()
388
+ else:
389
+ self._loop = loop
390
+ self._buffer = bytearray()
391
+ self._eof = False # Whether we're done.
392
+ self._waiter = None # A future used by _wait_for_data()
393
+ self._exception = None
394
+ self._transport = None
395
+ self._paused = False
396
+ if self._loop.get_debug():
397
+ self._source_traceback = format_helpers.extract_stack(
398
+ sys._getframe(1))
399
+
400
+ def __repr__(self):
401
+ info = ['StreamReader']
402
+ if self._buffer:
403
+ info.append(f'{len(self._buffer)} bytes')
404
+ if self._eof:
405
+ info.append('eof')
406
+ if self._limit != _DEFAULT_LIMIT:
407
+ info.append(f'limit={self._limit}')
408
+ if self._waiter:
409
+ info.append(f'waiter={self._waiter!r}')
410
+ if self._exception:
411
+ info.append(f'exception={self._exception!r}')
412
+ if self._transport:
413
+ info.append(f'transport={self._transport!r}')
414
+ if self._paused:
415
+ info.append('paused')
416
+ return '<{}>'.format(' '.join(info))
417
+
418
+ def exception(self):
419
+ return self._exception
420
+
421
+ def set_exception(self, exc):
422
+ self._exception = exc
423
+
424
+ waiter = self._waiter
425
+ if waiter is not None:
426
+ self._waiter = None
427
+ if not waiter.cancelled():
428
+ waiter.set_exception(exc)
429
+
430
+ def _wakeup_waiter(self):
431
+ """Wakeup read*() functions waiting for data or EOF."""
432
+ waiter = self._waiter
433
+ if waiter is not None:
434
+ self._waiter = None
435
+ if not waiter.cancelled():
436
+ waiter.set_result(None)
437
+
438
+ def set_transport(self, transport):
439
+ assert self._transport is None, 'Transport already set'
440
+ self._transport = transport
441
+
442
+ def _maybe_resume_transport(self):
443
+ if self._paused and len(self._buffer) <= self._limit:
444
+ self._paused = False
445
+ self._transport.resume_reading()
446
+
447
+ def feed_eof(self):
448
+ self._eof = True
449
+ self._wakeup_waiter()
450
+
451
+ def at_eof(self):
452
+ """Return True if the buffer is empty and 'feed_eof' was called."""
453
+ return self._eof and not self._buffer
454
+
455
+ def feed_data(self, data):
456
+ assert not self._eof, 'feed_data after feed_eof'
457
+
458
+ if not data:
459
+ return
460
+
461
+ self._buffer.extend(data)
462
+ self._wakeup_waiter()
463
+
464
+ if (self._transport is not None and
465
+ not self._paused and
466
+ len(self._buffer) > 2 * self._limit):
467
+ try:
468
+ self._transport.pause_reading()
469
+ except NotImplementedError:
470
+ # The transport can't be paused.
471
+ # We'll just have to buffer all data.
472
+ # Forget the transport so we don't keep trying.
473
+ self._transport = None
474
+ else:
475
+ self._paused = True
476
+
477
+ async def _wait_for_data(self, func_name):
478
+ """Wait until feed_data() or feed_eof() is called.
479
+
480
+ If stream was paused, automatically resume it.
481
+ """
482
+ # StreamReader uses a future to link the protocol feed_data() method
483
+ # to a read coroutine. Running two read coroutines at the same time
484
+ # would have an unexpected behaviour. It would not possible to know
485
+ # which coroutine would get the next data.
486
+ if self._waiter is not None:
487
+ raise RuntimeError(
488
+ f'{func_name}() called while another coroutine is '
489
+ f'already waiting for incoming data')
490
+
491
+ assert not self._eof, '_wait_for_data after EOF'
492
+
493
+ # Waiting for data while paused will make deadlock, so prevent it.
494
+ # This is essential for readexactly(n) for case when n > self._limit.
495
+ if self._paused:
496
+ self._paused = False
497
+ self._transport.resume_reading()
498
+
499
+ self._waiter = self._loop.create_future()
500
+ try:
501
+ await self._waiter
502
+ finally:
503
+ self._waiter = None
504
+
505
+ async def readline(self):
506
+ """Read chunk of data from the stream until newline (b'\n') is found.
507
+
508
+ On success, return chunk that ends with newline. If only partial
509
+ line can be read due to EOF, return incomplete line without
510
+ terminating newline. When EOF was reached while no bytes read, empty
511
+ bytes object is returned.
512
+
513
+ If limit is reached, ValueError will be raised. In that case, if
514
+ newline was found, complete line including newline will be removed
515
+ from internal buffer. Else, internal buffer will be cleared. Limit is
516
+ compared against part of the line without newline.
517
+
518
+ If stream was paused, this function will automatically resume it if
519
+ needed.
520
+ """
521
+ sep = b'\n'
522
+ seplen = len(sep)
523
+ try:
524
+ line = await self.readuntil(sep)
525
+ except exceptions.IncompleteReadError as e:
526
+ return e.partial
527
+ except exceptions.LimitOverrunError as e:
528
+ if self._buffer.startswith(sep, e.consumed):
529
+ del self._buffer[:e.consumed + seplen]
530
+ else:
531
+ self._buffer.clear()
532
+ self._maybe_resume_transport()
533
+ raise ValueError(e.args[0])
534
+ return line
535
+
536
+ async def readuntil(self, separator=b'\n'):
537
+ """Read data from the stream until ``separator`` is found.
538
+
539
+ On success, the data and separator will be removed from the
540
+ internal buffer (consumed). Returned data will include the
541
+ separator at the end.
542
+
543
+ Configured stream limit is used to check result. Limit sets the
544
+ maximal length of data that can be returned, not counting the
545
+ separator.
546
+
547
+ If an EOF occurs and the complete separator is still not found,
548
+ an IncompleteReadError exception will be raised, and the internal
549
+ buffer will be reset. The IncompleteReadError.partial attribute
550
+ may contain the separator partially.
551
+
552
+ If the data cannot be read because of over limit, a
553
+ LimitOverrunError exception will be raised, and the data
554
+ will be left in the internal buffer, so it can be read again.
555
+ """
556
+ seplen = len(separator)
557
+ if seplen == 0:
558
+ raise ValueError('Separator should be at least one-byte string')
559
+
560
+ if self._exception is not None:
561
+ raise self._exception
562
+
563
+ # Consume whole buffer except last bytes, which length is
564
+ # one less than seplen. Let's check corner cases with
565
+ # separator='SEPARATOR':
566
+ # * we have received almost complete separator (without last
567
+ # byte). i.e buffer='some textSEPARATO'. In this case we
568
+ # can safely consume len(separator) - 1 bytes.
569
+ # * last byte of buffer is first byte of separator, i.e.
570
+ # buffer='abcdefghijklmnopqrS'. We may safely consume
571
+ # everything except that last byte, but this require to
572
+ # analyze bytes of buffer that match partial separator.
573
+ # This is slow and/or require FSM. For this case our
574
+ # implementation is not optimal, since require rescanning
575
+ # of data that is known to not belong to separator. In
576
+ # real world, separator will not be so long to notice
577
+ # performance problems. Even when reading MIME-encoded
578
+ # messages :)
579
+
580
+ # `offset` is the number of bytes from the beginning of the buffer
581
+ # where there is no occurrence of `separator`.
582
+ offset = 0
583
+
584
+ # Loop until we find `separator` in the buffer, exceed the buffer size,
585
+ # or an EOF has happened.
586
+ while True:
587
+ buflen = len(self._buffer)
588
+
589
+ # Check if we now have enough data in the buffer for `separator` to
590
+ # fit.
591
+ if buflen - offset >= seplen:
592
+ isep = self._buffer.find(separator, offset)
593
+
594
+ if isep != -1:
595
+ # `separator` is in the buffer. `isep` will be used later
596
+ # to retrieve the data.
597
+ break
598
+
599
+ # see upper comment for explanation.
600
+ offset = buflen + 1 - seplen
601
+ if offset > self._limit:
602
+ raise exceptions.LimitOverrunError(
603
+ 'Separator is not found, and chunk exceed the limit',
604
+ offset)
605
+
606
+ # Complete message (with full separator) may be present in buffer
607
+ # even when EOF flag is set. This may happen when the last chunk
608
+ # adds data which makes separator be found. That's why we check for
609
+ # EOF *ater* inspecting the buffer.
610
+ if self._eof:
611
+ chunk = bytes(self._buffer)
612
+ self._buffer.clear()
613
+ raise exceptions.IncompleteReadError(chunk, None)
614
+
615
+ # _wait_for_data() will resume reading if stream was paused.
616
+ await self._wait_for_data('readuntil')
617
+
618
+ if isep > self._limit:
619
+ raise exceptions.LimitOverrunError(
620
+ 'Separator is found, but chunk is longer than limit', isep)
621
+
622
+ chunk = self._buffer[:isep + seplen]
623
+ del self._buffer[:isep + seplen]
624
+ self._maybe_resume_transport()
625
+ return bytes(chunk)
626
+
627
+ async def read(self, n=-1):
628
+ """Read up to `n` bytes from the stream.
629
+
630
+ If `n` is not provided or set to -1,
631
+ read until EOF, then return all read bytes.
632
+ If EOF was received and the internal buffer is empty,
633
+ return an empty bytes object.
634
+
635
+ If `n` is 0, return an empty bytes object immediately.
636
+
637
+ If `n` is positive, return at most `n` available bytes
638
+ as soon as at least 1 byte is available in the internal buffer.
639
+ If EOF is received before any byte is read, return an empty
640
+ bytes object.
641
+
642
+ Returned value is not limited with limit, configured at stream
643
+ creation.
644
+
645
+ If stream was paused, this function will automatically resume it if
646
+ needed.
647
+ """
648
+
649
+ if self._exception is not None:
650
+ raise self._exception
651
+
652
+ if n == 0:
653
+ return b''
654
+
655
+ if n < 0:
656
+ # This used to just loop creating a new waiter hoping to
657
+ # collect everything in self._buffer, but that would
658
+ # deadlock if the subprocess sends more than self.limit
659
+ # bytes. So just call self.read(self._limit) until EOF.
660
+ blocks = []
661
+ while True:
662
+ block = await self.read(self._limit)
663
+ if not block:
664
+ break
665
+ blocks.append(block)
666
+ return b''.join(blocks)
667
+
668
+ if not self._buffer and not self._eof:
669
+ await self._wait_for_data('read')
670
+
671
+ # This will work right even if buffer is less than n bytes
672
+ data = bytes(self._buffer[:n])
673
+ del self._buffer[:n]
674
+
675
+ self._maybe_resume_transport()
676
+ return data
677
+
678
+ async def readexactly(self, n):
679
+ """Read exactly `n` bytes.
680
+
681
+ Raise an IncompleteReadError if EOF is reached before `n` bytes can be
682
+ read. The IncompleteReadError.partial attribute of the exception will
683
+ contain the partial read bytes.
684
+
685
+ if n is zero, return empty bytes object.
686
+
687
+ Returned value is not limited with limit, configured at stream
688
+ creation.
689
+
690
+ If stream was paused, this function will automatically resume it if
691
+ needed.
692
+ """
693
+ if n < 0:
694
+ raise ValueError('readexactly size can not be less than zero')
695
+
696
+ if self._exception is not None:
697
+ raise self._exception
698
+
699
+ if n == 0:
700
+ return b''
701
+
702
+ while len(self._buffer) < n:
703
+ if self._eof:
704
+ incomplete = bytes(self._buffer)
705
+ self._buffer.clear()
706
+ raise exceptions.IncompleteReadError(incomplete, n)
707
+
708
+ await self._wait_for_data('readexactly')
709
+
710
+ if len(self._buffer) == n:
711
+ data = bytes(self._buffer)
712
+ self._buffer.clear()
713
+ else:
714
+ data = bytes(self._buffer[:n])
715
+ del self._buffer[:n]
716
+ self._maybe_resume_transport()
717
+ return data
718
+
719
+ def __aiter__(self):
720
+ return self
721
+
722
+ async def __anext__(self):
723
+ val = await self.readline()
724
+ if val == b'':
725
+ raise StopAsyncIteration
726
+ return val
evalkit_llava/lib/python3.10/asyncio/subprocess.py ADDED
@@ -0,0 +1,223 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ __all__ = 'create_subprocess_exec', 'create_subprocess_shell'
2
+
3
+ import subprocess
4
+
5
+ from . import events
6
+ from . import protocols
7
+ from . import streams
8
+ from . import tasks
9
+ from .log import logger
10
+
11
+
12
+ PIPE = subprocess.PIPE
13
+ STDOUT = subprocess.STDOUT
14
+ DEVNULL = subprocess.DEVNULL
15
+
16
+
17
+ class SubprocessStreamProtocol(streams.FlowControlMixin,
18
+ protocols.SubprocessProtocol):
19
+ """Like StreamReaderProtocol, but for a subprocess."""
20
+
21
+ def __init__(self, limit, loop):
22
+ super().__init__(loop=loop)
23
+ self._limit = limit
24
+ self.stdin = self.stdout = self.stderr = None
25
+ self._transport = None
26
+ self._process_exited = False
27
+ self._pipe_fds = []
28
+ self._stdin_closed = self._loop.create_future()
29
+
30
+ def __repr__(self):
31
+ info = [self.__class__.__name__]
32
+ if self.stdin is not None:
33
+ info.append(f'stdin={self.stdin!r}')
34
+ if self.stdout is not None:
35
+ info.append(f'stdout={self.stdout!r}')
36
+ if self.stderr is not None:
37
+ info.append(f'stderr={self.stderr!r}')
38
+ return '<{}>'.format(' '.join(info))
39
+
40
+ def connection_made(self, transport):
41
+ self._transport = transport
42
+
43
+ stdout_transport = transport.get_pipe_transport(1)
44
+ if stdout_transport is not None:
45
+ self.stdout = streams.StreamReader(limit=self._limit,
46
+ loop=self._loop)
47
+ self.stdout.set_transport(stdout_transport)
48
+ self._pipe_fds.append(1)
49
+
50
+ stderr_transport = transport.get_pipe_transport(2)
51
+ if stderr_transport is not None:
52
+ self.stderr = streams.StreamReader(limit=self._limit,
53
+ loop=self._loop)
54
+ self.stderr.set_transport(stderr_transport)
55
+ self._pipe_fds.append(2)
56
+
57
+ stdin_transport = transport.get_pipe_transport(0)
58
+ if stdin_transport is not None:
59
+ self.stdin = streams.StreamWriter(stdin_transport,
60
+ protocol=self,
61
+ reader=None,
62
+ loop=self._loop)
63
+
64
+ def pipe_data_received(self, fd, data):
65
+ if fd == 1:
66
+ reader = self.stdout
67
+ elif fd == 2:
68
+ reader = self.stderr
69
+ else:
70
+ reader = None
71
+ if reader is not None:
72
+ reader.feed_data(data)
73
+
74
+ def pipe_connection_lost(self, fd, exc):
75
+ if fd == 0:
76
+ pipe = self.stdin
77
+ if pipe is not None:
78
+ pipe.close()
79
+ self.connection_lost(exc)
80
+ if exc is None:
81
+ self._stdin_closed.set_result(None)
82
+ else:
83
+ self._stdin_closed.set_exception(exc)
84
+ return
85
+ if fd == 1:
86
+ reader = self.stdout
87
+ elif fd == 2:
88
+ reader = self.stderr
89
+ else:
90
+ reader = None
91
+ if reader is not None:
92
+ if exc is None:
93
+ reader.feed_eof()
94
+ else:
95
+ reader.set_exception(exc)
96
+
97
+ if fd in self._pipe_fds:
98
+ self._pipe_fds.remove(fd)
99
+ self._maybe_close_transport()
100
+
101
+ def process_exited(self):
102
+ self._process_exited = True
103
+ self._maybe_close_transport()
104
+
105
+ def _maybe_close_transport(self):
106
+ if len(self._pipe_fds) == 0 and self._process_exited:
107
+ self._transport.close()
108
+ self._transport = None
109
+
110
+ def _get_close_waiter(self, stream):
111
+ if stream is self.stdin:
112
+ return self._stdin_closed
113
+
114
+
115
+ class Process:
116
+ def __init__(self, transport, protocol, loop):
117
+ self._transport = transport
118
+ self._protocol = protocol
119
+ self._loop = loop
120
+ self.stdin = protocol.stdin
121
+ self.stdout = protocol.stdout
122
+ self.stderr = protocol.stderr
123
+ self.pid = transport.get_pid()
124
+
125
+ def __repr__(self):
126
+ return f'<{self.__class__.__name__} {self.pid}>'
127
+
128
+ @property
129
+ def returncode(self):
130
+ return self._transport.get_returncode()
131
+
132
+ async def wait(self):
133
+ """Wait until the process exit and return the process return code."""
134
+ return await self._transport._wait()
135
+
136
+ def send_signal(self, signal):
137
+ self._transport.send_signal(signal)
138
+
139
+ def terminate(self):
140
+ self._transport.terminate()
141
+
142
+ def kill(self):
143
+ self._transport.kill()
144
+
145
+ async def _feed_stdin(self, input):
146
+ debug = self._loop.get_debug()
147
+ self.stdin.write(input)
148
+ if debug:
149
+ logger.debug(
150
+ '%r communicate: feed stdin (%s bytes)', self, len(input))
151
+ try:
152
+ await self.stdin.drain()
153
+ except (BrokenPipeError, ConnectionResetError) as exc:
154
+ # communicate() ignores BrokenPipeError and ConnectionResetError
155
+ if debug:
156
+ logger.debug('%r communicate: stdin got %r', self, exc)
157
+
158
+ if debug:
159
+ logger.debug('%r communicate: close stdin', self)
160
+ self.stdin.close()
161
+
162
+ async def _noop(self):
163
+ return None
164
+
165
+ async def _read_stream(self, fd):
166
+ transport = self._transport.get_pipe_transport(fd)
167
+ if fd == 2:
168
+ stream = self.stderr
169
+ else:
170
+ assert fd == 1
171
+ stream = self.stdout
172
+ if self._loop.get_debug():
173
+ name = 'stdout' if fd == 1 else 'stderr'
174
+ logger.debug('%r communicate: read %s', self, name)
175
+ output = await stream.read()
176
+ if self._loop.get_debug():
177
+ name = 'stdout' if fd == 1 else 'stderr'
178
+ logger.debug('%r communicate: close %s', self, name)
179
+ transport.close()
180
+ return output
181
+
182
+ async def communicate(self, input=None):
183
+ if input is not None:
184
+ stdin = self._feed_stdin(input)
185
+ else:
186
+ stdin = self._noop()
187
+ if self.stdout is not None:
188
+ stdout = self._read_stream(1)
189
+ else:
190
+ stdout = self._noop()
191
+ if self.stderr is not None:
192
+ stderr = self._read_stream(2)
193
+ else:
194
+ stderr = self._noop()
195
+ stdin, stdout, stderr = await tasks.gather(stdin, stdout, stderr)
196
+ await self.wait()
197
+ return (stdout, stderr)
198
+
199
+
200
+ async def create_subprocess_shell(cmd, stdin=None, stdout=None, stderr=None,
201
+ limit=streams._DEFAULT_LIMIT, **kwds):
202
+ loop = events.get_running_loop()
203
+ protocol_factory = lambda: SubprocessStreamProtocol(limit=limit,
204
+ loop=loop)
205
+ transport, protocol = await loop.subprocess_shell(
206
+ protocol_factory,
207
+ cmd, stdin=stdin, stdout=stdout,
208
+ stderr=stderr, **kwds)
209
+ return Process(transport, protocol, loop)
210
+
211
+
212
+ async def create_subprocess_exec(program, *args, stdin=None, stdout=None,
213
+ stderr=None, limit=streams._DEFAULT_LIMIT,
214
+ **kwds):
215
+ loop = events.get_running_loop()
216
+ protocol_factory = lambda: SubprocessStreamProtocol(limit=limit,
217
+ loop=loop)
218
+ transport, protocol = await loop.subprocess_exec(
219
+ protocol_factory,
220
+ program, *args,
221
+ stdin=stdin, stdout=stdout,
222
+ stderr=stderr, **kwds)
223
+ return Process(transport, protocol, loop)