Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
fail-fast: false
matrix:
os: ["ubuntu-latest"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13", "3.14"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13", "3.14", "3.14t"]
include:
- os: macos-latest
python-version: "3.x"
Expand Down
25 changes: 25 additions & 0 deletions test/python_magic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

import pytest

try:
from concurrent.futures import ThreadPoolExecutor
HAS_CONCURRENT_FUTURES = True
except ImportError: # python 2.7
HAS_CONCURRENT_FUTURES = False

# for output which reports a local time
os.environ["TZ"] = "GMT"

Expand Down Expand Up @@ -321,6 +327,25 @@ def test_symlink(self):

self.assertRaises(IOError, m_follow.from_file, tmp_broken)

@unittest.skipIf(not HAS_CONCURRENT_FUTURES, "concurrent.futures not available in Python 2.7")
def test_thread_safety(self):
"""Test that concurrent from_file calls don't crash (would SEGV without global lock)"""
filename = os.path.join(self.TESTDATA_DIR, "test.pdf")

m = magic.Magic(mime=True)

def check_file(_):
result = m.from_file(filename)
self.assertEqual(result, "application/pdf")
return result

with ThreadPoolExecutor(100) as executor:
results = list(executor.map(check_file, range(100)))

# All calls should complete successfully
self.assertEqual(len(results), 100)
self.assertTrue(all(r == "application/pdf" for r in results))


if __name__ == "__main__":
unittest.main()
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ envlist =
py311,
py312,
py313,
py314,
py314t,
mypy

[testenv]
Expand Down