1
0

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

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