diff --git a/comtypes/__init__.py b/comtypes/__init__.py index 78b69d8ad..e508dd413 100644 --- a/comtypes/__init__.py +++ b/comtypes/__init__.py @@ -38,9 +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 -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 +166,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 @@ -193,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/_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/_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/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..75bc6b0b0 100644 --- a/comtypes/_post_coinit/unknwn.py +++ b/comtypes/_post_coinit/unknwn.py @@ -8,12 +8,11 @@ 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 comtypes._post_coinit.instancemethod import instancemethod logger = logging.getLogger(__name__)