15 __all__
= ["LogReader", "ENTER", "EXIT", "LINE"]
24 def __init__(self
, logfn
):
27 # (fileno, lineno) -> filename, funcname
30 self
._reader
= _hotshot
.logreader(logfn
)
31 self
._nextitem
= self
._reader
.next
32 self
._info
= self
._reader
.info
33 if 'current-directory' in self
._info
:
34 self
.cwd
= self
._info
['current-directory']
38 # This mirrors the call stack of the profiled code as the log
39 # is read back in. It contains tuples of the form:
41 # (file name, line number of function def, function name)
44 self
._append
= self
._stack
.append
45 self
._pop
= self
._stack
.pop
51 """Return the file descriptor of the log reader's log file."""
52 return self
._reader
.fileno()
54 def addinfo(self
, key
, value
):
55 """This method is called for each additional ADD_INFO record.
57 This can be overridden by applications that want to receive
58 these events. The default implementation does not need to be
59 called by alternate implementations.
61 The initial set of ADD_INFO records do not pass through this
62 mechanism; this is only needed to receive notification when
63 new values are added. Subclasses can inspect self._info after
64 calling LogReader.__init__().
68 def get_filename(self
, fileno
):
70 return self
._filemap
[fileno
]
72 raise ValueError, "unknown fileno"
74 def get_filenames(self
):
75 return self
._filemap
.values()
77 def get_fileno(self
, filename
):
78 filename
= os
.path
.normcase(os
.path
.normpath(filename
))
79 for fileno
, name
in self
._filemap
.items():
82 raise ValueError, "unknown filename"
84 def get_funcname(self
, fileno
, lineno
):
86 return self
._funcmap
[(fileno
, lineno
)]
88 raise ValueError, "unknown function location"
91 # This adds an optional (& ignored) parameter to next() so that the
92 # same bound method can be used as the __getitem__() method -- this
93 # avoids using an additional method call which kills the performance.
95 def next(self
, index
=0):
97 # This call may raise StopIteration:
98 what
, tdelta
, fileno
, lineno
= self
._nextitem
()
100 # handle the most common cases first
102 if what
== WHAT_ENTER
:
103 filename
, funcname
= self
._decode
_location
(fileno
, lineno
)
104 t
= (filename
, lineno
, funcname
)
106 return what
, t
, tdelta
108 if what
== WHAT_EXIT
:
109 return what
, self
._pop
(), tdelta
111 if what
== WHAT_LINENO
:
112 filename
, firstlineno
, funcname
= self
._stack
[-1]
113 return what
, (filename
, lineno
, funcname
), tdelta
115 if what
== WHAT_DEFINE_FILE
:
116 filename
= os
.path
.normcase(os
.path
.normpath(tdelta
))
117 self
._filemap
[fileno
] = filename
118 elif what
== WHAT_DEFINE_FUNC
:
119 filename
= self
._filemap
[fileno
]
120 self
._funcmap
[(fileno
, lineno
)] = (filename
, tdelta
)
121 elif what
== WHAT_ADD_INFO
:
122 # value already loaded into self.info; call the
123 # overridable addinfo() handler so higher-level code
124 # can pick up the new value
125 if tdelta
== 'current-directory':
127 self
.addinfo(tdelta
, lineno
)
129 raise ValueError, "unknown event type"
138 def _decode_location(self
, fileno
, lineno
):
140 return self
._funcmap
[(fileno
, lineno
)]
143 # This should only be needed when the log file does not
144 # contain all the DEFINE_FUNC records needed to allow the
145 # function name to be retrieved from the log file.
147 if self
._loadfile
(fileno
):
148 filename
= funcname
= None
150 filename
, funcname
= self
._funcmap
[(fileno
, lineno
)]
152 filename
= self
._filemap
.get(fileno
)
154 self
._funcmap
[(fileno
, lineno
)] = (filename
, funcname
)
155 return filename
, funcname
157 def _loadfile(self
, fileno
):
159 filename
= self
._filemap
[fileno
]
161 print "Could not identify fileId", fileno
165 absname
= os
.path
.normcase(os
.path
.join(self
.cwd
, filename
))
171 st
= parser
.suite(fp
.read())
174 # Scan the tree looking for def and lambda nodes, filling in
175 # self._funcmap with all the available information.
176 funcdef
= symbol
.funcdef
177 lambdef
= symbol
.lambdef
179 stack
= [st
.totuple(1)]
185 except (IndexError, TypeError):
188 self
._funcmap
[(fileno
, tree
[2][2])] = filename
, tree
[2][1]
190 self
._funcmap
[(fileno
, tree
[1][2])] = filename
, "<lambda>"
191 stack
.extend(list(tree
[1:]))