Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Handle AttributeError: 'NoneType' in contract parsing (#6388)

Suvan Banerjee3f76853bd27d
diff --git a/scrapy/contracts/__init__.py b/scrapy/contracts/__init__.py
index b300b8457..27bc2fcba 100644
--- a/scrapy/contracts/__init__.py
+++ b/scrapy/contracts/__init__.py
@@ -120,7 +120,8 @@ class ContractsManager:
if line.startswith("@"):
m = re.match(r"@(\w+)\s*(.*)", line)
- assert m is not None
+ if m is None:
+ continue
name, args = m.groups()
args = re.split(r"\s+", args)
diff --git a/tests/test_contracts.py b/tests/test_contracts.py
index 1459e0b5f..c9c12f0d8 100644
--- a/tests/test_contracts.py
+++ b/tests/test_contracts.py
@@ -182,6 +182,19 @@ class TestSpider(Spider):
"""
pass
+ def invalid_regex(self, response):
+ """method with invalid regex
+ @ Scrapy is awsome
+ """
+ pass
+
+ def invalid_regex_with_valid_contract(self, response):
+ """method with invalid regex
+ @ scrapy is awsome
+ @url http://scrapy.org
+ """
+ pass
+
class CustomContractSuccessSpider(Spider):
name = "custom_contract_success_spider"
@@ -385,6 +398,21 @@ class ContractsManagerTest(unittest.TestCase):
message = "ContractFail: Missing fields: name, url"
assert message in self.results.failures[-1][-1]
+ def test_regex(self):
+ spider = TestSpider()
+ response = ResponseMock()
+
+ # invalid regex
+ request = self.conman.from_method(spider.invalid_regex, self.results)
+ self.should_succeed()
+
+ # invalid regex with valid contract
+ request = self.conman.from_method(
+ spider.invalid_regex_with_valid_contract, self.results
+ )
+ self.should_succeed()
+ request.callback(response)
+
def test_custom_contracts(self):
self.conman.from_spider(CustomContractSuccessSpider(), self.results)
self.should_succeed()