Patchwork D3536: json: reject unicode on py2 as well

login
register
mail settings
Submitter phabricator
Date May 12, 2018, 4:35 a.m.
Message ID <differential-rev-PHID-DREV-dzm76473igg2pzjmbx2b-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/31541/
State Superseded
Headers show

Comments

phabricator - May 12, 2018, 4:35 a.m.
martinvonz created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  This makes it consistent with the behavior on py3.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3536

AFFECTED FILES
  mercurial/templatefilters.py

CHANGE DETAILS




To: martinvonz, #hg-reviewers
Cc: mercurial-devel
phabricator - May 12, 2018, 4:43 a.m.
indygreg requested changes to this revision.
indygreg added a comment.
This revision now requires changes to proceed.


  `unicode` is not a type on Python 3. I think this should be changed to `(str, pycompat.unicode)`. That will evaluate to `(str, unicode)` on Python 2 and `(str, str)` on Python 3. If you go this route, please rephrase the comment to denote the Python 2 behavior only triggering on `unicode` because of `str == bytes`.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3536

To: martinvonz, #hg-reviewers, indygreg
Cc: indygreg, mercurial-devel
Yuya Nishihara - May 12, 2018, 5:39 a.m.
>   `unicode` is not a type on Python 3. I think this should be changed to `(str, pycompat.unicode)`.

Alternatively, it could be `type(u'')`. I have no preference though.
phabricator - May 12, 2018, 5:52 a.m.
yuja added a comment.


  >   `unicode` is not a type on Python 3. I think this should be changed to `(str, pycompat.unicode)`.
  
  Alternatively, it could be `type(u'')`. I have no preference though.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3536

To: martinvonz, #hg-reviewers, indygreg
Cc: yuja, indygreg, mercurial-devel
phabricator - May 12, 2018, 6:13 a.m.
martinvonz added a comment.


  In https://phab.mercurial-scm.org/D3536#56022, @indygreg wrote:
  
  > `unicode` is not a type on Python 3. I think this should be changed to `(str, pycompat.unicode)`. That will evaluate to `(str, unicode)` on Python 2 and `(str, str)` on Python 3. If you go this route, please rephrase the comment to denote the Python 2 behavior only triggering on `unicode` because of `str == bytes`.
  
  
  Done, I believe.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3536

To: martinvonz, #hg-reviewers, indygreg
Cc: yuja, indygreg, mercurial-devel
phabricator - May 16, 2018, 5:49 p.m.
martinvonz added a comment.


  In https://phab.mercurial-scm.org/D3536#56051, @yuja wrote:
  
  > > - elif isinstance(obj, str):
  > > - # This branch is unreachable on Python 2, because bytes == str
  > > - # and we'll return in the next-earlier block in the elif
  > > - # ladder. On Python 3, this helps us catch bugs before they
  > > - # hurt someone. +    elif isinstance(obj, (str, pycompat.unicode)): +        # On Python 2, we'll only get here if obj is unicode, because +        # str == bytes, so str is handled above.
  >
  > Doh, I never thought we already had pycompat.unicode to be imported
  >  automagically by the code transformer, and it's unavailable on Python 2!
  >  Since we'll eventually ditch the transformer, we'll have to add
  >  `unicode = __builtin__.unicode` to the py2 side of pycompat.
  
  
  Yep, tests caught that too (once I ran them :P). I switched to type(u'') instead and I'll leave the fix of pycompat out of this patch (since pycompat probably needs further changes for xrange and others anyway).
  
  > Nit: `(str,` can be removed as `pycompat.unicode` is `str` on Python 3.
  
  Good point. I fixed that and removed the comment because it it didn't seem to add anything anymore.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3536

To: martinvonz, #hg-reviewers, indygreg
Cc: yuja, indygreg, mercurial-devel

Patch

diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -249,13 +249,9 @@ 
         return pycompat.bytestr(obj)
     elif isinstance(obj, bytes):
         return '"%s"' % encoding.jsonescape(obj, paranoid=paranoid)
-    elif isinstance(obj, str):
-        # This branch is unreachable on Python 2, because bytes == str
-        # and we'll return in the next-earlier block in the elif
-        # ladder. On Python 3, this helps us catch bugs before they
-        # hurt someone.
+    elif isinstance(obj, (str, unicode)):
         raise error.ProgrammingError(
-            'Mercurial only does output with bytes on Python 3: %r' % obj)
+            'Mercurial only does output with bytes: %r' % obj)
     elif util.safehasattr(obj, 'keys'):
         out = ['"%s": %s' % (encoding.jsonescape(k, paranoid=paranoid),
                              json(v, paranoid))