16 __all__
= ["LogReader", "ENTER", "EXIT", "LINE"]
25 def __init__(self
, logfn
):
28 # (fileno, lineno) -> filename, funcname
31 self
._reader
= _hotshot
.logreader(logfn
)
32 self
._nextitem
= self
._reader
.next
33 self
._info
= self
._reader
.info
34 if self
._info
.has_key('current-directory'):
35 self
.cwd
= self
._info
['current-directory']
39 # This mirrors the call stack of the profiled code as the log
40 # is read back in. It contains tuples of the form:
42 # (file name, line number of function def, function name)
45 self
._append
= self
._stack
.append
46 self
._pop
= self
._stack
.pop
52 """Return the file descriptor of the log reader's log file."""
53 return self
._reader
.fileno()
55 def addinfo(self
, key
, value
):
56 """This method is called for each additional ADD_INFO record.
58 This can be overridden by applications that want to receive
59 these events. The default implementation does not need to be
60 called by alternate implementations.
62 The initial set of ADD_INFO records do not pass through this
63 mechanism; this is only needed to receive notification when
64 new values are added. Subclasses can inspect self._info after
65 calling LogReader.__init__().
69 def get_filename(self
, fileno
):
71 return self
._filemap
[fileno
]
73 raise ValueError, "unknown fileno"
75 def get_filenames(self
):
76 return self
._filemap
.values()
78 def get_fileno(self
, filename
):
79 filename
= os
.path
.normcase(os
.path
.normpath(filename
))
80 for fileno
, name
in self
._filemap
.items():
83 raise ValueError, "unknown filename"
85 def get_funcname(self
, fileno
, lineno
):
87 return self
._funcmap
[(fileno
, lineno
)]
89 raise ValueError, "unknown function location"
92 # This adds an optional (& ignored) parameter to next() so that the
93 # same bound method can be used as the __getitem__() method -- this
94 # avoids using an additional method call which kills the performance.
96 def next(self
, index
=0):
98 # This call may raise StopIteration:
99 what
, tdelta
, fileno
, lineno
= self
._nextitem
()
101 # handle the most common cases first
103 if what
== WHAT_ENTER
:
104 filename
, funcname
= self
._decode
_location
(fileno
, lineno
)
105 t
= (filename
, lineno
, funcname
)
107 return what
, t
, tdelta
109 if what
== WHAT_EXIT
:
110 return what
, self
._pop
(), tdelta
112 if what
== WHAT_LINENO
:
113 filename
, firstlineno
, funcname
= self
._stack
[-1]
114 return what
, (filename
, lineno
, funcname
), tdelta
116 if what
== WHAT_DEFINE_FILE
:
117 filename
= os
.path
.normcase(os
.path
.normpath(tdelta
))
118 self
._filemap
[fileno
] = filename
119 elif what
== WHAT_DEFINE_FUNC
:
120 filename
= self
._filemap
[fileno
]
121 self
._funcmap
[(fileno
, lineno
)] = (filename
, tdelta
)
122 elif what
== WHAT_ADD_INFO
:
123 # value already loaded into self.info; call the
124 # overridable addinfo() handler so higher-level code
125 # can pick up the new value
126 if tdelta
== 'current-directory':
128 self
.addinfo(tdelta
, lineno
)
130 raise ValueError, "unknown event type"
139 def _decode_location(self
, fileno
, lineno
):
141 return self
._funcmap
[(fileno
, lineno
)]
144 # This should only be needed when the log file does not
145 # contain all the DEFINE_FUNC records needed to allow the
146 # function name to be retrieved from the log file.
148 if self
._loadfile
(fileno
):
149 filename
= funcname
= None
151 filename
, funcname
= self
._funcmap
[(fileno
, lineno
)]
153 filename
= self
._filemap
.get(fileno
)
155 self
._funcmap
[(fileno
, lineno
)] = (filename
, funcname
)
156 return filename
, funcname
158 def _loadfile(self
, fileno
):
160 filename
= self
._filemap
[fileno
]
162 print "Could not identify fileId", fileno
166 absname
= os
.path
.normcase(os
.path
.join(self
.cwd
, filename
))
172 st
= parser
.suite(fp
.read())
175 # Scan the tree looking for def and lambda nodes, filling in
176 # self._funcmap with all the available information.
177 funcdef
= symbol
.funcdef
178 lambdef
= symbol
.lambdef
180 stack
= [st
.totuple(1)]
186 except (IndexError, TypeError):
189 self
._funcmap
[(fileno
, tree
[2][2])] = filename
, tree
[2][1]
191 self
._funcmap
[(fileno
, tree
[1][2])] = filename
, "<lambda>"
192 stack
.extend(list(tree
[1:]))