[PATCH] lsprof: support PyPy (issue4573)

Gregory Szorc gregory.szorc at gmail.com
Sun Nov 22 07:26:37 UTC 2015


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1448177182 28800
#      Sat Nov 21 23:26:22 2015 -0800
# Node ID 138cc82144ee0335335533dcfc33968987aa3dc8
# Parent  1ca55c7fa6209e6c2393fd98eb963b367585d04a
lsprof: support PyPy (issue4573)

PyPy's _lsprof module doesn't export a "profiler_entry" symbol. This
patch treats the symbol as optional and falls back to verifying the
attribute is present on the first entry in the collected data as
part of validation.

There is a chance not every entry will contain the requested sort
attribute. But, this patch does unbust lsprof on PyPy for the hg
commands I've tested, so I assume it is sufficient. It's certainly
better than the ImportError we encountered before.

As part of the import refactor, I snuck in the addition of
absolute_import.

diff --git a/mercurial/lsprof.py b/mercurial/lsprof.py
--- a/mercurial/lsprof.py
+++ b/mercurial/lsprof.py
@@ -1,10 +1,17 @@
+from __future__ import absolute_import
+
+import _lsprof
 import sys
-from _lsprof import Profiler, profiler_entry
+
+Profiler = _lsprof.Profiler
+
+# PyPy doesn't expose profiler_entry from the module.
+profiler_entry = getattr(_lsprof, 'profiler_entry', None)
 
 __all__ = ['profile', 'Stats']
 
 def profile(f, *args, **kwds):
     """XXX docstring"""
     p = Profiler()
     p.enable(subcalls=True, builtins=True)
     try:
@@ -17,18 +24,22 @@ def profile(f, *args, **kwds):
 class Stats(object):
     """XXX docstring"""
 
     def __init__(self, data):
         self.data = data
 
     def sort(self, crit="inlinetime"):
         """XXX docstring"""
-        if crit not in profiler_entry.__dict__:
+        # profiler_entries isn't defined when running under PyPy.
+        if profiler_entry and crit not in profiler_entry.__dict__:
             raise ValueError("Can't sort by %s" % crit)
+        elif self.data and not getattr(self.data[0], crit, None):
+            raise ValueError("Can't sort by %s" % crit)
+
         self.data.sort(key=lambda x: getattr(x, crit), reverse=True)
         for e in self.data:
             if e.calls:
                 e.calls.sort(key=lambda x: getattr(x, crit), reverse=True)
 
     def pprint(self, top=None, file=None, limit=None, climit=None):
         """XXX docstring"""
         if file is None:


More information about the Mercurial-devel mailing list