changeset 33761:f5fc54e7e467

encoding: drop circular import by proxying through '<policy>.charencode' I decided not to split charencode.c to new C extension module because it would duplicate binary codes unnecessarily.
author Yuya Nishihara <yuya@tcha.org>
date Mon, 31 Jul 2017 23:13:47 +0900
parents cd2aca0808f8
children c26a76e1af36
files mercurial/encoding.py mercurial/policy.py mercurial/pure/charencode.py
diffstat 3 files changed, 29 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/encoding.py	Mon Jul 31 23:40:36 2017 +0900
+++ b/mercurial/encoding.py	Mon Jul 31 23:13:47 2017 +0900
@@ -18,6 +18,11 @@
     pycompat,
 )
 
+charencode = policy.importmod(r'charencode')
+
+asciilower = charencode.asciilower
+asciiupper = charencode.asciiupper
+
 _sysstr = pycompat.sysstr
 
 if pycompat.ispy3:
@@ -318,38 +323,6 @@
             return concat(usub.encode(_sysstr(encoding)))
     return ellipsis # no enough room for multi-column characters
 
-def _asciilower(s):
-    '''convert a string to lowercase if ASCII
-
-    Raises UnicodeDecodeError if non-ASCII characters are found.'''
-    s.decode('ascii')
-    return s.lower()
-
-def asciilower(s):
-    # delay importing avoids cyclic dependency around "parsers" in
-    # pure Python build (util => i18n => encoding => parsers => util)
-    parsers = policy.importmod(r'parsers')
-    impl = getattr(parsers, 'asciilower', _asciilower)
-    global asciilower
-    asciilower = impl
-    return impl(s)
-
-def _asciiupper(s):
-    '''convert a string to uppercase if ASCII
-
-    Raises UnicodeDecodeError if non-ASCII characters are found.'''
-    s.decode('ascii')
-    return s.upper()
-
-def asciiupper(s):
-    # delay importing avoids cyclic dependency around "parsers" in
-    # pure Python build (util => i18n => encoding => parsers => util)
-    parsers = policy.importmod(r'parsers')
-    impl = getattr(parsers, 'asciiupper', _asciiupper)
-    global asciiupper
-    asciiupper = impl
-    return impl(s)
-
 def lower(s):
     "best-effort encoding-aware case-folding of local string s"
     try:
--- a/mercurial/policy.py	Mon Jul 31 23:40:36 2017 +0900
+++ b/mercurial/policy.py	Mon Jul 31 23:13:47 2017 +0900
@@ -80,7 +80,9 @@
 
 # map import request to other package or module
 _modredirects = {
+    (r'cext', r'charencode'): (r'cext', r'parsers'),
     (r'cffi', r'base85'): (r'pure', r'base85'),
+    (r'cffi', r'charencode'): (r'pure', r'charencode'),
     (r'cffi', r'diffhelpers'): (r'pure', r'diffhelpers'),
     (r'cffi', r'parsers'): (r'pure', r'parsers'),
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial/pure/charencode.py	Mon Jul 31 23:13:47 2017 +0900
@@ -0,0 +1,22 @@
+# charencode.py - miscellaneous character encoding
+#
+#  Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+from __future__ import absolute_import
+
+def asciilower(s):
+    '''convert a string to lowercase if ASCII
+
+    Raises UnicodeDecodeError if non-ASCII characters are found.'''
+    s.decode('ascii')
+    return s.lower()
+
+def asciiupper(s):
+    '''convert a string to uppercase if ASCII
+
+    Raises UnicodeDecodeError if non-ASCII characters are found.'''
+    s.decode('ascii')
+    return s.upper()