blob: f108266816e15f1028fc7f70e4a5f97798688734 [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#!/usr/bin/env python3
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +00002
3# portions copyright 2001, Autonomous Zones Industries, Inc., all rights...
4# err... reserved and offered to the public under the terms of the
5# Python 2.2 license.
6# Author: Zooko O'Whielacronx
7# http://zooko.com/
8# mailto:zooko@zooko.com
9#
10# Copyright 2000, Mojam Media, Inc., all rights reserved.
11# Author: Skip Montanaro
12#
13# Copyright 1999, Bioreason, Inc., all rights reserved.
14# Author: Andrew Dalke
15#
16# Copyright 1995-1997, Automatrix, Inc., all rights reserved.
17# Author: Skip Montanaro
18#
19# Copyright 1991-1995, Stichting Mathematisch Centrum, all rights reserved.
20#
21#
22# Permission to use, copy, modify, and distribute this Python software and
23# its associated documentation for any purpose without fee is hereby
24# granted, provided that the above copyright notice appears in all copies,
25# and that both that copyright notice and this permission notice appear in
26# supporting documentation, and that the name of neither Automatrix,
27# Bioreason or Mojam Media be used in advertising or publicity pertaining to
28# distribution of the software without specific, written prior permission.
29#
30"""program/module to trace Python program or function execution
31
32Sample use, command line:
33 trace.py -c -f counts --ignore-dir '$prefix' spam.py eggs
34 trace.py -t --ignore-dir '$prefix' spam.py eggs
Skip Montanaro5bfd9842004-04-10 16:29:58 +000035 trace.py --trackcalls spam.py eggs
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +000036
37Sample use, programmatically
Thomas Wouters477c8d52006-05-27 19:21:47 +000038 import sys
39
40 # create a Trace object, telling it what to ignore, and whether to
41 # do tracing or line-counting or both.
Vinay Sajip7ded1f02012-05-26 03:45:29 +010042 tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,],
43 trace=0, count=1)
Thomas Wouters477c8d52006-05-27 19:21:47 +000044 # run the new command using the given tracer
45 tracer.run('main()')
46 # make a report, placing output in /tmp
47 r = tracer.results()
48 r.write_results(show_missing=True, coverdir="/tmp")
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +000049"""
Alexander Belopolsky44454af2010-11-20 18:21:07 +000050__all__ = ['Trace', 'CoverageResults']
Jeremy Hylton38732e12003-04-21 22:04:46 +000051import linecache
Jeremy Hylton38732e12003-04-21 22:04:46 +000052import os
53import re
54import sys
55import token
56import tokenize
Alexander Belopolsky4d770172010-09-13 18:14:34 +000057import inspect
Skip Montanaro5bfd9842004-04-10 16:29:58 +000058import gc
Alexander Belopolskyff09ce22010-09-24 18:03:12 +000059import dis
Guido van Rossum99603b02007-07-20 00:22:32 +000060import pickle
Ezio Melotti23e043f2013-02-15 21:20:50 +020061from warnings import warn as _warn
Victor Stinnerae586492014-09-02 23:18:25 +020062from time import monotonic as _time
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +000063
Alexander Belopolsky25b57412010-11-06 01:31:16 +000064try:
65 import threading
Brett Cannoncd171c82013-07-04 17:43:24 -040066except ImportError:
Alexander Belopolsky25b57412010-11-06 01:31:16 +000067 _settrace = sys.settrace
68
69 def _unsettrace():
70 sys.settrace(None)
71else:
72 def _settrace(func):
73 threading.settrace(func)
74 sys.settrace(func)
75
76 def _unsettrace():
77 sys.settrace(None)
78 threading.settrace(None)
79
Alexander Belopolsky44454af2010-11-20 18:21:07 +000080def _usage(outfile):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +000081 outfile.write("""Usage: %s [OPTIONS] <file> [ARGS]
82
83Meta-options:
84--help Display this help then exit.
85--version Output version information then exit.
86
87Otherwise, exactly one of the following three options must be given:
88-t, --trace Print each line to sys.stdout before it is executed.
89-c, --count Count the number of times each line is executed
90 and write the counts to <module>.cover for each
91 module executed, in the module's directory.
92 See also `--coverdir', `--file', `--no-report' below.
Skip Montanaroa7b8ac62003-06-27 19:09:33 +000093-l, --listfuncs Keep track of which functions are executed at least
Fred Drake01c623b2003-06-27 19:22:11 +000094 once and write the results to sys.stdout after the
Skip Montanaroa7b8ac62003-06-27 19:09:33 +000095 program exits.
Skip Montanarocafc8112004-04-07 15:46:05 +000096-T, --trackcalls Keep track of caller/called pairs and write the
97 results to sys.stdout after the program exits.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +000098-r, --report Generate a report from a counts file; do not execute
99 any code. `--file' must specify the results file to
100 read, which must have been created in a previous run
101 with `--count --file=FILE'.
102
103Modifiers:
104-f, --file=<file> File to accumulate counts over several runs.
105-R, --no-report Do not generate the coverage report files.
106 Useful if you want to accumulate over several runs.
107-C, --coverdir=<dir> Directory where the report files. The coverage
108 report for <package>.<module> is written to file
109 <dir>/<package>/<module>.cover.
110-m, --missing Annotate executable lines that were not executed
111 with '>>>>>> '.
112-s, --summary Write a brief summary on stdout for each file.
113 (Can only be used with --count or --report.)
Christian Heimes380f7f22008-02-28 11:19:05 +0000114-g, --timing Prefix each line with the time since the program started.
115 Only used while tracing.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000116
117Filters, may be repeated multiple times:
Georg Brandlfceab5a2008-01-19 20:08:23 +0000118--ignore-module=<mod> Ignore the given module(s) and its submodules
119 (if it is a package). Accepts comma separated
120 list of module names
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000121--ignore-dir=<dir> Ignore files in the given directory (multiple
122 directories can be joined by os.pathsep).
123""" % sys.argv[0])
124
Jeremy Hylton38732e12003-04-21 22:04:46 +0000125PRAGMA_NOCOVER = "#pragma NO COVER"
126
127# Simple rx to find lines with no code.
128rx_blank = re.compile(r'^\s*(#.*)?$')
129
Alexander Belopolsky44454af2010-11-20 18:21:07 +0000130class _Ignore:
Alexander Belopolsky6672ea92010-11-08 18:32:40 +0000131 def __init__(self, modules=None, dirs=None):
132 self._mods = set() if not modules else set(modules)
133 self._dirs = [] if not dirs else [os.path.normpath(d)
134 for d in dirs]
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000135 self._ignore = { '<string>': 1 }
136
137 def names(self, filename, modulename):
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000138 if modulename in self._ignore:
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000139 return self._ignore[modulename]
140
141 # haven't seen this one before, so see if the module name is
Alexander Belopolsky6672ea92010-11-08 18:32:40 +0000142 # on the ignore list.
143 if modulename in self._mods: # Identical names, so ignore
144 self._ignore[modulename] = 1
145 return 1
146
147 # check if the module is a proper submodule of something on
148 # the ignore list
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000149 for mod in self._mods:
Alexander Belopolsky6672ea92010-11-08 18:32:40 +0000150 # Need to take some care since ignoring
151 # "cmp" mustn't mean ignoring "cmpcache" but ignoring
152 # "Spam" must also mean ignoring "Spam.Eggs".
153 if modulename.startswith(mod + '.'):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000154 self._ignore[modulename] = 1
155 return 1
156
Alexander Belopolsky6672ea92010-11-08 18:32:40 +0000157 # Now check that filename isn't in one of the directories
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000158 if filename is None:
159 # must be a built-in, so we must ignore
160 self._ignore[modulename] = 1
161 return 1
162
163 # Ignore a file when it contains one of the ignorable paths
164 for d in self._dirs:
165 # The '+ os.sep' is to ensure that d is a parent directory,
166 # as compared to cases like:
167 # d = "/usr/local"
168 # filename = "/usr/local.py"
169 # or
170 # d = "/usr/local.py"
171 # filename = "/usr/local.py"
172 if filename.startswith(d + os.sep):
173 self._ignore[modulename] = 1
174 return 1
175
176 # Tried the different ways, so we don't ignore this module
177 self._ignore[modulename] = 0
178 return 0
179
Alexander Belopolsky44454af2010-11-20 18:21:07 +0000180def _modname(path):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000181 """Return a plausible module name for the patch."""
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000182
Jeremy Hylton38732e12003-04-21 22:04:46 +0000183 base = os.path.basename(path)
184 filename, ext = os.path.splitext(base)
185 return filename
186
Alexander Belopolsky44454af2010-11-20 18:21:07 +0000187def _fullmodname(path):
Jeremy Hyltonc8c8b942003-04-22 15:35:51 +0000188 """Return a plausible module name for the path."""
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000189
190 # If the file 'path' is part of a package, then the filename isn't
191 # enough to uniquely identify it. Try to do the right thing by
192 # looking in sys.path for the longest matching prefix. We'll
193 # assume that the rest is the package name.
194
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000195 comparepath = os.path.normcase(path)
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000196 longest = ""
197 for dir in sys.path:
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000198 dir = os.path.normcase(dir)
199 if comparepath.startswith(dir) and comparepath[len(dir)] == os.sep:
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000200 if len(dir) > len(longest):
201 longest = dir
202
Guido van Rossumb427c002003-10-10 23:02:01 +0000203 if longest:
204 base = path[len(longest) + 1:]
205 else:
206 base = path
Georg Brandl120d6332010-08-01 14:38:17 +0000207 # the drive letter is never part of the module name
208 drive, base = os.path.splitdrive(base)
Guido van Rossumbbca8da2004-02-19 19:16:50 +0000209 base = base.replace(os.sep, ".")
210 if os.altsep:
211 base = base.replace(os.altsep, ".")
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000212 filename, ext = os.path.splitext(base)
Georg Brandl120d6332010-08-01 14:38:17 +0000213 return filename.lstrip(".")
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000214
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000215class CoverageResults:
216 def __init__(self, counts=None, calledfuncs=None, infile=None,
Skip Montanarocafc8112004-04-07 15:46:05 +0000217 callers=None, outfile=None):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000218 self.counts = counts
219 if self.counts is None:
220 self.counts = {}
221 self.counter = self.counts.copy() # map (filename, lineno) to count
222 self.calledfuncs = calledfuncs
223 if self.calledfuncs is None:
224 self.calledfuncs = {}
225 self.calledfuncs = self.calledfuncs.copy()
Skip Montanarocafc8112004-04-07 15:46:05 +0000226 self.callers = callers
227 if self.callers is None:
228 self.callers = {}
229 self.callers = self.callers.copy()
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000230 self.infile = infile
231 self.outfile = outfile
232 if self.infile:
Jeremy Hyltond7ce86d2003-07-07 16:08:47 +0000233 # Try to merge existing counts file.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000234 try:
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300235 with open(self.infile, 'rb') as f:
236 counts, calledfuncs, callers = pickle.load(f)
Skip Montanarocafc8112004-04-07 15:46:05 +0000237 self.update(self.__class__(counts, calledfuncs, callers))
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200238 except (OSError, EOFError, ValueError) as err:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000239 print(("Skipping counts file %r: %s"
240 % (self.infile, err)), file=sys.stderr)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000241
Georg Brandl33c28812009-04-01 23:07:29 +0000242 def is_ignored_filename(self, filename):
243 """Return True if the filename does not refer to a file
244 we want to have reported.
245 """
Brett Cannon9fe92d12012-04-10 21:05:53 -0400246 return filename.startswith('<') and filename.endswith('>')
Georg Brandl33c28812009-04-01 23:07:29 +0000247
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000248 def update(self, other):
249 """Merge in the data from another CoverageResults"""
250 counts = self.counts
251 calledfuncs = self.calledfuncs
Skip Montanarocafc8112004-04-07 15:46:05 +0000252 callers = self.callers
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000253 other_counts = other.counts
254 other_calledfuncs = other.calledfuncs
Skip Montanarocafc8112004-04-07 15:46:05 +0000255 other_callers = other.callers
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000256
Alexander Belopolsky0ae33612010-09-27 15:49:20 +0000257 for key in other_counts:
Jeremy Hylton38732e12003-04-21 22:04:46 +0000258 counts[key] = counts.get(key, 0) + other_counts[key]
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000259
Alexander Belopolsky0ae33612010-09-27 15:49:20 +0000260 for key in other_calledfuncs:
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000261 calledfuncs[key] = 1
262
Alexander Belopolsky0ae33612010-09-27 15:49:20 +0000263 for key in other_callers:
Skip Montanarocafc8112004-04-07 15:46:05 +0000264 callers[key] = 1
265
Jeremy Hylton38732e12003-04-21 22:04:46 +0000266 def write_results(self, show_missing=True, summary=False, coverdir=None):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000267 """
268 @param coverdir
269 """
Skip Montanarocafc8112004-04-07 15:46:05 +0000270 if self.calledfuncs:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000271 print()
272 print("functions called:")
Alexander Belopolsky0ae33612010-09-27 15:49:20 +0000273 calls = self.calledfuncs
Alexander Belopolsky533a1672010-07-20 19:55:18 +0000274 for filename, modulename, funcname in sorted(calls):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000275 print(("filename: %s, modulename: %s, funcname: %s"
276 % (filename, modulename, funcname)))
Skip Montanarocafc8112004-04-07 15:46:05 +0000277
278 if self.callers:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000279 print()
280 print("calling relationships:")
Skip Montanarocafc8112004-04-07 15:46:05 +0000281 lastfile = lastcfile = ""
Georg Brandl33c28812009-04-01 23:07:29 +0000282 for ((pfile, pmod, pfunc), (cfile, cmod, cfunc)) \
Alexander Belopolsky0ae33612010-09-27 15:49:20 +0000283 in sorted(self.callers):
Skip Montanarocafc8112004-04-07 15:46:05 +0000284 if pfile != lastfile:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000285 print()
286 print("***", pfile, "***")
Skip Montanarocafc8112004-04-07 15:46:05 +0000287 lastfile = pfile
288 lastcfile = ""
289 if cfile != pfile and lastcfile != cfile:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000290 print(" -->", cfile)
Skip Montanarocafc8112004-04-07 15:46:05 +0000291 lastcfile = cfile
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000292 print(" %s.%s -> %s.%s" % (pmod, pfunc, cmod, cfunc))
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000293
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000294 # turn the counts data ("(filename, lineno) = count") into something
295 # accessible on a per-file basis
296 per_file = {}
Alexander Belopolsky0ae33612010-09-27 15:49:20 +0000297 for filename, lineno in self.counts:
Jeremy Hylton38732e12003-04-21 22:04:46 +0000298 lines_hit = per_file[filename] = per_file.get(filename, {})
299 lines_hit[lineno] = self.counts[(filename, lineno)]
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000300
301 # accumulate summary info, if needed
302 sums = {}
303
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000304 for filename, count in per_file.items():
Georg Brandl33c28812009-04-01 23:07:29 +0000305 if self.is_ignored_filename(filename):
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +0000306 continue
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000307
Brett Cannonf299abd2015-04-13 14:21:02 -0400308 if filename.endswith(".pyc"):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000309 filename = filename[:-1]
310
Jeremy Hylton38732e12003-04-21 22:04:46 +0000311 if coverdir is None:
312 dir = os.path.dirname(os.path.abspath(filename))
Alexander Belopolsky44454af2010-11-20 18:21:07 +0000313 modulename = _modname(filename)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000314 else:
Jeremy Hylton38732e12003-04-21 22:04:46 +0000315 dir = coverdir
316 if not os.path.exists(dir):
317 os.makedirs(dir)
Alexander Belopolsky44454af2010-11-20 18:21:07 +0000318 modulename = _fullmodname(filename)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000319
320 # If desired, get a list of the line numbers which represent
321 # executable content (returned as a dict for better lookup speed)
322 if show_missing:
Alexander Belopolsky44454af2010-11-20 18:21:07 +0000323 lnotab = _find_executable_linenos(filename)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000324 else:
Jeremy Hylton38732e12003-04-21 22:04:46 +0000325 lnotab = {}
Alexander Belopolskyf026dae2014-06-29 17:44:05 -0400326 if lnotab:
327 source = linecache.getlines(filename)
328 coverpath = os.path.join(dir, modulename + ".cover")
329 with open(filename, 'rb') as fp:
330 encoding, _ = tokenize.detect_encoding(fp.readline)
331 n_hits, n_lines = self.write_results_file(coverpath, source,
332 lnotab, count, encoding)
333 if summary and n_lines:
334 percent = int(100 * n_hits / n_lines)
335 sums[modulename] = n_lines, percent, modulename, filename
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000336
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000337
338 if summary and sums:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000339 print("lines cov% module (path)")
Alexander Belopolsky0ae33612010-09-27 15:49:20 +0000340 for m in sorted(sums):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000341 n_lines, percent, modulename, filename = sums[m]
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000342 print("%5d %3d%% %s (%s)" % sums[m])
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000343
344 if self.outfile:
345 # try and store counts and module info into self.outfile
346 try:
Skip Montanarocafc8112004-04-07 15:46:05 +0000347 pickle.dump((self.counts, self.calledfuncs, self.callers),
Jeremy Hyltond0e27052003-10-14 20:12:06 +0000348 open(self.outfile, 'wb'), 1)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200349 except OSError as err:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000350 print("Can't save counts files because %s" % err, file=sys.stderr)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000351
Victor Stinner64bc3b22010-11-07 15:47:36 +0000352 def write_results_file(self, path, lines, lnotab, lines_hit, encoding=None):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000353 """Return a coverage results file in path."""
354
355 try:
Victor Stinner64bc3b22010-11-07 15:47:36 +0000356 outfile = open(path, "w", encoding=encoding)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200357 except OSError as err:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000358 print(("trace: Could not open %r for writing: %s"
359 "- skipping" % (path, err)), file=sys.stderr)
Guido van Rossumbbca8da2004-02-19 19:16:50 +0000360 return 0, 0
Jeremy Hylton38732e12003-04-21 22:04:46 +0000361
362 n_lines = 0
363 n_hits = 0
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300364 with outfile:
365 for lineno, line in enumerate(lines, 1):
366 # do the blank/comment match to try to mark more lines
367 # (help the reader find stuff that hasn't been covered)
368 if lineno in lines_hit:
369 outfile.write("%5d: " % lines_hit[lineno])
370 n_hits += 1
Jeremy Hylton546e34b2003-06-26 14:56:17 +0000371 n_lines += 1
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300372 elif rx_blank.match(line):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000373 outfile.write(" ")
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300374 else:
375 # lines preceded by no marks weren't hit
376 # Highlight them if so indicated, unless the line contains
377 # #pragma: NO COVER
378 if lineno in lnotab and not PRAGMA_NOCOVER in line:
379 outfile.write(">>>>>> ")
380 n_lines += 1
381 else:
382 outfile.write(" ")
383 outfile.write(line.expandtabs(8))
Jeremy Hylton38732e12003-04-21 22:04:46 +0000384
385 return n_hits, n_lines
386
Alexander Belopolsky44454af2010-11-20 18:21:07 +0000387def _find_lines_from_code(code, strs):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000388 """Return dict where keys are lines in the line number table."""
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000389 linenos = {}
390
Alexander Belopolskyff09ce22010-09-24 18:03:12 +0000391 for _, lineno in dis.findlinestarts(code):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000392 if lineno not in strs:
393 linenos[lineno] = 1
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000394
395 return linenos
396
Alexander Belopolsky44454af2010-11-20 18:21:07 +0000397def _find_lines(code, strs):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000398 """Return lineno dict for all code objects reachable from code."""
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000399 # get all of the lineno information from the code of this scope level
Alexander Belopolsky44454af2010-11-20 18:21:07 +0000400 linenos = _find_lines_from_code(code, strs)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000401
402 # and check the constants for references to other code objects
403 for c in code.co_consts:
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000404 if inspect.iscode(c):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000405 # find another code object, so recurse into it
Alexander Belopolsky44454af2010-11-20 18:21:07 +0000406 linenos.update(_find_lines(c, strs))
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000407 return linenos
408
Alexander Belopolsky44454af2010-11-20 18:21:07 +0000409def _find_strings(filename, encoding=None):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000410 """Return a dict of possible docstring positions.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000411
Jeremy Hylton38732e12003-04-21 22:04:46 +0000412 The dict maps line numbers to strings. There is an entry for
413 line that contains only a string or a part of a triple-quoted
414 string.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000415 """
Jeremy Hylton38732e12003-04-21 22:04:46 +0000416 d = {}
417 # If the first token is a string, then it's the module docstring.
418 # Add this special case so that the test in the loop passes.
419 prev_ttype = token.INDENT
Benjamin Petersonb2fda232010-10-30 23:51:34 +0000420 with open(filename, encoding=encoding) as f:
421 tok = tokenize.generate_tokens(f.readline)
422 for ttype, tstr, start, end, line in tok:
423 if ttype == token.STRING:
424 if prev_ttype == token.INDENT:
425 sline, scol = start
426 eline, ecol = end
427 for i in range(sline, eline + 1):
428 d[i] = 1
429 prev_ttype = ttype
Jeremy Hylton38732e12003-04-21 22:04:46 +0000430 return d
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000431
Alexander Belopolsky44454af2010-11-20 18:21:07 +0000432def _find_executable_linenos(filename):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000433 """Return dict where keys are line numbers in the line number table."""
Jeremy Hylton38732e12003-04-21 22:04:46 +0000434 try:
Victor Stinner58c07522010-11-09 01:08:59 +0000435 with tokenize.open(filename) as f:
Benjamin Petersonb2fda232010-10-30 23:51:34 +0000436 prog = f.read()
Victor Stinner58c07522010-11-09 01:08:59 +0000437 encoding = f.encoding
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200438 except OSError as err:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000439 print(("Not printing coverage data for %r: %s"
440 % (filename, err)), file=sys.stderr)
Jeremy Hylton38732e12003-04-21 22:04:46 +0000441 return {}
442 code = compile(prog, filename, "exec")
Alexander Belopolsky44454af2010-11-20 18:21:07 +0000443 strs = _find_strings(filename, encoding)
444 return _find_lines(code, strs)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000445
446class Trace:
Skip Montanarocafc8112004-04-07 15:46:05 +0000447 def __init__(self, count=1, trace=1, countfuncs=0, countcallers=0,
Christian Heimes380f7f22008-02-28 11:19:05 +0000448 ignoremods=(), ignoredirs=(), infile=None, outfile=None,
449 timing=False):
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000450 """
451 @param count true iff it should count number of times each
Tim Petersf2715e02003-02-19 02:35:07 +0000452 line is executed
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000453 @param trace true iff it should print out each line that is
Tim Petersf2715e02003-02-19 02:35:07 +0000454 being counted
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000455 @param countfuncs true iff it should just output a list of
456 (filename, modulename, funcname,) for functions
457 that were called at least once; This overrides
Tim Petersf2715e02003-02-19 02:35:07 +0000458 `count' and `trace'
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000459 @param ignoremods a list of the names of modules to ignore
460 @param ignoredirs a list of the names of directories to ignore
Tim Petersf2715e02003-02-19 02:35:07 +0000461 all of the (recursive) contents of
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000462 @param infile file from which to read stored counts to be
Tim Petersf2715e02003-02-19 02:35:07 +0000463 added into the results
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000464 @param outfile file in which to write the results
Christian Heimes380f7f22008-02-28 11:19:05 +0000465 @param timing true iff timing information be displayed
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000466 """
467 self.infile = infile
468 self.outfile = outfile
Alexander Belopolsky44454af2010-11-20 18:21:07 +0000469 self.ignore = _Ignore(ignoremods, ignoredirs)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000470 self.counts = {} # keys are (filename, linenumber)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000471 self.pathtobasename = {} # for memoizing os.path.basename
472 self.donothing = 0
473 self.trace = trace
474 self._calledfuncs = {}
Skip Montanarocafc8112004-04-07 15:46:05 +0000475 self._callers = {}
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000476 self._caller_cache = {}
Christian Heimes380f7f22008-02-28 11:19:05 +0000477 self.start_time = None
478 if timing:
Victor Stinner949d8c92012-05-30 13:30:32 +0200479 self.start_time = _time()
Skip Montanarocafc8112004-04-07 15:46:05 +0000480 if countcallers:
481 self.globaltrace = self.globaltrace_trackcallers
482 elif countfuncs:
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000483 self.globaltrace = self.globaltrace_countfuncs
484 elif trace and count:
485 self.globaltrace = self.globaltrace_lt
486 self.localtrace = self.localtrace_trace_and_count
487 elif trace:
488 self.globaltrace = self.globaltrace_lt
489 self.localtrace = self.localtrace_trace
490 elif count:
491 self.globaltrace = self.globaltrace_lt
492 self.localtrace = self.localtrace_count
493 else:
494 # Ahem -- do nothing? Okay.
495 self.donothing = 1
496
497 def run(self, cmd):
498 import __main__
499 dict = __main__.__dict__
Alexander Belopolsky0ae33612010-09-27 15:49:20 +0000500 self.runctx(cmd, dict, dict)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000501
502 def runctx(self, cmd, globals=None, locals=None):
503 if globals is None: globals = {}
504 if locals is None: locals = {}
505 if not self.donothing:
Alexander Belopolsky25b57412010-11-06 01:31:16 +0000506 _settrace(self.globaltrace)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000507 try:
Georg Brandl7cae87c2006-09-06 06:51:57 +0000508 exec(cmd, globals, locals)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000509 finally:
510 if not self.donothing:
Alexander Belopolsky25b57412010-11-06 01:31:16 +0000511 _unsettrace()
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000512
513 def runfunc(self, func, *args, **kw):
514 result = None
515 if not self.donothing:
516 sys.settrace(self.globaltrace)
517 try:
Guido van Rossum68468eb2003-02-27 20:14:51 +0000518 result = func(*args, **kw)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000519 finally:
520 if not self.donothing:
521 sys.settrace(None)
522 return result
523
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000524 def file_module_function_of(self, frame):
525 code = frame.f_code
526 filename = code.co_filename
527 if filename:
Alexander Belopolsky44454af2010-11-20 18:21:07 +0000528 modulename = _modname(filename)
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000529 else:
530 modulename = None
531
532 funcname = code.co_name
533 clsname = None
534 if code in self._caller_cache:
535 if self._caller_cache[code] is not None:
536 clsname = self._caller_cache[code]
537 else:
538 self._caller_cache[code] = None
539 ## use of gc.get_referrers() was suggested by Michael Hudson
540 # all functions which refer to this code object
541 funcs = [f for f in gc.get_referrers(code)
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000542 if inspect.isfunction(f)]
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000543 # require len(func) == 1 to avoid ambiguity caused by calls to
544 # new.function(): "In the face of ambiguity, refuse the
545 # temptation to guess."
546 if len(funcs) == 1:
547 dicts = [d for d in gc.get_referrers(funcs[0])
548 if isinstance(d, dict)]
549 if len(dicts) == 1:
550 classes = [c for c in gc.get_referrers(dicts[0])
551 if hasattr(c, "__bases__")]
552 if len(classes) == 1:
553 # ditto for new.classobj()
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000554 clsname = classes[0].__name__
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000555 # cache the result - assumption is that new.* is
556 # not called later to disturb this relationship
557 # _caller_cache could be flushed if functions in
558 # the new module get called.
559 self._caller_cache[code] = clsname
560 if clsname is not None:
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000561 funcname = "%s.%s" % (clsname, funcname)
562
563 return filename, modulename, funcname
564
Skip Montanarocafc8112004-04-07 15:46:05 +0000565 def globaltrace_trackcallers(self, frame, why, arg):
566 """Handler for call events.
567
568 Adds information about who called who to the self._callers dict.
569 """
570 if why == 'call':
571 # XXX Should do a better job of identifying methods
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000572 this_func = self.file_module_function_of(frame)
573 parent_func = self.file_module_function_of(frame.f_back)
Skip Montanarocafc8112004-04-07 15:46:05 +0000574 self._callers[(parent_func, this_func)] = 1
575
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000576 def globaltrace_countfuncs(self, frame, why, arg):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000577 """Handler for call events.
Tim Peters0eadaac2003-04-24 16:02:54 +0000578
Jeremy Hylton38732e12003-04-21 22:04:46 +0000579 Adds (filename, modulename, funcname) to the self._calledfuncs dict.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000580 """
581 if why == 'call':
Skip Montanaro5bfd9842004-04-10 16:29:58 +0000582 this_func = self.file_module_function_of(frame)
583 self._calledfuncs[this_func] = 1
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000584
585 def globaltrace_lt(self, frame, why, arg):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000586 """Handler for call events.
587
588 If the code block being entered is to be ignored, returns `None',
589 else returns self.localtrace.
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000590 """
591 if why == 'call':
Jeremy Hylton38732e12003-04-21 22:04:46 +0000592 code = frame.f_code
Thomas Wouterscf297e42007-02-23 15:07:44 +0000593 filename = frame.f_globals.get('__file__', None)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000594 if filename:
Alexander Belopolsky44454af2010-11-20 18:21:07 +0000595 # XXX _modname() doesn't work right for packages, so
Jeremy Hyltondfbfe732003-04-21 22:49:17 +0000596 # the ignore support won't work right for packages
Alexander Belopolsky44454af2010-11-20 18:21:07 +0000597 modulename = _modname(filename)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000598 if modulename is not None:
599 ignore_it = self.ignore.names(filename, modulename)
600 if not ignore_it:
601 if self.trace:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000602 print((" --- modulename: %s, funcname: %s"
603 % (modulename, code.co_name)))
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000604 return self.localtrace
605 else:
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000606 return None
607
608 def localtrace_trace_and_count(self, frame, why, arg):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000609 if why == "line":
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000610 # record the file name and line number of every trace
Jeremy Hylton38732e12003-04-21 22:04:46 +0000611 filename = frame.f_code.co_filename
612 lineno = frame.f_lineno
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000613 key = filename, lineno
614 self.counts[key] = self.counts.get(key, 0) + 1
Tim Petersf2715e02003-02-19 02:35:07 +0000615
Christian Heimes380f7f22008-02-28 11:19:05 +0000616 if self.start_time:
Victor Stinner949d8c92012-05-30 13:30:32 +0200617 print('%.2f' % (_time() - self.start_time), end=' ')
Jeremy Hylton38732e12003-04-21 22:04:46 +0000618 bname = os.path.basename(filename)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000619 print("%s(%d): %s" % (bname, lineno,
Georg Brandldc50c692010-08-02 12:40:22 +0000620 linecache.getline(filename, lineno)), end='')
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000621 return self.localtrace
622
623 def localtrace_trace(self, frame, why, arg):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000624 if why == "line":
625 # record the file name and line number of every trace
626 filename = frame.f_code.co_filename
627 lineno = frame.f_lineno
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000628
Christian Heimes380f7f22008-02-28 11:19:05 +0000629 if self.start_time:
Victor Stinner949d8c92012-05-30 13:30:32 +0200630 print('%.2f' % (_time() - self.start_time), end=' ')
Jeremy Hylton38732e12003-04-21 22:04:46 +0000631 bname = os.path.basename(filename)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000632 print("%s(%d): %s" % (bname, lineno,
Georg Brandldc50c692010-08-02 12:40:22 +0000633 linecache.getline(filename, lineno)), end='')
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000634 return self.localtrace
635
636 def localtrace_count(self, frame, why, arg):
Jeremy Hylton38732e12003-04-21 22:04:46 +0000637 if why == "line":
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000638 filename = frame.f_code.co_filename
639 lineno = frame.f_lineno
640 key = filename, lineno
641 self.counts[key] = self.counts.get(key, 0) + 1
642 return self.localtrace
643
644 def results(self):
645 return CoverageResults(self.counts, infile=self.infile,
646 outfile=self.outfile,
Skip Montanarocafc8112004-04-07 15:46:05 +0000647 calledfuncs=self._calledfuncs,
648 callers=self._callers)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000649
650def _err_exit(msg):
651 sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))
652 sys.exit(1)
653
654def main(argv=None):
655 import getopt
656
657 if argv is None:
658 argv = sys.argv
659 try:
Christian Heimes380f7f22008-02-28 11:19:05 +0000660 opts, prog_argv = getopt.getopt(argv[1:], "tcrRf:d:msC:lTg",
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000661 ["help", "version", "trace", "count",
662 "report", "no-report", "summary",
663 "file=", "missing",
664 "ignore-module=", "ignore-dir=",
Skip Montanarocafc8112004-04-07 15:46:05 +0000665 "coverdir=", "listfuncs",
Christian Heimes380f7f22008-02-28 11:19:05 +0000666 "trackcalls", "timing"])
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000667
Guido van Rossumb940e112007-01-10 16:19:56 +0000668 except getopt.error as msg:
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000669 sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))
670 sys.stderr.write("Try `%s --help' for more information\n"
671 % sys.argv[0])
672 sys.exit(1)
673
674 trace = 0
675 count = 0
676 report = 0
677 no_report = 0
678 counts_file = None
679 missing = 0
680 ignore_modules = []
681 ignore_dirs = []
682 coverdir = None
683 summary = 0
684 listfuncs = False
Skip Montanarocafc8112004-04-07 15:46:05 +0000685 countcallers = False
Christian Heimes380f7f22008-02-28 11:19:05 +0000686 timing = False
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000687
688 for opt, val in opts:
689 if opt == "--help":
Éric Araujoe7d36fe2011-04-17 16:48:52 +0200690 _usage(sys.stdout)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000691 sys.exit(0)
692
693 if opt == "--version":
694 sys.stdout.write("trace 2.0\n")
695 sys.exit(0)
696
Skip Montanarocafc8112004-04-07 15:46:05 +0000697 if opt == "-T" or opt == "--trackcalls":
698 countcallers = True
699 continue
700
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000701 if opt == "-l" or opt == "--listfuncs":
702 listfuncs = True
703 continue
704
Christian Heimes380f7f22008-02-28 11:19:05 +0000705 if opt == "-g" or opt == "--timing":
706 timing = True
707 continue
708
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000709 if opt == "-t" or opt == "--trace":
710 trace = 1
711 continue
712
713 if opt == "-c" or opt == "--count":
714 count = 1
715 continue
716
717 if opt == "-r" or opt == "--report":
718 report = 1
719 continue
720
721 if opt == "-R" or opt == "--no-report":
722 no_report = 1
723 continue
724
725 if opt == "-f" or opt == "--file":
726 counts_file = val
727 continue
728
729 if opt == "-m" or opt == "--missing":
730 missing = 1
731 continue
732
733 if opt == "-C" or opt == "--coverdir":
734 coverdir = val
735 continue
736
737 if opt == "-s" or opt == "--summary":
738 summary = 1
739 continue
740
741 if opt == "--ignore-module":
Georg Brandlfceab5a2008-01-19 20:08:23 +0000742 for mod in val.split(","):
743 ignore_modules.append(mod.strip())
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000744 continue
745
746 if opt == "--ignore-dir":
747 for s in val.split(os.pathsep):
748 s = os.path.expandvars(s)
749 # should I also call expanduser? (after all, could use $HOME)
750
751 s = s.replace("$prefix",
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100752 os.path.join(sys.base_prefix, "lib",
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000753 "python" + sys.version[:3]))
754 s = s.replace("$exec_prefix",
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100755 os.path.join(sys.base_exec_prefix, "lib",
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000756 "python" + sys.version[:3]))
757 s = os.path.normpath(s)
758 ignore_dirs.append(s)
759 continue
760
761 assert 0, "Should never get here"
762
763 if listfuncs and (count or trace):
764 _err_exit("cannot specify both --listfuncs and (--trace or --count)")
765
Skip Montanarocafc8112004-04-07 15:46:05 +0000766 if not (count or trace or report or listfuncs or countcallers):
767 _err_exit("must specify one of --trace, --count, --report, "
768 "--listfuncs, or --trackcalls")
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000769
770 if report and no_report:
771 _err_exit("cannot specify both --report and --no-report")
772
773 if report and not counts_file:
774 _err_exit("--report requires a --file")
775
776 if no_report and len(prog_argv) == 0:
777 _err_exit("missing name of file to run")
778
779 # everything is ready
780 if report:
781 results = CoverageResults(infile=counts_file, outfile=counts_file)
782 results.write_results(missing, summary=summary, coverdir=coverdir)
783 else:
784 sys.argv = prog_argv
785 progname = prog_argv[0]
786 sys.path[0] = os.path.split(progname)[0]
787
788 t = Trace(count, trace, countfuncs=listfuncs,
Skip Montanarocafc8112004-04-07 15:46:05 +0000789 countcallers=countcallers, ignoremods=ignore_modules,
790 ignoredirs=ignore_dirs, infile=counts_file,
Christian Heimes380f7f22008-02-28 11:19:05 +0000791 outfile=counts_file, timing=timing)
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000792 try:
Alexander Belopolsky3f8ecab2010-07-21 17:43:42 +0000793 with open(progname) as fp:
794 code = compile(fp.read(), progname, 'exec')
Georg Brandl8f9f4662010-08-01 08:35:29 +0000795 # try to emulate __main__ namespace as much as possible
796 globs = {
797 '__file__': progname,
798 '__name__': '__main__',
799 '__package__': None,
800 '__cached__': None,
801 }
802 t.runctx(code, globs, globs)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200803 except OSError as err:
Jeremy Hylton38732e12003-04-21 22:04:46 +0000804 _err_exit("Cannot run file %r because: %s" % (sys.argv[0], err))
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000805 except SystemExit:
806 pass
807
808 results = t.results()
809
810 if not no_report:
811 results.write_results(missing, summary=summary, coverdir=coverdir)
812
Alexander Belopolsky44454af2010-11-20 18:21:07 +0000813# Deprecated API
814def usage(outfile):
815 _warn("The trace.usage() function is deprecated",
816 DeprecationWarning, 2)
817 _usage(outfile)
818
819class Ignore(_Ignore):
820 def __init__(self, modules=None, dirs=None):
821 _warn("The class trace.Ignore is deprecated",
822 DeprecationWarning, 2)
823 _Ignore.__init__(self, modules, dirs)
824
825def modname(path):
826 _warn("The trace.modname() function is deprecated",
827 DeprecationWarning, 2)
828 return _modname(path)
829
830def fullmodname(path):
831 _warn("The trace.fullmodname() function is deprecated",
832 DeprecationWarning, 2)
833 return _fullmodname(path)
834
835def find_lines_from_code(code, strs):
836 _warn("The trace.find_lines_from_code() function is deprecated",
837 DeprecationWarning, 2)
838 return _find_lines_from_code(code, strs)
839
840def find_lines(code, strs):
841 _warn("The trace.find_lines() function is deprecated",
842 DeprecationWarning, 2)
843 return _find_lines(code, strs)
844
845def find_strings(filename, encoding=None):
846 _warn("The trace.find_strings() function is deprecated",
847 DeprecationWarning, 2)
848 return _find_strings(filename, encoding=None)
849
850def find_executable_linenos(filename):
851 _warn("The trace.find_executable_linenos() function is deprecated",
852 DeprecationWarning, 2)
853 return _find_executable_linenos(filename)
854
Jeremy Hylton4edaa0d2003-02-18 15:06:17 +0000855if __name__=='__main__':
856 main()