From f1764bff105d70c8340b57e2ecdd4ed71ebea65f Mon Sep 17 00:00:00 2001 From: Avasam Date: Tue, 28 May 2024 10:19:24 -0400 Subject: [PATCH] Simplify simple functions by using assignments (#2214) --- CHANGES.txt | 3 ++- com/win32com/client/build.py | 5 +---- com/win32com/client/dynamic.py | 12 +++--------- com/win32comext/axscript/client/scriptdispatch.py | 12 +++++------- 4 files changed, 11 insertions(+), 21 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index fda87832c..4f85fb3e8 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -100,11 +100,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__` 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 6dcb0a613..227467e5c 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)