Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""Python 2/3 compatibility.""" 

2# flake8: noqa 

3# Standard Library 

4import string 

5import sys 

6import types 

7import urllib 

8 

9 

10try: 

11 uppercase = string.uppercase 

12except AttributeError: # pragma: no cover 

13 uppercase = string.ascii_uppercase 

14 

15# True if we are running on Python 3. 

16PY3 = sys.version_info[0] == 3 

17 

18if PY3: # pragma: no cover 

19 string_types = (str,) 

20 integer_types = (int,) 

21 class_types = (type,) 

22 text_type = str 

23 binary_type = bytes 

24 long = int 

25 sequence_types = ( 

26 list, 

27 tuple, 

28 range, 

29 ) 

30else: 

31 string_types = (basestring,) 

32 integer_types = (int, long) 

33 class_types = (type, types.ClassType) 

34 text_type = unicode 

35 binary_type = str 

36 long = long 

37 sequence_types = ( 

38 list, 

39 tuple, 

40 xrange, 

41 ) 

42 

43 

44def text_(s, encoding="latin-1", errors="strict"): 

45 """If ``s`` is an instance of ``binary_type``, return 

46 ``s.decode(encoding, errors)``, otherwise return ``s``""" 

47 if isinstance(s, binary_type): 

48 return s.decode(encoding, errors) 

49 return s # pragma: no cover 

50 

51 

52def bytes_(s, encoding="latin-1", errors="strict"): 

53 """If ``s`` is an instance of ``text_type``, return 

54 ``s.encode(encoding, errors)``, otherwise return ``s``""" 

55 if isinstance(s, text_type): # pragma: no cover 

56 return s.encode(encoding, errors) 

57 return s # pragma: no cover 

58 

59 

60try: 

61 from StringIO import StringIO 

62except ImportError: # pragma: no cover 

63 # Standard Library 

64 from io import StringIO 

65 

66 

67try: 

68 url_quote = urllib.quote 

69 url_unquote = urllib.unquote 

70except AttributeError: # pragma: no cover 

71 # Standard Library 

72 import urllib.parse 

73 

74 url_quote = urllib.parse.quote 

75 url_unquote = urllib.parse.unquote