test_sandbox.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. """develop tests
  2. """
  3. import os
  4. import types
  5. import pytest
  6. import pkg_resources
  7. import setuptools.sandbox
  8. from setuptools.sandbox import DirectorySandbox
  9. class TestSandbox:
  10. def test_devnull(self, tmpdir):
  11. sandbox = DirectorySandbox(str(tmpdir))
  12. sandbox.run(self._file_writer(os.devnull))
  13. @staticmethod
  14. def _file_writer(path):
  15. def do_write():
  16. with open(path, 'w') as f:
  17. f.write('xxx')
  18. return do_write
  19. def test_win32com(self, tmpdir):
  20. """
  21. win32com should not be prevented from caching COM interfaces
  22. in gen_py.
  23. """
  24. win32com = pytest.importorskip('win32com')
  25. gen_py = win32com.__gen_path__
  26. target = os.path.join(gen_py, 'test_write')
  27. sandbox = DirectorySandbox(str(tmpdir))
  28. try:
  29. # attempt to create gen_py file
  30. sandbox.run(self._file_writer(target))
  31. finally:
  32. if os.path.exists(target):
  33. os.remove(target)
  34. def test_setup_py_with_BOM(self):
  35. """
  36. It should be possible to execute a setup.py with a Byte Order Mark
  37. """
  38. target = pkg_resources.resource_filename(__name__,
  39. 'script-with-bom.py')
  40. namespace = types.ModuleType('namespace')
  41. setuptools.sandbox._execfile(target, vars(namespace))
  42. assert namespace.result == 'passed'
  43. def test_setup_py_with_CRLF(self, tmpdir):
  44. setup_py = tmpdir / 'setup.py'
  45. with setup_py.open('wb') as stream:
  46. stream.write(b'"degenerate script"\r\n')
  47. setuptools.sandbox._execfile(str(setup_py), globals())
  48. class TestExceptionSaver:
  49. def test_exception_trapped(self):
  50. with setuptools.sandbox.ExceptionSaver():
  51. raise ValueError("details")
  52. def test_exception_resumed(self):
  53. with setuptools.sandbox.ExceptionSaver() as saved_exc:
  54. raise ValueError("details")
  55. with pytest.raises(ValueError) as caught:
  56. saved_exc.resume()
  57. assert isinstance(caught.value, ValueError)
  58. assert str(caught.value) == 'details'
  59. def test_exception_reconstructed(self):
  60. orig_exc = ValueError("details")
  61. with setuptools.sandbox.ExceptionSaver() as saved_exc:
  62. raise orig_exc
  63. with pytest.raises(ValueError) as caught:
  64. saved_exc.resume()
  65. assert isinstance(caught.value, ValueError)
  66. assert caught.value is not orig_exc
  67. def test_no_exception_passes_quietly(self):
  68. with setuptools.sandbox.ExceptionSaver() as saved_exc:
  69. pass
  70. saved_exc.resume()
  71. def test_unpickleable_exception(self):
  72. class CantPickleThis(Exception):
  73. "This Exception is unpickleable because it's not in globals"
  74. with setuptools.sandbox.ExceptionSaver() as saved_exc:
  75. raise CantPickleThis('detail')
  76. with pytest.raises(setuptools.sandbox.UnpickleableException) as caught:
  77. saved_exc.resume()
  78. assert str(caught.value) == "CantPickleThis('detail',)"
  79. def test_unpickleable_exception_when_hiding_setuptools(self):
  80. """
  81. As revealed in #440, an infinite recursion can occur if an unpickleable
  82. exception while setuptools is hidden. Ensure this doesn't happen.
  83. """
  84. class ExceptionUnderTest(Exception):
  85. """
  86. An unpickleable exception (not in globals).
  87. """
  88. with pytest.raises(setuptools.sandbox.UnpickleableException) as caught:
  89. with setuptools.sandbox.save_modules():
  90. setuptools.sandbox.hide_setuptools()
  91. raise ExceptionUnderTest()
  92. msg, = caught.value.args
  93. assert msg == 'ExceptionUnderTest()'
  94. def test_sandbox_violation_raised_hiding_setuptools(self, tmpdir):
  95. """
  96. When in a sandbox with setuptools hidden, a SandboxViolation
  97. should reflect a proper exception and not be wrapped in
  98. an UnpickleableException.
  99. """
  100. def write_file():
  101. "Trigger a SandboxViolation by writing outside the sandbox"
  102. with open('/etc/foo', 'w'):
  103. pass
  104. sandbox = DirectorySandbox(str(tmpdir))
  105. with pytest.raises(setuptools.sandbox.SandboxViolation) as caught:
  106. with setuptools.sandbox.save_modules():
  107. setuptools.sandbox.hide_setuptools()
  108. sandbox.run(write_file)
  109. cmd, args, kwargs = caught.value.args
  110. assert cmd == 'open'
  111. assert args == ('/etc/foo', 'w')
  112. assert kwargs == {}