[PATCH] templater: add recursive template detection (issue4758)

Pierre-Yves David pierre-yves.david at ens-lyon.org
Fri Jan 22 15:48:51 CST 2016



On 01/22/2016 08:45 AM, Kostia Balytskyi wrote:
> # HG changeset patch
> # User Kostia Balytskyi <ikostia at fb.com>
> # Date 1453481091 0
> #      Fri Jan 22 16:44:51 2016 +0000
> # Node ID bf244ba18a2ff50783a17fecf134118921892020
> # Parent  d73a5ab18015f61ac61e6e77256512fd82b03818
> templater: add recursive template detection (issue4758)
>
> When mercurial is given a recursive template it currently fails with
> 'maximum recursion depth' exception. To avoid that we need to
> introduce a set of symbols currenly in resolution which would be added to
> as we go deeper in call start and removed from when we move out, just
> like the actual call stack. However, since we want to completely prohibit
> recursion, on every addition to this set we need to check whether
> it already contains the same value, in which case we have to abort.
>
> diff --git a/mercurial/templater.py b/mercurial/templater.py
> --- a/mercurial/templater.py
> +++ b/mercurial/templater.py
> @@ -760,8 +760,15 @@
>
>   stringify = templatefilters.stringify
>
> -def _flatten(thing):
> +namesinresolution = set([])

As well already well discussed in this thread: urg! please avoid global 
variables.

> +def _flatten(thing, key=''):
>       '''yield a single stream from a possibly nested set of iterators'''
> +    if key and key in namesinresolution:
> +        raise error.Abort("recursive template definition")

We try to include as much data as possible (while staying short) in our 
error message. Here we at least want the name of the recursive definition.

"recursive template definition" is not very actionable

"recursive template definition: foobar" is much more useful to the user.

-- 
Pierre-Yves David


More information about the Mercurial-devel mailing list