[PATCH 6 of 6] encoding: drop circular import by proxying through '<policy>.charencode'

Yuya Nishihara yuya at tcha.org
Tue Aug 8 10:30:25 EDT 2017


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1501510427 -32400
#      Mon Jul 31 23:13:47 2017 +0900
# Node ID 0299b63e488b11ff46208b7bb7fd1efe3a797a37
# Parent  6713774405f9b183f5500f65901f1dadbbd4639a
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.

diff --git a/mercurial/encoding.py b/mercurial/encoding.py
--- a/mercurial/encoding.py
+++ b/mercurial/encoding.py
@@ -18,6 +18,11 @@ from . import (
     pycompat,
 )
 
+charencode = policy.importmod(r'charencode')
+
+asciilower = charencode.asciilower
+asciiupper = charencode.asciiupper
+
 _sysstr = pycompat.sysstr
 
 if pycompat.ispy3:
@@ -318,38 +323,6 @@ def trim(s, width, ellipsis='', leftside
             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:
diff --git a/mercurial/policy.py b/mercurial/policy.py
--- a/mercurial/policy.py
+++ b/mercurial/policy.py
@@ -80,7 +80,9 @@ def _importfrom(pkgname, modname):
 
 # 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'),
 }
diff --git a/mercurial/pure/charencode.py b/mercurial/pure/charencode.py
new file mode 100644
--- /dev/null
+++ b/mercurial/pure/charencode.py
@@ -0,0 +1,22 @@
+# charencode.py - miscellaneous character encoding
+#
+#  Copyright 2005-2009 Matt Mackall <mpm at 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()


More information about the Mercurial-devel mailing list