diff --git a/Lib/imghdr.py b/Lib/imghdr.py index 6e01fd857469ad8..3bd0e9496e2484d 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 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') h = f.read(32) diff --git a/Lib/test/test_imghdr.py b/Lib/test/test_imghdr.py index b2d1fc8322a038c..6c6cc974e966079 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: @@ -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): @@ -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): 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