Skip to content

Commit

Permalink
Merge pull request #254 from joerick/show-built-in
Browse files Browse the repository at this point in the history
Stop built-in code showing as 'None' in traces
  • Loading branch information
joerick committed Jul 22, 2023
2 parents 8ab07d5 + b6a1da4 commit 6c60d1a
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions pyinstrument/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,24 +151,29 @@ def file_path_short(self) -> str | None:
return self.parent.file_path_short

if not hasattr(self, "_file_path_short"):
if self.file_path:
result = None

for path in sys.path:
# On Windows, if self.file_path and path are on different drives, relpath
# will result in exception, because it cannot compute a relpath in this case.
# The root cause is that on Windows, there is no root dir like '/' on Linux.
try:
candidate = os.path.relpath(self.file_path, path)
except ValueError:
continue

if not result or (len(candidate.split(os.sep)) < len(result.split(os.sep))):
result = candidate
result = None

self._file_path_short = result
else:
self._file_path_short = None
if self.file_path:
if len(self.file_path.split(os.sep)) == 1:
# probably not a file path at all, more likely <built-in>
# or similar
result = self.file_path
else:
for path in sys.path:
# On Windows, if self.file_path and path are on
# different drives, relpath will result in exception,
# because it cannot compute a relpath in this case.
# The root cause is that on Windows, there is no root
# dir like '/' on Linux.
try:
candidate = os.path.relpath(self.file_path, path)
except ValueError:
continue

if not result or (len(candidate.split(os.sep)) < len(result.split(os.sep))):
result = candidate

self._file_path_short = result

return self._file_path_short

Expand Down Expand Up @@ -204,6 +209,7 @@ def is_application_code(self) -> bool:
def code_position_short(self) -> str | None:
if self.file_path_short and self.line_no:
return "%s:%i" % (self.file_path_short, self.line_no)
return self.file_path_short

_children: list[Frame]
attributes: dict[str, float]
Expand Down

0 comments on commit 6c60d1a

Please sign in to comment.