py31compat.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. __all__ = ['get_config_vars', 'get_path']
  2. try:
  3. # Python 2.7 or >=3.2
  4. from sysconfig import get_config_vars, get_path
  5. except ImportError:
  6. from distutils.sysconfig import get_config_vars, get_python_lib
  7. def get_path(name):
  8. if name not in ('platlib', 'purelib'):
  9. raise ValueError("Name must be purelib or platlib")
  10. return get_python_lib(name == 'platlib')
  11. try:
  12. # Python >=3.2
  13. from tempfile import TemporaryDirectory
  14. except ImportError:
  15. import shutil
  16. import tempfile
  17. class TemporaryDirectory(object):
  18. """
  19. Very simple temporary directory context manager.
  20. Will try to delete afterward, but will also ignore OS and similar
  21. errors on deletion.
  22. """
  23. def __init__(self):
  24. self.name = None # Handle mkdtemp raising an exception
  25. self.name = tempfile.mkdtemp()
  26. def __enter__(self):
  27. return self.name
  28. def __exit__(self, exctype, excvalue, exctrace):
  29. try:
  30. shutil.rmtree(self.name, True)
  31. except OSError: # removal errors are not the only possible
  32. pass
  33. self.name = None