ยปCore Development>Code coverage>Tools/unicode/genwincodec.py

Python code coverage for Tools/unicode/genwincodec.py

#countcontent
1n/a"""This script generates a Python codec module from a Windows Code Page.
2n/a
3n/aIt uses the function MultiByteToWideChar to generate a decoding table.
4n/a"""
5n/a
6n/aimport ctypes
7n/afrom ctypes import wintypes
8n/afrom gencodec import codegen
9n/aimport unicodedata
10n/a
11n/adef genwinmap(codepage):
12n/a MultiByteToWideChar = ctypes.windll.kernel32.MultiByteToWideChar
13n/a MultiByteToWideChar.argtypes = [wintypes.UINT, wintypes.DWORD,
14n/a wintypes.LPCSTR, ctypes.c_int,
15n/a wintypes.LPWSTR, ctypes.c_int]
16n/a MultiByteToWideChar.restype = ctypes.c_int
17n/a
18n/a enc2uni = {}
19n/a
20n/a for i in list(range(32)) + [127]:
21n/a enc2uni[i] = (i, 'CONTROL CHARACTER')
22n/a
23n/a for i in range(256):
24n/a buf = ctypes.create_unicode_buffer(2)
25n/a ret = MultiByteToWideChar(
26n/a codepage, 0,
27n/a bytes([i]), 1,
28n/a buf, 2)
29n/a assert ret == 1, "invalid code page"
30n/a assert buf[1] == '\x00'
31n/a try:
32n/a name = unicodedata.name(buf[0])
33n/a except ValueError:
34n/a try:
35n/a name = enc2uni[i][1]
36n/a except KeyError:
37n/a name = ''
38n/a
39n/a enc2uni[i] = (ord(buf[0]), name)
40n/a
41n/a return enc2uni
42n/a
43n/adef genwincodec(codepage):
44n/a import platform
45n/a map = genwinmap(codepage)
46n/a encodingname = 'cp%d' % codepage
47n/a code = codegen("", map, encodingname)
48n/a # Replace first lines with our own docstring
49n/a code = '''\
50n/a"""Python Character Mapping Codec %s generated on Windows:
51n/a%s with the command:
52n/a python Tools/unicode/genwincodec.py %s
53n/a"""#"
54n/a''' % (encodingname, ' '.join(platform.win32_ver()), codepage
55n/a ) + code.split('"""#"', 1)[1]
56n/a
57n/a print(code)
58n/a
59n/aif __name__ == '__main__':
60n/a import sys
61n/a genwincodec(int(sys.argv[1]))