[tor-commits] [stem/master] Adding a default arg to get_circuits()

atagar at torproject.org atagar at torproject.org
Mon Dec 31 03:13:07 UTC 2012


commit 22f167fa48bf51d0f66736624293050bb022a936
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Dec 30 15:03:49 2012 -0800

    Adding a default arg to get_circuits()
    
    Getter methods should have a default argument (though admittedly I've been
    doing a bad job of requiring this, #7832).
---
 stem/control.py |   36 +++++++++++++++++++++---------------
 1 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/stem/control.py b/stem/control.py
index 4d21849..641b647 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -1565,27 +1565,33 @@ class Controller(BaseController):
       
       raise ValueError("Tor presently does not have a circuit with the id of '%s'" % circuit_id)
     except Exception, exc:
-      if default: return default
-      else: raise exc
+      if default == UNDEFINED: raise exc
+      else: return default
   
-  def get_circuits(self):
+  def get_circuits(self, default = UNDEFINED):
     """
-    Provides the list of circuits Tor is currently handling.
-    
-    :returns: **list** of :class:`stem.events.CircuitEvent` objects
+    Provides tor's currently available circuits.
     
-    :raises: :class:`stem.ControllerError` if the call fails
-    """
+    :param object default: response if the query fails
     
-    circuits = []
-    response = self.get_info("circuit-status")
+    :returns: **list** of :class:`stem.events.CircuitEvent` for our circuits
     
-    for circ in response.splitlines():
-      circ_message = stem.socket.recv_message(StringIO.StringIO("650 CIRC " + circ + "\r\n"))
-      stem.response.convert("EVENT", circ_message, arrived_at = 0)
-      circuits.append(circ_message)
+    :raises: :class:`stem.ControllerError` if the call fails and no default was provided
+    """
     
-    return circuits
+    try:
+      circuits = []
+      response = self.get_info("circuit-status")
+      
+      for circ in response.splitlines():
+        circ_message = stem.socket.recv_message(StringIO.StringIO("650 CIRC " + circ + "\r\n"))
+        stem.response.convert("EVENT", circ_message, arrived_at = 0)
+        circuits.append(circ_message)
+      
+      return circuits
+    except Exception, exc:
+      if default == UNDEFINED: raise exc
+      else: return default
   
   def attach_stream(self, stream_id, circuit_id, hop = None):
     """





More information about the tor-commits mailing list