Skip to content

Commit a21810c

Browse files
committed
Feedback from code review
1 parent 517c517 commit a21810c

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

magic/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,10 @@ def what(file: os.PathLike | str | None, h: bytes | None) -> str:
424424
imghdr source code: https://github.com/python/cpython/blob/3.12/Lib/imghdr.py
425425
"""
426426
if not h:
427-
return from_file(file, False).split()[0].lower()
427+
return from_file(file).split()[0].lower()
428428

429429
if isinstance(h, str):
430-
bytes.fromhex(h)
430+
h = bytes.fromhex(h)
431431
ext = from_buffer(h).split()[0].lower()
432432
return imghdr_exts.get(ext, ext)
433433

test/test_what.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ def test_what_from_string_py311(expected, h):
8888
@pytest.mark.parametrize(
8989
"expected, h",
9090
[
91-
# ("bmp", "424d"),
92-
# ("bmp", "424d787878785c3030305c303030"),
91+
("bmp", "424d"),
92+
("bmp", "424d787878785c3030305c303030"),
9393
("bmp", b"BM"),
9494
("jpeg", b"______JFIF"),
9595
("jpeg", b"______Exif"),
@@ -109,7 +109,7 @@ def test_what_from_string_py311(expected, h):
109109
("pgm", b"P5\n"),
110110
("pgm", b"P5\r"),
111111
("pgm", b"P5\t"),
112-
# ("png", "89504e470d0a1a0a"),
112+
("png", "89504e470d0a1a0a"),
113113
("png", b"\211PNG\r\n\032\n"),
114114
("ppm", b"P3 "),
115115
("ppm", b"P3\n"),
@@ -129,5 +129,7 @@ def test_what_from_string_todo(expected, h):
129129
These tests pass with imghdr but fail with magic.
130130
TODO: (cclauss) Fix these magic fails
131131
"""
132+
if isinstance(h, str): # In imgdir.what() h must be bytes, not str.
133+
h = bytes.fromhex(h)
132134
assert imghdr_what(None, h) == expected
133135
assert what(None, h) is None

0 commit comments

Comments
 (0)