From 2308b3506bfe5330691d790d2ec5a72cf49bceb9 Mon Sep 17 00:00:00 2001 From: Avasam Date: Sat, 16 Mar 2024 22:57:49 -0400 Subject: [PATCH 1/2] Simplify simple functions by using assignments --- com/win32com/client/build.py | 5 +---- com/win32com/client/dynamic.py | 12 +++--------- com/win32comext/axscript/client/scriptdispatch.py | 12 +++++------- 3 files changed, 9 insertions(+), 20 deletions(-) diff --git a/com/win32com/client/build.py b/com/win32com/client/build.py index 8318b8894..72f8e4c3b 100644 --- a/com/win32com/client/build.py +++ b/com/win32com/client/build.py @@ -24,13 +24,10 @@ import winerror from pywintypes import TimeType - # It isn't really clear what the quoting rules are in a C/IDL string and # literals like a quote char and backslashes makes life a little painful to # always render the string perfectly - so just punt and fall-back to a repr() -def _makeDocString(s): - return repr(s) - +_makeDocString = repr error = "PythonCOM.Client.Build error" diff --git a/com/win32com/client/dynamic.py b/com/win32com/client/dynamic.py index 18dd751bb..523fb0dc1 100644 --- a/com/win32com/client/dynamic.py +++ b/com/win32com/client/dynamic.py @@ -17,7 +17,7 @@ """ import traceback -import types +from types import MethodType import pythoncom # Needed as code we eval() references it. import win32com.client @@ -64,16 +64,11 @@ def debug_attr_print(*args): print() -def MakeMethod(func, inst, cls): - return types.MethodType(func, inst) - - # get the type objects for IDispatch and IUnknown PyIDispatchType = pythoncom.TypeIIDs[pythoncom.IID_IDispatch] PyIUnknownType = pythoncom.TypeIIDs[pythoncom.IID_IUnknown] _GoodDispatchTypes = (str, IIDType) -_defaultDispatchItem = build.DispatchItem def _GetGoodDispatch(IDispatch, clsctx=pythoncom.CLSCTX_SERVER): @@ -424,8 +419,7 @@ def _make_method_(self, name): name = methodName # Save the function in map. fn = self._builtMethods_[name] = tempNameSpace[name] - newMeth = MakeMethod(fn, self, self.__class__) - return newMeth + return MethodType(fn, self) except: debug_print("Error building OLE definition for code ", methodCode) traceback.print_exc() @@ -573,7 +567,7 @@ def __call__(self): raise AttributeError(attr) # If a known method, create new instance and return. try: - return MakeMethod(self._builtMethods_[attr], self, self.__class__) + return MethodType(self._builtMethods_[attr], self) except KeyError: pass # XXX - Note that we current are case sensitive in the method. diff --git a/com/win32comext/axscript/client/scriptdispatch.py b/com/win32comext/axscript/client/scriptdispatch.py index 2c15d7436..819158a7c 100644 --- a/com/win32comext/axscript/client/scriptdispatch.py +++ b/com/win32comext/axscript/client/scriptdispatch.py @@ -21,10 +21,8 @@ PyIDispatchType = pythoncom.TypeIIDs[pythoncom.IID_IDispatch] - -def _is_callable(obj): - return isinstance(obj, (types.FunctionType, types.MethodType)) - # ignore hasattr(obj, "__call__") as this means all COM objects! +# ignore hasattr(obj, "__call__") as this means all COM objects! +_CallableTypes = (types.FunctionType, types.MethodType) class ScriptDispatch: @@ -42,7 +40,7 @@ def _dynamic_(self, name, lcid, wFlags, args): # attempt to call a function try: func = getattr(self.scriptNamespace, name) - if not _is_callable(func): + if not isinstance(func, _CallableTypes): raise AttributeError(name) # Not a function. realArgs = [] for arg in args: @@ -60,7 +58,7 @@ def _dynamic_(self, name, lcid, wFlags, args): # attempt to get a property try: ret = getattr(self.scriptNamespace, name) - if _is_callable(ret): + if isinstance(ret, _CallableTypes): raise AttributeError(name) # Not a property. except AttributeError: raise COMException(scode=winerror.DISP_E_MEMBERNOTFOUND) @@ -91,7 +89,7 @@ def _getdispid_(self, name, fdex): func = getattr(self._obj_.scriptNamespace, str(name)) except AttributeError: raise COMException(scode=winerror.DISP_E_MEMBERNOTFOUND) - # if not _is_callable(func): + # if not isinstance(func, _CallableTypes): return win32com.server.policy.DynamicPolicy._getdispid_(self, name, fdex) From bfb6b7f1460a3995a492f177abcefbbeb31fb27e Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 9 May 2024 21:53:58 -0400 Subject: [PATCH 2/2] Add CHANGES --- CHANGES.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGES.txt b/CHANGES.txt index dda19c322..8c60aab6e 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -13,6 +13,7 @@ https://mhammond.github.io/pywin32_installers.html. Coming in build 307, as yet unreleased -------------------------------------- + ### pywin32 * Release GIL when calling CreateService or StartService * Drop support for Internet Explorer 10 (#2229, @Avasam) @@ -84,11 +85,12 @@ Coming in build 307, as yet unreleased * Fixed registering Python as a scripting language for `axscript` * Fixed `isapi` install * Use collection literals and comprehensions where applicable (slight performance improvement) (#2108, @Avasam) -* Cleanup obsolete code for unsupported Python versions (#1990, #2127, #2205, @Avasam) +* Cleanup obsolete code for unsupported Python versions (#1990, #2127, #2205, #2214, @Avasam) The following public names have been removed: * `pywin.framework.app.Win32RawInput` * `win32com.client.makepy.error` * Long obsoleted `dbi` module, use the `odbc` module instead + * `win32com.client.dynamic.MakeMethod` Added support for the following Python 3 methods: * `pywin.mfc.dialog.Dialog.__contains__` * `win32com.client.CoClassBaseClass.__bool__`