overlay.py 783 B

12345678910111213141516171819202122232425262728293031323334353637
  1. """
  2. Expose zipp.Path as .zipfile.Path.
  3. Includes everything else in ``zipfile`` to match future usage. Just
  4. use:
  5. >>> from zipp.compat.overlay import zipfile
  6. in place of ``import zipfile``.
  7. Relative imports are supported too.
  8. >>> from zipp.compat.overlay.zipfile import ZipInfo
  9. The ``zipfile`` object added to ``sys.modules`` needs to be
  10. hashable (#126).
  11. >>> _ = hash(sys.modules['zipp.compat.overlay.zipfile'])
  12. """
  13. import importlib
  14. import sys
  15. import types
  16. import zipp
  17. class HashableNamespace(types.SimpleNamespace):
  18. def __hash__(self):
  19. return hash(tuple(vars(self)))
  20. zipfile = HashableNamespace(**vars(importlib.import_module('zipfile')))
  21. zipfile.Path = zipp.Path
  22. zipfile._path = zipp
  23. sys.modules[__name__ + '.zipfile'] = zipfile # type: ignore[assignment]