Skip to content

from_buffer() issue with file-5.47 #362

Description

@shosca

Using archlinux, with python-magic 0.4.27, there seems to be an issue with file-5.47-1 and python-magic when using from_buffer

In [1]: import magic

In [2]: magic.from_file("sample.mp4", mime=True)
Out[2]: 'video/mp4'

In [3]: magic.from_buffer(open("sample.mp4", "rb").read(2048), mime=True)
Out[3]: 'application/octet-stream'

In [4]: magic.from_file("sample.webm", mime=True)
Out[4]: 'video/webm'

In [5]: magic.from_buffer(open("sample.webm", "rb").read(2048), mime=True)
Out[5]: 'application/octet-stream'

after downgrading to file-5.46-5 everything works

In [1]: import magic

In [2]: magic.from_file("sample.mp4", mime=True)
Out[2]: 'video/mp4'

In [3]: magic.from_buffer(open("sample.mp4", "rb").read(2048), mime=True)
Out[3]: 'video/mp4'

In [4]: magic.from_file("sample.webm", mime=True)
Out[4]: 'video/webm'

In [5]: magic.from_buffer(open("sample.webm", "rb").read(2048), mime=True)
Out[5]: 'video/webm'

Activity

  1. ddelange commented on Mar 21, 2026

    @ddelange
    Contributor

    I can confirm this issue, just tried bumping to file-5.47 in #294 and the smoke test starts failing:

      Successfully installed python-magic-0.4.28
      + /bin/sh -c 'python -c "import magic; assert magic.Magic(mime=True).from_buffer(b'"'"'\x00\x00\x00\x1cftypiso5\x00\x00\x00\x01isomiso5hlsf\x00\x00'"'"') == '"'"'video/mp4'"'"'"'
      Traceback (most recent call last):
        File "<string>", line 1, in <module>
      AssertionError

    Sounds like an upstream issue because this library is just a CDLL wrapper.

  2. bvergnaud commented on Apr 1, 2026

    @bvergnaud

    Hiya,

    For anyone stumbling here, a similar problem was reported upstream here (requires creating an account).

    Date Submitted: 2026-03-04 12:58
    Last Update: 2026-04-01 08:54

    They seem to be on the right track. I spent some time digging tonight, I had identified a commit that messed with offset calculations in file_buffer(). I intended to report it, but I found that issue, and based on the latest comments from today, they found the same commit and code paths I was looking at.

    In the meantime:

    • Using magic.from_file() or magic.from_descriptor() works fine.
    • If you really want to use buffers (e.g. to avoid reading huge files), save the buffer to a temporary file and use magic.from_file() on that.
    • Or pin your file package ≤ 5.46, until further news.

    UPDATE: The bug is reported as patched by one of the maintainers, as of 2026-04-12 22:15 (UTC). All we need now is to wait for a 5.48 release. Which unfortunately might take a while: 5.47 landed Feb 26, 2026, 5.46 landed Nov 27, 2024. Let's hope we won't have to wait 1 more year. 😅

  3. added a commit that references this issue on Apr 8, 2026
  4. doronbehar commented on May 4, 2026

    @doronbehar

    Did anyone manage to figure out what upstream commit fixed the issue? I didn't find a clear candidate here:

    I'd like to patch it for my distribution...

  5. bvergnaud commented on May 4, 2026

    @bvergnaud

    Did anyone manage to figure out what upstream commit fixed the issue? I didn't find a clear candidate here:

    * https://bugs.astron.com/view.php?id=725
    

    I'd like to patch it for my distribution...

    Here you go:
    file/file@c546057

  6. RafayAK commented on May 18, 2026

    @RafayAK

    Just faced this issue, for somereason audio/ogg is getting picked up as application/octet-stream

     python -u - <<'PY'
    import magic
    p='/Path/To/Audio-Notes/Q1.ogg'
    m=magic.Magic(mime=True)
    print('from_file', m.from_file(p))
    with open(p,'rb') as f:
        b=f.read(8192)
    print('from_buffer_2048', m.from_buffer(b[:2048]))
    print('from_buffer_8192', m.from_buffer(b))
    PY
    from_file audio/ogg
    from_buffer_2048 application/octet-stream
    from_buffer_8192 application/octet-stream
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions