robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

dundargoctest: move oldtests to test directory (#22536)af23d17388

master
1.9 KiB69 linesraw
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4# Test program for :make, :grep and :cgetfile.
5
6from __future__ import print_function, unicode_literals
7import locale
8import io
9import sys
10
11
12def set_output_encoding(enc=None):
13    """Set the encoding of stdout and stderr
14
15    arguments:
16      enc -- Encoding name.
17             If omitted, locale.getpreferredencoding() is used.
18    """
19    if enc is None:
20        enc = locale.getpreferredencoding()
21
22    def get_text_writer(fo, **kwargs):
23        kw = dict(kwargs)
24        kw.setdefault('errors', 'backslashreplace')  # use \uXXXX style
25        kw.setdefault('closefd', False)
26
27        if sys.version_info[0] < 3:
28            # Work around for Python 2.x
29            # New line conversion isn't needed here. Done in somewhere else.
30            writer = io.open(fo.fileno(), mode='w', newline='', **kw)
31            write = writer.write    # save the original write() function
32            enc = locale.getpreferredencoding()
33
34            def convwrite(s):
35                if isinstance(s, bytes):
36                    write(s.decode(enc))    # convert to unistr
37                else:
38                    write(s)
39                try:
40                    writer.flush()  # needed on Windows
41                except IOError:
42                    pass
43            writer.write = convwrite
44        else:
45            writer = io.open(fo.fileno(), mode='w', **kw)
46        return writer
47
48    sys.stdout = get_text_writer(sys.stdout, encoding=enc)
49    sys.stderr = get_text_writer(sys.stderr, encoding=enc)
50
51
52def main():
53    enc = 'utf-8'
54    if len(sys.argv) > 1:
55        enc = sys.argv[1]
56    set_output_encoding(enc)
57
58    message_tbl = {
59            'utf-8': 'ÀÈÌÒÙ こんにちは 你好',
60            'latin1': 'ÀÈÌÒÙ',
61            'cp932': 'こんにちは',
62            'cp936': '你好',
63            }
64
65    print('Xfoobar.c(10) : %s (%s)' % (message_tbl[enc], enc))
66
67
68if __name__ == "__main__":
69    main()