Also find correct column width of wide characters

Martin Geisler mg at daimi.au.dk
Wed Jan 21 04:03:47 CST 2009


Shun-ichi Goto <shunichi.goto at gmail.com> writes:

> -def locallen(s):
> -    """Find the length in characters of a local string"""
> -    return len(s.decode(_encoding, "replace"))
> +_colwidth = None
> +def colwidth(s):
> +    """Find the column width of string to display."""
> +    global _colwidth
> +    us = s.decode(_encoding, "replace")
> +    if _colwidth is None:
> +        try:
> +            _colwidth = lambda s: sum([unicodedata.east_asian_width(c) in 'WF'
> +                                       and 2 or 1 for c in s])
> +            # need calling here due to demand loading
> +            return _colwidth(us)
> +        except:

east_asian_width was introduced in Python 2.4 -- is that why you have
wrapped the code in a try-except clause? If so, would it not be more
explicit do a hasattr(unicodedata, 'east_asian_width') test?

I've just tested this with Python 2.3 and 2.4 and it works fine with
the demandimport system, i.e., the module is loaded at that point.

-- 
Martin Geisler

VIFF (Virtual Ideal Functionality Framework) brings easy and efficient
SMPC (Secure Multiparty Computation) to Python. See: http://viff.dk/.


More information about the Mercurial-devel mailing list