[tor-commits] [stem/master] Issuing IOError for cookie read failures

atagar at torproject.org atagar at torproject.org
Fri Dec 9 18:03:00 UTC 2011


commit 3b89feb04f2d9389e387f7d2e1af5f69158d52e9
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Dec 2 07:47:49 2011 -0800

    Issuing IOError for cookie read failures
    
    Replacing OSError for read failures with IOError (more appropriate for this).
---
 stem/connection.py                      |   27 ++++++++++++---------------
 test/integ/connection/authentication.py |    2 +-
 2 files changed, 13 insertions(+), 16 deletions(-)

diff --git a/stem/connection.py b/stem/connection.py
index 0b654d8..9906b26 100644
--- a/stem/connection.py
+++ b/stem/connection.py
@@ -112,13 +112,13 @@ def authenticate_cookie(control_socket, cookie_path):
   
   Raises:
     ValueError if the authentication credentials aren't accepted
-    OSError if the cookie file doesn't exist or we're unable to read it
+    IOError if the cookie file doesn't exist or we're unable to read it
     stem.socket.ProtocolError the content from the socket is malformed
     stem.socket.SocketError if problems arise in using the socket
   """
   
   if not os.path.exists(cookie_path):
-    raise OSError(AUTH_COOKIE_MISSING % cookie_path)
+    raise IOError(AUTH_COOKIE_MISSING % cookie_path)
   
   # Abort if the file isn't 32 bytes long. This is to avoid exposing arbitrary
   # file content to the port.
@@ -134,19 +134,16 @@ def authenticate_cookie(control_socket, cookie_path):
   if auth_cookie_size != 32:
     raise ValueError(AUTH_COOKIE_WRONG_SIZE % (cookie_path, auth_cookie_size))
   
-  try:
-    auth_cookie_file = open(cookie_path, "r")
-    auth_cookie_contents = auth_cookie_file.read()
-    auth_cookie_file.close()
-    
-    control_socket.send("AUTHENTICATE %s" % binascii.b2a_hex(auth_cookie_contents))
-    auth_response = control_socket.recv()
-    
-    # if we got anything but an OK response then error
-    if str(auth_response) != "OK":
-      raise ValueError(str(auth_response))
-  except IOError, exc:
-    raise OSError(exc)
+  auth_cookie_file = open(cookie_path, "r")
+  auth_cookie_contents = auth_cookie_file.read()
+  auth_cookie_file.close()
+  
+  control_socket.send("AUTHENTICATE %s" % binascii.b2a_hex(auth_cookie_contents))
+  auth_response = control_socket.recv()
+  
+  # if we got anything but an OK response then error
+  if str(auth_response) != "OK":
+    raise ValueError(str(auth_response))
 
 def get_protocolinfo_by_port(control_addr = "127.0.0.1", control_port = 9051, get_socket = False):
   """
diff --git a/test/integ/connection/authentication.py b/test/integ/connection/authentication.py
index 7883c8a..9df2db3 100644
--- a/test/integ/connection/authentication.py
+++ b/test/integ/connection/authentication.py
@@ -73,7 +73,7 @@ class TestAuthenticate(unittest.TestCase):
     """
     
     test_path = "/if/this/exists/then/they're/asking/for/a/failure"
-    expected_exc = OSError(stem.connection.AUTH_COOKIE_MISSING % test_path)
+    expected_exc = IOError(stem.connection.AUTH_COOKIE_MISSING % test_path)
     self._check_auth(stem.connection.AuthMethod.COOKIE, test_path, False, expected_exc)
   
   def test_authenticate_cookie_wrong_size(self):





More information about the tor-commits mailing list