-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
bpo-41749: Little improve on imghdr #22166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e9a4f50
3d3d6c6
de767b5
bf6a433
e05389a
bdf5745
0dee831
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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 == '': | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| 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) | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that the signature change should not affect this call, I don't understand the error change. |
||
| 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): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Allow to programmer to don't use `file` parameter on `what` on imghdr library. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs revision, but I won't bother unless we decide to apply change. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To catch 'what()' properly, the default should be a sentinel that people do not pass. Before this add