Skip to content
Permalink
Browse files

fix unit test for python 2.7

  • Loading branch information...
sdpython committed May 5, 2015
1 parent 78f352d commit caa5c80f411e6f76bf27ffb1bf709823b88e3bbe
Showing with 36 additions and 12 deletions.
  1. +8 −0 _unittests/ut_filehelper/test_download_helper.py
  2. +28 −12 src/pyquickhelper/filehelper/download_helper.py
@@ -36,6 +36,10 @@ def test_download_notimeout(self):
self._testMethodName,
OutputPrint=__name__ == "__main__")

if sys.version_info[0] == 2:
# issue with timeout for python 2.7
return

url = "https://raw.githubusercontent.com/sdpython/pyquickhelper/master/src/pyquickhelper/ipythonhelper/magic_parser.py"
content = get_url_content_timeout(url, encoding="utf8")
assert "MagicCommandParser" in content
@@ -48,6 +52,10 @@ def test_download_timeout(self):
self._testMethodName,
OutputPrint=__name__ == "__main__")

if sys.version_info[0] == 2:
# issue with timeout for python 2.7
return

url = "https://localhost:878777/should_not_exists"
try:
content = get_url_content_timeout(url, encoding="utf8", timeout=2)
@@ -10,6 +10,13 @@
import urllib2 as urllib_error
import urllib2 as urllib_request
import httplib as http_client

class ConnectionResetError(Exception):

"""
missing from Python 2.7
"""
pass
else:
import urllib.error as urllib_error
import urllib.request as urllib_request
@@ -48,51 +55,60 @@ def get_url_content_timeout(url, timeout=10, output=None, encoding="utf8", raise
It comes from `pyrsslocal <http://www.xavierdupre.fr/app/pyrsslocal/helpsphinx/index.html>`_.
"""
try:
if timeout != -1:
with urllib_request.urlopen(url, timeout=timeout) as ur:
res = ur.read()
if sys.version_info[0] == 2:
if timeout != -1:
raise NotImplementedError(
"for python 2.7, timeout cannot be -1")
else:
fu = urllib_request.urlopen(url)
res = fu.read()
fu.close()
else:
with urllib_request.urlopen(url) as ur:
res = ur.read()
if timeout != -1:
with urllib_request.urlopen(url, timeout=timeout) as ur:
res = ur.read()
else:
with urllib_request.urlopen(url) as ur:
res = ur.read()
except (urllib_error.HTTPError, urllib_error.URLError) as e:
if raise_exception:
raise InternetException(
"unable to retrieve content from {0}".format(url)) from e
"unable to retrieve content, url={0}".format(url)) from e
warnings.warn(
"unable to retrieve content from {0} exc: {1}".format(url, e))
return None
except socket.timeout as e:
if raise_exception:
raise InternetException(
"unable to retrieve content from {0}".format(url)) from e
"unable to retrieve content, url={0}".format(url)) from e
warnings.warn("unable to retrieve content from {0} because of timeout {1}: {2}".format(
url, timeout, e))
return None
except ConnectionResetError as e:
if raise_exception:
raise InternetException(
"unable to retrieve content from {0}".format(url)) from e
"unable to retrieve content, url={0}".format(url)) from e
warnings.warn(
"unable to retrieve content from {0} because of ConnectionResetError: {1}".format(url, e))
return None
except http_client.BadStatusLine as e:
if raise_exception:
raise InternetException(
"unable to retrieve content from {0}".format(url)) from e
"unable to retrieve content, url={0}".format(url)) from e
warnings.warn(
"unable to retrieve content from {0} because of http.client.BadStatusLine: {1}".format(url, e))
return None
except http_client.IncompleteRead as e:
if raise_exception:
raise InternetException(
"unable to retrieve content from {0}".format(url)) from e
"unable to retrieve content url={0}".format(url)) from e
warnings.warn(
"unable to retrieve content from {0} because of http.client.IncompleteRead: {1}".format(url, e))
"unable to retrieve content from {0} because of http.client.IncompleteRead: {1}".format(url, e))
return None
except Exception as e:
if raise_exception:
raise InternetException(
"unable to retrieve content from {0}".format(url)) from e
"unable to retrieve content, url={0}, exc={1}".format(url, e)) from e
warnings.warn(
"unable to retrieve content from {0} because of unknown exception: {1}".format(url, e))
raise e

0 comments on commit caa5c80

Please sign in to comment.
You can’t perform that action at this time.