Coverage for /Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm/teamcity/common.py: 9%

98 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2025-06-06 12:04 +0200

1# coding=utf-8 

2 

3import sys 

4import traceback 

5import inspect 

6 

7 

8_max_reported_output_size = 1 * 1024 * 1024 

9_reported_output_chunk_size = 50000 

10 

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

12if PY2: 

13 text_type = unicode # noqa: F821 

14 binary_type = str 

15else: 

16 text_type = str 

17 binary_type = bytes 

18 

19_sys_stdout_encoding = sys.stdout.encoding 

20 

21if PY2: 

22 from StringIO import StringIO # noqa: F821 

23else: 

24 from io import StringIO 

25 

26# stdin and stdout encodings should be the same. 

27# Since stdout may already be monkeypatches, we use stdin 

28_ENCODING = sys.stdin.encoding if sys.stdin.encoding else "UTF-8" 

29 

30 

31class FlushingStringIO(StringIO, object): 

32 

33 encoding = _ENCODING # stdout must have encoding 

34 

35 def __init__(self, flush_function): 

36 super(FlushingStringIO, self).__init__() 

37 

38 self._flush_function = flush_function 

39 self.encoding = _ENCODING 

40 

41 def _flush_to_flush_function(self): 

42 self._flush_function(self.getvalue()) 

43 self.seek(0) 

44 self.truncate() 

45 

46 def write(self, str): 

47 super(FlushingStringIO, self).write(str) 

48 

49 if '\n' in str: 

50 self._flush_to_flush_function() 

51 

52 def flush(self, *args, **kwargs): 

53 self._flush_to_flush_function() 

54 return super(FlushingStringIO, self).flush(*args, **kwargs) 

55 

56 

57def limit_output(data): 

58 return data[:_max_reported_output_size] 

59 

60 

61def split_output(data): 

62 while len(data) > 0: 

63 if len(data) <= _reported_output_chunk_size: 63 ↛ 67line 63 didn't jump to line 67 because the condition on line 63 was always true

64 yield data 

65 data = '' 

66 else: 

67 yield data[:_reported_output_chunk_size] 

68 data = data[_reported_output_chunk_size:] 

69 

70 

71def dump_test_stdout(messages, test_id, flow_id, data): 

72 for chunk in split_output(limit_output(data)): 

73 messages.testStdOut(test_id, chunk, flowId=flow_id) 

74 

75 

76def dump_test_stderr(messages, test_id, flow_id, data): 

77 for chunk in split_output(limit_output(data)): 

78 messages.testStdErr(test_id, chunk, flowId=flow_id) 

79 

80 

81def is_string(obj): 

82 if sys.version_info >= (3, 0): 

83 return isinstance(obj, str) 

84 else: 

85 return isinstance(obj, basestring) # noqa: F821 

86 

87 

88def get_output_encoding(): 

89 import locale 

90 loc = locale.getdefaultlocale() 

91 if loc[1]: 

92 return loc[1] 

93 return _sys_stdout_encoding 

94 

95 

96def get_exception_message(e): 

97 if e.args and isinstance(e.args[0], binary_type): 

98 return e.args[0].decode(get_output_encoding()) 

99 return text_type(e) 

100 

101 

102def to_unicode(obj): 

103 if isinstance(obj, binary_type): 

104 return obj.decode(get_output_encoding()) 

105 elif isinstance(obj, text_type): 

106 return obj 

107 else: 

108 if PY2: 

109 raise TypeError("Expected str or unicode") 

110 else: 

111 raise TypeError("Expected bytes or str") 

112 

113 

114def get_class_fullname(something): 

115 if inspect.isclass(something): 

116 cls = something 

117 else: 

118 cls = something.__class__ 

119 

120 module = cls.__module__ 

121 if module is None or module == str.__class__.__module__: 

122 return cls.__name__ 

123 return module + '.' + cls.__name__ 

124 

125 

126def convert_error_to_string(err, frames_to_skip_from_tail=None): 

127 """ 

128 

129 :param frames_to_skip_from_tail: may be int or list of str. In latter case frames with these strings are skipped 

130 """ 

131 try: 

132 if hasattr(err, "type") and hasattr(err, "value") and hasattr(err, "tb"): 

133 exctype, value, tb = err.type, err.value, err.tb 

134 else: 

135 exctype, value, tb = err 

136 trace = traceback.format_exception(exctype, value, tb) 

137 if frames_to_skip_from_tail: 

138 if isinstance(frames_to_skip_from_tail, list): 

139 new_trace = [] 

140 for line in trace: 

141 if len([w for w in frames_to_skip_from_tail if w in line]) > 0: 

142 continue 

143 else: 

144 new_trace += line 

145 trace = new_trace 

146 if isinstance(frames_to_skip_from_tail, int): 

147 trace = trace[:-frames_to_skip_from_tail] 

148 return ''.join(trace) 

149 except Exception: 

150 tb = traceback.format_exc() 

151 return "*FAILED TO GET TRACEBACK*: " + tb