View Issue Details

IDProjectCategoryView StatusLast Update
0000007file[All Projects] Generalpublic2018-08-01 09:03
ReporterbodoAssigned Tochristos 
PrioritylowSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
Product Version 
Target VersionFixed in Version 
Summary0000007: python function magic.detect_from_filename does not handle non-existent file
DescriptionThe python api provides the function magic.detect_from_filename. When the file given as argument does not exist, it throws the exception
ValueError: not enough values to unpack (expected 2, got 1)
which does not correctly represent the failure condition.

detect_from_filename calls _create_filemagic with the return value of mime_magic.file.
If the file does not exist, mime_magic.file returns "cannot open `foobar' (No such file or directory)".
_create_filemagic then goes on to split that value into mime_type and mime_encoding.
But as the error message does not contain a semicolon, this leads to the above exception.
Steps To ReproduceDo not have the file 'foobar' in the current directory and execute the following in the python intepreter:

>>> import magic
>>> magic.detect_from_filename('foobar')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.5/site-packages/magic.py", line 264, in detect_from_filename
    none_magic.file(filename))
  File "/usr/lib64/python3.5/site-packages/magic.py", line 251, in _create_filemagic
    mime_type, mime_encoding = mime_detected.split('; ')
ValueError: not enough values to unpack (expected 2, got 1)
Additional Informationversions:
- sys-apps/file-5.32 on gentoo linux with USE="+python"
- python-3.5.5
TagsNo tags attached.
Attach Tags

Activities

christos

2018-06-23 14:58

manager   ~0000002

Committed a change to raise the actual error as a ValueError

bodo

2018-06-24 14:11

reporter   ~0000008

Works fine in python2 now. Python3 gives:

TabError: inconsistent use of tabs and spaces in indentation

Please replace your tabs with 8 spaces.

christos

2018-06-24 15:51

manager   ~0000009

Fixed, thanks!

christos

2018-07-25 06:08

manager   ~0000012

Should be fixed in HEAD:

$ python
Python 2.7.13 (default, Feb 13 2017, 09:50:26)
[GCC 5.4.0] on netbsd7
Type "help", "copyright", "credits" or "license" for more information.
>>> import magic
>>> magic.detect_from_filename('foobar')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "magic.py", line 267, in detect_from_filename
    none_magic.file(filename))
  File "magic.py", line 254, in _create_filemagic
    raise ValueError(mime_detected)
ValueError: cannot open `foobar' (No such file or directory)
>>>

christos

2018-08-01 09:03

manager   ~0000024

feedback timeout

Issue History

Date Modified Username Field Change
2018-06-22 21:54 bodo New Issue
2018-06-23 14:56 christos Assigned To => christos
2018-06-23 14:56 christos Status new => assigned
2018-06-23 14:58 christos Status assigned => feedback
2018-06-23 14:58 christos Note Added: 0000002
2018-06-24 14:11 bodo Note Added: 0000008
2018-06-24 14:11 bodo Status feedback => assigned
2018-06-24 15:51 christos Note Added: 0000009
2018-07-25 06:07 christos Assigned To christos =>
2018-07-25 06:08 christos Assigned To => christos
2018-07-25 06:08 christos Status assigned => feedback
2018-07-25 06:08 christos Note Added: 0000012
2018-08-01 09:03 christos Status feedback => resolved
2018-08-01 09:03 christos Resolution open => fixed
2018-08-01 09:03 christos Note Added: 0000024