Skip to content

Commit

Permalink
Simplify simple functions by using assignments (#2214)
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed May 28, 2024
1 parent 1f5358a commit f1764bf
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 21 deletions.
3 changes: 2 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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__`
Expand Down
5 changes: 1 addition & 4 deletions com/win32com/client/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
12 changes: 3 additions & 9 deletions com/win32com/client/dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"""

import traceback
import types
from types import MethodType

import pythoncom # Needed as code we eval() references it.
import win32com.client
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 5 additions & 7 deletions com/win32comext/axscript/client/scriptdispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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)


Expand Down

0 comments on commit f1764bf

Please sign in to comment.