encoding: avoid cyclic dependency around "parsers" in pure Python build

39fbe33f95fa brought "asciilower" and "import parsers" into
"encoding.py".

This works fine with "parsers" module in C implementation, but doesn't
with one in pure Python implementation, because the latter causes
cyclic dependency below and aborting execution:

    util => i18n => encoding => parsers => util

This patch delays importing "parsers" module until it is really
needed, to avoid cyclic dependency around "parsers" in pure Python
build.
This commit is contained in:
FUJIWARA Katsunori 2014-10-17 02:07:04 +09:00
parent 830b7b0082
commit 7120dc2e96

View File

@ -5,7 +5,7 @@
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
import error, parsers
import error
import unicodedata, locale, os
def _getpreferredencoding():
@ -258,14 +258,21 @@ def trim(s, width, ellipsis='', leftside=False):
return concat(usub.encode(encoding))
return ellipsis # no enough room for multi-column characters
def asciilower(s):
def _asciilower(s):
'''convert a string to lowercase if ASCII
Raises UnicodeDecodeError if non-ASCII characters are found.'''
s.decode('ascii')
return s.lower()
asciilower = getattr(parsers, 'asciilower', asciilower)
def asciilower(s):
# delay importing avoids cyclic dependency around "parsers" in
# pure Python build (util => i18n => encoding => parsers => util)
import parsers
impl = getattr(parsers, 'asciilower', _asciilower)
global asciilower
asciilower = impl
return impl(s)
def lower(s):
"best-effort encoding-aware case-folding of local string s"