Permalink
Browse files

Always return a Deferred from get_reader and get_writer.

  • Loading branch information...
1 parent 9d70905 commit 33074ebe8e641b2b4a9f123a3655cc0835f75a9f @allenap committed Jul 25, 2012
Showing with 19 additions and 16 deletions.
  1. +9 −10 tftp/backend.py
  2. +10 −6 tftp/test/test_protocol.py
View
19 tftp/backend.py
@@ -3,6 +3,7 @@
'''
from os import fstat
from tftp.errors import Unsupported, FileExists, AccessViolation, FileNotFound
+from twisted.internet.defer import succeed
from twisted.python.filepath import FilePath, InsecurePath
import shutil
import tempfile
@@ -33,8 +34,7 @@ def get_reader(file_name):
@raise BackendError: for any other errors, that were encountered while
attempting to construct a reader
- @return: an object, that provides L{IReader}, or a L{Deferred} that
- will fire with an L{IReader}
+ @return: a L{Deferred} that will fire with an L{IReader}
"""
@@ -57,8 +57,7 @@ def get_writer(file_name):
@raise BackendError: for any other errors, that were encountered while
attempting to construct a writer
- @return: an object, that provides L{IWriter}, or a L{Deferred} that
- will fire with an L{IWriter}
+ @return: a L{Deferred} that will fire with an L{IWriter}
"""
@@ -261,8 +260,7 @@ def get_reader(self, file_name):
"""
@see: L{IBackend.get_reader}
- @return: an object, providing L{IReader}
- @rtype: L{FilesystemReader}
+ @rtype: L{Deferred}, yielding a L{FilesystemReader}
"""
if not self.can_read:
@@ -271,14 +269,14 @@ def get_reader(self, file_name):
target_path = self.base.child(file_name)
except InsecurePath, e:
raise AccessViolation("Insecure path: %s" % e)
- return FilesystemReader(target_path)
+ reader = FilesystemReader(target_path)
+ return succeed(reader)
def get_writer(self, file_name):
"""
@see: L{IBackend.get_writer}
- @return: an object, providing L{IWriter}
- @rtype: L{FilesystemWriter}
+ @rtype: L{Deferred}, yielding a L{FilesystemWriter}
"""
if not self.can_write:
@@ -287,4 +285,5 @@ def get_writer(self, file_name):
target_path = self.base.child(file_name)
except InsecurePath, e:
raise AccessViolation("Insecure path: %s" % e)
- return FilesystemWriter(target_path)
+ writer = FilesystemWriter(target_path)
+ return succeed(writer)
View
16 tftp/test/test_protocol.py
@@ -202,15 +202,19 @@ def __init__(self, base_path, clock):
self.clock = clock
def get_reader(self, file_name):
- reader = super(FilesystemAsyncBackend, self).get_reader(file_name)
+ d_get = super(FilesystemAsyncBackend, self).get_reader(file_name)
d = Deferred()
- self.clock.callLater(0, d.callback, reader)
+ # d_get has already fired, so don't chain d_get to d until later,
+ # otherwise d will be fired too early.
+ self.clock.callLater(0, d_get.chainDeferred, d)
return d
def get_writer(self, file_name):
- writer = super(FilesystemAsyncBackend, self).get_writer(file_name)
+ d_get = super(FilesystemAsyncBackend, self).get_writer(file_name)
d = Deferred()
- self.clock.callLater(0, d.callback, writer)
+ # d_get has already fired, so don't chain d_get to d until later,
+ # otherwise d will be fired too early.
+ self.clock.callLater(0, d_get.chainDeferred, d)
return d
@@ -224,7 +228,7 @@ def setUp(self):
self.backend = FilesystemAsyncBackend(self.tmp_dir_path, self.clock)
self.tftp = TFTP(self.backend, self.clock)
- def test_get_reader_can_defer(self):
+ def test_get_reader_defers(self):
rrq_datagram = RRQDatagram('nonempty', 'NetASCiI', {})
rrq_addr = ('127.0.0.1', 1069)
rrq_mode = "octet"
@@ -234,7 +238,7 @@ def test_get_reader_can_defer(self):
self.assertTrue(d.called)
self.assertTrue(IReader.providedBy(d.result.backend))
- def test_get_writer_can_defer(self):
+ def test_get_writer_defers(self):
wrq_datagram = WRQDatagram('foobar', 'NetASCiI', {})
wrq_addr = ('127.0.0.1', 1069)
wrq_mode = "octet"

0 comments on commit 33074eb

Please sign in to comment.