From 24845555abc95ac759420d1ca8c67c5eb5512fc2 Mon Sep 17 00:00:00 2001 From: Cameron Dale Date: Tue, 15 Apr 2008 16:07:05 -0700 Subject: [PATCH] Make better use of defer.Fail for returning deferred errors. --- apt_p2p/Hash.py | 4 +--- apt_p2p/MirrorManager.py | 4 +--- apt_p2p_Khashmir/DHT.py | 18 ++++++------------ 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/apt_p2p/Hash.py b/apt_p2p/Hash.py index bf23e96..fe26a01 100644 --- a/apt_p2p/Hash.py +++ b/apt_p2p/Hash.py @@ -157,9 +157,7 @@ class HashObject: """Hashes a file in a separate thread, returning a deferred that will callback with the result.""" file.restat(False) if not file.exists(): - df = defer.Deferred() - df.errback(HashError("file not found")) - return df + return defer.fail(HashError("file not found")) df = threads.deferToThread(self._hashInThread, file) return df diff --git a/apt_p2p/MirrorManager.py b/apt_p2p/MirrorManager.py index 5f51175..301c0db 100644 --- a/apt_p2p/MirrorManager.py +++ b/apt_p2p/MirrorManager.py @@ -111,9 +111,7 @@ class MirrorManager: site, baseDir, path = self.extractPath(url) if site in self.apt_caches and baseDir in self.apt_caches[site]: return self.apt_caches[site][baseDir].findHash(path) - d = defer.Deferred() - d.errback(MirrorError("Site Not Found")) - return d + return defer.fail(MirrorError("Site Not Found")) def cleanup(self): for site in self.apt_caches.keys(): diff --git a/apt_p2p_Khashmir/DHT.py b/apt_p2p_Khashmir/DHT.py index 400b10d..1870841 100644 --- a/apt_p2p_Khashmir/DHT.py +++ b/apt_p2p_Khashmir/DHT.py @@ -249,15 +249,12 @@ class DHT: def getValue(self, key): """See L{apt_p2p.interfaces.IDHT}.""" - d = defer.Deferred() - if self.config is None: - d.errback(DHTError("configuration not loaded")) - return d + return defer.fail(DHTError("configuration not loaded")) if not self.joined: - d.errback(DHTError("have not joined a network yet")) - return d + return defer.fail(DHTError("have not joined a network yet")) + d = defer.Deferred() key = self._normKey(key) if key not in self.retrieving: @@ -283,15 +280,12 @@ class DHT: def storeValue(self, key, value): """See L{apt_p2p.interfaces.IDHT}.""" - d = defer.Deferred() - if self.config is None: - d.errback(DHTError("configuration not loaded")) - return d + return defer.fail(DHTError("configuration not loaded")) if not self.joined: - d.errback(DHTError("have not joined a network yet")) - return d + return defer.fail(DHTError("have not joined a network yet")) + d = defer.Deferred() key = self._normKey(key) bvalue = bencode(value) -- 2.39.5