pkgdata.py 935 B

123456789101112131415161718192021222324252627282930313233
  1. # This module is deprecated and will be removed.
  2. import sys
  3. import os
  4. from io import StringIO
  5. from sympy.utilities.decorator import deprecated
  6. @deprecated(
  7. """
  8. The sympy.utilities.pkgdata module and its get_resource function are
  9. deprecated. Use the stdlib importlib.resources module instead.
  10. """,
  11. deprecated_since_version="1.12",
  12. active_deprecations_target="pkgdata",
  13. )
  14. def get_resource(identifier, pkgname=__name__):
  15. mod = sys.modules[pkgname]
  16. fn = getattr(mod, '__file__', None)
  17. if fn is None:
  18. raise OSError("%r has no __file__!")
  19. path = os.path.join(os.path.dirname(fn), identifier)
  20. loader = getattr(mod, '__loader__', None)
  21. if loader is not None:
  22. try:
  23. data = loader.get_data(path)
  24. except (OSError, AttributeError):
  25. pass
  26. else:
  27. return StringIO(data.decode('utf-8'))
  28. return open(os.path.normpath(path), 'rb')