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 8import os 9import sys 10 11__all__ = ['cache_from_source', 'callable', 'fsencode'] 12 13 14try: 15 from imp import cache_from_source 16except ImportError: 17 def cache_from_source(py_file, debug=__debug__): 18 ext = debug and 'c' or 'o' 19 return py_file + ext 20 21 22try: 23 callable = callable 24except NameError: 25 from collections import Callable 26 27 def callable(obj): 28 return isinstance(obj, Callable) 29 30 31try: 32 fsencode = os.fsencode 33except AttributeError: 34 def fsencode(filename): 35 if isinstance(filename, bytes): 36 return filename 37 elif isinstance(filename, str): 38 return filename.encode(sys.getfilesystemencoding()) 39 else: 40 raise TypeError("expect bytes or str, not %s" % 41 type(filename).__name__) 42