Skip to content

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

verify if Request callback is callable

Valdir Stumm Junior60727dedf605
diff --git a/scrapy/http/request/__init__.py b/scrapy/http/request/__init__.py
index 1435d91de..b9c5f8541 100644
--- a/scrapy/http/request/__init__.py
+++ b/scrapy/http/request/__init__.py
@@ -27,6 +27,10 @@ class Request(object_ref):
assert isinstance(priority, int), "Request priority not an integer: %r" % priority
self.priority = priority
+ if callback is not None and not callable(callback):
+ raise TypeError('callback must be a function, got %s' % type(callback).__name__)
+ if errback is not None and not callable(errback):
+ raise TypeError('errback must be a function, got %s' % type(errback).__name__)
assert callback or not errback, "Cannot use errback without a callback"
self.callback = callback
self.errback = errback
diff --git a/tests/test_http_request.py b/tests/test_http_request.py
index bbce537f4..9b0ee63dc 100644
--- a/tests/test_http_request.py
+++ b/tests/test_http_request.py
@@ -235,6 +235,26 @@ class RequestTest(unittest.TestCase):
self.assertRaises(AttributeError, setattr, r, 'url', 'http://example2.com')
self.assertRaises(AttributeError, setattr, r, 'body', 'xxx')
+ def test_callback_is_callable(self):
+ def a_function():
+ pass
+ r = self.request_class('http://example.com')
+ self.assertIsNone(r.callback)
+ r = self.request_class('http://example.com', a_function)
+ self.assertIs(r.callback, a_function)
+ with self.assertRaises(TypeError):
+ self.request_class('http://example.com', 'a_function')
+
+ def test_errback_is_callable(self):
+ def a_function():
+ pass
+ r = self.request_class('http://example.com')
+ self.assertIsNone(r.errback)
+ r = self.request_class('http://example.com', a_function, errback=a_function)
+ self.assertIs(r.errback, a_function)
+ with self.assertRaises(TypeError):
+ self.request_class('http://example.com', a_function, errback='a_function')
+
class FormRequestTest(RequestTest):