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

Yuya Nishihara yuya at tcha.org
Mon Dec 7 08:07:37 CST 2015


On Sun, 06 Dec 2015 12:45:12 -0800, Pierre-Yves David wrote:
> # 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

I might be paranoid, but if we copy the behavior of warnings.warn, the default
stacklevel should be 1.

   stacklevel=0: develwarn()
   stacklevel=1: caller of develwarn()
   stacklevel=2: wrapper function that calls develwarn()

https://docs.python.org/2.7/library/warnings.html#warnings.warn

>          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)

getouterframes() doesn't need stacklevel. The second argument is the number of
lines to be returned as a context.

https://docs.python.org/2.7/library/inspect.html#inspect.getouterframes


More information about the Mercurial-devel mailing list