Scrapy, a fast high-level web crawling & scraping framework for Python.
test for deprecation warning
Anubhav Patel33925a776102
diff --git a/scrapy/pipelines/images.py b/scrapy/pipelines/images.pyindex ca8ac7b83..9776817bc 100644--- a/scrapy/pipelines/images.py+++ b/scrapy/pipelines/images.py@@ -141,7 +141,7 @@ class ImagesPipeline(FilesPipeline):from scrapy.exceptions import ScrapyDeprecationWarningimport warningswarnings.warn('ImagesPipeline.convert_image() method overriden in a incompatible way, '- 'overriden method does not accept response_body attribute.',+ 'overriden method does not accept response_body argument.',category=ScrapyDeprecationWarning, stacklevel=1)convert_image_overriden = _is_convert_image_overriden()@@ -162,6 +162,13 @@ class ImagesPipeline(FilesPipeline):yield thumb_path, thumb_image, thumb_bufdef convert_image(self, image, size=None, response_body=None):+ if not response_body:+ from scrapy.exceptions import ScrapyDeprecationWarning+ import warnings+ warnings.warn('ImagesPipeline.convert_image() method called in a incompatible way, '+ 'method called without response_body argument.',+ category=ScrapyDeprecationWarning, stacklevel=1)+if image.format == 'PNG' and image.mode == 'RGBA':background = Image.new('RGBA', image.size, (255, 255, 255))background.paste(image, image)@@ -177,15 +184,8 @@ class ImagesPipeline(FilesPipeline):if size:image = image.copy()image.thumbnail(size, Image.ANTIALIAS)- else:- if not response_body:- from scrapy.exceptions import ScrapyDeprecationWarning- import warnings- warnings.warn('ImagesPipeline.convert_image() method called in a incompatible way, '- 'method called without response_body attribute.',- category=ScrapyDeprecationWarning, stacklevel=1)- elif image.format == 'JPEG':- return image, response_body+ elif response_body and image.format == 'JPEG':+ return image, response_bodybuf = BytesIO()image.save(buf, 'JPEG')diff --git a/tests/test_pipeline_images.py b/tests/test_pipeline_images.pyindex 3fe71589d..651691862 100644--- a/tests/test_pipeline_images.py+++ b/tests/test_pipeline_images.py@@ -76,37 +76,48 @@ class ImagesPipelineTestCase(unittest.TestCase):'thumbs/50/850233df65a5b83361798f532f1fc549cd13cbe9.jpg')def test_convert_image(self):- SIZE = (100, 100)- # straigh forward case: RGB and JPEG+ # tests for old API+ with warnings.catch_warnings(record=True) as w:+ warnings.simplefilter('always')++ SIZE = (100, 100)+ # straigh forward case: RGB and JPEG+ COLOUR = (0, 127, 255)+ im, buf = _create_image('JPEG', 'RGB', SIZE, COLOUR)+ converted, converted_buf = self.pipeline.convert_image(im)+ self.assertEqual(converted.mode, 'RGB')+ self.assertEqual(converted.getcolors(), [(10000, COLOUR)])++ # check that thumbnail keep image ratio+ thumbnail, _ = self.pipeline.convert_image(converted, size=(10, 25))+ self.assertEqual(thumbnail.mode, 'RGB')+ self.assertEqual(thumbnail.size, (10, 10))++ # transparency case: RGBA and PNG+ COLOUR = (0, 127, 255, 50)+ im, buf = _create_image('PNG', 'RGBA', SIZE, COLOUR)+ converted, _ = self.pipeline.convert_image(im)+ self.assertEqual(converted.mode, 'RGB')+ self.assertEqual(converted.getcolors(), [(10000, (205, 230, 255))])++ # transparency case with palette: P and PNG+ COLOUR = (0, 127, 255, 50)+ im, buf = _create_image('PNG', 'RGBA', SIZE, COLOUR)+ im = im.convert('P')+ converted, _ = self.pipeline.convert_image(im)+ self.assertEqual(converted.mode, 'RGB')+ self.assertEqual(converted.getcolors(), [(10000, (205, 230, 255))])++ # ensure that we recieved deprecation warnings+ self.assertTrue(len(w) >= 4)++ # tests for new API+ # check that we don't convert JPEGs againCOLOUR = (0, 127, 255)im, buf = _create_image('JPEG', 'RGB', SIZE, COLOUR)converted, converted_buf = self.pipeline.convert_image(im, response_body=buf)- self.assertEqual(converted.mode, 'RGB')- self.assertEqual(converted.getcolors(), [(10000, COLOUR)])- # check that we don't convert JPEGs againself.assertEqual(converted_buf, buf)- # check that thumbnail keep image ratio- thumbnail, _ = self.pipeline.convert_image(converted, size=(10, 25), response_body=converted_buf)- self.assertEqual(thumbnail.mode, 'RGB')- self.assertEqual(thumbnail.size, (10, 10))-- # transparency case: RGBA and PNG- COLOUR = (0, 127, 255, 50)- im, buf = _create_image('PNG', 'RGBA', SIZE, COLOUR)- converted, _ = self.pipeline.convert_image(im, response_body=buf)- self.assertEqual(converted.mode, 'RGB')- self.assertEqual(converted.getcolors(), [(10000, (205, 230, 255))])-- # transparency case with palette: P and PNG- COLOUR = (0, 127, 255, 50)- im, buf = _create_image('PNG', 'RGBA', SIZE, COLOUR)- im = im.convert('P')- converted, _ = self.pipeline.convert_image(im, response_body=buf)- self.assertEqual(converted.mode, 'RGB')- self.assertEqual(converted.getcolors(), [(10000, (205, 230, 255))])--class DeprecatedImagesPipeline(ImagesPipeline):def file_key(self, url):return self.image_key(url)