From e9a4f50641670c6f39aa42985b9731554a4047a5 Mon Sep 17 00:00:00 2001 From: Emmanuel Arias Date: Wed, 9 Sep 2020 00:49:05 -0300 Subject: [PATCH 1/6] Little improve on imghdr For the `what()` function, the `file` parameter is always needed. In despite of that users can use `h` for send a bytes stream to detect the kind of the image, the `file` parameter is need. Don't have sense ask for `file` parameter when this parameter will not any effect on the result of the `what` function. --- Lib/imghdr.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/imghdr.py b/Lib/imghdr.py index 6e01fd857469ad8..5c4d846815f224f 100644 --- a/Lib/imghdr.py +++ b/Lib/imghdr.py @@ -8,10 +8,13 @@ # Recognize image headers # #-------------------------# -def what(file, h=None): +def what(file='', h=None): f = None try: if h is None: + if file == '': + raise ValueError("You need specify a str or PathLike for file, " + "or pass a byte stream as h parameter") if isinstance(file, (str, PathLike)): f = open(file, 'rb') h = f.read(32) From 3d3d6c67869d7e3abb109728d233d569bd9c6c14 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 9 Sep 2020 04:04:49 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2020-09-09-04-04-46.bpo-41749.xqeeeI.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2020-09-09-04-04-46.bpo-41749.xqeeeI.rst diff --git a/Misc/NEWS.d/next/Library/2020-09-09-04-04-46.bpo-41749.xqeeeI.rst b/Misc/NEWS.d/next/Library/2020-09-09-04-04-46.bpo-41749.xqeeeI.rst new file mode 100644 index 000000000000000..8860baf1bdab36c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-09-09-04-04-46.bpo-41749.xqeeeI.rst @@ -0,0 +1 @@ +Allow to programmer to don't use `file` parameter on `what` on imghdr library. \ No newline at end of file From de767b5f55b50072dd0c7bf673b87b48c5a4005d Mon Sep 17 00:00:00 2001 From: Emmanuel Arias Date: Wed, 9 Sep 2020 08:38:03 -0300 Subject: [PATCH 3/6] Update Tests --- Lib/test/test_imghdr.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_imghdr.py b/Lib/test/test_imghdr.py index b2d1fc8322a038c..c37587e4cddd39c 100644 --- a/Lib/test/test_imghdr.py +++ b/Lib/test/test_imghdr.py @@ -49,8 +49,8 @@ def test_data(self): self.assertEqual(imghdr.what(stream), expected) with open(filename, 'rb') as stream: data = stream.read() - self.assertEqual(imghdr.what(None, data), expected) - self.assertEqual(imghdr.what(None, bytearray(data)), expected) + self.assertEqual(imghdr.what(h=data), expected) + self.assertEqual(imghdr.what(h=bytearray(data)), expected) def test_pathlike_filename(self): for filename, expected in TEST_FILES: @@ -64,7 +64,7 @@ def test_jumbo(h, file): return 'ham' imghdr.tests.append(test_jumbo) self.addCleanup(imghdr.tests.pop) - self.assertEqual(imghdr.what(None, b'eggs'), 'ham') + self.assertEqual(imghdr.what(h=b'eggs'), 'ham') def test_file_pos(self): with open(TESTFN, 'wb') as stream: @@ -77,7 +77,7 @@ def test_file_pos(self): self.assertEqual(stream.tell(), pos) def test_bad_args(self): - with self.assertRaises(TypeError): + with self.assertRaises(ValueError): imghdr.what() with self.assertRaises(AttributeError): imghdr.what(None) @@ -108,7 +108,7 @@ def test_string_data(self): with self.assertRaises(TypeError): imghdr.what(io.StringIO(data)) with self.assertRaises(TypeError): - imghdr.what(None, data) + imghdr.what(h=data) def test_missing_file(self): with self.assertRaises(FileNotFoundError): From e05389a7e59127605beff5c2d63c264b2c8da0d7 Mon Sep 17 00:00:00 2001 From: Emmanuel Arias Date: Fri, 11 Sep 2020 08:07:35 -0300 Subject: [PATCH 4/6] Change assert Raise for assertwarns --- Lib/test/test_imghdr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_imghdr.py b/Lib/test/test_imghdr.py index c37587e4cddd39c..6a1f3948b60ffe9 100644 --- a/Lib/test/test_imghdr.py +++ b/Lib/test/test_imghdr.py @@ -83,7 +83,7 @@ def test_bad_args(self): imghdr.what(None) with self.assertRaises(TypeError): imghdr.what(self.testfile, 1) - with self.assertRaises(AttributeError): + with self.assertRaises(BytesWarning): imghdr.what(os.fsencode(self.testfile)) with open(self.testfile, 'rb') as f: with self.assertRaises(AttributeError): From bdf57458222939de4a9f2b0b4d52babfc1a38464 Mon Sep 17 00:00:00 2001 From: Emmanuel Arias Date: Sat, 12 Sep 2020 22:55:27 -0300 Subject: [PATCH 5/6] Update Lib/test/test_imghdr.py Co-authored-by: Terry Jan Reedy --- Lib/test/test_imghdr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_imghdr.py b/Lib/test/test_imghdr.py index 6a1f3948b60ffe9..6c6cc974e966079 100644 --- a/Lib/test/test_imghdr.py +++ b/Lib/test/test_imghdr.py @@ -77,7 +77,7 @@ def test_file_pos(self): self.assertEqual(stream.tell(), pos) def test_bad_args(self): - with self.assertRaises(ValueError): + with self.assertRaises(TypeError): imghdr.what() with self.assertRaises(AttributeError): imghdr.what(None) From 0dee831841168cf01c99cf4182aef688a109ca3b Mon Sep 17 00:00:00 2001 From: Emmanuel Arias Date: Sat, 12 Sep 2020 22:55:33 -0300 Subject: [PATCH 6/6] Update Lib/imghdr.py Co-authored-by: Terry Jan Reedy --- Lib/imghdr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/imghdr.py b/Lib/imghdr.py index 5c4d846815f224f..3bd0e9496e2484d 100644 --- a/Lib/imghdr.py +++ b/Lib/imghdr.py @@ -13,7 +13,7 @@ def what(file='', h=None): try: if h is None: if file == '': - raise ValueError("You need specify a str or PathLike for file, " + raise TypeError("You need specify a str or PathLike for file, " "or pass a byte stream as h parameter") if isinstance(file, (str, PathLike)): f = open(file, 'rb')