Skip to content

Commit

Permalink
Merge pull request #403 from cjwatson/remove-defer-returnValue
Browse files Browse the repository at this point in the history
Stop using twisted.internet.defer.returnValue
  • Loading branch information
meejah authored Dec 10, 2024
2 parents 1e6f661 + 0c8bb47 commit 6a7f544
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 84 deletions.
2 changes: 1 addition & 1 deletion examples/web_onion_service_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def on_progress(percent, tag, msg):
port = yield ep.listen(server.Site(Simple()))
except error.ConnectionRefusedError:
print("Couldn't connect; is Tor listening on localhost:{}?".format(default_control_port()))
defer.returnValue(1)
return 1

print("Site listening: {}".format(port.getHost()))
print("Private key:\n{}".format(port.getHost().onion_key))
Expand Down
6 changes: 2 additions & 4 deletions test/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,7 @@ def more_listen(arg):
yield arg.stopListening()
ep.listen(NoOpProtocolFactory())

defer.returnValue(arg)
return
return arg
d0.addBoth(more_listen)
self.protocol.commands[0][1].callback(
'ServiceID=blarglyfoo\nPrivateKey=bigbadkeyblob'
Expand Down Expand Up @@ -662,8 +661,7 @@ def more_listen(arg):
yield arg.stopListening()
ep.listen(NoOpProtocolFactory())

defer.returnValue(arg)
return
return arg
d0.addBoth(more_listen)
if True:
self.protocol.events['HS_DESC'](
Expand Down
7 changes: 2 additions & 5 deletions test/test_torconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -1457,11 +1457,8 @@ def __call__(self, proto, trans):
)
self.assertIs(tpp, fake_tor.process)
calls = warn.mock_calls
# on Twisted 24.7.0 and higher, there's an extra deprecation
# warning due to returnValue being deprecated
self.assertTrue(len(calls) >= 1)
for call in calls:
self.assertEqual(call[1][1], DeprecationWarning)
self.assertEqual(1, len(calls))
self.assertEqual(calls[0][1][1], DeprecationWarning)


class ErrorTests(unittest.TestCase):
Expand Down
6 changes: 3 additions & 3 deletions txtorcon/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def attach_stream(self, stream, circuits):
)))
return
d.callback(None)
defer.returnValue(circuit)
return circuit
except Exception:
d.errback(Failure())

Expand All @@ -114,7 +114,7 @@ def _get_circuit_attacher(reactor, state):
if _get_circuit_attacher.attacher is None:
_get_circuit_attacher.attacher = _CircuitAttacher()
yield state.set_attacher(_get_circuit_attacher.attacher, reactor)
defer.returnValue(_get_circuit_attacher.attacher)
return _get_circuit_attacher.attacher


_get_circuit_attacher.attacher = None
Expand Down Expand Up @@ -150,7 +150,7 @@ def connect(self, protocol_factory):
attached_d = attacher.add_endpoint(self._target_endpoint, self._circuit)
proto = yield connect_d
yield attached_d
defer.returnValue(proto)
return proto


class Circuit(object):
Expand Down
51 changes: 23 additions & 28 deletions txtorcon/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from twisted.python import log
from twisted.python.failure import Failure
from twisted.internet.defer import inlineCallbacks, returnValue, Deferred, succeed, fail
from twisted.internet.defer import inlineCallbacks, Deferred, succeed, fail
from twisted.internet import protocol, error
from twisted.internet.endpoints import TCP4ClientEndpoint
from twisted.internet.endpoints import UNIXClientEndpoint
Expand Down Expand Up @@ -362,14 +362,12 @@ def launch(reactor,
# note that attach_protocol waits for the protocol to be
# boostrapped if necessary

returnValue(
Tor(
reactor,
config.protocol,
_tor_config=config,
_process_proto=process_protocol,
_non_anonymous=True if non_anonymous_mode else False,
)
return Tor(
reactor,
config.protocol,
_tor_config=config,
_process_proto=process_protocol,
_non_anonymous=True if non_anonymous_mode else False,
)


Expand Down Expand Up @@ -420,8 +418,7 @@ def try_endpoint(control_ep):
)
)
config = yield TorConfig.from_protocol(proto)
tor = Tor(reactor, proto, _tor_config=config)
returnValue(tor)
return Tor(reactor, proto, _tor_config=config)

if control_endpoint is None:
to_try = [
Expand Down Expand Up @@ -450,7 +447,7 @@ def try_endpoint(control_ep):
try:
tor = yield try_endpoint(ep)
txtorlog.msg("Connected via '{}'".format(ep))
returnValue(tor)
return tor
except Exception as e:
errors.append(e)
if len(errors) == 1:
Expand Down Expand Up @@ -559,7 +556,7 @@ def get_config(self):
"""
if self._config is None:
self._config = yield TorConfig.from_protocol(self._protocol)
returnValue(self._config)
return self._config

def web_agent(self, pool=None, socks_endpoint=None, tls_context_factory=None):
"""
Expand Down Expand Up @@ -609,7 +606,7 @@ def dns_resolve(self, hostname):
"""
socks_ep = yield self._default_socks_endpoint()
ans = yield socks.resolve(socks_ep, hostname)
returnValue(ans)
return ans

@inlineCallbacks
def dns_resolve_ptr(self, ip):
Expand All @@ -622,7 +619,7 @@ def dns_resolve_ptr(self, ip):
"""
socks_ep = yield self._default_socks_endpoint()
ans = yield socks.resolve_ptr(socks_ep, ip)
returnValue(ans)
return ans

@inlineCallbacks
def add_onion_authentication(self, onion_host, token):
Expand Down Expand Up @@ -678,8 +675,8 @@ def remove_onion_authentication(self, onion_host):
if to_remove is not None:
config.HidServAuth.remove(to_remove)
yield config.save()
returnValue(True)
returnValue(False)
return True
return False

def onion_authentication(self, onion_host, token):
"""
Expand Down Expand Up @@ -913,7 +910,7 @@ def create_state(self):
"""
state = TorState(self.protocol)
yield state.post_bootstrap
returnValue(state)
return state

def __str__(self):
return "<Tor version='{tor_version}'>".format(
Expand All @@ -933,12 +930,10 @@ def is_ready(self):
"status/enough-dir-info",
"status/circuit-established",
)
returnValue(
not (
int(info["dormant"]) or
not int(info["status/enough-dir-info"]) or
not int(info["status/circuit-established"])
)
return not (
int(info["dormant"]) or
not int(info["status/enough-dir-info"]) or
not int(info["status/circuit-established"])
)

@inlineCallbacks
Expand Down Expand Up @@ -967,7 +962,7 @@ def _default_socks_endpoint(self):
)
if self._socks_endpoint is None:
self._socks_endpoint = yield _create_socks_endpoint(self._reactor, self._protocol)
returnValue(self._socks_endpoint)
return self._socks_endpoint

# For all these create_*() methods, instead of magically computing
# the class-name from arguments (e.g. we could decide "it's a
Expand Down Expand Up @@ -1050,7 +1045,7 @@ def create_onion_service(self, ports, private_key=None, version=3,
single_hop=single_hop,
detach=detach,
)
returnValue(service)
return service

@inlineCallbacks
def create_filesystem_onion_service(self, ports, onion_service_dir,
Expand Down Expand Up @@ -1111,7 +1106,7 @@ def create_filesystem_onion_service(self, ports, onion_service_dir,
progress=progress,
await_all_uploads=await_all_uploads,
)
returnValue(service)
return service


class TorNotFound(RuntimeError):
Expand Down Expand Up @@ -1412,4 +1407,4 @@ def _tor_connected(self, proto):
yield self.tor_protocol.queue_command('RESETCONF __OwningControllerProcess')
if self.config is not None and self.config.protocol is None:
yield self.config.attach_protocol(proto)
returnValue(self) # XXX or "proto"?
return self # XXX or "proto"?
22 changes: 10 additions & 12 deletions txtorcon/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def get_global_tor_instance(reactor,
control_port,
)

defer.returnValue(_global_tor)
return _global_tor
finally:
_global_tor_lock.release()

Expand Down Expand Up @@ -131,7 +131,7 @@ def get_global_tor(reactor, control_port=None,
_tor_launcher=_tor_launcher,
)
cfg = yield tor.get_config()
defer.returnValue(cfg)
return cfg


class IProgressProvider(Interface):
Expand Down Expand Up @@ -669,13 +669,11 @@ def listen(self, protocolfactory):
# (so can provide .local_port -> shoujld be local_endpoint I
# guess actually...)
# 2. anyway, can provide access to the "real" hs anyway if we want
defer.returnValue(
TorOnionListeningPort(
self.tcp_listening_port,
self.public_port,
self.hiddenservice,
self._config,
)
return TorOnionListeningPort(
self.tcp_listening_port,
self.public_port,
self.hiddenservice,
self._config,
)


Expand Down Expand Up @@ -1033,7 +1031,7 @@ def _create_socks_endpoint(reactor, control_protocol, socks_config=None):
socks_endpoint = _endpoint_from_socksport_line(reactor, socks_config)

assert socks_endpoint is not None
defer.returnValue(socks_endpoint)
return socks_endpoint


@implementer(IStreamClientEndpoint)
Expand Down Expand Up @@ -1160,7 +1158,7 @@ def connect(self, protocolfactory):
# forward the address to any listeners we have
socks_ep._get_address().addCallback(self._when_address.fire)
proto = yield socks_ep.connect(protocolfactory)
defer.returnValue(proto)
return proto
else:
for socks_port in self.socks_ports_to_try:
tor_ep = TCP4ClientEndpoint(
Expand All @@ -1173,7 +1171,7 @@ def connect(self, protocolfactory):
socks_ep._get_address().addCallback(self._when_address.fire)
try:
proto = yield socks_ep.connect(protocolfactory)
defer.returnValue(proto)
return proto

except error.ConnectError as e0:
last_error = e0
Expand Down
10 changes: 5 additions & 5 deletions txtorcon/onion.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def create(reactor, config, hsdir, ports,

yield config.save()
yield uploaded[0]
defer.returnValue(fhs)
return fhs

def __init__(self, config, thedir, ports, version=3, group_readable=0):
"""
Expand Down Expand Up @@ -744,7 +744,7 @@ def create(reactor, config, ports,
)
yield _add_ephemeral_service(config, onion, progress, version, auth, await_all_uploads)

defer.returnValue(onion)
return onion

def __init__(self, config, ports, hostname=None, private_key=None, auth=[], version=3,
detach=False, single_hop=None):
Expand Down Expand Up @@ -898,7 +898,7 @@ def create(reactor, config, ports,

yield _add_ephemeral_service(config, onion, progress, version, None, await_all_uploads)

defer.returnValue(onion)
return onion

def __init__(self, config, ports, hostname=None, private_key=None, version=3,
detach=False, await_all_uploads=None, single_hop=None, **kwarg):
Expand Down Expand Up @@ -1164,7 +1164,7 @@ def translate_progress(pct, tag, description):

yield config.save()
yield uploaded[0]
defer.returnValue(fhs)
return fhs

def __init__(self, config, thedir, ports, auth, version=3, group_readable=0):
# XXX do we need version here? probably...
Expand Down Expand Up @@ -1389,7 +1389,7 @@ def _validate_ports(reactor, ports):
processed_ports.append(
"{} 127.0.0.1:{}".format(remote, local)
)
defer.returnValue(processed_ports)
return processed_ports


def _validate_ports_low_level(ports):
Expand Down
4 changes: 2 additions & 2 deletions txtorcon/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from base64 import b64encode, b64decode
from binascii import b2a_hex, a2b_hex

from twisted.internet.defer import inlineCallbacks, returnValue, succeed
from twisted.internet.defer import inlineCallbacks, succeed
from twisted.python.deprecate import deprecated
from twisted.web.client import readBody

Expand Down Expand Up @@ -215,7 +215,7 @@ def get_onionoo_details(self, agent):
raise RuntimeError(
'Expected "{}" but got data for "{}"'.format(self.id_hex, relay_data['fingerprint'])
)
returnValue(relay_data)
return relay_data

# note that exit-policy is no longer included in the
# microdescriptors by default, so this stuff is mostly here as a
Expand Down
10 changes: 5 additions & 5 deletions txtorcon/socks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import struct
from socket import inet_pton, inet_ntoa, inet_aton, AF_INET6, AF_INET

from twisted.internet.defer import inlineCallbacks, returnValue, Deferred
from twisted.internet.defer import inlineCallbacks, Deferred
from twisted.internet.protocol import Protocol, Factory
from twisted.internet.address import IPv4Address, IPv6Address, HostnameAddress
from twisted.python.failure import Failure
Expand Down Expand Up @@ -650,7 +650,7 @@ def resolve(tor_endpoint, hostname):
)
proto = yield tor_endpoint.connect(factory)
result = yield proto.when_done()
returnValue(result)
return result


@inlineCallbacks
Expand All @@ -669,7 +669,7 @@ def resolve_ptr(tor_endpoint, ip):
)
proto = yield tor_endpoint.connect(factory)
result = yield proto.when_done()
returnValue(result)
return result


@implementer(IStreamClientEndpoint)
Expand Down Expand Up @@ -742,6 +742,6 @@ def connect(self, factory):
proto = yield proxy_ep.connect(socks_factory)
wrapped_proto = yield proto.when_done()
if self._tls:
returnValue(wrapped_proto.wrappedProtocol)
return wrapped_proto.wrappedProtocol
else:
returnValue(wrapped_proto)
return wrapped_proto
2 changes: 1 addition & 1 deletion txtorcon/testutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def get_info_incremental(self, info, cb):
text = yield self.get_info_raw(info)
for line in text.split('\r\n'):
cb(line)
defer.returnValue('') # FIXME uh....what's up at torstate.py:350?
return '' # FIXME uh....what's up at torstate.py:350?

def get_conf(self, info):
if len(self.answers) == 0:
Expand Down
Loading

0 comments on commit 6a7f544

Please sign in to comment.