Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify simple functions by using assignments #2214

Merged
merged 5 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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__`
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)
Comment on lines -67 to -68
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This really should've been removed with 9a11ef0



# 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)
Copy link
Collaborator Author

@Avasam Avasam Mar 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is gonna be beneficial for type-checkers too! (Either that or making this function a TypeGuard)



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
Loading