Patchwork [3,of,6,V3] templatefilters: don't stringify None into "None"

login
register
mail settings
Submitter Jordi Gutiérrez Hermoso
Date May 10, 2015, 6:44 p.m.
Message ID <341419c705647953b0b5.1431283483@Iris>
Download mbox | patch
Permalink /patch/9007/
State Accepted
Headers show

Comments

Jordi Gutiérrez Hermoso - May 10, 2015, 6:44 p.m.
# HG changeset patch
# User Jordi Gutiérrez Hermoso <jordigh@octave.org>
# Date 1431279231 14400
#      Sun May 10 13:33:51 2015 -0400
# Node ID 341419c705647953b0b5958a652a6918f15fbb23
# Parent  f55866c0fb93041bd435a2af78de4f1043dea774
templatefilters: don't stringify None into "None"

A few template keywords can in fact return None, such as {bisect}. In
some contexts, these get stringified into None instead of "". This is
leaking Python details into the UI.
Yuya Nishihara - May 10, 2015, 10:36 p.m.
On Sun, 10 May 2015 14:44:43 -0400, Jordi Gutiérrez Hermoso wrote:
> # HG changeset patch
> # User Jordi Gutiérrez Hermoso <jordigh@octave.org>
> # Date 1431279231 14400
> #      Sun May 10 13:33:51 2015 -0400
> # Node ID 341419c705647953b0b5958a652a6918f15fbb23
> # Parent  f55866c0fb93041bd435a2af78de4f1043dea774
> templatefilters: don't stringify None into "None"
> 
> A few template keywords can in fact return None, such as {bisect}. In
> some contexts, these get stringified into None instead of "". This is
> leaking Python details into the UI.
> 
> diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
> --- a/mercurial/templatefilters.py
> +++ b/mercurial/templatefilters.py
> @@ -326,6 +326,8 @@ def stringify(thing):
>      """
>      if util.safehasattr(thing, '__iter__') and not isinstance(thing, str):
>          return "".join([stringify(t) for t in thing if t is not None])
> +    if thing is None:
> +        return ""
>      return str(thing)

+1. This is what templater._flatten() does implicitly. stringify should do
the same way.

http://selenic.com/repo/hg/file/015adbcd92f3/mercurial/templater.py#l603
Pierre-Yves David - May 12, 2015, 2:25 a.m.
On 05/10/2015 03:36 PM, Yuya Nishihara wrote:
> On Sun, 10 May 2015 14:44:43 -0400, Jordi Gutiérrez Hermoso wrote:
>> # HG changeset patch
>> # User Jordi Gutiérrez Hermoso <jordigh@octave.org>
>> # Date 1431279231 14400
>> #      Sun May 10 13:33:51 2015 -0400
>> # Node ID 341419c705647953b0b5958a652a6918f15fbb23
>> # Parent  f55866c0fb93041bd435a2af78de4f1043dea774
>> templatefilters: don't stringify None into "None"

That patch have been pushed to the clowncopter.

Thanks Ryan for the review.

-
Pierre-Yves David

Patch

diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -326,6 +326,8 @@  def stringify(thing):
     """
     if util.safehasattr(thing, '__iter__') and not isinstance(thing, str):
         return "".join([stringify(t) for t in thing if t is not None])
+    if thing is None:
+        return ""
     return str(thing)
 
 def strip(text):