0.4.27-52-g892543d.add-test.patch 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. Subject: Add test
  2. Origin: upstream, commit 0.4.27-52-g892543d <https://github.com/ahupp/python-magic/commit/0.4.27-52-g892543d>
  3. Author: ddelange <14880945+ddelange@users.noreply.github.com>
  4. Date: Tue Oct 14 14:39:45 2025 +0300
  5. --- a/magic/__init__.py
  6. +++ b/magic/__init__.py
  7. @@ -416,7 +416,8 @@
  8. raise NotImplementedError("magic_getparam not implemented")
  9. val = c_size_t()
  10. with LOCK:
  11. - return _magic_getparam(cookie, param, byref(val)).value
  12. + _magic_getparam(cookie, param, byref(val))
  13. + return val.value
  14. _has_version = False
  15. @@ -427,7 +428,7 @@
  16. magic_version.argtypes = []
  17. -def version(lock=None):
  18. +def version():
  19. if not _has_version:
  20. raise NotImplementedError("magic_version not implemented")
  21. with LOCK:
  22. --- a/test/python_magic_test.py
  23. +++ b/test/python_magic_test.py
  24. @@ -10,6 +10,12 @@
  25. import pytest
  26. +try:
  27. + from concurrent.futures import ThreadPoolExecutor
  28. + HAS_CONCURRENT_FUTURES = True
  29. +except ImportError: # python 2.7
  30. + HAS_CONCURRENT_FUTURES = False
  31. +
  32. # for output which reports a local time
  33. os.environ["TZ"] = "GMT"
  34. @@ -321,6 +327,25 @@
  35. self.assertRaises(IOError, m_follow.from_file, tmp_broken)
  36. + @unittest.skipIf(not HAS_CONCURRENT_FUTURES, "concurrent.futures not available in Python 2.7")
  37. + def test_thread_safety(self):
  38. + """Test that concurrent from_file calls don't crash (would SEGV without global lock)"""
  39. + filename = os.path.join(self.TESTDATA_DIR, "test.pdf")
  40. +
  41. + m = magic.Magic(mime=True)
  42. +
  43. + def check_file(_):
  44. + result = m.from_file(filename)
  45. + self.assertEqual(result, "application/pdf")
  46. + return result
  47. +
  48. + with ThreadPoolExecutor(100) as executor:
  49. + results = list(executor.map(check_file, range(100)))
  50. +
  51. + # All calls should complete successfully
  52. + self.assertEqual(len(results), 100)
  53. + self.assertTrue(all(r == "application/pdf" for r in results))
  54. +
  55. if __name__ == "__main__":
  56. unittest.main()