Skip to content

Commit bc06b43

Browse files
committed
refactor: duck-typing reading magic string and try to restore the reader position if possible
1 parent ee75f0b commit bc06b43

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

filetype/utils.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,13 @@ def get_bytes(obj):
7373
if isinstance(obj, pathlib.PurePath):
7474
return get_signature_bytes(obj)
7575

76-
if isinstance(obj, BufferedIOBase):
77-
start_pos = obj.tell()
78-
obj.seek(0)
79-
magic_bytes = obj.read(_NUM_SIGNATURE_BYTES)
80-
obj.seek(start_pos) # restore reader position
81-
return get_bytes(magic_bytes)
82-
8376
if hasattr(obj, 'read'):
77+
if hasattr(obj, 'tell') and hasattr(obj, 'seek'):
78+
start_pos = obj.tell()
79+
obj.seek(0)
80+
magic_bytes = obj.read(_NUM_SIGNATURE_BYTES)
81+
obj.seek(start_pos)
82+
return get_bytes(magic_bytes)
8483
return get_bytes(obj.read(_NUM_SIGNATURE_BYTES))
8584

8685
raise TypeError('Unsupported type as file input: %s' % type(obj))

0 commit comments

Comments
 (0)