You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.9KB

  1. import codecs
  2. import sys
  3. import typing as t
  4. import warnings
  5. # We do not trust traditional unixes.
  6. has_likely_buggy_unicode_filesystem = (
  7. sys.platform.startswith("linux") or "bsd" in sys.platform
  8. )
  9. def _is_ascii_encoding(encoding: t.Optional[str]) -> bool:
  10. """Given an encoding this figures out if the encoding is actually ASCII (which
  11. is something we don't actually want in most cases). This is necessary
  12. because ASCII comes under many names such as ANSI_X3.4-1968.
  13. """
  14. if encoding is None:
  15. return False
  16. try:
  17. return codecs.lookup(encoding).name == "ascii"
  18. except LookupError:
  19. return False
  20. class BrokenFilesystemWarning(RuntimeWarning, UnicodeWarning):
  21. """The warning used by Werkzeug to signal a broken filesystem. Will only be
  22. used once per runtime."""
  23. _warned_about_filesystem_encoding = False
  24. def get_filesystem_encoding() -> str:
  25. """Returns the filesystem encoding that should be used. Note that this is
  26. different from the Python understanding of the filesystem encoding which
  27. might be deeply flawed. Do not use this value against Python's string APIs
  28. because it might be different. See :ref:`filesystem-encoding` for the exact
  29. behavior.
  30. The concept of a filesystem encoding in generally is not something you
  31. should rely on. As such if you ever need to use this function except for
  32. writing wrapper code reconsider.
  33. """
  34. global _warned_about_filesystem_encoding
  35. rv = sys.getfilesystemencoding()
  36. if has_likely_buggy_unicode_filesystem and not rv or _is_ascii_encoding(rv):
  37. if not _warned_about_filesystem_encoding:
  38. warnings.warn(
  39. "Detected a misconfigured UNIX filesystem: Will use"
  40. f" UTF-8 as filesystem encoding instead of {rv!r}",
  41. BrokenFilesystemWarning,
  42. )
  43. _warned_about_filesystem_encoding = True
  44. return "utf-8"
  45. return rv