i18n: calculate terminal columns by width information of each characters
neither number of 'bytes' in any encoding nor 'characters' is
appropriate to calculate terminal columns for specified string.
this patch modifies MBTextWrapper for:
- overriding '_wrap_chunks()' to make it use not built-in 'len()'
but 'encoding.colwidth()' for columns of string
- fixing '_cutdown()' to make it use 'encoding.colwidth()' instead
of local, similar but incorrect implementation
this patch also modifies 'encoding.py':
- dividing 'colwith()' into 2 pieces: one for calculation columns of
specified UNICODE string, and another for rest part of original
one. the former is used from MBTextWrapper in 'util.py'.
- preventing 'colwidth()' from evaluating HGENCODINGAMBIGUOUS
configuration per each invocation: 'unicodedata.east_asian_width'
checking is kept intact for reducing startup cost.
import os
import glob
from mercurial.util import atomictempfile
# basic usage
def test1_simple():
if os.path.exists('foo'):
os.remove('foo')
file = atomictempfile('foo')
(dir, basename) = os.path.split(file._tempname)
assert not os.path.isfile('foo')
assert basename in glob.glob('.foo-*')
file.write('argh\n')
file.rename()
assert os.path.isfile('foo')
assert basename not in glob.glob('.foo-*')
print 'OK'
# close() removes the temp file but does not make the write
# permanent -- essentially discards your work (WTF?!)
def test2_close():
if os.path.exists('foo'):
os.remove('foo')
file = atomictempfile('foo')
(dir, basename) = os.path.split(file._tempname)
file.write('yo\n')
file.close()
assert not os.path.isfile('foo')
assert basename not in os.listdir('.')
print 'OK'
# if a programmer screws up and passes bad args to atomictempfile, they
# get a plain ordinary TypeError, not infinite recursion
def test3_oops():
try:
file = atomictempfile()
except TypeError:
print "OK"
else:
print "expected TypeError"
if __name__ == '__main__':
test1_simple()
test2_close()
test3_oops()