Patchwork templatefilter: add support for 'long' to json()

login
register
mail settings
Submitter Matt Harbison
Date April 1, 2017, 6:06 p.m.
Message ID <a3d441253abc38df20c9.1491069984@Envy>
Download mbox | patch
Permalink /patch/19892/
State Accepted
Headers show

Comments

Matt Harbison - April 1, 2017, 6:06 p.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1491020477 14400
#      Sat Apr 01 00:21:17 2017 -0400
# Node ID a3d441253abc38df20c9890b207c1ab454bb691d
# Parent  2632df096fc0ac7582382b1f94ea4b9ad0bce8f2
templatefilter: add support for 'long' to json()

When disabling the '#requires serve' check in test-hgwebdir.t and running it on
Windows, several 500 errors popped up when querying '?style=json', with the
following in the error log:

    File "...\\mercurial\\templater.py", line 393, in runfilter
      "keyword '%s'") % (filt.func_name, dt))
    Abort: template filter 'json' is not compatible with keyword 'lastchange'

The swallowed exception at that point was:

    File "...\\mercurial\\templatefilters.py", line 242, in json
      raise TypeError('cannot encode type %s' % obj.__class__.__name__)
    TypeError: cannot encode type long

This corresponds to 'lastchange' being populated by hgweb.common.get_stat(),
which uses os.stat().st_mtime.  os.stat_float_times() is being disabled in util,
so the type for the times is 'long' on Windows, and 'int' on Linux.
Matt Harbison - April 1, 2017, 6:17 p.m.
On Sat, 01 Apr 2017 14:06:24 -0400, Matt Harbison <mharbison72@gmail.com>  
wrote:

> # HG changeset patch
> # User Matt Harbison <matt_harbison@yahoo.com>
> # Date 1491020477 14400
> #      Sat Apr 01 00:21:17 2017 -0400
> # Node ID a3d441253abc38df20c9890b207c1ab454bb691d
> # Parent  2632df096fc0ac7582382b1f94ea4b9ad0bce8f2
> templatefilter: add support for 'long' to json()
>
> When disabling the '#requires serve' check in test-hgwebdir.t and  
> running it on
> Windows, several 500 errors popped up when querying '?style=json', with  
> the
> following in the error log:

BTW, does anybody know why the 'serve' tests are disabled on Windows?   
hghave.has_serve() simply says "platform and python can manage 'hg serve  
-d'", and the commit that introduces it doesn't explain what the issues  
were.  I made has_serve() return True unconditionally, and while there  
were errors in the newly run code, none looked like 'serve -d' were  
malfunctioning.
Gregory Szorc - April 1, 2017, 7:20 p.m.
On Sat, Apr 1, 2017 at 11:06 AM, Matt Harbison <mharbison72@gmail.com>
wrote:

> # HG changeset patch
> # User Matt Harbison <matt_harbison@yahoo.com>
> # Date 1491020477 14400
> #      Sat Apr 01 00:21:17 2017 -0400
> # Node ID a3d441253abc38df20c9890b207c1ab454bb691d
> # Parent  2632df096fc0ac7582382b1f94ea4b9ad0bce8f2
> templatefilter: add support for 'long' to json()
>
> When disabling the '#requires serve' check in test-hgwebdir.t and running
> it on
> Windows, several 500 errors popped up when querying '?style=json', with the
> following in the error log:
>
>     File "...\\mercurial\\templater.py", line 393, in runfilter
>       "keyword '%s'") % (filt.func_name, dt))
>     Abort: template filter 'json' is not compatible with keyword
> 'lastchange'
>
> The swallowed exception at that point was:
>
>     File "...\\mercurial\\templatefilters.py", line 242, in json
>       raise TypeError('cannot encode type %s' % obj.__class__.__name__)
>     TypeError: cannot encode type long
>
> This corresponds to 'lastchange' being populated by
> hgweb.common.get_stat(),
> which uses os.stat().st_mtime.  os.stat_float_times() is being disabled in
> util,
> so the type for the times is 'long' on Windows, and 'int' on Linux.
>
> diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
> --- a/mercurial/templatefilters.py
> +++ b/mercurial/templatefilters.py
> @@ -221,7 +221,8 @@
>  def json(obj):
>      if obj is None or obj is False or obj is True:
>          return {None: 'null', False: 'false', True: 'true'}[obj]
> -    elif isinstance(obj, int) or isinstance(obj, float):
> +    elif (isinstance(obj, int) or isinstance(obj, long)
> +          or isinstance(obj, float)):
>

isinstance() accepts a tuple of types as its second argument. Let's use
that to make this code shorter. (This could probably be done in flight.)


>          return str(obj)
>      elif isinstance(obj, str):
>          return '"%s"' % encoding.jsonescape(obj, paranoid=True)
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
>
Matt Harbison - April 1, 2017, 10:35 p.m.
On Sat, 01 Apr 2017 15:20:02 -0400, Gregory Szorc  
<gregory.szorc@gmail.com> wrote:

> On Sat, Apr 1, 2017 at 11:06 AM, Matt Harbison <mharbison72@gmail.com>
> wrote:
>
>> # HG changeset patch
>> # User Matt Harbison <matt_harbison@yahoo.com>
>> # Date 1491020477 14400
>> #      Sat Apr 01 00:21:17 2017 -0400
>> # Node ID a3d441253abc38df20c9890b207c1ab454bb691d
>> # Parent  2632df096fc0ac7582382b1f94ea4b9ad0bce8f2
>> templatefilter: add support for 'long' to json()
>>
>> When disabling the '#requires serve' check in test-hgwebdir.t and  
>> running
>> it on
>> Windows, several 500 errors popped up when querying '?style=json', with  
>> the
>> following in the error log:
>>
>>     File "...\\mercurial\\templater.py", line 393, in runfilter
>>       "keyword '%s'") % (filt.func_name, dt))
>>     Abort: template filter 'json' is not compatible with keyword
>> 'lastchange'
>>
>> The swallowed exception at that point was:
>>
>>     File "...\\mercurial\\templatefilters.py", line 242, in json
>>       raise TypeError('cannot encode type %s' % obj.__class__.__name__)
>>     TypeError: cannot encode type long
>>
>> This corresponds to 'lastchange' being populated by
>> hgweb.common.get_stat(),
>> which uses os.stat().st_mtime.  os.stat_float_times() is being disabled  
>> in
>> util,
>> so the type for the times is 'long' on Windows, and 'int' on Linux.
>>
>> diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
>> --- a/mercurial/templatefilters.py
>> +++ b/mercurial/templatefilters.py
>> @@ -221,7 +221,8 @@
>>  def json(obj):
>>      if obj is None or obj is False or obj is True:
>>          return {None: 'null', False: 'false', True: 'true'}[obj]
>> -    elif isinstance(obj, int) or isinstance(obj, float):
>> +    elif (isinstance(obj, int) or isinstance(obj, long)
>> +          or isinstance(obj, float)):
>>
>
> isinstance() accepts a tuple of types as its second argument. Let's use
> that to make this code shorter. (This could probably be done in flight.)
>

Ah, thanks.  I meant to look into if it did, but spent a lot of time  
tracking down the int vs float vs long part, and forgot.

I've got a growing series of fixes for hg serve tests on Windows, so I'll  
just send a v2 if nobody does a fix-in-flight in the meantime.

>>          return str(obj)
>>      elif isinstance(obj, str):
>>          return '"%s"' % encoding.jsonescape(obj, paranoid=True)
>> _______________________________________________
>> Mercurial-devel mailing list
>> Mercurial-devel@mercurial-scm.org
>> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Yuya Nishihara - April 2, 2017, 2:05 a.m.
On Sat, 1 Apr 2017 12:20:02 -0700, Gregory Szorc wrote:
> On Sat, Apr 1, 2017 at 11:06 AM, Matt Harbison <mharbison72@gmail.com>
> wrote:
> 
> > # HG changeset patch
> > # User Matt Harbison <matt_harbison@yahoo.com>
> > # Date 1491020477 14400
> > #      Sat Apr 01 00:21:17 2017 -0400
> > # Node ID a3d441253abc38df20c9890b207c1ab454bb691d
> > # Parent  2632df096fc0ac7582382b1f94ea4b9ad0bce8f2
> > templatefilter: add support for 'long' to json()
> >
> > When disabling the '#requires serve' check in test-hgwebdir.t and running
> > it on
> > Windows, several 500 errors popped up when querying '?style=json', with the
> > following in the error log:
> >
> >     File "...\\mercurial\\templater.py", line 393, in runfilter
> >       "keyword '%s'") % (filt.func_name, dt))
> >     Abort: template filter 'json' is not compatible with keyword
> > 'lastchange'
> >
> > The swallowed exception at that point was:
> >
> >     File "...\\mercurial\\templatefilters.py", line 242, in json
> >       raise TypeError('cannot encode type %s' % obj.__class__.__name__)
> >     TypeError: cannot encode type long
> >
> > This corresponds to 'lastchange' being populated by
> > hgweb.common.get_stat(),
> > which uses os.stat().st_mtime.  os.stat_float_times() is being disabled in
> > util,
> > so the type for the times is 'long' on Windows, and 'int' on Linux.
> >
> > diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
> > --- a/mercurial/templatefilters.py
> > +++ b/mercurial/templatefilters.py
> > @@ -221,7 +221,8 @@
> >  def json(obj):
> >      if obj is None or obj is False or obj is True:
> >          return {None: 'null', False: 'false', True: 'true'}[obj]
> > -    elif isinstance(obj, int) or isinstance(obj, float):
> > +    elif (isinstance(obj, int) or isinstance(obj, long)
> > +          or isinstance(obj, float)):
> >
> 
> isinstance() accepts a tuple of types as its second argument. Let's use
> that to make this code shorter. (This could probably be done in flight.)

Queued, thanks. Updated to use isinstance(obj, (...)).
Yuya Nishihara - April 2, 2017, 2:11 a.m.
On Sat, 01 Apr 2017 14:17:33 -0400, Matt Harbison wrote:
> On Sat, 01 Apr 2017 14:06:24 -0400, Matt Harbison <mharbison72@gmail.com>  
> wrote:
> 
> > # HG changeset patch
> > # User Matt Harbison <matt_harbison@yahoo.com>
> > # Date 1491020477 14400
> > #      Sat Apr 01 00:21:17 2017 -0400
> > # Node ID a3d441253abc38df20c9890b207c1ab454bb691d
> > # Parent  2632df096fc0ac7582382b1f94ea4b9ad0bce8f2
> > templatefilter: add support for 'long' to json()
> >
> > When disabling the '#requires serve' check in test-hgwebdir.t and  
> > running it on
> > Windows, several 500 errors popped up when querying '?style=json', with  
> > the
> > following in the error log:
> 
> BTW, does anybody know why the 'serve' tests are disabled on Windows?   
> hghave.has_serve() simply says "platform and python can manage 'hg serve  
> -d'", and the commit that introduces it doesn't explain what the issues  
> were.  I made has_serve() return True unconditionally, and while there  
> were errors in the newly run code, none looked like 'serve -d' were  
> malfunctioning.

Perhaps killdaemons.py wouldn't work at that time, which was fixed by
2d4a096e213c, d5a3bda6e170, and 448d0c452140.

Patch

diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -221,7 +221,8 @@ 
 def json(obj):
     if obj is None or obj is False or obj is True:
         return {None: 'null', False: 'false', True: 'true'}[obj]
-    elif isinstance(obj, int) or isinstance(obj, float):
+    elif (isinstance(obj, int) or isinstance(obj, long)
+          or isinstance(obj, float)):
         return str(obj)
     elif isinstance(obj, str):
         return '"%s"' % encoding.jsonescape(obj, paranoid=True)