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

1import sys 

2import types 

3 

4PY2 = sys.version_info[0] == 2 

5 

6if PY2: 

7 string_types = (basestring,) 

8 integer_types = (int, long) 

9 class_types = (type, types.ClassType) 

10 text_type = unicode 

11 binary_type = str 

12 long = long 

13 

14else: 

15 string_types = (str,) 

16 integer_types = (int,) 

17 class_types = (type,) 

18 text_type = str 

19 binary_type = bytes 

20 long = int 

21 

22 

23def text_(s, encoding='latin-1', errors='strict'): 

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

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

26 if isinstance(s, binary_type): 

27 return s.decode(encoding, errors) 

28 return s 

29 

30 

31if PY2: 

32 

33 def is_nonstr_iter(v): 

34 return hasattr(v, '__iter__') 

35 

36 

37else: 

38 

39 def is_nonstr_iter(v): 

40 if isinstance(v, str): 

41 return False 

42 return hasattr(v, '__iter__') 

43 

44 

45if PY2: # pragma: no cover 

46 

47 def exec_(code, globs=None, locs=None): 

48 """Execute code in a namespace.""" 

49 if globs is None: 

50 frame = sys._getframe(1) 

51 globs = frame.f_globals 

52 if locs is None: 

53 locs = frame.f_locals 

54 del frame 

55 elif locs is None: 

56 locs = globs 

57 exec("""exec code in globs, locs""") 

58 

59 exec_( 

60 """def reraise(tp, value, tb=None): 

61 raise tp, value, tb 

62""" 

63 ) 

64 

65else: # pragma: no cover 

66 import builtins 

67 

68 exec_ = getattr(builtins, "exec") 

69 

70 def reraise(tp, value, tb=None): 

71 if value is None: 

72 value = tp 

73 if value.__traceback__ is not tb: 

74 raise value.with_traceback(tb) 

75 raise value 

76 

77 del builtins