This repository was archived by the owner on Jan 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathpython_test_util.py
More file actions
185 lines (136 loc) · 5.02 KB
/
Copy pathpython_test_util.py
File metadata and controls
185 lines (136 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"""Set of helper methods for Python debuglet unit and component tests."""
import inspect
import re
def GetModuleInfo(obj):
"""Gets the source file path and breakpoint tags for a module.
Breakpoint tag is a named label of a source line. The tag is marked
with "# BPTAG: XXX" comment.
Args:
obj: any object inside the queried module.
Returns:
(path, tags) tuple where tags is a dictionary mapping tag name to
line numbers where this tag appears.
"""
return (inspect.getsourcefile(obj), GetSourceFileTags(obj))
def GetSourceFileTags(source):
"""Gets breakpoint tags for the specified source file.
Breakpoint tag is a named label of a source line. The tag is marked
with "# BPTAG: XXX" comment.
Args:
source: either path to the .py file to analyze or any code related
object (e.g. module, function, code object).
Returns:
Dictionary mapping tag name to line numbers where this tag appears.
"""
if isinstance(source, str):
lines = open(source, 'r').read().splitlines()
start_line = 1 # line number is 1 based
else:
lines, start_line = inspect.getsourcelines(source)
if not start_line: # "getsourcelines" returns start_line of 0 for modules.
start_line = 1
tags = {}
regex = re.compile(r'# BPTAG: ([0-9a-zA-Z_]+)\s*$')
for n, line in enumerate(lines):
m = regex.search(line)
if m:
tag = m.group(1)
if tag in tags:
tags[tag].append(n + start_line)
else:
tags[tag] = [n + start_line]
return tags
def ResolveTag(obj, tag):
"""Resolves the breakpoint tag into source file path and a line number.
Breakpoint tag is a named label of a source line. The tag is marked
with "# BPTAG: XXX" comment.
Raises
Args:
obj: any object inside the queried module.
tag: tag name to resolve.
Raises:
Exception: if no line in the source file define the specified tag or if
more than one line define the tag.
Returns:
(path, line) tuple, where line is the line number where the tag appears.
"""
path, tags = GetModuleInfo(obj)
if tag not in tags:
raise Exception('tag %s not found' % tag)
lines = tags[tag]
if len(lines) != 1:
raise Exception('tag %s is ambiguous (lines: %s)' % (tag, lines))
return path, lines[0]
def DateTimeToTimestamp(t):
"""Converts the specified time to Timestamp format.
Args:
t: datetime instance
Returns:
Time in Timestamp format
"""
return t.strftime('%Y-%m-%dT%H:%M:%S.%f') + 'Z'
def DateTimeToTimestampNew(t):
"""Converts the specified time to Timestamp format in seconds granularity.
Args:
t: datetime instance
Returns:
Time in Timestamp format in seconds granularity
"""
return t.strftime('%Y-%m-%dT%H:%M:%S') + 'Z'
def PackFrameVariable(breakpoint, name, frame=0, collection='locals'):
"""Finds local variable or argument by name.
Indirections created through varTableIndex are recursively collapsed. Fails
the test case if the named variable is not found.
Args:
breakpoint: queried breakpoint.
name: name of the local variable or argument.
frame: stack frame index to examine.
collection: 'locals' to get local variable or 'arguments' for an argument.
Returns:
Single dictionary of variable data.
Raises:
AssertionError: if the named variable not found.
"""
for variable in breakpoint['stackFrames'][frame][collection]:
if variable['name'] == name:
return _Pack(variable, breakpoint)
raise AssertionError('Variable %s not found in frame %d collection %s' %
(name, frame, collection))
def PackWatchedExpression(breakpoint, expression):
"""Finds watched expression by index.
Indirections created through varTableIndex are recursively collapsed. Fails
the test case if the named variable is not found.
Args:
breakpoint: queried breakpoint.
expression: index of the watched expression.
Returns:
Single dictionary of variable data.
"""
return _Pack(breakpoint['evaluatedExpressions'][expression], breakpoint)
def _Pack(variable, breakpoint):
"""Recursively collapses indirections created through varTableIndex.
Circular references by objects are not supported. If variable subtree
has circular references, this function will hang.
Variable members are sorted by name. This helps asserting the content of
variable since Python has no guarantees over the order of keys of a
dictionary.
Args:
variable: variable object to pack. Not modified.
breakpoint: queried breakpoint.
Returns:
A new dictionary with packed variable object.
"""
packed = dict(variable)
while 'varTableIndex' in packed:
ref = breakpoint['variableTable'][packed['varTableIndex']]
assert 'name' not in ref
assert 'value' not in packed
assert 'members' not in packed
assert 'status' not in ref and 'status' not in packed
del packed['varTableIndex']
packed.update(ref)
if 'members' in packed:
packed['members'] = sorted(
[_Pack(m, breakpoint) for m in packed['members']],
key=lambda m: m.get('name', ''))
return packed