Skip to content

Commit 918f9ac

Browse files
author
Ubuntu
committed
asyncio_net/net_asyncio.py: async/await syntax for python 3.11.1 compatibility
1 parent 53b9da8 commit 918f9ac

File tree

1 file changed

+74
-52
lines changed

1 file changed

+74
-52
lines changed

rethinkdb/asyncio_net/net_asyncio.py

Lines changed: 74 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@
3939
pQuery = ql2_pb2.Query.QueryType
4040

4141

42-
@asyncio.coroutine
43-
def _read_until(streamreader, delimiter):
42+
# @asyncio.coroutine
43+
async def _read_until(streamreader, delimiter):
4444
"""Naive implementation of reading until a delimiter"""
4545
buffer = bytearray()
4646

4747
while True:
48-
c = yield from streamreader.read(1)
48+
# c = yield from streamreader.read(1)
49+
c = await streamreader.read(1)
4950
if c == b"":
5051
break # EOF
5152
buffer.append(c[0])
@@ -69,13 +70,14 @@ def reusable_waiter(loop, timeout):
6970
else:
7071
deadline = None
7172

72-
@asyncio.coroutine
73-
def wait(future):
73+
# @asyncio.coroutine
74+
async def wait(future):
7475
if deadline is not None:
7576
new_timeout = max(deadline - loop.time(), 0)
7677
else:
7778
new_timeout = None
78-
return (yield from asyncio.wait_for(future, new_timeout))
79+
# return (yield from asyncio.wait_for(future, new_timeout))
80+
return (await asyncio.wait_for(future, new_timeout))
7981

8082
return wait
8183

@@ -101,20 +103,22 @@ def __init__(self, *args, **kwargs):
101103
def __aiter__(self):
102104
return self
103105

104-
@asyncio.coroutine
105-
def __anext__(self):
106+
# @asyncio.coroutine
107+
async def __anext__(self):
106108
try:
107-
return (yield from self._get_next(None))
109+
# return (yield from self._get_next(None))
110+
return (await self._get_next(None))
108111
except ReqlCursorEmpty:
109112
raise StopAsyncIteration
110113

111-
@asyncio.coroutine
112-
def close(self):
114+
# @asyncio.coroutine
115+
async def close(self):
113116
if self.error is None:
114117
self.error = self._empty_error()
115118
if self.conn.is_open():
116119
self.outstanding_requests += 1
117-
yield from self.conn._parent._stop(self)
120+
# yield from self.conn._parent._stop(self)
121+
await self.conn._parent._stop(self)
118122

119123
def _extend(self, res_buf):
120124
Cursor._extend(self, res_buf)
@@ -123,16 +127,17 @@ def _extend(self, res_buf):
123127

124128
# Convenience function so users know when they've hit the end of the cursor
125129
# without having to catch an exception
126-
@asyncio.coroutine
127-
def fetch_next(self, wait=True):
130+
# @asyncio.coroutine
131+
async def fetch_next(self, wait=True):
128132
timeout = Cursor._wait_to_timeout(wait)
129133
waiter = reusable_waiter(self.conn._io_loop, timeout)
130134
while len(self.items) == 0 and self.error is None:
131135
self._maybe_fetch_batch()
132136
if self.error is not None:
133137
raise self.error
134138
with translate_timeout_errors():
135-
yield from waiter(asyncio.shield(self.new_response))
139+
# yield from waiter(asyncio.shield(self.new_response))
140+
await waiter(asyncio.shield(self.new_response))
136141
# If there is a (non-empty) error to be received, we return True, so the
137142
# user will receive it on the next `next` call.
138143
return len(self.items) != 0 or not isinstance(self.error, RqlCursorEmpty)
@@ -142,15 +147,16 @@ def _empty_error(self):
142147
# with mechanisms to return from a coroutine.
143148
return RqlCursorEmpty()
144149

145-
@asyncio.coroutine
146-
def _get_next(self, timeout):
150+
# @asyncio.coroutine
151+
async def _get_next(self, timeout):
147152
waiter = reusable_waiter(self.conn._io_loop, timeout)
148153
while len(self.items) == 0:
149154
self._maybe_fetch_batch()
150155
if self.error is not None:
151156
raise self.error
152157
with translate_timeout_errors():
153-
yield from waiter(asyncio.shield(self.new_response))
158+
# yield from waiter(asyncio.shield(self.new_response))
159+
await waiter(asyncio.shield(self.new_response))
154160
return self.items.popleft()
155161

156162
def _maybe_fetch_batch(self):
@@ -187,8 +193,8 @@ def client_address(self):
187193
if self.is_open():
188194
return self._streamwriter.get_extra_info("sockname")[0]
189195

190-
@asyncio.coroutine
191-
def connect(self, timeout):
196+
# @asyncio.coroutine
197+
async def connect(self, timeout):
192198
try:
193199
ssl_context = None
194200
if len(self._parent.ssl) > 0:
@@ -200,7 +206,8 @@ def connect(self, timeout):
200206
ssl_context.check_hostname = True # redundant with match_hostname
201207
ssl_context.load_verify_locations(self._parent.ssl["ca_certs"])
202208

203-
self._streamreader, self._streamwriter = yield from asyncio.open_connection(
209+
# self._streamreader, self._streamwriter = yield from asyncio.open_connection(
210+
self._streamreader, self._streamwriter = await asyncio.open_connection(
204211
self._parent.host,
205212
self._parent.port,
206213
ssl=ssl_context,
@@ -230,22 +237,26 @@ def connect(self, timeout):
230237
if request is not "":
231238
self._streamwriter.write(request)
232239

233-
response = yield from asyncio.wait_for(
240+
# response = yield from asyncio.wait_for(
241+
response = await asyncio.wait_for(
234242
_read_until(self._streamreader, b"\0"),
235243
timeout,
236244
)
237245
response = response[:-1]
238246
except ReqlAuthError:
239-
yield from self.close()
247+
# yield from self.close()
248+
await self.close()
240249
raise
241250
except ReqlTimeoutError as err:
242-
yield from self.close()
251+
# yield from self.close()
252+
await self.close()
243253
raise ReqlDriverError(
244254
"Connection interrupted during handshake with %s:%s. Error: %s"
245255
% (self._parent.host, self._parent.port, str(err))
246256
)
247257
except Exception as err:
248-
yield from self.close()
258+
# yield from self.close()
259+
await self.close()
249260
raise ReqlDriverError(
250261
"Could not connect to %s:%s. Error: %s"
251262
% (self._parent.host, self._parent.port, str(err))
@@ -259,8 +270,8 @@ def connect(self, timeout):
259270
def is_open(self):
260271
return not (self._closing or self._streamreader.at_eof())
261272

262-
@asyncio.coroutine
263-
def close(self, noreply_wait=False, token=None, exception=None):
273+
# @asyncio.coroutine
274+
async def close(self, noreply_wait=False, token=None, exception=None):
264275
self._closing = True
265276
if exception is not None:
266277
err_message = "Connection is closed (%s)." % str(exception)
@@ -280,38 +291,43 @@ def close(self, noreply_wait=False, token=None, exception=None):
280291

281292
if noreply_wait:
282293
noreply = Query(pQuery.NOREPLY_WAIT, token, None, None)
283-
yield from self.run_query(noreply, False)
294+
# yield from self.run_query(noreply, False)
295+
await self.run_query(noreply, False)
284296

285297
self._streamwriter.close()
286298
# We must not wait for the _reader_task if we got an exception, because that
287299
# means that we were called from it. Waiting would lead to a deadlock.
288300
if self._reader_task and exception is None:
289-
yield from self._reader_task
301+
# yield from self._reader_task
302+
await self._reader_task
290303

291304
return None
292305

293-
@asyncio.coroutine
294-
def run_query(self, query, noreply):
306+
# @asyncio.coroutine
307+
async def run_query(self, query, noreply):
295308
self._streamwriter.write(query.serialize(self._parent._get_json_encoder(query)))
296309
if noreply:
297310
return None
298311

299312
response_future = asyncio.Future()
300313
self._user_queries[query.token] = (query, response_future)
301-
return (yield from response_future)
314+
# return (yield from response_future)
315+
return (await response_future)
302316

303317
# The _reader coroutine runs in parallel, reading responses
304318
# off of the socket and forwarding them to the appropriate Future or Cursor.
305319
# This is shut down as a consequence of closing the stream, or an error in the
306320
# socket/protocol from the server. Unexpected errors in this coroutine will
307321
# close the ConnectionInstance and be passed to any open Futures or Cursors.
308-
@asyncio.coroutine
309-
def _reader(self):
322+
# @asyncio.coroutine
323+
async def _reader(self):
310324
try:
311325
while True:
312-
buf = yield from self._streamreader.readexactly(12)
326+
# buf = yield from self._streamreader.readexactly(12)
327+
buf = await self._streamreader.readexactly(12)
313328
(token, length,) = struct.unpack("<qL", buf)
314-
buf = yield from self._streamreader.readexactly(length)
329+
# buf = yield from self._streamreader.readexactly(length)
330+
buf = await self._streamreader.readexactly(length)
315331

316332
cursor = self._cursor_cache.get(token)
317333
if cursor is not None:
@@ -340,7 +356,8 @@ def _reader(self):
340356
raise ReqlDriverError("Unexpected response received.")
341357
except Exception as ex:
342358
if not self._closing:
343-
yield from self.close(exception=ex)
359+
# yield from self.close(exception=ex)
360+
await self.close(exception=ex)
344361

345362

346363
class Connection(ConnectionBase):
@@ -353,30 +370,35 @@ def __init__(self, *args, **kwargs):
353370
"Could not convert port %s to an integer." % self.port
354371
)
355372

356-
@asyncio.coroutine
357-
def __aenter__(self):
373+
# @asyncio.coroutine
374+
async def __aenter__(self):
358375
return self
359376

360-
@asyncio.coroutine
361-
def __aexit__(self, exception_type, exception_val, traceback):
362-
yield from self.close(False)
377+
# @asyncio.coroutine
378+
async def __aexit__(self, exception_type, exception_val, traceback):
379+
# yield from self.close(False)
380+
await self.close(False)
363381

364-
@asyncio.coroutine
365-
def _stop(self, cursor):
382+
# @asyncio.coroutine
383+
async def _stop(self, cursor):
366384
self.check_open()
367385
q = Query(pQuery.STOP, cursor.query.token, None, None)
368-
return (yield from self._instance.run_query(q, True))
386+
# return (yield from self._instance.run_query(q, True))
387+
return (await self._instance.run_query(q, True))
369388

370-
@asyncio.coroutine
371-
def reconnect(self, noreply_wait=True, timeout=None):
389+
# @asyncio.coroutine
390+
async def reconnect(self, noreply_wait=True, timeout=None):
372391
# We close before reconnect so reconnect doesn't try to close us
373392
# and then fail to return the Future (this is a little awkward).
374-
yield from self.close(noreply_wait)
393+
# yield from self.close(noreply_wait)
394+
await self.close(noreply_wait)
375395
self._instance = self._conn_type(self, **self._child_kwargs)
376-
return (yield from self._instance.connect(timeout))
396+
# return (yield from self._instance.connect(timeout))
397+
return (await self._instance.connect(timeout))
377398

378-
@asyncio.coroutine
379-
def close(self, noreply_wait=True):
399+
# @asyncio.coroutine
400+
async def close(self, noreply_wait=True):
380401
if self._instance is None:
381402
return None
382-
return (yield from ConnectionBase.close(self, noreply_wait=noreply_wait))
403+
# return (yield from ConnectionBase.close(self, noreply_wait=noreply_wait))
404+
return (await ConnectionBase.close(self, noreply_wait=noreply_wait))

0 commit comments

Comments
 (0)