[pywin32-checkins] /hgrepo/p/py/pywin32/pywin32: 3 new changesets
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
From: <pyw...@li...> - 2011-09-08 02:07:25
|
changeset c45701ca8b4a in /hgrepo/p/py/pywin32/pywin32 details: http://pywin32.hg.sourceforge.net/hgweb/pywin32/pywin32/hgrepo/p/py/pywin32/pywin32?cmd=changeset;node=c45701ca8b4a summary: add simple test for win32com.client.CastTo changeset 47f22d76cbf9 in /hgrepo/p/py/pywin32/pywin32 details: http://pywin32.hg.sourceforge.net/hgweb/pywin32/pywin32/hgrepo/p/py/pywin32/pywin32?cmd=changeset;node=47f22d76cbf9 summary: fix some 64bit issues in win32com.shell changeset c324bf81d7b9 in /hgrepo/p/py/pywin32/pywin32 details: http://pywin32.hg.sourceforge.net/hgweb/pywin32/pywin32/hgrepo/p/py/pywin32/pywin32?cmd=changeset;node=c324bf81d7b9 summary: fix various structure size issues on 64bits diffstat: com/win32com/test/testPyComTest.py | 12 ++- com/win32comext/shell/src/PyIShellBrowser.cpp | 4 +- com/win32comext/shell/src/PyIShellFolder.cpp | 8 +- com/win32comext/shell/src/PyIShellView.cpp | 2 +- win32/Demos/win32gui_dialog.py | 10 +- win32/Lib/win32gui_struct.py | 115 ++++++++++++++++--------- 6 files changed, 96 insertions(+), 55 deletions(-) diffs (truncated from 397 to 300 lines): diff -r 1452829d69ec -r c324bf81d7b9 com/win32com/test/testPyComTest.py --- a/com/win32com/test/testPyComTest.py Thu Aug 25 14:48:47 2011 -0400 +++ b/com/win32com/test/testPyComTest.py Thu Sep 08 12:05:33 2011 +1000 @@ -4,10 +4,11 @@ import sys; sys.coinit_flags=0 # Must be free-threaded! import win32api, pythoncom, time import pywintypes -import os, win32com, win32com.client.connect +import os +import win32com +import win32com.client.connect from win32com.test.util import CheckClean -from win32com.client import constants, DispatchBaseClass -import win32com +from win32com.client import constants, DispatchBaseClass, CastTo from win32com.test.util import RegisterPythonServer from pywin32_testutil import str2memory import datetime @@ -421,6 +422,11 @@ if o.ParamProp(0) != 1: raise RuntimeError(o.paramProp(0)) + # Make sure CastTo works - even though it is only casting it to itself! + o2 = CastTo(o, "IPyCOMTest") + if o != o2: + raise error("CastTo should have returned the same object") + # Do the connection point thing... # Create a connection object. progress("Testing connection points") diff -r 1452829d69ec -r c324bf81d7b9 com/win32comext/shell/src/PyIShellBrowser.cpp --- a/com/win32comext/shell/src/PyIShellBrowser.cpp Thu Aug 25 14:48:47 2011 -0400 +++ b/com/win32comext/shell/src/PyIShellBrowser.cpp Thu Sep 08 12:05:33 2011 +1000 @@ -519,9 +519,9 @@ { PY_GATEWAY_METHOD; PyObject *result; - HRESULT hr=InvokeViaPolicy("SendControlMsg", &result, "iiil", id, uMsg, wParam, lParam); + HRESULT hr=InvokeViaPolicy("SendControlMsg", &result, "iiNN", id, uMsg, PyWinObject_FromPARAM(wParam), PyWinObject_FromPARAM(lParam)); if (PyInt_Check(result) || PyLong_Check(result)) - *pret = PyInt_AsLong(result); + PyWinLong_AsULONG_PTR(result, (ULONG_PTR *)pret); Py_DECREF(result); return hr; } diff -r 1452829d69ec -r c324bf81d7b9 com/win32comext/shell/src/PyIShellFolder.cpp --- a/com/win32comext/shell/src/PyIShellFolder.cpp Thu Aug 25 14:48:47 2011 -0400 +++ b/com/win32comext/shell/src/PyIShellFolder.cpp Thu Sep 08 12:05:33 2011 +1000 @@ -201,10 +201,14 @@ PyObject *obpidl1; PyObject *obpidl2; PyObject *ret=NULL; - long lparam; + PyObject *oblparam; + LPARAM lparam; ITEMIDLIST *pidl1=NULL; ITEMIDLIST *pidl2=NULL; - if ( !PyArg_ParseTuple(args, "kOO:CompareIDs", &lparam, &obpidl1, &obpidl2) ) + if ( !PyArg_ParseTuple(args, "OOO:CompareIDs", &oblparam, &obpidl1, &obpidl2) ) + return NULL; + + if (!PyWinObject_AsPARAM(oblparam, &lparam)) return NULL; if (PyObject_AsPIDL(obpidl1, &pidl1) && diff -r 1452829d69ec -r c324bf81d7b9 com/win32comext/shell/src/PyIShellView.cpp --- a/com/win32comext/shell/src/PyIShellView.cpp Thu Aug 25 14:48:47 2011 -0400 +++ b/com/win32comext/shell/src/PyIShellView.cpp Thu Sep 08 12:05:33 2011 +1000 @@ -428,7 +428,7 @@ PyObject *obpfn = Py_None; Py_INCREF(Py_None); if (obpfn==NULL) return MAKE_PYCOM_GATEWAY_FAILURE_CODE("AddPropertySheetPages"); - HRESULT hr=InvokeViaPolicy("AddPropertySheetPages", NULL, "lOl", dwReserved, obpfn, lparam); + HRESULT hr=InvokeViaPolicy("AddPropertySheetPages", NULL, "lON", dwReserved, obpfn, PyWinObject_FromPARAM(lparam)); Py_DECREF(obpfn); return hr; } diff -r 1452829d69ec -r c324bf81d7b9 win32/Demos/win32gui_dialog.py --- a/win32/Demos/win32gui_dialog.py Thu Aug 25 14:48:47 2011 -0400 +++ b/win32/Demos/win32gui_dialog.py Thu Sep 08 12:05:33 2011 +1000 @@ -19,6 +19,7 @@ import win32gui else: import winxpgui as win32gui +import win32gui_struct import win32api import win32con, winerror import struct, array @@ -300,12 +301,9 @@ print "OnSearchFinished" def OnNotify(self, hwnd, msg, wparam, lparam): - format = "PPiiiiiiiiP" - buf = win32gui.PyMakeBuffer(struct.calcsize(format), lparam) - hwndFrom, idFrom, code, iItem, iSubItem, uNewState, uOldState, uChanged, actionx, actiony, lParam \ - = struct.unpack(format, buf) - if code == commctrl.NM_DBLCLK: - print "Double click on item", iItem+1 + info = win32gui_struct.UnpackNMITEMACTIVATE(lparam) + if info.code == commctrl.NM_DBLCLK: + print "Double click on item", info.iItem+1 return 1 def OnCommand(self, hwnd, msg, wparam, lparam): diff -r 1452829d69ec -r c324bf81d7b9 win32/Lib/win32gui_struct.py --- a/win32/Lib/win32gui_struct.py Thu Aug 25 14:48:47 2011 -0400 +++ b/win32/Lib/win32gui_struct.py Thu Sep 08 12:05:33 2011 +1000 @@ -35,6 +35,19 @@ import commctrl import pywintypes +is64bit = "64 bit" in sys.version + +try: + from collections import namedtuple + def _MakeResult(names_str, values): + names = names_str.split() + nt = namedtuple(names[0], names[1:]) + return nt(*values) +except ImportError: + # no namedtuple support - just return the values as a normal tuple. + def _MakeResult(names_str, values): + return values + # 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 @@ -73,11 +86,21 @@ # Generic WM_NOTIFY unpacking def UnpackWMNOTIFY(lparam): - format = "iii" + format = "PPi" buf = win32gui.PyGetMemory(lparam, struct.calcsize(format)) - hwndFrom, idFrom, code = struct.unpack(format, buf) - return hwndFrom, idFrom, code - + return _MakeResult("WMNOTIFY hwndFrom idFrom code", struct.unpack(format, buf)) + +def UnpackNMITEMACTIVATE(lparam): + if is64bit: + # the struct module doesn't handle this correctly as some of the items + # are actually structs in structs, which get individually aligned. + format = "PPixxxxiiiiiiixxxxP" + else: + format = "PPiiiiiiiiP" + buf = win32gui.PyMakeBuffer(struct.calcsize(format), lparam) + return _MakeResult("NMITEMACTIVATE hwndFrom idFrom code iItem iSubItem uNewState uOldState uChanged actionx actiony lParam", + struct.unpack(format, buf)) + # MENUITEMINFO struct # http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/Resources/Menus/MenuReference/MenuStructures/MENUITEMINFO.asp # We use the struct module to pack and unpack strings as MENUITEMINFO @@ -180,8 +203,10 @@ text = win32gui.PyGetString(lptext, cch) else: text = None - return fType, fState, wID, hSubMenu, hbmpChecked, hbmpUnchecked, \ - dwItemData, text, hbmpItem + return _MakeResult("MENUITEMINFO fType fState wID hSubMenu hbmpChecked " + "hbmpUnchecked dwItemData text hbmpItem", + (fType, fState, wID, hSubMenu, hbmpChecked, hbmpUnchecked, \ + dwItemData, text, hbmpItem)) def EmptyMENUITEMINFO(mask = None, text_buf_size=512): # text_buf_size is number of *characters* - not necessarily no of bytes. @@ -261,7 +286,8 @@ if fMask & win32con.MIM_BACKGROUND==0: hbrBack = None if fMask & win32con.MIM_HELPID==0: dwContextHelpID = None if fMask & win32con.MIM_MENUDATA==0: dwMenuData = None - return dwStyle, cyMax, hbrBack, dwContextHelpID, dwMenuData + return _MakeResult("MENUINFO dwStyle cyMax hbrBack dwContextHelpID dwMenuData", + (dwStyle, cyMax, hbrBack, dwContextHelpID, dwMenuData)) def EmptyMENUINFO(mask = None): if mask is None: @@ -291,6 +317,7 @@ # XXX - from the SpamBayes project. It may not quite work correctly yet - I # XXX - intend checking them later - but having them is better than not at all! +_tvitem_fmt = "iPiiPiiiiP" # Helpers for the ugly win32 structure packing/unpacking # XXX - Note that functions using _GetMaskAndVal run 3x faster if they are # 'inlined' into the function - see PackLVITEM. If the profiler points at @@ -307,7 +334,7 @@ def PackTVINSERTSTRUCT(parent, insertAfter, tvitem): tvitem_buf, extra = PackTVITEM(*tvitem) tvitem_buf = tvitem_buf.tostring() - format = "ii%ds" % len(tvitem_buf) + format = "PP%ds" % len(tvitem_buf) return struct.pack(format, parent, insertAfter, tvitem_buf), extra def PackTVITEM(hitem, state, stateMask, text, image, selimage, citems, param): @@ -329,8 +356,7 @@ text_len = len(text) extra.append(text_buffer) text_addr, _ = text_buffer.buffer_info() - format = "iiiiiiiiii" - buf = struct.pack(format, + buf = struct.pack(_tvitem_fmt, mask, hitem, state, stateMask, text_addr, text_len, # text @@ -351,8 +377,7 @@ text_addr, _ = text_buffer.buffer_info() else: text_addr = text_buf_size = 0 - format = "iiiiiiiiii" - buf = struct.pack(format, + buf = struct.pack(_tvitem_fmt, mask, hitem, 0, 0, text_addr, text_buf_size, # text @@ -363,7 +388,7 @@ def UnpackTVITEM(buffer): item_mask, item_hItem, item_state, item_stateMask, \ item_textptr, item_cchText, item_image, item_selimage, \ - item_cChildren, item_param = struct.unpack("10i", buffer) + item_cChildren, item_param = struct.unpack(_tvitem_fmt, buffer) # ensure only items listed by the mask are valid (except we assume the # handle is always valid - some notifications (eg, TVN_ENDLABELEDIT) set a # mask that doesn't include the handle, but the docs explicity say it is.) @@ -378,29 +403,34 @@ text = win32gui.PyGetString(item_textptr) else: text = None - return item_hItem, item_state, item_stateMask, \ - text, item_image, item_selimage, \ - item_cChildren, item_param + return _MakeResult("TVITEM item_hItem item_state item_stateMask " + "text item_image item_selimage item_cChildren item_param", + (item_hItem, item_state, item_stateMask, text, + item_image, item_selimage, item_cChildren, item_param)) # Unpack the lparm from a "TVNOTIFY" message def UnpackTVNOTIFY(lparam): - format = "iiii40s40s" + format = "Piii40s40s" buf = win32gui.PyGetMemory(lparam, struct.calcsize(format)) hwndFrom, id, code, action, buf_old, buf_new \ = struct.unpack(format, buf) item_old = UnpackTVITEM(buf_old) item_new = UnpackTVITEM(buf_new) - return hwndFrom, id, code, action, item_old, item_new + return _MakeResult("TVNOTIFY hwndFrom id code action item_old item_new", + (hwndFrom, id, code, action, item_old, item_new)) def UnpackTVDISPINFO(lparam): - format = "iii40s" + format = "Pii40s" buf = win32gui.PyGetMemory(lparam, struct.calcsize(format)) hwndFrom, id, code, buf_item = struct.unpack(format, buf) item = UnpackTVITEM(buf_item) - return hwndFrom, id, code, item + return _MakeResult("TVDISPINFO hwndFrom id code item", + (hwndFrom, id, code, item)) # # List view items +_lvitem_fmt = "iiiiiPiiPi" + def PackLVITEM(item=None, subItem=None, state=None, stateMask=None, text=None, image=None, param=None, indent=None): extra = [] # objects we must keep references to mask = 0 @@ -429,8 +459,7 @@ text_len = len(text) extra.append(text_buffer) text_addr, _ = text_buffer.buffer_info() - format = "iiiiiiiiii" - buf = struct.pack(format, + buf = struct.pack(_lvitem_fmt, mask, item, subItem, state, stateMask, text_addr, text_len, # text @@ -441,7 +470,7 @@ item_mask, item_item, item_subItem, \ item_state, item_stateMask, \ item_textptr, item_cchText, item_image, \ - item_param, item_indent = struct.unpack("10i", buffer) + item_param, item_indent = struct.unpack(_lvitem_fmt, buffer) # ensure only items listed by the mask are valid if not (item_mask & commctrl.LVIF_TEXT): item_textptr = item_cchText = None if not (item_mask & commctrl.LVIF_IMAGE): item_image = None @@ -453,24 +482,29 @@ text = win32gui.PyGetString(item_textptr) else: text = None - return item_item, item_subItem, item_state, item_stateMask, \ - text, item_image, item_param, item_indent + return _MakeResult("LVITEM item_item item_subItem item_state " + "item_stateMask text item_image item_param item_indent", + (item_item, item_subItem, item_state, item_stateMask, + text, item_image, item_param, item_indent)) # Unpack an "LVNOTIFY" message def UnpackLVDISPINFO(lparam): - format = "iii40s" + format = "Pii40s" buf = win32gui.PyGetMemory(lparam, struct.calcsize(format)) hwndFrom, id, code, buf_item = struct.unpack(format, buf) |