From c4020d74fd2d042cf69f6153b595edf89e3ee8ff Mon Sep 17 00:00:00 2001 From: junkmd Date: Wed, 13 Nov 2024 08:54:26 +0900 Subject: [PATCH 1/2] Integrate `_idl_stuff` into `_memberspec`. `_idl_stuff.py` each line blame: 55c56cc1 3579c214 e1d61858 dbe4fa1b --- comtypes/__init__.py | 15 ++++++- comtypes/_idl_stuff.py | 67 ------------------------------ comtypes/_memberspec.py | 72 +++++++++++++++++++++++++++++++++ comtypes/_post_coinit/misc.py | 2 +- comtypes/_post_coinit/unknwn.py | 3 +- 5 files changed, 87 insertions(+), 72 deletions(-) delete mode 100644 comtypes/_idl_stuff.py diff --git a/comtypes/__init__.py b/comtypes/__init__.py index 78b69d8ad..e3cc7cbed 100644 --- a/comtypes/__init__.py +++ b/comtypes/__init__.py @@ -39,8 +39,6 @@ from comtypes._npsupport import interop as npsupport # noqa from comtypes._tlib_version_checker import _check_version # noqa from comtypes._py_instance_method import instancemethod # noqa -from comtypes._idl_stuff import defaultvalue, helpstring, dispid # noqa -from comtypes._idl_stuff import STDMETHOD, DISPMETHOD, DISPPROPERTY, COMMETHOD # noqa _all_slice = slice(None, None, None) @@ -169,6 +167,19 @@ def CoUninitialize(): com_coclass_registry = {} +################################################################ +# IDL stuff + +from comtypes._memberspec import ( # noqa + COMMETHOD, + DISPMETHOD, + DISPPROPERTY, + STDMETHOD, + defaultvalue, + dispid, + helpstring, +) + ################################################################ # IUnknown, the root of all evil... from comtypes._post_coinit import _shutdown diff --git a/comtypes/_idl_stuff.py b/comtypes/_idl_stuff.py deleted file mode 100644 index 0f734ef41..000000000 --- a/comtypes/_idl_stuff.py +++ /dev/null @@ -1,67 +0,0 @@ -from comtypes._memberspec import _ComMemberSpec, _DispMemberSpec, _resolve_argspec - - -class helpstring(str): - "Specifies the helpstring for a COM method or property." - - -class defaultvalue(object): - "Specifies the default value for parameters marked optional." - - def __init__(self, value): - self.value = value - - -class dispid(int): - "Specifies the DISPID of a method or property." - - -# XXX STDMETHOD, COMMETHOD, DISPMETHOD, and DISPPROPERTY should return -# instances with more methods or properties, and should not behave as an unpackable. - - -def STDMETHOD(restype, name, argtypes=()) -> _ComMemberSpec: - "Specifies a COM method slot without idlflags" - return _ComMemberSpec(restype, name, argtypes, None, (), None) - - -def DISPMETHOD(idlflags, restype, name, *argspec) -> _DispMemberSpec: - "Specifies a method of a dispinterface" - return _DispMemberSpec("DISPMETHOD", name, tuple(idlflags), restype, argspec) - - -def DISPPROPERTY(idlflags, proptype, name) -> _DispMemberSpec: - "Specifies a property of a dispinterface" - return _DispMemberSpec("DISPPROPERTY", name, tuple(idlflags), proptype, ()) - - -# tuple(idlflags) is for the method itself: (dispid, 'readonly') - -# sample generated code: -# DISPPROPERTY([5, 'readonly'], OLE_YSIZE_HIMETRIC, 'Height'), -# DISPMETHOD( -# [6], None, 'Render', ([], c_int, 'hdc'), ([], c_int, 'x'), ([], c_int, 'y') -# ) - - -def COMMETHOD(idlflags, restype, methodname, *argspec) -> _ComMemberSpec: - """Specifies a COM method slot with idlflags. - - XXX should explain the sematics of the arguments. - """ - # collect all helpstring instances - # We should suppress docstrings when Python is started with -OO - # join them together(does this make sense?) and replace by None if empty. - helptext = "".join(t for t in idlflags if isinstance(t, helpstring)) or None - paramflags, argtypes = _resolve_argspec(argspec) - if "propget" in idlflags: - name = "_get_%s" % methodname - elif "propput" in idlflags: - name = "_set_%s" % methodname - elif "propputref" in idlflags: - name = "_setref_%s" % methodname - else: - name = methodname - return _ComMemberSpec( - restype, name, argtypes, paramflags, tuple(idlflags), helptext - ) diff --git a/comtypes/_memberspec.py b/comtypes/_memberspec.py index bb7a795ed..1797c3f2b 100644 --- a/comtypes/_memberspec.py +++ b/comtypes/_memberspec.py @@ -118,6 +118,78 @@ def _is_spec_prop(m: _MemberSpec): return any(f in ("propget", "propput", "propputref") for f in m.idlflags) +################################################################ +# IDL stuff + + +class helpstring(str): + "Specifies the helpstring for a COM method or property." + + +class defaultvalue(object): + "Specifies the default value for parameters marked optional." + + def __init__(self, value): + self.value = value + + +class dispid(int): + "Specifies the DISPID of a method or property." + + +# XXX STDMETHOD, COMMETHOD, DISPMETHOD, and DISPPROPERTY should return +# instances with more methods or properties, and should not behave as an unpackable. + + +def STDMETHOD(restype, name, argtypes=()) -> _ComMemberSpec: + "Specifies a COM method slot without idlflags" + return _ComMemberSpec(restype, name, argtypes, None, (), None) + + +def DISPMETHOD(idlflags, restype, name, *argspec) -> _DispMemberSpec: + "Specifies a method of a dispinterface" + return _DispMemberSpec("DISPMETHOD", name, tuple(idlflags), restype, argspec) + + +def DISPPROPERTY(idlflags, proptype, name) -> _DispMemberSpec: + "Specifies a property of a dispinterface" + return _DispMemberSpec("DISPPROPERTY", name, tuple(idlflags), proptype, ()) + + +# tuple(idlflags) is for the method itself: (dispid, 'readonly') + +# sample generated code: +# DISPPROPERTY([5, 'readonly'], OLE_YSIZE_HIMETRIC, 'Height'), +# DISPMETHOD( +# [6], None, 'Render', ([], c_int, 'hdc'), ([], c_int, 'x'), ([], c_int, 'y') +# ) + + +def COMMETHOD(idlflags, restype, methodname, *argspec) -> _ComMemberSpec: + """Specifies a COM method slot with idlflags. + + XXX should explain the sematics of the arguments. + """ + # collect all helpstring instances + # We should suppress docstrings when Python is started with -OO + # join them together(does this make sense?) and replace by None if empty. + helptext = "".join(t for t in idlflags if isinstance(t, helpstring)) or None + paramflags, argtypes = _resolve_argspec(argspec) + if "propget" in idlflags: + name = "_get_%s" % methodname + elif "propput" in idlflags: + name = "_set_%s" % methodname + elif "propputref" in idlflags: + name = "_setref_%s" % methodname + else: + name = methodname + return _ComMemberSpec( + restype, name, argtypes, paramflags, tuple(idlflags), helptext + ) + + +################################################################ + _PropFunc = Optional[Callable[..., Any]] _DocType = Optional[str] diff --git a/comtypes/_post_coinit/misc.py b/comtypes/_post_coinit/misc.py index 7fa865b32..3db9d7c63 100644 --- a/comtypes/_post_coinit/misc.py +++ b/comtypes/_post_coinit/misc.py @@ -15,7 +15,7 @@ if TYPE_CHECKING: from comtypes import hints as hints # noqa # type: ignore -from comtypes._idl_stuff import COMMETHOD # noqa +from comtypes._memberspec import COMMETHOD from comtypes import CLSCTX_SERVER, CLSCTX_LOCAL_SERVER, CLSCTX_REMOTE_SERVER from comtypes import _ole32, oledll, DWORD from comtypes._post_coinit.unknwn import IUnknown diff --git a/comtypes/_post_coinit/unknwn.py b/comtypes/_post_coinit/unknwn.py index 7ccf9d9a2..12d12479c 100644 --- a/comtypes/_post_coinit/unknwn.py +++ b/comtypes/_post_coinit/unknwn.py @@ -8,9 +8,8 @@ from typing import List, Type from comtypes import _ole32_nohresult, com_interface_registry -from comtypes._idl_stuff import STDMETHOD from comtypes._memberspec import ComMemberGenerator, DispMemberGenerator -from comtypes._memberspec import _ComMemberSpec, _DispMemberSpec +from comtypes._memberspec import STDMETHOD, _ComMemberSpec, _DispMemberSpec from comtypes._post_coinit import _cominterface_meta_patcher as _meta_patch from comtypes._post_coinit.guid import GUID from comtypes._py_instance_method import instancemethod From a4b44fa01fd07597d919901c8d6a1c9d7df56b54 Mon Sep 17 00:00:00 2001 From: junkmd Date: Wed, 13 Nov 2024 08:54:26 +0900 Subject: [PATCH 2/2] Move the module that defines `instancemethod`. --- comtypes/__init__.py | 2 +- .../{_py_instance_method.py => _post_coinit/instancemethod.py} | 0 comtypes/_post_coinit/unknwn.py | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename comtypes/{_py_instance_method.py => _post_coinit/instancemethod.py} (100%) diff --git a/comtypes/__init__.py b/comtypes/__init__.py index e3cc7cbed..e508dd413 100644 --- a/comtypes/__init__.py +++ b/comtypes/__init__.py @@ -38,7 +38,6 @@ from comtypes import patcher # noqa from comtypes._npsupport import interop as npsupport # noqa from comtypes._tlib_version_checker import _check_version # noqa -from comtypes._py_instance_method import instancemethod # noqa _all_slice = slice(None, None, None) @@ -204,6 +203,7 @@ def CoUninitialize(): ################################################################ +from comtypes._post_coinit.instancemethod import instancemethod # noqa from comtypes._post_coinit.misc import ( # noqa _is_object, CoGetObject, diff --git a/comtypes/_py_instance_method.py b/comtypes/_post_coinit/instancemethod.py similarity index 100% rename from comtypes/_py_instance_method.py rename to comtypes/_post_coinit/instancemethod.py diff --git a/comtypes/_post_coinit/unknwn.py b/comtypes/_post_coinit/unknwn.py index 12d12479c..75bc6b0b0 100644 --- a/comtypes/_post_coinit/unknwn.py +++ b/comtypes/_post_coinit/unknwn.py @@ -12,7 +12,7 @@ from comtypes._memberspec import STDMETHOD, _ComMemberSpec, _DispMemberSpec from comtypes._post_coinit import _cominterface_meta_patcher as _meta_patch from comtypes._post_coinit.guid import GUID -from comtypes._py_instance_method import instancemethod +from comtypes._post_coinit.instancemethod import instancemethod logger = logging.getLogger(__name__)