[tor-commits] [stem/master] Changing None checks to be by identity

atagar at torproject.org atagar at torproject.org
Mon Apr 16 01:37:20 UTC 2012


commit 67dc38b9c7d9304caa1bffde05e912849f7dc5f1
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Apr 15 16:21:48 2012 -0700

    Changing None checks to be by identity
    
    The 'is' keyword is almost completely useless, but the one place where identity
    checks are preferred is when checking for None. It's faster and won't be
    tricked by buggy equality methods...
    http://jaredgrubb.blogspot.com/2009/04/python-is-none-vs-none.html
    
    This is the suggestion as per PEP 8...
    http://www.python.org/dev/peps/pep-0008/#programming-recommendations
---
 stem/connection.py                         |    6 +++---
 stem/descriptor/server_descriptor.py       |    4 ++--
 stem/util/conf.py                          |    4 ++--
 stem/util/proc.py                          |    4 ++--
 test/integ/descriptor/server_descriptor.py |    2 +-
 test/integ/util/system.py                  |    2 +-
 test/output.py                             |    2 +-
 test/unit/descriptor/server_descriptor.py  |    4 ++--
 8 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/stem/connection.py b/stem/connection.py
index 0fd2017..fdb3a09 100644
--- a/stem/connection.py
+++ b/stem/connection.py
@@ -259,7 +259,7 @@ def _connect(control_socket, password, controller):
     if controller == Controller.NONE:
       return control_socket
   except MissingPassword:
-    assert password == None, "BUG: authenticate raised MissingPassword despite getting one"
+    assert password is None, "BUG: authenticate raised MissingPassword despite getting one"
     
     try: password = getpass.getpass("Controller password: ")
     except KeyboardInterrupt: return None
@@ -375,11 +375,11 @@ def authenticate(control_socket, password = None, protocolinfo_response = None):
     else:
       log.debug("Authenticating to a socket with unrecognized auth method%s, ignoring them: %s" % (plural_label, methods_label))
   
-  if AuthMethod.COOKIE in auth_methods and protocolinfo_response.cookie_path == None:
+  if AuthMethod.COOKIE in auth_methods and protocolinfo_response.cookie_path is None:
     auth_methods.remove(AuthMethod.COOKIE)
     auth_exceptions.append(NoAuthCookie("our PROTOCOLINFO response did not have the location of our authentication cookie"))
   
-  if AuthMethod.PASSWORD in auth_methods and password == None:
+  if AuthMethod.PASSWORD in auth_methods and password is None:
     auth_methods.remove(AuthMethod.PASSWORD)
     auth_exceptions.append(MissingPassword("no passphrase provided"))
   
diff --git a/stem/descriptor/server_descriptor.py b/stem/descriptor/server_descriptor.py
index 780e7b5..525322c 100644
--- a/stem/descriptor/server_descriptor.py
+++ b/stem/descriptor/server_descriptor.py
@@ -312,7 +312,7 @@ class ServerDescriptorV3(stem.descriptor.Descriptor):
       dict with the key/value pairs in our annotations
     """
     
-    if self._annotation_dict == None:
+    if self._annotation_dict is None:
       annotation_dict = {}
       
       for line in self._annotation_lines:
@@ -619,7 +619,7 @@ class RelayDescriptorV3(ServerDescriptorV3):
       str with the digest value for this server descriptor
     """
     
-    if self._digest == None:
+    if self._digest is None:
       # our digest is calculated from everything except our signature
       raw_content, ending = str(self), "\nrouter-signature\n"
       raw_content = raw_content[:raw_content.find(ending) + len(ending)]
diff --git a/stem/util/conf.py b/stem/util/conf.py
index 05e98f8..7c07a31 100644
--- a/stem/util/conf.py
+++ b/stem/util/conf.py
@@ -307,7 +307,7 @@ class Config():
                              (min, max)"
     """
     
-    if limits == None: limits = {}
+    if limits is None: limits = {}
     
     for entry in conf_mappings.keys():
       val = self.get(entry, conf_mappings[entry])
@@ -518,7 +518,7 @@ class Config():
     if sub_key: conf_value = self.get(key, {}).get(sub_key)
     else: conf_value = self.get_value(key)
     
-    if conf_value == None: return default
+    if conf_value is None: return default
     elif not conf_value.strip(): return [] # empty string
     else:
       conf_comp = [entry.strip() for entry in conf_value.split(",")]
diff --git a/stem/util/proc.py b/stem/util/proc.py
index 7cf12bf..1d30a05 100644
--- a/stem/util/proc.py
+++ b/stem/util/proc.py
@@ -53,7 +53,7 @@ def is_available():
   
   global IS_PROC_AVAILABLE
   
-  if IS_PROC_AVAILABLE == None:
+  if IS_PROC_AVAILABLE is None:
     if platform.system() != "Linux":
       IS_PROC_AVAILABLE = False
     else:
@@ -229,7 +229,7 @@ def get_stats(pid, *stat_types):
     IOError if it can't be determined
   """
   
-  if CLOCK_TICKS == None:
+  if CLOCK_TICKS is None:
     raise IOError("Unable to look up SC_CLK_TCK")
   
   start_time, parameter = time.time(), "process %s" % ", ".join(stat_types)
diff --git a/test/integ/descriptor/server_descriptor.py b/test/integ/descriptor/server_descriptor.py
index b974098..bd270b4 100644
--- a/test/integ/descriptor/server_descriptor.py
+++ b/test/integ/descriptor/server_descriptor.py
@@ -27,7 +27,7 @@ class TestServerDescriptor(unittest.TestCase):
     # a full tor initialization then the cached descriptors won't exist yet.
     # Noting if they exist or not since some tests need them.
     
-    if self.is_descriptors_available == None:
+    if self.is_descriptors_available is None:
       test_dir = test.runner.get_runner().get_test_dir()
       descriptor_path = os.path.join(test_dir, "cached-descriptors")
       self.is_descriptors_available = os.path.exists(descriptor_path)
diff --git a/test/integ/util/system.py b/test/integ/util/system.py
index f9c00a6..607d9f7 100644
--- a/test/integ/util/system.py
+++ b/test/integ/util/system.py
@@ -41,7 +41,7 @@ class TestSystem(unittest.TestCase):
     # isn't the end of the world. It's just used to skip tests if they should
     # legitemately fail.
     
-    if self.is_extra_tor_running == None:
+    if self.is_extra_tor_running is None:
       if not stem.util.system.is_bsd():
         pgrep_results = stem.util.system.call(stem.util.system.GET_PID_BY_NAME_PGREP % "tor")
         self.is_extra_tor_running = len(pgrep_results) > 1
diff --git a/test/output.py b/test/output.py
index 83a3a80..db692af 100644
--- a/test/output.py
+++ b/test/output.py
@@ -100,7 +100,7 @@ def apply_filters(testing_output, *filters):
     
     for result_filter in filters:
       line = result_filter(line_type, line)
-      if line == None: break
+      if line is None: break
     
     if line != None:
       results.append(line)
diff --git a/test/unit/descriptor/server_descriptor.py b/test/unit/descriptor/server_descriptor.py
index d170e96..90d448e 100644
--- a/test/unit/descriptor/server_descriptor.py
+++ b/test/unit/descriptor/server_descriptor.py
@@ -46,8 +46,8 @@ def _make_descriptor(attr = None, exclude = None, is_bridge = False):
   """
   
   descriptor_lines = []
-  if attr == None: attr = {}
-  if exclude == None: exclude = []
+  if attr is None: attr = {}
+  if exclude is None: exclude = []
   desc_attr = BRIDGE_DESCRIPTOR_ATTR if is_bridge else RELAY_DESCRIPTOR_ATTR
   attr = dict(attr) # shallow copy since we're destructive
   





More information about the tor-commits mailing list