test_pycapsule.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python
  2. #
  3. # Author: Mike McKerns (mmckerns @caltech and @uqfoundation)
  4. # Author: Anirudh Vegesana (avegesan@cs.stanford.edu)
  5. # Copyright (c) 2022-2024 The Uncertainty Quantification Foundation.
  6. # License: 3-clause BSD. The full license text is available at:
  7. # - https://github.com/uqfoundation/dill/blob/master/LICENSE
  8. """
  9. test pickling a PyCapsule object
  10. """
  11. import dill
  12. import warnings
  13. test_pycapsule = None
  14. if dill._dill._testcapsule is not None:
  15. import ctypes
  16. def test_pycapsule():
  17. name = ctypes.create_string_buffer(b'dill._testcapsule')
  18. capsule = dill._dill._PyCapsule_New(
  19. ctypes.cast(dill._dill._PyCapsule_New, ctypes.c_void_p),
  20. name,
  21. None
  22. )
  23. with warnings.catch_warnings():
  24. warnings.simplefilter("ignore")
  25. dill.copy(capsule)
  26. dill._testcapsule = capsule
  27. with warnings.catch_warnings():
  28. warnings.simplefilter("ignore")
  29. dill.copy(capsule)
  30. dill._testcapsule = None
  31. try:
  32. with warnings.catch_warnings():
  33. warnings.simplefilter("ignore", dill.PicklingWarning)
  34. dill.copy(capsule)
  35. except dill.UnpicklingError:
  36. pass
  37. else:
  38. raise AssertionError("Expected a different error")
  39. if __name__ == '__main__':
  40. if test_pycapsule is not None:
  41. test_pycapsule()