[tor-commits] [arm/master] Dropping getOption()'s suppressExc arg

atagar at torproject.org atagar at torproject.org
Mon Dec 17 04:25:17 UTC 2012


commit 62ab6da737090f9f013b00f606f28ac0f84b7e37
Author: Damian Johnson <atagar at torproject.org>
Date:   Wed Dec 12 08:50:04 2012 -0800

    Dropping getOption()'s suppressExc arg
    
    Like getInfo() we're going to be doing exception suppression by the presence of
    a default (this is both what stem does, and a more intuitive usage).
---
 src/cli/connections/connEntry.py   |   10 +++---
 src/cli/connections/connPanel.py   |    2 +-
 src/cli/controller.py              |    2 +-
 src/cli/graphing/bandwidthStats.py |    4 +-
 src/cli/headerPanel.py             |    6 ++--
 src/util/torTools.py               |   57 +++++++++++++++++-------------------
 6 files changed, 39 insertions(+), 42 deletions(-)

diff --git a/src/cli/connections/connEntry.py b/src/cli/connections/connEntry.py
index f7f08c3..7650149 100644
--- a/src/cli/connections/connEntry.py
+++ b/src/cli/connections/connEntry.py
@@ -218,14 +218,14 @@ class ConnectionLine(entries.ConnectionPanelLine):
     self.appPid = None
     self.isAppResolving = False
     
-    myOrPort = conn.getOption("ORPort")
-    myDirPort = conn.getOption("DirPort")
+    myOrPort = conn.getOption("ORPort", None)
+    myDirPort = conn.getOption("DirPort", None)
     mySocksPort = conn.getOption("SocksPort", "9050")
-    myCtlPort = conn.getOption("ControlPort")
+    myCtlPort = conn.getOption("ControlPort", None)
     myHiddenServicePorts = conn.getHiddenServicePorts()
     
     # the ORListenAddress can overwrite the ORPort
-    listenAddr = conn.getOption("ORListenAddress")
+    listenAddr = conn.getOption("ORListenAddress", None)
     if listenAddr and ":" in listenAddr:
       myOrPort = listenAddr[listenAddr.find(":") + 1:]
     
@@ -363,7 +363,7 @@ class ConnectionLine(entries.ConnectionPanelLine):
       # known relay then it might be client traffic
       
       conn = torTools.getConn()
-      if "Guard" in conn.getMyFlags([]) or conn.getOption("BridgeRelay") == "1":
+      if "Guard" in conn.getMyFlags([]) or conn.getOption("BridgeRelay", None) == "1":
         allMatches = conn.getRelayFingerprint(self.foreign.getIpAddr(), getAllMatches = True)
         return allMatches == []
     elif myType == Category.EXIT:
diff --git a/src/cli/connections/connPanel.py b/src/cli/connections/connPanel.py
index 927826b..632669c 100644
--- a/src/cli/connections/connPanel.py
+++ b/src/cli/connections/connPanel.py
@@ -188,7 +188,7 @@ class ConnectionPanel(panel.Panel, threading.Thread):
     """
     
     conn = torTools.getConn()
-    return "Guard" in conn.getMyFlags([]) or conn.getOption("BridgeRelay") == "1"
+    return "Guard" in conn.getMyFlags([]) or conn.getOption("BridgeRelay", None) == "1"
   
   def isExitsAllowed(self):
     """
diff --git a/src/cli/controller.py b/src/cli/controller.py
index e79ca5a..9c3a4eb 100644
--- a/src/cli/controller.py
+++ b/src/cli/controller.py
@@ -672,7 +672,7 @@ def startTorMonitor(startTime):
     # functioning. It'll have circuits, but little else. If this is the case then
     # notify the user and tell them what they can do to fix it.
     
-    if conn.getOption("DisableDebuggerAttachment") == "1":
+    if conn.getOption("DisableDebuggerAttachment", None) == "1":
       log.log(log.NOTICE, "Tor is preventing system utilities like netstat and lsof from working. This means that arm can't provide you with connection information. You can change this by adding 'DisableDebuggerAttachment 0' to your torrc and restarting tor. For more information see...\nhttps://trac.torproject.org/3313")
       connections.getResolver("tor").setPaused(True)
     else:
diff --git a/src/cli/graphing/bandwidthStats.py b/src/cli/graphing/bandwidthStats.py
index 390ad16..2b60c7e 100644
--- a/src/cli/graphing/bandwidthStats.py
+++ b/src/cli/graphing/bandwidthStats.py
@@ -115,7 +115,7 @@ class BandwidthStats(graphPanel.GraphStats):
     
     # checks that this is a relay (if ORPort is unset, then skip)
     conn = torTools.getConn()
-    orPort = conn.getOption("ORPort")
+    orPort = conn.getOption("ORPort", None)
     if orPort == "0": return
     
     # gets the uptime (using the same parameters as the header panel to take
@@ -140,7 +140,7 @@ class BandwidthStats(graphPanel.GraphStats):
       return False
     
     # get the user's data directory (usually '~/.tor')
-    dataDir = conn.getOption("DataDirectory")
+    dataDir = conn.getOption("DataDirectory", None)
     if not dataDir:
       msg = PREPOPULATE_FAILURE_MSG % "data directory not found"
       log.log(self._config["log.graph.bw.prepopulateFailure"], msg)
diff --git a/src/cli/headerPanel.py b/src/cli/headerPanel.py
index 50222f1..c9541c4 100644
--- a/src/cli/headerPanel.py
+++ b/src/cli/headerPanel.py
@@ -510,15 +510,15 @@ class HeaderPanel(panel.Panel, threading.Thread):
       self.vals["tor/dirPort"] = conn.getOption("DirPort", "0")
       self.vals["tor/controlPort"] = conn.getOption("ControlPort", "0")
       self.vals["tor/socketPath"] = conn.getOption("ControlSocket", "")
-      self.vals["tor/isAuthPassword"] = conn.getOption("HashedControlPassword") != None
-      self.vals["tor/isAuthCookie"] = conn.getOption("CookieAuthentication") == "1"
+      self.vals["tor/isAuthPassword"] = conn.getOption("HashedControlPassword", None) != None
+      self.vals["tor/isAuthCookie"] = conn.getOption("CookieAuthentication", None) == "1"
       
       # orport is reported as zero if unset
       if self.vals["tor/orPort"] == "0": self.vals["tor/orPort"] = ""
       
       # overwrite address if ORListenAddress is set (and possibly orPort too)
       self.vals["tor/orListenAddr"] = ""
-      listenAddr = conn.getOption("ORListenAddress")
+      listenAddr = conn.getOption("ORListenAddress", None)
       if listenAddr:
         if ":" in listenAddr:
           # both ip and port overwritten
diff --git a/src/util/torTools.py b/src/util/torTools.py
index 86b7ee9..464690f 100644
--- a/src/util/torTools.py
+++ b/src/util/torTools.py
@@ -723,8 +723,8 @@ class Controller(TorCtl.PostEventListener):
     response, control port closed, initiated, etc).
     
     Arguments:
-      param       - GETINFO option to be queried
-      default     - result if the query fails
+      param   - GETINFO option to be queried
+      default - result if the query fails
     """
     
     self.connLock.acquire()
@@ -740,7 +740,7 @@ class Controller(TorCtl.PostEventListener):
     finally:
       self.connLock.release()
   
-  def getOption(self, param, default = None, multiple = False, suppressExc = True):
+  def getOption(self, param, default = UNDEFINED, multiple = False):
     """
     Queries the control port for the given configuration option, providing the
     default if the response is undefined or fails for any reason. If multiple
@@ -748,12 +748,10 @@ class Controller(TorCtl.PostEventListener):
     flag is set.
     
     Arguments:
-      param       - configuration option to be queried
-      default     - result if the query fails and exception's suppressed
-      multiple    - provides a list with all returned values if true, otherwise
-                    this just provides the first result
-      suppressExc - suppresses lookup errors (returning the default) if true,
-                    otherwise this raises the original exception
+      param     - configuration option to be queried
+      default   - result if the query fails
+      multiple  - provides a list with all returned values if true, otherwise
+                  this just provides the first result
     """
     
     fetchType = "list" if multiple else "str"
@@ -762,15 +760,15 @@ class Controller(TorCtl.PostEventListener):
       # This is among the options fetched via a special command. The results
       # are a set of values that (hopefully) contain the one we were
       # requesting.
-      configMappings = self._getOption(CONFIG["torrc.map"][param], default, "map", suppressExc)
+      configMappings = self._getOption(CONFIG["torrc.map"][param], default, "map")
       if param in configMappings:
         if fetchType == "list": return configMappings[param]
         else: return configMappings[param][0]
       else: return default
     else:
-      return self._getOption(param, default, fetchType, suppressExc)
+      return self._getOption(param, default, fetchType)
   
-  def getOptionMap(self, param, default = None, suppressExc = True):
+  def getOptionMap(self, param, default = UNDEFINED):
     """
     Queries the control port for the given configuration option, providing back
     a mapping of config options to a list of the values returned.
@@ -792,17 +790,15 @@ class Controller(TorCtl.PostEventListener):
     single response.
     
     Arguments:
-      param       - configuration option to be queried
-      default     - result if the query fails and exception's suppressed
-      suppressExc - suppresses lookup errors (returning the default) if true,
-                    otherwise this raises the original exception
+      param   - configuration option to be queried
+      default - result if the query fails
     """
     
-    return self._getOption(param, default, "map", suppressExc)
+    return self._getOption(param, default, "map")
   
   # TODO: cache isn't updated (or invalidated) during SETCONF events:
   # https://trac.torproject.org/projects/tor/ticket/1692
-  def _getOption(self, param, default, fetchType, suppressExc):
+  def _getOption(self, param, default, fetchType):
     if not fetchType in ("str", "list", "map"):
       msg = "BUG: unrecognized fetchType in torTools._getOption (%s)" % fetchType
       log.log(log.ERR, msg)
@@ -847,7 +843,8 @@ class Controller(TorCtl.PostEventListener):
     
     self.connLock.release()
     
-    if not suppressExc and raisedExc: raise raisedExc
+    if raisedExc and default == UNDEFINED:
+      raise raisedExc
     elif result == []: return default
     else: return result
   
@@ -1253,7 +1250,7 @@ class Controller(TorCtl.PostEventListener):
     
     result = None
     if self.isAlive():
-      if self.getOption("ORPort"):
+      if self.getOption("ORPort", None):
         policyEntries = []
         for exitPolicy in self.getOption("ExitPolicy", [], True):
           policyEntries += [policy.strip() for policy in exitPolicy.split(",")]
@@ -1498,7 +1495,7 @@ class Controller(TorCtl.PostEventListener):
         if relayFingerprint == self.getInfo("fingerprint", None):
           # this is us, simply check the config
           myAddress = self.getInfo("address", None)
-          myOrPort = self.getOption("ORPort")
+          myOrPort = self.getOption("ORPort", None)
           
           if myAddress and myOrPort:
             self._addressLookupCache[relayFingerprint] = (myAddress, myOrPort)
@@ -1813,7 +1810,7 @@ class Controller(TorCtl.PostEventListener):
     raisedException = None
     if self.isAlive():
       try:
-        isRelay = self.getOption("ORPort") != None
+        isRelay = self.getOption("ORPort", None) != None
         signal = "HALT" if force else "SHUTDOWN"
         self.conn.send_signal(signal)
         
@@ -2014,7 +2011,7 @@ class Controller(TorCtl.PostEventListener):
     
     # checks if this matches us
     if relayAddress == self.getInfo("address", None):
-      if not relayPort or relayPort == self.getOption("ORPort"):
+      if not relayPort or relayPort == self.getOption("ORPort", None):
         return self.getInfo("fingerprint", None)
     
     # if we haven't yet populated the ip -> fingerprint mappings then do so
@@ -2137,21 +2134,21 @@ class Controller(TorCtl.PostEventListener):
       elif key == "bwRate":
         # effective relayed bandwidth is the minimum of BandwidthRate,
         # MaxAdvertisedBandwidth, and RelayBandwidthRate (if set)
-        effectiveRate = int(self.getOption("BandwidthRate"))
+        effectiveRate = int(self.getOption("BandwidthRate", None))
         
-        relayRate = self.getOption("RelayBandwidthRate")
+        relayRate = self.getOption("RelayBandwidthRate", None)
         if relayRate and relayRate != "0":
           effectiveRate = min(effectiveRate, int(relayRate))
         
-        maxAdvertised = self.getOption("MaxAdvertisedBandwidth")
+        maxAdvertised = self.getOption("MaxAdvertisedBandwidth", None)
         if maxAdvertised: effectiveRate = min(effectiveRate, int(maxAdvertised))
         
         result = effectiveRate
       elif key == "bwBurst":
         # effective burst (same for BandwidthBurst and RelayBandwidthBurst)
-        effectiveBurst = int(self.getOption("BandwidthBurst"))
+        effectiveBurst = int(self.getOption("BandwidthBurst", None))
         
-        relayBurst = self.getOption("RelayBandwidthBurst")
+        relayBurst = self.getOption("RelayBandwidthBurst", None)
         if relayBurst and relayBurst != "0":
           effectiveBurst = min(effectiveBurst, int(relayBurst))
         
@@ -2184,7 +2181,7 @@ class Controller(TorCtl.PostEventListener):
         result = self.getInfo("process/pid", None)
         
         if not result:
-          result = getPid(int(self.getOption("ControlPort", 9051)), self.getOption("PidFile"))
+          result = getPid(int(self.getOption("ControlPort", 9051)), self.getOption("PidFile", None))
       elif key == "user":
         # provides the empty string if the query fails
         queriedUser = self.getInfo("process/user", None)
@@ -2356,7 +2353,7 @@ class Controller(TorCtl.PostEventListener):
             result.append((int(lineComp[0]), lineComp[1], lineComp[3][8:], tuple(path)))
       elif key == "hsPorts":
         result = []
-        hsOptions = self.getOptionMap("HiddenServiceOptions")
+        hsOptions = self.getOptionMap("HiddenServiceOptions", None)
         
         if hsOptions and "HiddenServicePort" in hsOptions:
           for hsEntry in hsOptions["HiddenServicePort"]:





More information about the tor-commits mailing list