mirror of https://github.com/scrapy/scrapy.git
IOError and other cleanup (#4716)
This commit is contained in:
parent
ee215a2970
commit
5360ba34bc
|
|
@ -30,7 +30,7 @@ def main():
|
||||||
try:
|
try:
|
||||||
with Path("build/linkcheck/output.txt").open(encoding="utf-8") as out:
|
with Path("build/linkcheck/output.txt").open(encoding="utf-8") as out:
|
||||||
output_lines = out.readlines()
|
output_lines = out.readlines()
|
||||||
except IOError:
|
except OSError:
|
||||||
print("linkcheck output not found; please run linkcheck first.")
|
print("linkcheck output not found; please run linkcheck first.")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,7 @@ class DecompressionMiddleware:
|
||||||
archive = BytesIO(response.body)
|
archive = BytesIO(response.body)
|
||||||
try:
|
try:
|
||||||
body = gzip.GzipFile(fileobj=archive).read()
|
body = gzip.GzipFile(fileobj=archive).read()
|
||||||
except IOError:
|
except OSError:
|
||||||
return
|
return
|
||||||
|
|
||||||
respcls = responsetypes.from_args(body=body)
|
respcls = responsetypes.from_args(body=body)
|
||||||
|
|
@ -72,7 +72,7 @@ class DecompressionMiddleware:
|
||||||
def _is_bzip2(self, response):
|
def _is_bzip2(self, response):
|
||||||
try:
|
try:
|
||||||
body = bz2.decompress(response.body)
|
body = bz2.decompress(response.body)
|
||||||
except IOError:
|
except OSError:
|
||||||
return
|
return
|
||||||
|
|
||||||
respcls = responsetypes.from_args(body=body)
|
respcls = responsetypes.from_args(body=body)
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ class HttpCacheMiddleware:
|
||||||
ConnectionLost,
|
ConnectionLost,
|
||||||
TCPTimedOutError,
|
TCPTimedOutError,
|
||||||
ResponseFailed,
|
ResponseFailed,
|
||||||
IOError,
|
OSError,
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self, settings: Settings, stats: StatsCollector) -> None:
|
def __init__(self, settings: Settings, stats: StatsCollector) -> None:
|
||||||
|
|
|
||||||
|
|
@ -268,9 +268,9 @@ RETRY_EXCEPTIONS = [
|
||||||
"twisted.internet.error.ConnectionLost",
|
"twisted.internet.error.ConnectionLost",
|
||||||
"twisted.internet.error.TCPTimedOutError",
|
"twisted.internet.error.TCPTimedOutError",
|
||||||
"twisted.web.client.ResponseFailed",
|
"twisted.web.client.ResponseFailed",
|
||||||
# IOError is raised by the HttpCompression middleware when trying to
|
# OSError is raised by the HttpCompression middleware when trying to
|
||||||
# decompress an empty response
|
# decompress an empty response
|
||||||
IOError,
|
OSError,
|
||||||
"scrapy.core.downloader.handlers.http11.TunnelError",
|
"scrapy.core.downloader.handlers.http11.TunnelError",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ def gunzip(data):
|
||||||
try:
|
try:
|
||||||
chunk = f.read1(8196)
|
chunk = f.read1(8196)
|
||||||
output_list.append(chunk)
|
output_list.append(chunk)
|
||||||
except (IOError, EOFError, struct.error):
|
except (OSError, EOFError, struct.error):
|
||||||
# complete only if there is some data, otherwise re-raise
|
# complete only if there is some data, otherwise re-raise
|
||||||
# see issue 87 about catching struct.error
|
# see issue 87 about catching struct.error
|
||||||
# some pages are quite small so output_list is empty and f.extrabuf
|
# some pages are quite small so output_list is empty and f.extrabuf
|
||||||
|
|
|
||||||
|
|
@ -291,7 +291,7 @@ def without_none_values(iterable):
|
||||||
try:
|
try:
|
||||||
return {k: v for k, v in iterable.items() if v is not None}
|
return {k: v for k, v in iterable.items() if v is not None}
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return type(iterable)((v for v in iterable if v is not None))
|
return type(iterable)(v for v in iterable if v is not None)
|
||||||
|
|
||||||
|
|
||||||
def global_object_name(obj):
|
def global_object_name(obj):
|
||||||
|
|
|
||||||
|
|
@ -129,7 +129,7 @@ class FileTestCase(unittest.TestCase):
|
||||||
def test_non_existent(self):
|
def test_non_existent(self):
|
||||||
request = Request(f"file://{self.mktemp()}")
|
request = Request(f"file://{self.mktemp()}")
|
||||||
d = self.download_request(request, Spider("foo"))
|
d = self.download_request(request, Spider("foo"))
|
||||||
return self.assertFailure(d, IOError)
|
return self.assertFailure(d, OSError)
|
||||||
|
|
||||||
|
|
||||||
class ContentLengthHeaderResource(resource.Resource):
|
class ContentLengthHeaderResource(resource.Resource):
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,7 @@ class DefaultsTest(ManagerTestCase):
|
||||||
In particular when some website returns a 30x response with header
|
In particular when some website returns a 30x response with header
|
||||||
'Content-Encoding: gzip' giving as result the error below:
|
'Content-Encoding: gzip' giving as result the error below:
|
||||||
|
|
||||||
exceptions.IOError: Not a gzipped file
|
BadGzipFile: Not a gzipped file (...)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
req = Request("http://example.com")
|
req = Request("http://example.com")
|
||||||
|
|
@ -108,7 +108,7 @@ class DefaultsTest(ManagerTestCase):
|
||||||
"Location": "http://example.com/login",
|
"Location": "http://example.com/login",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
self.assertRaises(IOError, self._download, request=req, response=resp)
|
self.assertRaises(OSError, self._download, request=req, response=resp)
|
||||||
|
|
||||||
|
|
||||||
class ResponseFromProcessRequestTest(ManagerTestCase):
|
class ResponseFromProcessRequestTest(ManagerTestCase):
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
# coding=utf-8
|
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
from email.charset import Charset
|
from email.charset import Charset
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
# coding=utf-8
|
|
||||||
from twisted.trial import unittest
|
from twisted.trial import unittest
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ class GunzipTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_gunzip_no_gzip_file_raises(self):
|
def test_gunzip_no_gzip_file_raises(self):
|
||||||
self.assertRaises(
|
self.assertRaises(
|
||||||
IOError, gunzip, (SAMPLEDIR / "feed-sample1.xml").read_bytes()
|
OSError, gunzip, (SAMPLEDIR / "feed-sample1.xml").read_bytes()
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_gunzip_truncated_short(self):
|
def test_gunzip_truncated_short(self):
|
||||||
|
|
|
||||||
|
|
@ -346,8 +346,8 @@ class UtilsCsvTestCase(unittest.TestCase):
|
||||||
|
|
||||||
# explicit type check cuz' we no like stinkin' autocasting! yarrr
|
# explicit type check cuz' we no like stinkin' autocasting! yarrr
|
||||||
for result_row in result:
|
for result_row in result:
|
||||||
self.assertTrue(all((isinstance(k, str) for k in result_row.keys())))
|
self.assertTrue(all(isinstance(k, str) for k in result_row.keys()))
|
||||||
self.assertTrue(all((isinstance(v, str) for v in result_row.values())))
|
self.assertTrue(all(isinstance(v, str) for v in result_row.values()))
|
||||||
|
|
||||||
def test_csviter_delimiter(self):
|
def test_csviter_delimiter(self):
|
||||||
body = get_testdata("feeds", "feed-sample3.csv").replace(b",", b"\t")
|
body = get_testdata("feeds", "feed-sample3.csv").replace(b",", b"\t")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue