lsprof: support PyPy (issue4573)
authorGregory Szorc <gregory.szorc@gmail.com>
Sat, 21 Nov 2015 23:26:22 -0800
changeset 27061 9c75daf89450
parent 27060 4613a89bea42
child 27062 e4f2e4f5e98d
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.
mercurial/lsprof.py
--- a/mercurial/lsprof.py	Wed Nov 11 19:10:45 2015 -0500
+++ b/mercurial/lsprof.py	Sat Nov 21 23:26:22 2015 -0800
@@ -1,5 +1,12 @@
+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']
 
@@ -22,8 +29,13 @@
 
     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:
+            if 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: