Back to index

python3.2  3.2.2
Functions
genwincodec Namespace Reference

Functions

def genwinmap
def genwincodec

Detailed Description

This script generates a Python codec module from a Windows Code Page.

It uses the function MultiByteToWideChar to generate a decoding table.

Function Documentation

def genwincodec.genwincodec (   codepage)

Definition at line 43 of file genwincodec.py.

00043 
00044 def genwincodec(codepage):
00045     import platform
00046     map = genwinmap(codepage)
00047     encodingname = 'cp%d' % codepage
00048     code = codegen("", map, encodingname)
00049     # Replace first lines with our own docstring
00050     code = '''\
00051 """Python Character Mapping Codec %s generated on Windows:
00052 %s with the command:
00053   python Tools/unicode/genwincodec.py %s
00054 """#"
00055 ''' % (encodingname, ' '.join(platform.win32_ver()), codepage
00056       ) + code.split('"""#"', 1)[1]
00057 
00058     print(code)

Here is the call graph for this function:

def genwincodec.genwinmap (   codepage)

Definition at line 11 of file genwincodec.py.

00011 
00012 def genwinmap(codepage):
00013     MultiByteToWideChar = ctypes.windll.kernel32.MultiByteToWideChar
00014     MultiByteToWideChar.argtypes = [wintypes.UINT, wintypes.DWORD,
00015                                     wintypes.LPCSTR, ctypes.c_int,
00016                                     wintypes.LPWSTR, ctypes.c_int]
00017     MultiByteToWideChar.restype = ctypes.c_int
00018 
00019     enc2uni = {}
00020 
00021     for i in list(range(32)) + [127]:
00022         enc2uni[i] = (i, 'CONTROL CHARACTER')
00023 
00024     for i in range(256):
00025         buf = ctypes.create_unicode_buffer(2)
00026         ret = MultiByteToWideChar(
00027             codepage, 0,
00028             bytes([i]), 1,
00029             buf, 2)
00030         assert ret == 1, "invalid code page"
00031         assert buf[1] == '\x00'
00032         try:
00033             name = unicodedata.name(buf[0])
00034         except ValueError:
00035             try:
00036                 name = enc2uni[i][1]
00037             except KeyError:
00038                 name = ''
00039 
00040         enc2uni[i] = (ord(buf[0]), name)
00041 
00042     return enc2uni

Here is the call graph for this function:

Here is the caller graph for this function: