1
0
mirror of https://github.com/scrapy/scrapy.git synced 2025-02-06 10:24:24 +00:00

IOError and other cleanup (#4716)

This commit is contained in:
Marc Hernández 2023-06-21 11:08:53 -07:00 committed by GitHub
parent ee215a2970
commit 5360ba34bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 14 additions and 17 deletions

View File

@ -30,7 +30,7 @@ def main():
try:
with Path("build/linkcheck/output.txt").open(encoding="utf-8") as out:
output_lines = out.readlines()
except IOError:
except OSError:
print("linkcheck output not found; please run linkcheck first.")
sys.exit(1)

View File

@ -63,7 +63,7 @@ class DecompressionMiddleware:
archive = BytesIO(response.body)
try:
body = gzip.GzipFile(fileobj=archive).read()
except IOError:
except OSError:
return
respcls = responsetypes.from_args(body=body)
@ -72,7 +72,7 @@ class DecompressionMiddleware:
def _is_bzip2(self, response):
try:
body = bz2.decompress(response.body)
except IOError:
except OSError:
return
respcls = responsetypes.from_args(body=body)

View File

@ -37,7 +37,7 @@ class HttpCacheMiddleware:
ConnectionLost,
TCPTimedOutError,
ResponseFailed,
IOError,
OSError,
)
def __init__(self, settings: Settings, stats: StatsCollector) -> None:

View File

@ -268,9 +268,9 @@ RETRY_EXCEPTIONS = [
"twisted.internet.error.ConnectionLost",
"twisted.internet.error.TCPTimedOutError",
"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
IOError,
OSError,
"scrapy.core.downloader.handlers.http11.TunnelError",
]

View File

@ -15,7 +15,7 @@ def gunzip(data):
try:
chunk = f.read1(8196)
output_list.append(chunk)
except (IOError, EOFError, struct.error):
except (OSError, EOFError, struct.error):
# complete only if there is some data, otherwise re-raise
# see issue 87 about catching struct.error
# some pages are quite small so output_list is empty and f.extrabuf

View File

@ -291,7 +291,7 @@ def without_none_values(iterable):
try:
return {k: v for k, v in iterable.items() if v is not None}
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):

View File

@ -129,7 +129,7 @@ class FileTestCase(unittest.TestCase):
def test_non_existent(self):
request = Request(f"file://{self.mktemp()}")
d = self.download_request(request, Spider("foo"))
return self.assertFailure(d, IOError)
return self.assertFailure(d, OSError)
class ContentLengthHeaderResource(resource.Resource):

View File

@ -70,7 +70,7 @@ class DefaultsTest(ManagerTestCase):
In particular when some website returns a 30x response with header
'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")
@ -108,7 +108,7 @@ class DefaultsTest(ManagerTestCase):
"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):

View File

@ -1,5 +1,3 @@
# coding=utf-8
import unittest
from email.charset import Charset
from io import BytesIO

View File

@ -1,4 +1,3 @@
# coding=utf-8
from twisted.trial import unittest

View File

@ -28,7 +28,7 @@ class GunzipTest(unittest.TestCase):
def test_gunzip_no_gzip_file_raises(self):
self.assertRaises(
IOError, gunzip, (SAMPLEDIR / "feed-sample1.xml").read_bytes()
OSError, gunzip, (SAMPLEDIR / "feed-sample1.xml").read_bytes()
)
def test_gunzip_truncated_short(self):

View File

@ -346,8 +346,8 @@ class UtilsCsvTestCase(unittest.TestCase):
# explicit type check cuz' we no like stinkin' autocasting! yarrr
for result_row in result:
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(k, str) for k in result_row.keys()))
self.assertTrue(all(isinstance(v, str) for v in result_row.values()))
def test_csviter_delimiter(self):
body = get_testdata("feeds", "feed-sample3.csv").replace(b",", b"\t")