Skip to content

Commit

Permalink
doc: replace deprecated defer.returnValue() with return
Browse files Browse the repository at this point in the history
`defer.returnValue()` was required to maintain compatability with Python 2. But today twisted and txdbus
only support Python 3. On Python 3, `defer.returnValue()` can be replaced with a standard `return`.

`defer.returnValue()` was deprecated in Twisted 24.7.0
  • Loading branch information
gudnimg committed Aug 4, 2024
1 parent 32c8ada commit 445f4e7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 10 deletions.
4 changes: 2 additions & 2 deletions doc/examples/fd_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def call_remote_verbose(obj, method, *args, **kwargs):
print(f'calling {method}{args}', end=' = ')
result = yield obj.callRemote(method, *args, **kwargs)
print(repr(result))
defer.returnValue(result)
return result


@defer.inlineCallbacks
Expand All @@ -40,7 +40,7 @@ def main(reactor):
print('obtained remote object')
except Exception as e:
print(f'failed obtaining remote object: {e}')
defer.returnValue(None)
return None

# Open this source file. Ask remote to read it and return byte count.
with open(__file__, 'rb') as f:
Expand Down
2 changes: 1 addition & 1 deletion doc/examples/fd_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def main(reactor):
except Exception as e:
print(f'failed connecting to dbus: {e}')
reactor.stop()
defer.returnValue(None)
return None

print('connected to dbus')
object = FDObject(PATH)
Expand Down
10 changes: 3 additions & 7 deletions doc/tutorial.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,8 @@ operator will be returned from the +yield+ statement. Alternatively, if the
operation resulted in an exception, the exception will be re-thrown from the
+yield+ statement.

The return value of functions decorated with +defer.inlineCallbacks+ is a
deferred. Due to the nature of generator functions, inline callbacks methods
cannot use the traditional +return+ keyword to return the result of the
asynchronous operation. Instead, they must use +defer.returnValue()+ to
return the result. If +defer.returnValue()+ is not used, the deferred
returned by the decorated function will result in +None+
The return value of functions decorated with +defer.inlineCallbacks+ is
propagated using +return+ as usual.

.Inline Callbacks Example
[source,python]
Expand All @@ -67,7 +63,7 @@ def checkIPUsage( ip_addr ):
if ip_addr in ip_txt:
# True will become the result of the Deferred
defer.returnValue( True )
return True
else:
# Will trigger an errback
raise Exception('IP NOT FOUND')
Expand Down

0 comments on commit 445f4e7

Please sign in to comment.