[tor-commits] [stem/master] Avoid circular import error for the controller

atagar at torproject.org atagar at torproject.org
Sat May 23 19:34:15 UTC 2015


commit cf046f4ff74ebdff829dbab8c3cf947e0fd6625a
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat May 23 12:28:15 2015 -0700

    Avoid circular import error for the controller
    
    Python, I find your imports confusing. I put 'import stem.connection' at the
    end of control.py to avoid this (seems to work for the descriptors), but
    evidently didn't work. A simple hello-world script...
    
      import stem.control
    
      with stem.control.Controller.from_port() as controller:
        controller.authenticate()
        print controller.get_version()
    
    ... failed with...
    
      Traceback (most recent call last):
        File "scrap.py", line 1, in <module>
          import stem.control
        File "/home/atagar/Desktop/stem/stem/control.py", line 3647, in <module>
          import stem.connection
        File "/home/atagar/Desktop/stem/stem/connection.py", line 202, in <module>
          def connect(control_port = ('127.0.0.1', 'default'), control_socket = '/var/run/tor/control', password = None, password_prompt = False, chroot_path = None, controller = stem.control.Controller):
      AttributeError: 'module' object has no attribute 'control'
---
 stem/control.py |    9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/stem/control.py b/stem/control.py
index efc0d18..26272fd 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -916,6 +916,8 @@ class Controller(BaseController):
     :raises: :class:`stem.SocketError` if we're unable to establish a connection
     """
 
+    import stem.connection
+
     if not stem.util.connection.is_valid_ipv4_address(address):
       raise ValueError('Invalid IP address: %s' % address)
     elif not stem.util.connection.is_valid_port(port):
@@ -1000,6 +1002,7 @@ class Controller(BaseController):
     pass-through to :func:`stem.connection.authenticate`.
     """
 
+    import stem.connection
     stem.connection.authenticate(self, *args, **kwargs)
 
   @with_default()
@@ -1401,6 +1404,7 @@ class Controller(BaseController):
       An exception is only raised if we weren't provided a default response.
     """
 
+    import stem.connection
     return stem.connection.get_protocolinfo(self)
 
   @with_default()
@@ -3640,8 +3644,3 @@ def _case_insensitive_lookup(entries, key, default = UNDEFINED):
           return entry
 
   raise ValueError("key '%s' doesn't exist in dict: %s" % (key, entries))
-
-
-# importing at the end to avoid circular dependency
-
-import stem.connection





More information about the tor-commits mailing list