[tor-commits] [stem/master] Avoid KeyError in stem.util.system.is_available() when path unset

atagar at torproject.org atagar at torproject.org
Tue May 23 23:40:45 UTC 2017


commit f9a7ad4d332e4b61c02b4771e367c65d65bf4a1a
Author: Damian Johnson <atagar at torproject.org>
Date:   Tue May 23 14:21:01 2017 -0700

    Avoid KeyError in stem.util.system.is_available() when path unset
    
    catalyst ran into testing errors where we made assertions that didn't hold for
    his system...
    
      https://trac.torproject.org/projects/tor/ticket/22301
    
    First gotcha I'm noticing is that is_available() raises an unexpected KeyError
    when we don't have a PATH environment variable...
    
      ======================================================================
      ERROR: test_is_available
      ----------------------------------------------------------------------
      Traceback (most recent call last):
        File "/home/atagar/Desktop/stem/test/integ/util/system.py", line 82, in test_is_available
          self.assertTrue(stem.util.system.is_available('ls'))
        File "/home/atagar/Desktop/stem/stem/util/system.py", line 251, in is_available
          cmd_exists = distutils.spawn.find_executable(command) is not None
        File "/usr/lib/python2.7/distutils/spawn.py", line 184, in find_executable
          path = os.environ['PATH']
        File "/usr/lib/python2.7/UserDict.py", line 23, in __getitem__
          raise KeyError(key)
      KeyError: 'PATH'
---
 stem/util/system.py | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/stem/util/system.py b/stem/util/system.py
index 4b705f3..7b31516 100644
--- a/stem/util/system.py
+++ b/stem/util/system.py
@@ -247,6 +247,8 @@ def is_available(command, cached=True):
     return True
   elif cached and command in CMD_AVAILABLE_CACHE:
     return CMD_AVAILABLE_CACHE[command]
+  elif 'PATH' not in os.environ:
+    return False  # lacking a path will cause find_executable() to internally fail
   else:
     cmd_exists = distutils.spawn.find_executable(command) is not None
     CMD_AVAILABLE_CACHE[command] = cmd_exists





More information about the tor-commits mailing list