Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0ae3361

Browse files
committed
Issue 9941: Minor code cleanup before implementing the context manager feature:
- Eliminated code repetition between run and runctx; - Removed redundant calls to dict.key; - Removed unused "blabbed" attribute; - Simplified the loop in write_results_file().
1 parent 76ca3b4 commit 0ae3361

1 file changed

Lines changed: 11 additions & 21 deletions

File tree

Lib/trace.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,13 @@ def update(self, other):
243243
other_calledfuncs = other.calledfuncs
244244
other_callers = other.callers
245245

246-
for key in other_counts.keys():
246+
for key in other_counts:
247247
counts[key] = counts.get(key, 0) + other_counts[key]
248248

249-
for key in other_calledfuncs.keys():
249+
for key in other_calledfuncs:
250250
calledfuncs[key] = 1
251251

252-
for key in other_callers.keys():
252+
for key in other_callers:
253253
callers[key] = 1
254254

255255
def write_results(self, show_missing=True, summary=False, coverdir=None):
@@ -259,7 +259,7 @@ def write_results(self, show_missing=True, summary=False, coverdir=None):
259259
if self.calledfuncs:
260260
print()
261261
print("functions called:")
262-
calls = self.calledfuncs.keys()
262+
calls = self.calledfuncs
263263
for filename, modulename, funcname in sorted(calls):
264264
print(("filename: %s, modulename: %s, funcname: %s"
265265
% (filename, modulename, funcname)))
@@ -269,7 +269,7 @@ def write_results(self, show_missing=True, summary=False, coverdir=None):
269269
print("calling relationships:")
270270
lastfile = lastcfile = ""
271271
for ((pfile, pmod, pfunc), (cfile, cmod, cfunc)) \
272-
in sorted(self.callers.keys()):
272+
in sorted(self.callers):
273273
if pfile != lastfile:
274274
print()
275275
print("***", pfile, "***")
@@ -283,7 +283,7 @@ def write_results(self, show_missing=True, summary=False, coverdir=None):
283283
# turn the counts data ("(filename, lineno) = count") into something
284284
# accessible on a per-file basis
285285
per_file = {}
286-
for filename, lineno in self.counts.keys():
286+
for filename, lineno in self.counts:
287287
lines_hit = per_file[filename] = per_file.get(filename, {})
288288
lines_hit[lineno] = self.counts[(filename, lineno)]
289289

@@ -324,7 +324,7 @@ def write_results(self, show_missing=True, summary=False, coverdir=None):
324324

325325
if summary and sums:
326326
print("lines cov% module (path)")
327-
for m in sorted(sums.keys()):
327+
for m in sorted(sums):
328328
n_lines, percent, modulename, filename = sums[m]
329329
print("%5d %3d%% %s (%s)" % sums[m])
330330

@@ -348,8 +348,7 @@ def write_results_file(self, path, lines, lnotab, lines_hit):
348348

349349
n_lines = 0
350350
n_hits = 0
351-
for i, line in enumerate(lines):
352-
lineno = i + 1
351+
for lineno, line in enumerate(lines, 1):
353352
# do the blank/comment match to try to mark more lines
354353
# (help the reader find stuff that hasn't been covered)
355354
if lineno in lines_hit:
@@ -362,12 +361,12 @@ def write_results_file(self, path, lines, lnotab, lines_hit):
362361
# lines preceded by no marks weren't hit
363362
# Highlight them if so indicated, unless the line contains
364363
# #pragma: NO COVER
365-
if lineno in lnotab and not PRAGMA_NOCOVER in lines[i]:
364+
if lineno in lnotab and not PRAGMA_NOCOVER in line:
366365
outfile.write(">>>>>> ")
367366
n_lines += 1
368367
else:
369368
outfile.write(" ")
370-
outfile.write(lines[i].expandtabs(8))
369+
outfile.write(line.expandtabs(8))
371370
outfile.close()
372371

373372
return n_hits, n_lines
@@ -456,7 +455,6 @@ def __init__(self, count=1, trace=1, countfuncs=0, countcallers=0,
456455
self.outfile = outfile
457456
self.ignore = Ignore(ignoremods, ignoredirs)
458457
self.counts = {} # keys are (filename, linenumber)
459-
self.blabbed = {} # for debugging
460458
self.pathtobasename = {} # for memoizing os.path.basename
461459
self.donothing = 0
462460
self.trace = trace
@@ -486,15 +484,7 @@ def __init__(self, count=1, trace=1, countfuncs=0, countcallers=0,
486484
def run(self, cmd):
487485
import __main__
488486
dict = __main__.__dict__
489-
if not self.donothing:
490-
threading.settrace(self.globaltrace)
491-
sys.settrace(self.globaltrace)
492-
try:
493-
exec(cmd, dict, dict)
494-
finally:
495-
if not self.donothing:
496-
sys.settrace(None)
497-
threading.settrace(None)
487+
self.runctx(cmd, dict, dict)
498488

499489
def runctx(self, cmd, globals=None, locals=None):
500490
if globals is None: globals = {}

0 commit comments

Comments
 (0)