@@ -24,13 +24,23 @@ def current_async_library() -> str:
24
24
try :
25
25
import sniffio
26
26
except ImportError : # pragma: nocover
27
- return "asyncio"
28
-
29
- environment = sniffio .current_async_library ()
27
+ environment = "asyncio"
28
+ else :
29
+ environment = sniffio .current_async_library ()
30
30
31
31
if environment not in ("asyncio" , "trio" ): # pragma: nocover
32
32
raise RuntimeError ("Running under an unsupported async environment." )
33
33
34
+ if environment == "asyncio" and anyio is None : # pragma: nocover
35
+ raise RuntimeError (
36
+ "Running with asyncio requires installation of 'httpcore[asyncio]'."
37
+ )
38
+
39
+ if environment == "trio" and trio is None : # pragma: nocover
40
+ raise RuntimeError (
41
+ "Running with trio requires installation of 'httpcore[trio]'."
42
+ )
43
+
34
44
return environment
35
45
36
46
@@ -45,16 +55,8 @@ def setup(self) -> None:
45
55
"""
46
56
self ._backend = current_async_library ()
47
57
if self ._backend == "trio" :
48
- if trio is None : # pragma: nocover
49
- raise RuntimeError (
50
- "Running with trio requires installation of 'httpcore[trio]'."
51
- )
52
58
self ._trio_lock = trio .Lock ()
53
- else :
54
- if anyio is None : # pragma: nocover
55
- raise RuntimeError (
56
- "Running with asyncio requires installation of 'httpcore[asyncio]'."
57
- )
59
+ elif self ._backend == "asyncio" :
58
60
self ._anyio_lock = anyio .Lock ()
59
61
60
62
async def __aenter__ (self ) -> "AsyncLock" :
@@ -63,7 +65,7 @@ async def __aenter__(self) -> "AsyncLock":
63
65
64
66
if self ._backend == "trio" :
65
67
await self ._trio_lock .acquire ()
66
- else :
68
+ elif self . _backend == "asyncio" :
67
69
await self ._anyio_lock .acquire ()
68
70
69
71
return self
@@ -76,7 +78,7 @@ async def __aexit__(
76
78
) -> None :
77
79
if self ._backend == "trio" :
78
80
self ._trio_lock .release ()
79
- else :
81
+ elif self . _backend == "asyncio" :
80
82
self ._anyio_lock .release ()
81
83
82
84
@@ -91,16 +93,8 @@ def setup(self) -> None:
91
93
"""
92
94
self ._backend = current_async_library ()
93
95
if self ._backend == "trio" :
94
- if trio is None : # pragma: nocover
95
- raise RuntimeError (
96
- "Running with trio requires installation of 'httpcore[trio]'."
97
- )
98
96
self ._trio_event = trio .Event ()
99
- else :
100
- if anyio is None : # pragma: nocover
101
- raise RuntimeError (
102
- "Running with asyncio requires installation of 'httpcore[asyncio]'."
103
- )
97
+ elif self ._backend == "asyncio" :
104
98
self ._anyio_event = anyio .Event ()
105
99
106
100
def set (self ) -> None :
@@ -109,30 +103,20 @@ def set(self) -> None:
109
103
110
104
if self ._backend == "trio" :
111
105
self ._trio_event .set ()
112
- else :
106
+ elif self . _backend == "asyncio" :
113
107
self ._anyio_event .set ()
114
108
115
109
async def wait (self , timeout : Optional [float ] = None ) -> None :
116
110
if not self ._backend :
117
111
self .setup ()
118
112
119
113
if self ._backend == "trio" :
120
- if trio is None : # pragma: nocover
121
- raise RuntimeError (
122
- "Running with trio requires installation of 'httpcore[trio]'."
123
- )
124
-
125
114
trio_exc_map : ExceptionMapping = {trio .TooSlowError : PoolTimeout }
126
115
timeout_or_inf = float ("inf" ) if timeout is None else timeout
127
116
with map_exceptions (trio_exc_map ):
128
117
with trio .fail_after (timeout_or_inf ):
129
118
await self ._trio_event .wait ()
130
- else :
131
- if anyio is None : # pragma: nocover
132
- raise RuntimeError (
133
- "Running with asyncio requires installation of 'httpcore[asyncio]'."
134
- )
135
-
119
+ elif self ._backend == "asyncio" :
136
120
anyio_exc_map : ExceptionMapping = {TimeoutError : PoolTimeout }
137
121
with map_exceptions (anyio_exc_map ):
138
122
with anyio .fail_after (timeout ):
@@ -151,20 +135,10 @@ def setup(self) -> None:
151
135
"""
152
136
self ._backend = current_async_library ()
153
137
if self ._backend == "trio" :
154
- if trio is None : # pragma: nocover
155
- raise RuntimeError (
156
- "Running with trio requires installation of 'httpcore[trio]'."
157
- )
158
-
159
138
self ._trio_semaphore = trio .Semaphore (
160
139
initial_value = self ._bound , max_value = self ._bound
161
140
)
162
- else :
163
- if anyio is None : # pragma: nocover
164
- raise RuntimeError (
165
- "Running with asyncio requires installation of 'httpcore[asyncio]'."
166
- )
167
-
141
+ elif self ._backend == "asyncio" :
168
142
self ._anyio_semaphore = anyio .Semaphore (
169
143
initial_value = self ._bound , max_value = self ._bound
170
144
)
@@ -175,13 +149,13 @@ async def acquire(self) -> None:
175
149
176
150
if self ._backend == "trio" :
177
151
await self ._trio_semaphore .acquire ()
178
- else :
152
+ elif self . _backend == "asyncio" :
179
153
await self ._anyio_semaphore .acquire ()
180
154
181
155
async def release (self ) -> None :
182
156
if self ._backend == "trio" :
183
157
self ._trio_semaphore .release ()
184
- else :
158
+ elif self . _backend == "asyncio" :
185
159
self ._anyio_semaphore .release ()
186
160
187
161
@@ -201,24 +175,14 @@ def __init__(self) -> None:
201
175
self ._backend = current_async_library ()
202
176
203
177
if self ._backend == "trio" :
204
- if trio is None : # pragma: nocover
205
- raise RuntimeError (
206
- "Running with trio requires installation of 'httpcore[trio]'."
207
- )
208
-
209
178
self ._trio_shield = trio .CancelScope (shield = True )
210
- else :
211
- if anyio is None : # pragma: nocover
212
- raise RuntimeError (
213
- "Running with asyncio requires installation of 'httpcore[asyncio]'."
214
- )
215
-
179
+ elif self ._backend == "asyncio" :
216
180
self ._anyio_shield = anyio .CancelScope (shield = True )
217
181
218
182
def __enter__ (self ) -> "AsyncShieldCancellation" :
219
183
if self ._backend == "trio" :
220
184
self ._trio_shield .__enter__ ()
221
- else :
185
+ elif self . _backend == "asyncio" :
222
186
self ._anyio_shield .__enter__ ()
223
187
return self
224
188
@@ -230,7 +194,7 @@ def __exit__(
230
194
) -> None :
231
195
if self ._backend == "trio" :
232
196
self ._trio_shield .__exit__ (exc_type , exc_value , traceback )
233
- else :
197
+ elif self . _backend == "asyncio" :
234
198
self ._anyio_shield .__exit__ (exc_type , exc_value , traceback )
235
199
236
200
0 commit comments