# Buggy code
def process_response(self, request, response, spider):
    if request.method == "HEAD":
        return response
    if isinstance(response, Response):
        content_encoding = response.headers.getlist("Content-Encoding")
        if content_encoding and not is_gzipped(response):
            encoding = content_encoding.pop()
            decoded_body = self._decode(response.body, encoding.lower())
            respcls = responsetypes.from_args(
                headers=response.headers, url=response.url, body=decoded_body
            )
            kwargs = dict(cls=respcls, body=decoded_body)
            if issubclass(respcls, TextResponse):
                # force recalculating the encoding until we make sure the
                # responsetypes guessing is reliable
                kwargs["encoding"] = None
            response = response.replace(**kwargs)
            if not content_encoding:
                del response.headers["Content-Encoding"]

    return response


# Traceback (AttributeError)
$ scrapy runspider spider.py
2016-11-09 15:53:10 [scrapy] INFO: Scrapy 1.2.1 started (bot: scrapybot)
(...)
2016-11-09 15:53:10 [scrapy] INFO: Spider opened
2016-11-09 15:53:10 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2016-11-09 15:53:10 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2016-11-09 15:53:11 [scrapy] DEBUG: Crawled (200) <GET http://www.newegg.com/Siteindex_USA.xml> (referer: None)
2016-11-09 15:53:11 [scrapy] DEBUG: Crawled (200) <GET http://www.newegg.com/Sitemap/USA/newegg_sitemap_store01.xml.gz> (referer: http://www.newegg.com/Siteindex_USA.xml)
2016-11-09 15:53:11 [scrapy] ERROR: Spider error processing <GET http://www.newegg.com/Sitemap/USA/newegg_sitemap_store01.xml.gz> (referer: http://www.newegg.com/Siteindex_USA.xml)
Traceback (most recent call last):
File "/home/paul/.virtualenvs/scrapy12/local/lib/python2.7/site-packages/scrapy/utils/defer.py", line 102, in iter_errback
yield next(it)
File "/home/paul/.virtualenvs/scrapy12/local/lib/python2.7/site-packages/scrapy/spidermiddlewares/offsite.py", line 29, in process_spider_output
for x in result:
File "/home/paul/.virtualenvs/scrapy12/local/lib/python2.7/site-packages/scrapy/spidermiddlewares/referer.py", line 22, in <genexpr>
return (_set_referer(r) for r in result or ())
File "/home/paul/.virtualenvs/scrapy12/local/lib/python2.7/site-packages/scrapy/spidermiddlewares/urllength.py", line 37, in <genexpr>
return (r for r in result or () if _filter(r))
File "/home/paul/.virtualenvs/scrapy12/local/lib/python2.7/site-packages/scrapy/spidermiddlewares/depth.py", line 58, in <genexpr>
return (r for r in result or () if _filter(r))
File "/home/paul/.virtualenvs/scrapy12/local/lib/python2.7/site-packages/scrapy/spiders/sitemap.py", line 44, in _parse_sitemap
s = Sitemap(body)
File "/home/paul/.virtualenvs/scrapy12/local/lib/python2.7/site-packages/scrapy/utils/sitemap.py", line 17, in __init__
rt = self._root.tag
AttributeError: 'NoneType' object has no attribute 'tag'

# Fixed code
def process_response(self, request, response, spider):
    if request.method == "HEAD":
        return response
    if isinstance(response, Response):
        content_encoding = response.headers.getlist("Content-Encoding")
        if content_encoding:
            encoding = content_encoding.pop()
            decoded_body = self._decode(response.body, encoding.lower())
            respcls = responsetypes.from_args(
                headers=response.headers, url=response.url, body=decoded_body
            )
            kwargs = dict(cls=respcls, body=decoded_body)
            if issubclass(respcls, TextResponse):
                # force recalculating the encoding until we make sure the
                # responsetypes guessing is reliable
                kwargs["encoding"] = None
            response = response.replace(**kwargs)
            if not content_encoding:
                del response.headers["Content-Encoding"]

    return response