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