diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 049880d..c478602 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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" diff --git a/test/python_magic_test.py b/test/python_magic_test.py index 5076044..41c1d07 100755 --- a/test/python_magic_test.py +++ b/test/python_magic_test.py @@ -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" @@ -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() diff --git a/tox.ini b/tox.ini index 5c1648b..01cb7b2 100644 --- a/tox.ini +++ b/tox.ini @@ -10,6 +10,8 @@ envlist = py311, py312, py313, + py314, + py314t, mypy [testenv]