annotate src/testdir/test_makeencoding.py @ 26419:63949665868b

Added tag v8.2.3740 for changeset 9a1b96ae26d1e2fa850188a13e5777a0909b452a
author Bram Moolenaar <Bram@vim.org>
date Sun, 05 Dec 2021 12:45:05 +0100
parents e71d3bdf3bc3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11063
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1 #!/usr/bin/python
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
3
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
4 # Test program for :make, :grep and :cgetfile.
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
5
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
6 from __future__ import print_function, unicode_literals
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
7 import locale
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
8 import io
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
9 import sys
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
10
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
11 def set_output_encoding(enc=None):
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
12 """Set the encoding of stdout and stderr
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
13
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
14 arguments:
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
15 enc -- Encoding name.
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
16 If omitted, locale.getpreferredencoding() is used.
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
17 """
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
18 if enc is None:
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
19 enc = locale.getpreferredencoding()
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
20
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
21 def get_text_writer(fo, **kwargs):
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
22 kw = dict(kwargs)
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
23 kw.setdefault('errors', 'backslashreplace') # use \uXXXX style
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
24 kw.setdefault('closefd', False)
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
25
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
26 if sys.version_info[0] < 3:
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
27 # Work around for Python 2.x
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
28 # New line conversion isn't needed here. Done in somewhere else.
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
29 writer = io.open(fo.fileno(), mode='w', newline='', **kw)
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
30 write = writer.write # save the original write() function
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
31 enc = locale.getpreferredencoding()
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
32 def convwrite(s):
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
33 if isinstance(s, bytes):
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
34 write(s.decode(enc)) # convert to unistr
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
35 else:
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
36 write(s)
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
37 try:
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
38 writer.flush() # needed on Windows
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
39 except IOError:
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
40 pass
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
41 writer.write = convwrite
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
42 else:
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
43 writer = io.open(fo.fileno(), mode='w', **kw)
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
44 return writer
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
45
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
46 sys.stdout = get_text_writer(sys.stdout, encoding=enc)
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
47 sys.stderr = get_text_writer(sys.stderr, encoding=enc)
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
48
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
49
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
50 def main():
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
51 enc = 'utf-8'
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
52 if len(sys.argv) > 1:
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
53 enc = sys.argv[1]
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
54 set_output_encoding(enc)
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
55
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
56 message_tbl = {
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
57 'utf-8': 'ÀÈÌÒÙ こんにちは 你好',
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
58 'latin1': 'ÀÈÌÒÙ',
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
59 'cp932': 'こんにちは',
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
60 'cp936': '你好',
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
61 }
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
62
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
63 print('Xfoobar.c(10) : %s (%s)' % (message_tbl[enc], enc))
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
64
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
65
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
66 if __name__ == "__main__":
e71d3bdf3bc3 patch 8.0.0420: text garbled when the system encoding differs from 'encoding'
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
67 main()