⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trace.py

📁 这个是内存数据库中的一个管理工具
💻 PY
📖 第 1 页 / 共 3 页
字号:
        try:            exec cmd in globals, locals        finally:            if not self.donothing:                sys.settrace(None)    def runfunc(self, func, *args, **kw):        result = None        if not self.donothing:            sys.settrace(self.globaltrace)        try:            result = apply(func, args, kw)        finally:            if not self.donothing:                sys.settrace(None)        return result    def globaltrace_countfuncs(self, frame, why, arg):        """        Handles `call' events (why == 'call') and adds the (filename, modulename, funcname,) to the self._calledfuncs dict.        """        if os.getpid() != self.pid:            return        if why == 'call':            (filename, lineno, funcname, context, lineindex,) = inspect.getframeinfo(frame, 0)            if filename:                modulename = inspect.getmodulename(filename)            else:                modulename = None            self._calledfuncs[(filename, modulename, funcname,)] = 1    def globaltrace_lt(self, frame, why, arg):        """        Handles `call' events (why == 'call') and if the code block being entered is to be ignored then it returns `None', else it returns `self.localtrace'.        """        if os.getpid() != self.pid:            return        if why == 'call':            (filename, lineno, funcname, context, lineindex,) = inspect.getframeinfo(frame, 0)            # if DEBUG_MODE and not filename:            #     print "%s.globaltrace(frame: %s, why: %s, arg: %s): filename: %s, lineno: %s, funcname: %s, context: %s, lineindex: %s\n" % (self, frame, why, arg, filename, lineno, funcname, context, lineindex,)            if filename:                modulename = inspect.getmodulename(filename)                if modulename is not None:                    ignore_it = self.ignore.names(filename, modulename)                    # if DEBUG_MODE and not self.blabbed.has_key((filename, modulename,)):                    #     self.blabbed[(filename, modulename,)] = None                    #     print "%s.globaltrace(frame: %s, why: %s, arg: %s, filename: %s, modulename: %s, ignore_it: %s\n" % (self, frame, why, arg, filename, modulename, ignore_it,)                    if not ignore_it:                        if self.trace:                            print " --- modulename: %s, funcname: %s" % (modulename, funcname,)                        # if DEBUG_MODE:                        #     print "%s.globaltrace(frame: %s, why: %s, arg: %s, filename: %s, modulename: %s, ignore_it: %s -- about to localtrace\n" % (self, frame, why, arg, filename, modulename, ignore_it,)                        return self.localtrace            else:                # XXX why no filename?                return None    def localtrace_trace_and_count(self, frame, why, arg):        if os.getpid() != self.pid:            return        if why == 'line':            # record the file name and line number of every trace            # XXX I wish inspect offered me an optimized `getfilename(frame)' to use in place of the presumably heavier `getframeinfo()'.  --Zooko 2001-10-14            (filename, lineno, funcname, context, lineindex,) = inspect.getframeinfo(frame, 1)            key = (filename, lineno,)            self.counts[key] = self.counts.get(key, 0) + 1            # XXX not convinced that this memoizing is a performance win -- I don't know enough about Python guts to tell.  --Zooko 2001-10-14            bname = self.pathtobasename.get(filename)            if bname is None:                # Using setdefault faster than two separate lines?  --Zooko 2001-10-14                bname = self.pathtobasename.setdefault(filename, os.path.basename(filename))            try:                print "%s(%d): %s" % (bname, lineno, context[lineindex],),            except IndexError:                # Uh.. sometimes getframeinfo gives me a context of length 1 and a lineindex of -2.  Oh well.                pass        return self.localtrace    def localtrace_trace(self, frame, why, arg):        if os.getpid() != self.pid:            return        if why == 'line':            # XXX shouldn't do the count increment when arg is exception?  But be careful to return self.localtrace when arg is exception! ?  --Zooko 2001-10-14            # record the file name and line number of every trace            # XXX I wish inspect offered me an optimized `getfilename(frame)' to use in place of the presumably heavier `getframeinfo()'.  --Zooko 2001-10-14            (filename, lineno, funcname, context, lineindex,) = inspect.getframeinfo(frame)            # if DEBUG_MODE:            #     print "%s.localtrace_trace(frame: %s, why: %s, arg: %s); filename: %s, lineno: %s, funcname: %s, context: %s, lineindex: %s\n" % (self, frame, why, arg, filename, lineno, funcname, context, lineindex,)            # XXX not convinced that this memoizing is a performance win -- I don't know enough about Python guts to tell.  --Zooko 2001-10-14            bname = self.pathtobasename.get(filename)            if bname is None:                # Using setdefault faster than two separate lines?  --Zooko 2001-10-14                bname = self.pathtobasename.setdefault(filename, os.path.basename(filename))            if context is not None:                try:                    print "%s(%d): %s" % (bname, lineno, context[lineindex],),                except IndexError:                    # Uh.. sometimes getframeinfo gives me a context of length 1 and a lineindex of -2.  Oh well.                    pass            else:                print "%s(???): ???" % bname        return self.localtrace    def localtrace_count(self, frame, why, arg):        if os.getpid() != self.pid:            return        if why == 'line':            # XXX shouldn't do the count increment when arg is exception?  But be careful to return self.localtrace when arg is exception! ?  --Zooko 2001-10-14            # record the file name and line number of every trace            # XXX I wish inspect offered me an optimized `getfilename(frame)' to use in place of the presumably heavier `getframeinfo()'.  --Zooko 2001-10-14            (filename, lineno, funcname, context, lineindex,) = inspect.getframeinfo(frame)            key = (filename, lineno,)            self.counts[key] = self.counts.get(key, 0) + 1        return self.localtrace    def results(self):        return CoverageResults(self.counts, infile=self.infile, outfile=self.outfile, calledfuncs=self._calledfuncs)def _err_exit(msg):    sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))    sys.exit(1)def main(argv=None):    import getopt    if argv is None:        argv = sys.argv    try:        opts, prog_argv = getopt.getopt(argv[1:], "tcrRf:d:msC:l",                                        ["help", "version", "trace", "count",                                         "report", "no-report",                                         "file=", "missing",                                         "ignore-module=", "ignore-dir=",                                         "coverdir=", "listfuncs",])    except getopt.error, msg:        sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))        sys.stderr.write("Try `%s --help' for more information\n" % sys.argv[0])        sys.exit(1)    trace = 0    count = 0    report = 0    no_report = 0    counts_file = None    missing = 0    ignore_modules = []    ignore_dirs = []    coverdir = None    summary = 0    listfuncs = false    for opt, val in opts:        if opt == "--help":            usage(sys.stdout)            sys.exit(0)        if opt == "--version":            sys.stdout.write("trace 2.0\n")            sys.exit(0)        if opt == "-l" or opt == "--listfuncs":            listfuncs = true            continue        if opt == "-t" or opt == "--trace":            trace = 1            continue        if opt == "-c" or opt == "--count":            count = 1            continue        if opt == "-r" or opt == "--report":            report = 1            continue        if opt == "-R" or opt == "--no-report":            no_report = 1            continue        if opt == "-f" or opt == "--file":            counts_file = val            continue        if opt == "-m" or opt == "--missing":            missing = 1            continue        if opt == "-C" or opt == "--coverdir":            coverdir = val            continue        if opt == "-s" or opt == "--summary":            summary = 1            continue        if opt == "--ignore-module":            ignore_modules.append(val)            continue        if opt == "--ignore-dir":            for s in string.split(val, os.pathsep):                s = os.path.expandvars(s)                # should I also call expanduser? (after all, could use $HOME)                s = string.replace(s, "$prefix",                                   os.path.join(sys.prefix, "lib",                                                "python" + sys.version[:3]))                s = string.replace(s, "$exec_prefix",                                   os.path.join(sys.exec_prefix, "lib",                                                "python" + sys.version[:3]))                s = os.path.normpath(s)                ignore_dirs.append(s)            continue        assert 0, "Should never get here"    if listfuncs and (count or trace):        _err_exit("cannot specify both --listfuncs and (--trace or --count)")    if not count and not trace and not report and not listfuncs:        _err_exit("must specify one of --trace, --count, --report or --listfuncs")    if report and no_report:        _err_exit("cannot specify both --report and --no-report")    if report and not counts_file:        _err_exit("--report requires a --file")    if no_report and len(prog_argv) == 0:        _err_exit("missing name of file to run")    # everything is ready    if report:        results = CoverageResults(infile=counts_file, outfile=counts_file)        results.write_results(missing, summary=summary, coverdir=coverdir)    else:        sys.argv = prog_argv        progname = prog_argv[0]        if eval(sys.version[:3])>1.3:            sys.path[0] = os.path.split(progname)[0] # ???        t = Trace(count, trace, countfuncs=listfuncs, ignoremods=ignore_modules, ignoredirs=ignore_dirs, infile=counts_file, outfile=counts_file)        try:            t.run('execfile(' + `progname` + ')')        except IOError, err:            _err_exit("Cannot run file %s because: %s" % (`sys.argv[0]`, err))        except SystemExit:            pass        results = t.results()        if not no_report:            results.write_results(missing, summary=summary, coverdir=coverdir)if __name__=='__main__':    main()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -