Patchwork [1,of,4,V2] ui: add a 'stacklevel' argument to 'develwarn'

login
register
mail settings
Submitter Pierre-Yves David
Date Dec. 7, 2015, 7:33 p.m.
Message ID <37c8432ae7a8e7ac7c21.1449516829@marginatus.alto.octopoid.net>
Download mbox | patch
Permalink /patch/11905/
State Accepted
Headers show

Comments

Pierre-Yves David - Dec. 7, 2015, 7:33 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@fb.com>
# Date 1449385533 28800
#      Sat Dec 05 23:05:33 2015 -0800
# Node ID 37c8432ae7a8e7ac7c213c5dea472d5060e325e9
# Parent  631308bb9b4604f2e2d34c841456b5edd5752893
# EXP-Topic deprecationwarning
# Available At http://hg.netv6.net/marmoute-wip/mercurial/
#              hg pull http://hg.netv6.net/marmoute-wip/mercurial/ -r 37c8432ae7a8
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.

Patch

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=1):
+        """issue a developer warning message
+
+        Use 'stacklevel' to report the offender some layers further up in the
+        stack.
+        """
         msg = 'devel-warn: ' + msg
+        stacklevel += 1 # get in 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]))
+            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