commit cfcf763c69b04fa6b24c8737a92dad91a743e395 Author: Damian Johnson atagar@torproject.org Date: Sun Dec 31 13:32:32 2017 -0800
Replace get_address, get_port, and get_socket_path with attributes
These are methods because I wanted to emphasize that they're read-only, but getter methods like this are pretty javay. Just providing attributes instead. --- docs/change_log.rst | 1 + stem/connection.py | 2 +- stem/control.py | 4 ++-- stem/socket.py | 36 +++++++++++++++++++++++------------- test/integ/control/controller.py | 4 ++-- 5 files changed, 29 insertions(+), 18 deletions(-)
diff --git a/docs/change_log.rst b/docs/change_log.rst index 2d2490b5..68dd35fc 100644 --- a/docs/change_log.rst +++ b/docs/change_log.rst @@ -48,6 +48,7 @@ The following are only available within Stem's `git repository
* Added support for limiting the maximum number of streams to :func:`~stem.control.Controller.create_ephemeral_hidden_service` (:spec:`2fcb1c2`) * Stacktrace if :func:`stem.connection.connect` had a string port argument + * Replaced socket's :func:`~stem.socket.ControlPort.get_address`, :func:`~stem.socket.ControlPort.get_port`, and :func:`~stem.socket.ControlSocketFile.get_socket_path` with attributes
* **Descriptors**
diff --git a/stem/connection.py b/stem/connection.py index 3aad575a..aba11c5e 100644 --- a/stem/connection.py +++ b/stem/connection.py @@ -375,7 +375,7 @@ def _connect_auth(control_socket, password, password_prompt, chroot_path, contro return controller(control_socket, is_authenticated = True) except IncorrectSocketType: if isinstance(control_socket, stem.socket.ControlPort): - print(CONNECT_MESSAGES['wrong_port_type'].format(port = control_socket.get_port())) + print(CONNECT_MESSAGES['wrong_port_type'].format(port = control_socket.port)) else: print(CONNECT_MESSAGES['wrong_socket_type'])
diff --git a/stem/control.py b/stem/control.py index a4297677..94ad5eaf 100644 --- a/stem/control.py +++ b/stem/control.py @@ -1606,9 +1606,9 @@ class Controller(BaseController): control_socket = self.get_socket()
if isinstance(control_socket, stem.socket.ControlPort): - pid = stem.util.system.pid_by_port(control_socket.get_port()) + pid = stem.util.system.pid_by_port(control_socket.port) elif isinstance(control_socket, stem.socket.ControlSocketFile): - pid = stem.util.system.pid_by_open_file(control_socket.get_socket_path()) + pid = stem.util.system.pid_by_open_file(control_socket.path)
if pid: self._set_cache({'pid': pid}) diff --git a/stem/socket.py b/stem/socket.py index 38354ec8..68ca36bb 100644 --- a/stem/socket.py +++ b/stem/socket.py @@ -49,11 +49,7 @@ Tor... BaseSocket - Thread safe socket. |- ControlSocket - Socket wrapper that speaks the tor control protocol. | |- ControlPort - Control connection via a port. - | | |- get_address - provides the ip address of our socket - | | +- get_port - provides the port of our socket - | | | |- ControlSocketFile - Control connection via a local file socket. - | | +- get_socket_path - provides the path of the socket we connect to | | | |- send - sends a message to the socket | +- recv - receives a ControlMessage from the socket @@ -365,6 +361,9 @@ class ControlPort(ControlSocket): """ Control connection to tor. For more information see tor's ControlPort torrc option. + + :var str address: address our socket connects to + :var int port: ControlPort our socket connects to """
def __init__(self, address = '127.0.0.1', port = 9051, connect = True): @@ -380,8 +379,8 @@ class ControlPort(ControlSocket): """
super(ControlPort, self).__init__() - self._control_addr = address - self._control_port = port + self.address = address + self.port = port
if connect: self.connect() @@ -390,27 +389,33 @@ class ControlPort(ControlSocket): """ Provides the ip address our socket connects to.
+ .. deprecated:: 1.7.0 + Use the **address** attribute instead. + :returns: str with the ip address of our socket """
- return self._control_addr + return self.address
def get_port(self): """ Provides the port our socket connects to.
+ .. deprecated:: 1.7.0 + Use the **port** attribute instead. + :returns: int with the port of our socket """
- return self._control_port + return self.port
def is_localhost(self): - return self._control_addr == '127.0.0.1' + return self.address == '127.0.0.1'
def _make_socket(self): try: control_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - control_socket.connect((self._control_addr, self._control_port)) + control_socket.connect((self.address, self.port)) return control_socket except socket.error as exc: raise stem.SocketError(exc) @@ -420,6 +425,8 @@ class ControlSocketFile(ControlSocket): """ Control connection to tor. For more information see tor's ControlSocket torrc option. + + :var str path: filesystem path of the socket we connect to """
def __init__(self, path = '/var/run/tor/control', connect = True): @@ -434,7 +441,7 @@ class ControlSocketFile(ControlSocket): """
super(ControlSocketFile, self).__init__() - self._socket_path = path + self.path = path
if connect: self.connect() @@ -443,10 +450,13 @@ class ControlSocketFile(ControlSocket): """ Provides the path our socket connects to.
+ .. deprecated:: 1.7.0 + Use the **path** attribute instead. + :returns: str with the path for our control socket """
- return self._socket_path + return self.path
def is_localhost(self): return True @@ -454,7 +464,7 @@ class ControlSocketFile(ControlSocket): def _make_socket(self): try: control_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - control_socket.connect(self._socket_path) + control_socket.connect(self.path) return control_socket except socket.error as exc: raise stem.SocketError(exc) diff --git a/test/integ/control/controller.py b/test/integ/control/controller.py index af7b384f..b2ba77a5 100644 --- a/test/integ/control/controller.py +++ b/test/integ/control/controller.py @@ -364,10 +364,10 @@ class TestController(unittest.TestCase): control_socket = controller.get_socket()
if isinstance(control_socket, stem.socket.ControlPort): - connection_value = str(control_socket.get_port()) + connection_value = str(control_socket.port) config_key = 'ControlPort' elif isinstance(control_socket, stem.socket.ControlSocketFile): - connection_value = str(control_socket.get_socket_path()) + connection_value = control_socket.path config_key = 'ControlSocket'
# successful single query