Skip to content

Commit

Permalink
Feedback from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
cclauss committed May 22, 2024
1 parent 517c517 commit a21810c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions magic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,10 @@ def what(file: os.PathLike | str | None, h: bytes | None) -> str:
imghdr source code: https://github.com/python/cpython/blob/3.12/Lib/imghdr.py
"""
if not h:
return from_file(file, False).split()[0].lower()
return from_file(file).split()[0].lower()

if isinstance(h, str):
bytes.fromhex(h)
h = bytes.fromhex(h)
ext = from_buffer(h).split()[0].lower()
return imghdr_exts.get(ext, ext)

Expand Down
8 changes: 5 additions & 3 deletions test/test_what.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ def test_what_from_string_py311(expected, h):
@pytest.mark.parametrize(
"expected, h",
[
# ("bmp", "424d"),
# ("bmp", "424d787878785c3030305c303030"),
("bmp", "424d"),
("bmp", "424d787878785c3030305c303030"),
("bmp", b"BM"),
("jpeg", b"______JFIF"),
("jpeg", b"______Exif"),
Expand All @@ -109,7 +109,7 @@ def test_what_from_string_py311(expected, h):
("pgm", b"P5\n"),
("pgm", b"P5\r"),
("pgm", b"P5\t"),
# ("png", "89504e470d0a1a0a"),
("png", "89504e470d0a1a0a"),
("png", b"\211PNG\r\n\032\n"),
("ppm", b"P3 "),
("ppm", b"P3\n"),
Expand All @@ -129,5 +129,7 @@ def test_what_from_string_todo(expected, h):
These tests pass with imghdr but fail with magic.
TODO: (cclauss) Fix these magic fails
"""
if isinstance(h, str): # In imgdir.what() h must be bytes, not str.
h = bytes.fromhex(h)
assert imghdr_what(None, h) == expected
assert what(None, h) is None

0 comments on commit a21810c

Please sign in to comment.