[pywin32-checkins] pywin32/win32/Lib win32gui_struct.py,1.11,1.12
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
|
From: Mark H. <mha...@us...> - 2009-01-13 22:57:19
|
Update of /cvsroot/pywin32/pywin32/win32/Lib In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv17485/Lib Modified Files: win32gui_struct.py Log Message: Make win32gui_struct py3k friendly by checking if win32gui itself is built with UNICODE defined and acting accordingly. Includes new tests and 64bit fixes. Index: win32gui_struct.py =================================================================== RCS file: /cvsroot/pywin32/pywin32/win32/Lib/win32gui_struct.py,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** win32gui_struct.py 4 May 2008 10:45:54 -0000 1.11 --- win32gui_struct.py 13 Jan 2009 22:57:11 -0000 1.12 *************** *** 35,38 **** --- 35,61 ---- import pywintypes + # Encode a string suitable for passing in a win32gui related structure + # If win32gui is built with UNICODE defined (ie, py3k), then functions + # like InsertMenuItem are actually calling InsertMenuItemW etc, so all + # strings will need to be unicode. + if win32gui.UNICODE: + def _make_text_buffer(text): + # XXX - at this stage win32gui.UNICODE is only True in py3k, + # and in py3k is makes sense to reject bytes. + if not isinstance(text, unicode): + raise TypeError('MENUITEMINFO text must be unicode') + data = (text+'\0').encode("unicode-internal") + return array.array("b", data) + + else: + def _make_text_buffer(text): + if isinstance(text, unicode): + text = text.encode("mbcs") + return array.array("b", text+'\0') + + # make an 'empty' buffer, ready for filling with cch characters. + def _make_empty_text_buffer(cch): + return _make_text_buffer("\0" * cch) + # Generic WM_NOTIFY unpacking def UnpackWMNOTIFY(lparam): *************** *** 48,52 **** # structure to avoid the caller needing to explicitly check validity # (None is used if the mask excludes/should exclude the value) ! menuitem_fmt = '5i5PiP' def PackMENUITEMINFO(fType=None, fState=None, wID=None, hSubMenu=None, --- 71,75 ---- # structure to avoid the caller needing to explicitly check validity # (None is used if the mask excludes/should exclude the value) ! _menuiteminfo_fmt = '5i5PiP' def PackMENUITEMINFO(fType=None, fState=None, wID=None, hSubMenu=None, *************** *** 89,96 **** if text is not None: fMask |= win32con.MIIM_STRING ! if isinstance(text, unicode): ! text = text.encode("mbcs") ! str_buf = array.array("c", text+'\0') ! cch = len(str_buf) # We are taking address of strbuf - it must not die until windows # has finished with our structure. --- 112,117 ---- if text is not None: fMask |= win32con.MIIM_STRING ! str_buf = _make_text_buffer(text) ! cch = len(text) # We are taking address of strbuf - it must not die until windows # has finished with our structure. *************** *** 103,108 **** # 'P' format does not accept PyHANDLE's ! item = struct.pack( ! menuitem_fmt, ! struct.calcsize(menuitem_fmt), # cbSize fMask, fType, --- 124,129 ---- # 'P' format does not accept PyHANDLE's ! item = struct.pack( ! _menuiteminfo_fmt, ! struct.calcsize(_menuiteminfo_fmt), # cbSize fMask, fType, *************** *** 119,123 **** # Now copy the string to a writable buffer, so that the result # could be passed to a 'Get' function ! return array.array("c", item), extras def UnpackMENUITEMINFO(s): --- 140,144 ---- # Now copy the string to a writable buffer, so that the result # could be passed to a 'Get' function ! return array.array("b", item), extras def UnpackMENUITEMINFO(s): *************** *** 133,137 **** lptext, cch, ! hbmpItem) = struct.unpack(menuitem_fmt, s) assert cb==len(s) if fMask & win32con.MIIM_FTYPE==0: fType = None --- 154,158 ---- lptext, cch, ! hbmpItem) = struct.unpack(_menuiteminfo_fmt, s) assert cb==len(s) if fMask & win32con.MIIM_FTYPE==0: fType = None *************** *** 150,153 **** --- 171,175 ---- def EmptyMENUITEMINFO(mask = None, text_buf_size=512): + # text_buf_size is number of *characters* - not necessarily no of bytes. extra = [] if mask is None: *************** *** 159,171 **** if mask & win32con.MIIM_STRING: ! text_buffer = array.array("c", "\0" * text_buf_size) extra.append(text_buffer) ! text_addr, text_len = text_buffer.buffer_info() else: ! text_addr = text_len = 0 buf = struct.pack( ! menuitem_fmt, ! struct.calcsize(menuitem_fmt), # cbSize mask, 0, #fType, --- 181,195 ---- if mask & win32con.MIIM_STRING: ! text_buffer = _make_empty_text_buffer(text_buf_size) extra.append(text_buffer) ! text_addr, _ = text_buffer.buffer_info() else: ! text_addr = text_buf_size = 0 + # Now copy the string to a writable buffer, so that the result + # could be passed to a 'Get' function buf = struct.pack( ! _menuiteminfo_fmt, ! struct.calcsize(_menuiteminfo_fmt), # cbSize mask, 0, #fType, *************** *** 177,187 **** 0, #dwItemData, text_addr, ! text_len, 0, #hbmpItem ) ! return array.array("c", buf), extra # MENUINFO struct ! menuinfo_fmt = '7i' def PackMENUINFO(dwStyle = None, cyMax = None, --- 201,211 ---- 0, #dwItemData, text_addr, ! text_buf_size, 0, #hbmpItem ) ! return array.array("b", buf), extra # MENUINFO struct ! _menuinfo_fmt = 'iiiiPiP' def PackMENUINFO(dwStyle = None, cyMax = None, *************** *** 200,205 **** # Create the struct. item = struct.pack( ! menuinfo_fmt, ! struct.calcsize(menuinfo_fmt), # cbSize fMask, dwStyle, --- 224,229 ---- # Create the struct. item = struct.pack( ! _menuinfo_fmt, ! struct.calcsize(_menuinfo_fmt), # cbSize fMask, dwStyle, *************** *** 208,212 **** dwContextHelpID, dwMenuData) ! return array.array("c", item) def UnpackMENUINFO(s): --- 232,236 ---- dwContextHelpID, dwMenuData) ! return array.array("b", item) def UnpackMENUINFO(s): *************** *** 217,221 **** hbrBack, dwContextHelpID, ! dwMenuData) = struct.unpack(menuinfo_fmt, s) assert cb==len(s) if fMask & win32con.MIM_STYLE==0: dwStyle = None --- 241,245 ---- hbrBack, dwContextHelpID, ! dwMenuData) = struct.unpack(_menuinfo_fmt, s) assert cb==len(s) if fMask & win32con.MIM_STYLE==0: dwStyle = None *************** *** 233,238 **** buf = struct.pack( ! menuinfo_fmt, ! struct.calcsize(menuinfo_fmt), # cbSize mask, 0, #dwStyle --- 257,262 ---- buf = struct.pack( ! _menuinfo_fmt, ! struct.calcsize(_menuinfo_fmt), # cbSize mask, 0, #dwStyle *************** *** 242,246 **** 0, #dwMenuData, ) ! return array.array("c", buf) ########################################################################## --- 266,270 ---- 0, #dwMenuData, ) ! return array.array("b", buf) ########################################################################## *************** *** 288,296 **** text_addr = text_len = 0 else: ! if isinstance(text, unicode): ! text = text.encode("mbcs") ! text_buffer = array.array("c", text+"\0") extra.append(text_buffer) ! text_addr, text_len = text_buffer.buffer_info() format = "iiiiiiiiii" buf = struct.pack(format, --- 312,319 ---- text_addr = text_len = 0 else: ! text_buffer = _make_text_buffer(text) ! text_len = len(text) extra.append(text_buffer) ! text_addr, _ = text_buffer.buffer_info() format = "iiiiiiiiii" buf = struct.pack(format, *************** *** 300,304 **** image, selimage, citems, param) ! return array.array("c", buf), extra # Make a new buffer suitable for querying hitem's attributes. --- 323,327 ---- image, selimage, citems, param) ! return array.array("b", buf), extra # Make a new buffer suitable for querying hitem's attributes. *************** *** 310,326 **** commctrl.TVIF_CHILDREN | commctrl.TVIF_PARAM if mask & commctrl.TVIF_TEXT: ! text_buffer = array.array("c", "\0" * text_buf_size) extra.append(text_buffer) ! text_addr, text_len = text_buffer.buffer_info() else: ! text_addr = text_len = 0 format = "iiiiiiiiii" buf = struct.pack(format, mask, hitem, 0, 0, ! text_addr, text_len, # text 0, 0, 0, 0) ! return array.array("c", buf), extra def UnpackTVITEM(buffer): --- 333,349 ---- commctrl.TVIF_CHILDREN | commctrl.TVIF_PARAM if mask & commctrl.TVIF_TEXT: ! text_buffer = _make_empty_text_buffer(text_buf_size) extra.append(text_buffer) ! text_addr, _ = text_buffer.buffer_info() else: ! text_addr = text_buf_size = 0 format = "iiiiiiiiii" buf = struct.pack(format, mask, hitem, 0, 0, ! text_addr, text_buf_size, # text 0, 0, 0, 0) ! return array.array("b", buf), extra def UnpackTVITEM(buffer): *************** *** 389,397 **** else: mask |= commctrl.LVIF_TEXT ! if isinstance(text, unicode): ! text = text.encode("mbcs") ! text_buffer = array.array("c", text+"\0") extra.append(text_buffer) ! text_addr, text_len = text_buffer.buffer_info() format = "iiiiiiiiii" buf = struct.pack(format, --- 412,419 ---- else: mask |= commctrl.LVIF_TEXT ! text_buffer = _make_text_buffer(text) ! text_len = len(text) extra.append(text_buffer) ! text_addr, _ = text_buffer.buffer_info() format = "iiiiiiiiii" buf = struct.pack(format, *************** *** 400,404 **** text_addr, text_len, # text image, param, indent) ! return array.array("c", buf), extra def UnpackLVITEM(buffer): --- 422,426 ---- text_addr, text_len, # text image, param, indent) ! return array.array("b", buf), extra def UnpackLVITEM(buffer): *************** *** 445,460 **** commctrl.LVIF_PARAM | commctrl.LVIF_STATE if mask & commctrl.LVIF_TEXT: ! text_buffer = array.array("c", "\0" * text_buf_size) extra.append(text_buffer) ! text_addr, text_len = text_buffer.buffer_info() else: ! text_addr = text_len = 0 format = "iiiiiiiiii" buf = struct.pack(format, mask, item, subitem, 0, 0, ! text_addr, text_len, # text 0, 0, 0) ! return array.array("c", buf), extra --- 467,482 ---- commctrl.LVIF_PARAM | commctrl.LVIF_STATE if mask & commctrl.LVIF_TEXT: ! text_buffer = _make_empty_text_buffer(text_buf_size) extra.append(text_buffer) ! text_addr, _ = text_buffer.buffer_info() else: ! text_addr = text_buf_size = 0 format = "iiiiiiiiii" buf = struct.pack(format, mask, item, subitem, 0, 0, ! text_addr, text_buf_size, # text 0, 0, 0) ! return array.array("b", buf), extra *************** *** 472,480 **** text_addr = text_len = 0 else: ! if isinstance(text, unicode): ! text = text.encode("mbcs") ! text_buffer = array.array("c", text+"\0") extra.append(text_buffer) ! text_addr, text_len = text_buffer.buffer_info() format = "iiiiiiii" buf = struct.pack(format, --- 494,501 ---- text_addr = text_len = 0 else: ! text_buffer = _make_text_buffer(text) extra.append(text_buffer) ! text_addr, _ = text_buffer.buffer_info() ! text_len = len(text) format = "iiiiiiii" buf = struct.pack(format, *************** *** 482,486 **** text_addr, text_len, # text subItem, image, order) ! return array.array("c", buf), extra def UnpackLVCOLUMN(lparam): --- 503,507 ---- text_addr, text_len, # text subItem, image, order) ! return array.array("b", buf), extra def UnpackLVCOLUMN(lparam): *************** *** 509,523 **** commctrl.LVCF_SUBITEM | commctrl.LVCF_IMAGE | commctrl.LVCF_ORDER if mask & commctrl.LVCF_TEXT: ! text_buffer = array.array("c", "\0" * text_buf_size) extra.append(text_buffer) ! text_addr, text_len = text_buffer.buffer_info() else: ! text_addr = text_len = 0 format = "iiiiiiii" buf = struct.pack(format, mask, 0, 0, ! text_addr, text_len, # text 0, 0, 0) ! return array.array("c", buf), extra # List view hit-test. --- 530,544 ---- commctrl.LVCF_SUBITEM | commctrl.LVCF_IMAGE | commctrl.LVCF_ORDER if mask & commctrl.LVCF_TEXT: ! text_buffer = _make_empty_text_buffer(text_buf_size) extra.append(text_buffer) ! text_addr, _ = text_buffer.buffer_info() else: ! text_addr = text_buf_size = 0 format = "iiiiiiii" buf = struct.pack(format, mask, 0, 0, ! text_addr, text_buf_size, # text 0, 0, 0) ! return array.array("b", buf), extra # List view hit-test. *************** *** 527,531 **** pt[0], pt[1], 0, 0, 0) ! return array.array("c", buf), None def UnpackLVHITTEST(buf): --- 548,552 ---- pt[0], pt[1], 0, 0, 0) ! return array.array("b", buf), None def UnpackLVHITTEST(buf): *************** *** 549,557 **** text_addr = text_len = 0 else: ! if isinstance(text, unicode): ! text = text.encode("mbcs") ! text_buffer = array.array("c", text+"\0") extra.append(text_buffer) ! text_addr, text_len = text_buffer.buffer_info() format = "iiiiiiiiiii" --- 570,577 ---- text_addr = text_len = 0 else: ! text_buffer = _make_text_buffer(text) extra.append(text_buffer) ! text_addr, _ = text_buffer.buffer_info() ! text_len = len(text) format = "iiiiiiiiiii" *************** *** 559,563 **** mask, cxy, text_addr, hbm, text_len, fmt, param, image, order, 0, 0) ! return array.array("c", buf), extra # Device notification stuff --- 579,583 ---- mask, cxy, text_addr, hbm, text_len, fmt, param, image, order, 0, 0) ! return array.array("b", buf), extra # Device notification stuff |