[tor-commits] [stem/master] Make `get_circuit` and `get_circuits` of `Controller` asynchronous

atagar at torproject.org atagar at torproject.org
Thu Jul 16 01:28:58 UTC 2020


commit 3ea6be9ed59fdd28fed568676f5624f7ffa7082c
Author: Illia Volochii <illia.volochii at gmail.com>
Date:   Tue Apr 21 21:48:40 2020 +0300

    Make `get_circuit` and `get_circuits` of `Controller` asynchronous
---
 stem/control.py | 10 +++++-----
 stem/socket.py  |  4 +++-
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/stem/control.py b/stem/control.py
index d0cbb34a..4f56bb95 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -3353,7 +3353,7 @@ class Controller(BaseController):
     self._enabled_features += [entry.upper() for entry in features]
 
   @with_default()
-  def get_circuit(self, circuit_id: int, default: Any = UNDEFINED) -> stem.response.events.CircuitEvent:
+  async def get_circuit(self, circuit_id: int, default: Any = UNDEFINED) -> stem.response.events.CircuitEvent:
     """
     get_circuit(circuit_id, default = UNDEFINED)
 
@@ -3371,14 +3371,14 @@ class Controller(BaseController):
       An exception is only raised if we weren't provided a default response.
     """
 
-    for circ in self.get_circuits():
+    for circ in await self.get_circuits():
       if circ.id == circuit_id:
         return circ
 
     raise ValueError("Tor currently does not have a circuit with the id of '%s'" % circuit_id)
 
   @with_default()
-  def get_circuits(self, default: Any = UNDEFINED) -> List[stem.response.events.CircuitEvent]:
+  async def get_circuits(self, default: Any = UNDEFINED) -> List[stem.response.events.CircuitEvent]:
     """
     get_circuits(default = UNDEFINED)
 
@@ -3392,10 +3392,10 @@ class Controller(BaseController):
     """
 
     circuits = []  # type: List[stem.response.events.CircuitEvent]
-    response = self.get_info('circuit-status')
+    response = await self.get_info('circuit-status')
 
     for circ in response.splitlines():
-      circ_message = stem.response._convert_to_event(stem.socket.recv_message(io.BytesIO(stem.util.str_tools._to_bytes('650 CIRC %s\r\n' % circ))))
+      circ_message = stem.response._convert_to_event(await stem.socket.recv_message(io.BytesIO(stem.util.str_tools._to_bytes('650 CIRC %s\r\n' % circ))))
       circuits.append(circ_message)  # type: ignore
 
     return circuits
diff --git a/stem/socket.py b/stem/socket.py
index dd123751..a82c4892 100644
--- a/stem/socket.py
+++ b/stem/socket.py
@@ -588,7 +588,9 @@ async def recv_message(reader: asyncio.StreamReader, arrived_at: Optional[float]
 
   while True:
     try:
-      line = await reader.readline()
+      line = reader.readline()
+      if asyncio.iscoroutine(line):
+        line = await line
     except AttributeError:
       # if the control_file has been closed then we will receive:
       # AttributeError: 'NoneType' object has no attribute 'recv'





More information about the tor-commits mailing list