[PATCH 2 of 5] ui: add a 'stacklevel' argument to 'develwarn'

Pierre-Yves David pierre-yves.david at ens-lyon.org
Sun Dec 6 14:45:12 CST 2015


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at fb.com>
# Date 1449385533 28800
#      Sat Dec 05 23:05:33 2015 -0800
# Node ID eca257699f241e8490ff79a0a2e869fd241ad596
# Parent  847325e9c631e096019eb97a882cba0cb91cedb8
# EXP-Topic deprecationwarning
# Available At http://hg.netv6.net/marmoute-wip/mercurial/
#              hg pull http://hg.netv6.net/marmoute-wip/mercurial/ -r eca257699f24
ui: add a 'stacklevel' argument to 'develwarn'

This allows helper functions (like deprecation warning) to prepare a devel
warning for higher up in the stack. The argument is named after the one in the
Python's 'warning,warn' function.

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -1042,19 +1042,25 @@ class ui(object):
         ui.write(s, 'label') is equivalent to
         ui.write(ui.label(s, 'label')).
         '''
         return msg
 
-    def develwarn(self, msg):
-        """issue a developer warning message"""
+    def develwarn(self, msg, stacklevel=0):
+        """issue a developer warning message
+
+        Use 'stacklevel' to report the offender some layers further up in the
+        stack.
+        """
         msg = 'devel-warn: ' + msg
+        stacklevel += 2 # get out of develwarn
         if self.tracebackflag:
-            util.debugstacktrace(msg, 2, self.ferr, self.fout)
+            util.debugstacktrace(msg, stacklevel, self.ferr, self.fout)
         else:
             curframe = inspect.currentframe()
-            calframe = inspect.getouterframes(curframe, 2)
-            self.write_err('%s at: %s:%s (%s)\n' % ((msg,) + calframe[2][1:4]))
+            calframe = inspect.getouterframes(curframe, stacklevel)
+            self.write_err('%s at: %s:%s (%s)\n'
+                           % ((msg,) + calframe[stacklevel][1:4]))
 
 class paths(dict):
     """Represents a collection of paths and their configs.
 
     Data is initially derived from ui instances and the config files they have


More information about the Mercurial-devel mailing list