1
0

py31compat.py 838 B

1234567891011121314151617181920212223242526272829303132
  1. __all__ = []
  2. __metaclass__ = type
  3. try:
  4. # Python >=3.2
  5. from tempfile import TemporaryDirectory
  6. except ImportError:
  7. import shutil
  8. import tempfile
  9. class TemporaryDirectory:
  10. """
  11. Very simple temporary directory context manager.
  12. Will try to delete afterward, but will also ignore OS and similar
  13. errors on deletion.
  14. """
  15. def __init__(self, **kwargs):
  16. self.name = None # Handle mkdtemp raising an exception
  17. self.name = tempfile.mkdtemp(**kwargs)
  18. def __enter__(self):
  19. return self.name
  20. def __exit__(self, exctype, excvalue, exctrace):
  21. try:
  22. shutil.rmtree(self.name, True)
  23. except OSError: # removal errors are not the only possible
  24. pass
  25. self.name = None