commit b0a1a878c34a45bd85f304a17e38a834ee074d41 Author: Damian Johnson atagar@torproject.org Date: Sun Aug 4 17:38:40 2019 -0700
Use DownloadFailed in directory and manual modules
Ah. No need to wait for stem 2.x since DownloadFailed is an IOError subclass. Our from_remote() functions raise errors aside from download failures so the overall function signatures are unchanged. --- stem/directory.py | 29 +++++++++++++++++------------ stem/manual.py | 8 ++++---- test/unit/directory/authority.py | 3 ++- test/unit/directory/fallback.py | 3 ++- 4 files changed, 25 insertions(+), 18 deletions(-)
diff --git a/stem/directory.py b/stem/directory.py index 075235c9..9e397d88 100644 --- a/stem/directory.py +++ b/stem/directory.py @@ -40,7 +40,9 @@ as follows...
import os import re +import sys
+import stem import stem.util import stem.util.conf
@@ -215,8 +217,6 @@ class Directory(object): :raises: **IOError** if unable to retrieve the fallback directories """
- # TODO: change IOError to DownloadFailed in stem 2.x - raise NotImplementedError('Unsupported Operation: this should be implemented by the Directory subclass')
def __hash__(self): @@ -266,11 +266,13 @@ class Authority(Directory): def from_remote(timeout = 60): try: lines = str_tools._to_unicode(urllib.urlopen(GITWEB_AUTHORITY_URL, timeout = timeout).read()).splitlines() - except Exception as exc: - raise IOError("Unable to download tor's directory authorities from %s: %s" % (GITWEB_AUTHORITY_URL, exc))
- if not lines: - raise IOError('%s did not have any content' % GITWEB_AUTHORITY_URL) + if not lines: + raise IOError('no content') + except: + exc, stacktrace = sys.exc_info()[1:3] + message = "Unable to download tor's directory authorities from %s: %s" % (GITWEB_AUTHORITY_URL, exc) + raise stem.DownloadFailed(GITWEB_AUTHORITY_URL, exc, stacktrace, message)
# Entries look like... # @@ -412,16 +414,19 @@ class Fallback(Directory): def from_remote(timeout = 60): try: lines = str_tools._to_unicode(urllib.urlopen(GITWEB_FALLBACK_URL, timeout = timeout).read()).splitlines() - except Exception as exc: - raise IOError("Unable to download tor's fallback directories from %s: %s" % (GITWEB_FALLBACK_URL, exc))
- if not lines: - raise IOError('%s did not have any content' % GITWEB_FALLBACK_URL) - elif lines[0] != '/* type=fallback */': - raise IOError('%s does not have a type field indicating it is fallback directory metadata' % GITWEB_FALLBACK_URL) + if not lines: + raise IOError('no content') + except: + exc, stacktrace = sys.exc_info()[1:3] + message = "Unable to download tor's fallback directories from %s: %s" % (GITWEB_FALLBACK_URL, exc) + raise stem.DownloadFailed(GITWEB_FALLBACK_URL, exc, stacktrace, message)
# header metadata
+ if lines[0] != '/* type=fallback */': + raise IOError('%s does not have a type field indicating it is fallback directory metadata' % GITWEB_FALLBACK_URL) + header = {}
for line in Fallback._pop_section(lines): diff --git a/stem/manual.py b/stem/manual.py index 729fb204..24596851 100644 --- a/stem/manual.py +++ b/stem/manual.py @@ -53,6 +53,7 @@ import shutil import sys import tempfile
+import stem import stem.prereq import stem.util import stem.util.conf @@ -296,8 +297,6 @@ def download_man_page(path = None, file_handle = None, url = GITWEB_MANUAL_URL, :raises: **IOError** if unable to retrieve the manual """
- # TODO: change IOError to DownloadFailed in stem 2.x - if not path and not file_handle: raise ValueError("Either the path or file_handle we're saving to must be provided") elif not stem.util.system.is_available('a2x'): @@ -313,8 +312,9 @@ def download_man_page(path = None, file_handle = None, url = GITWEB_MANUAL_URL, request = urllib.urlopen(url, timeout = timeout) shutil.copyfileobj(request, asciidoc_file) except: - exc = sys.exc_info()[1] - raise IOError("Unable to download tor's manual from %s to %s: %s" % (url, asciidoc_path, exc)) + exc, stacktrace = sys.exc_info()[1:3] + message = "Unable to download tor's manual from %s to %s: %s" % (url, asciidoc_path, exc) + raise stem.DownloadFailed(url, exc, stacktrace, message)
try: stem.util.system.call('a2x -f manpage %s' % asciidoc_path) diff --git a/test/unit/directory/authority.py b/test/unit/directory/authority.py index 56bd6ec8..1c2a86b4 100644 --- a/test/unit/directory/authority.py +++ b/test/unit/directory/authority.py @@ -5,6 +5,7 @@ Unit tests for stem.directory.Authority. import io import unittest
+import stem import stem.directory import stem.prereq
@@ -78,4 +79,4 @@ class TestAuthority(unittest.TestCase):
@patch(URL_OPEN, Mock(return_value = io.BytesIO(b''))) def test_from_remote_empty(self): - self.assertRaisesRegexp(IOError, 'did not have any content', stem.directory.Authority.from_remote) + self.assertRaisesRegexp(stem.DownloadFailed, 'no content', stem.directory.Authority.from_remote) diff --git a/test/unit/directory/fallback.py b/test/unit/directory/fallback.py index 2423932f..06b4510d 100644 --- a/test/unit/directory/fallback.py +++ b/test/unit/directory/fallback.py @@ -7,6 +7,7 @@ import re import tempfile import unittest
+import stem import stem.directory import stem.util.conf
@@ -123,7 +124,7 @@ class TestFallback(unittest.TestCase):
@patch(URL_OPEN, Mock(return_value = io.BytesIO(b''))) def test_from_remote_empty(self): - self.assertRaisesRegexp(IOError, 'did not have any content', stem.directory.Fallback.from_remote) + self.assertRaisesRegexp(stem.DownloadFailed, 'no content', stem.directory.Fallback.from_remote)
@patch(URL_OPEN, Mock(return_value = io.BytesIO(b'\n'.join(FALLBACK_GITWEB_CONTENT.splitlines()[1:])))) def test_from_remote_no_header(self):
tor-commits@lists.torproject.org