Patchwork D6043: UnicodeEncoding:change the default encoding of the whole script to be 'UTF-8'(issue6040)

login
register
mail settings
Submitter phabricator
Date March 2, 2019, 2:16 p.m.
Message ID <differential-rev-PHID-DREV-qdzo3kwrw37xjzy4tnnx-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/38994/
State New
Headers show

Comments

phabricator - March 2, 2019, 2:16 p.m.
akshjain.jain74 created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  hg
  hgext/pager.py
  hgext/phabricator.py
  mercurial/dispatch.py
  mercurial/scmutil.py
  mercurial/util.py

CHANGE DETAILS




To: akshjain.jain74, #hg-reviewers
Cc: mercurial-devel
phabricator - March 2, 2019, 8:41 p.m.
indygreg requested changes to this revision.
indygreg added a comment.
This revision now requires changes to proceed.


  If we wanted to call ``sys.setdefaultencoding()``, we should call it once during ``hg`` or at the top of ``mercurial/__init__.py``.
  
  But we don't want to call ``sys.setdefaultencoding()``. This topic has been discussed heavily over the years. The various answers at https://stackoverflow.com/questions/3828723/why-should-we-not-use-sys-setdefaultencodingutf-8-in-a-py-script have a lot of context and I won't rehash the reasons here.
  
  May I ask what compelled you to want to do this? Any bug you encountered that was fixed by this change represents a place where Mercurial isn't handling encoding properly. We should fix the actual places where that is occurring. Because even if we switch to UTF-8 as the default string encoding, UTF-8 isn't used everywhere and the underlying encoding problem may still exist!
  
  Any patch to introduce ``sys.setdefaultencoding()`` into the code base will likely never be accepted (unless it is doing something special like what ``HGUNICODEPEDANTRY`` is doing). So you may want to mark this code review as abandoned.

REPOSITORY
  rHG Mercurial

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

To: akshjain.jain74, #hg-reviewers, indygreg
Cc: indygreg, mercurial-devel

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -54,6 +54,9 @@ 
     stringutil,
 )
 
+reload(sys)
+sys.setdefaultencoding('utf8')
+
 base85 = policy.importmod(r'base85')
 osutil = policy.importmod(r'osutil')
 parsers = policy.importmod(r'parsers')
diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -45,6 +45,11 @@ 
     vfs,
 )
 
+import sys
+reload(sys)
+sys.setdefaultencoding('utf8')
+
+
 from .utils import (
     procutil,
     stringutil,
diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -18,6 +18,9 @@ 
 import time
 import traceback
 
+reload(sys)
+sys.setdefaultencoding('utf8')
+
 
 from .i18n import _
 
diff --git a/hgext/phabricator.py b/hgext/phabricator.py
--- a/hgext/phabricator.py
+++ b/hgext/phabricator.py
@@ -41,6 +41,7 @@ 
 
 from __future__ import absolute_import
 
+import sys
 import contextlib
 import itertools
 import json
@@ -72,6 +73,9 @@ 
     procutil,
     stringutil,
 )
+reload(sys)
+sys.setdefaultencoding('utf8')
+
 
 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
diff --git a/hgext/pager.py b/hgext/pager.py
--- a/hgext/pager.py
+++ b/hgext/pager.py
@@ -23,14 +23,20 @@ 
 '''
 from __future__ import absolute_import
 
+import sys
+
 from mercurial import (
     cmdutil,
     commands,
     dispatch,
     extensions,
     registrar,
     )
 
+reload(sys)
+sys.setdefaultencoding('utf8')
+
+
 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
 # be specifying the version(s) of Mercurial they are tested with, or
diff --git a/hg b/hg
--- a/hg
+++ b/hg
@@ -14,7 +14,7 @@ 
 if os.environ.get('HGUNICODEPEDANTRY', False):
     try:
         reload(sys)
-        sys.setdefaultencoding("undefined")
+        sys.setdefaultencoding("utf8")
     except NameError:
         pass