Development of an internal social media platform with personalised dashboards for students
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.

misc.py 971B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012 The Python Software Foundation.
  4. # See LICENSE.txt and CONTRIBUTORS.txt.
  5. #
  6. """Backports for individual classes and functions."""
  7. import os
  8. import sys
  9. __all__ = ['cache_from_source', 'callable', 'fsencode']
  10. try:
  11. from imp import cache_from_source
  12. except ImportError:
  13. def cache_from_source(py_file, debug=__debug__):
  14. ext = debug and 'c' or 'o'
  15. return py_file + ext
  16. try:
  17. callable = callable
  18. except NameError:
  19. from collections import Callable
  20. def callable(obj):
  21. return isinstance(obj, Callable)
  22. try:
  23. fsencode = os.fsencode
  24. except AttributeError:
  25. def fsencode(filename):
  26. if isinstance(filename, bytes):
  27. return filename
  28. elif isinstance(filename, str):
  29. return filename.encode(sys.getfilesystemencoding())
  30. else:
  31. raise TypeError("expect bytes or str, not %s" %
  32. type(filename).__name__)