Skip to content
20 changes: 8 additions & 12 deletions comtypes/_comobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@
################################################################
# COM object implementation

# so we don't have to import comtypes.automation
DISPATCH_METHOD = 1
DISPATCH_PROPERTYGET = 2
DISPATCH_PROPERTYPUT = 4
DISPATCH_PROPERTYPUTREF = 8
from comtypes.automation import ( # noqa
DISPATCH_METHOD,
DISPATCH_PROPERTYGET,
DISPATCH_PROPERTYPUT,
DISPATCH_PROPERTYPUTREF,
)

################################################################

Expand Down Expand Up @@ -515,10 +516,7 @@ def IDispatch_Invoke(
#
params = pDispParams[0]

if wFlags & (4 | 8):
# DISPATCH_PROPERTYPUT
# DISPATCH_PROPERTYPUTREF
#
if wFlags & (DISPATCH_PROPERTYPUT | DISPATCH_PROPERTYPUTREF):
# How are the parameters unpacked for propertyput
# operations with additional parameters? Can propput
# have additional args?
Expand All @@ -529,9 +527,7 @@ def IDispatch_Invoke(
# DISPATCH_PROPERTYPUTREF is specified.
return mth(this, *args)

else:
# DISPATCH_METHOD
# DISPATCH_PROPERTYGET
else: # wFlags & (DISPATCH_METHOD | DISPATCH_PROPERTYGET)
# the positions of named arguments
#
# 2to3 has problems to translate 'range(...)[::-1]'
Expand Down
52 changes: 30 additions & 22 deletions comtypes/_memberspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,27 @@
_OptionalArgSpecElmType = Tuple[List[str], Type[_CData], str, Any]
_ArgSpecElmType = _UnionT[_PositionalArgSpecElmType, _OptionalArgSpecElmType]

# so we don't have to import comtypes.automation to avoid a circular import.
DISPATCH_METHOD = 1
DISPATCH_PROPERTYGET = 2
DISPATCH_PROPERTYPUT = 4
DISPATCH_PROPERTYPUTREF = 8

PARAMFLAG_NONE = 0
PARAMFLAG_FIN = 1
PARAMFLAG_FOUT = 2
PARAMFLAG_FLCID = 4
PARAMFLAG_FRETVAL = 8
PARAMFLAG_FOPT = 16
PARAMFLAG_FHASDEFAULT = 32
PARAMFLAG_FHASCUSTDATA = 64

_PARAMFLAGS = {
"in": 1,
"out": 2,
"lcid": 4,
"retval": 8,
"optional": 16,
"in": PARAMFLAG_FIN,
"out": PARAMFLAG_FOUT,
"lcid": PARAMFLAG_FLCID,
"retval": PARAMFLAG_FRETVAL,
"optional": PARAMFLAG_FOPT,
}


Expand Down Expand Up @@ -231,8 +245,8 @@ def call_with_inout(self, *args, **kw):
# through the keyword arguments.
for i, info in enumerate(paramflags):
direction = info[0]
dir_in = direction & 1 == 1
dir_out = direction & 2 == 2
dir_in = bool(direction & PARAMFLAG_FIN)
dir_out = bool(direction & PARAMFLAG_FOUT)
is_positional = param_index < len(args)
if not (dir_in or dir_out):
# The original code here did not check for this special case and
Expand Down Expand Up @@ -510,15 +524,15 @@ def _make_disp_property(self, m: _DispMemberSpec) -> property:
memid = m.memid

def fget(obj):
return obj.Invoke(memid, _invkind=2) # DISPATCH_PROPERTYGET
return obj.Invoke(memid, _invkind=DISPATCH_PROPERTYGET)

if "readonly" in m.idlflags:
return property(fget)

def fset(obj, value):
# Detect whether to use DISPATCH_PROPERTYPUT or
# DISPATCH_PROPERTYPUTREF
invkind = 8 if comtypes._is_object(value) else 4
# Detect whether to use PUT or PUTREF
is_ref = comtypes._is_object(value)
invkind = DISPATCH_PROPERTYPUTREF if is_ref else DISPATCH_PROPERTYPUT
return obj.Invoke(memid, value, _invkind=invkind)

return property(fget, fset)
Expand All @@ -529,25 +543,19 @@ def _make_disp_method(self, m: _DispMemberSpec) -> Callable[..., Any]:
if "propget" in m.idlflags:

def getfunc(obj, *args, **kw):
return obj.Invoke(
memid, _invkind=2, *args, **kw
) # DISPATCH_PROPERTYGET
return obj.Invoke(memid, _invkind=DISPATCH_PROPERTYGET, *args, **kw)

return getfunc
elif "propput" in m.idlflags:

def putfunc(obj, *args, **kw):
return obj.Invoke(
memid, _invkind=4, *args, **kw
) # DISPATCH_PROPERTYPUT
return obj.Invoke(memid, _invkind=DISPATCH_PROPERTYPUT, *args, **kw)

return putfunc
elif "propputref" in m.idlflags:

def putreffunc(obj, *args, **kw):
return obj.Invoke(
memid, _invkind=8, *args, **kw
) # DISPATCH_PROPERTYPUTREF
return obj.Invoke(memid, _invkind=DISPATCH_PROPERTYPUTREF, *args, **kw)

return putreffunc
# a first attempt to make use of the restype. Still, support for
Expand All @@ -556,15 +564,15 @@ def putreffunc(obj, *args, **kw):
interface = m.restype.__com_interface__ # type: ignore

def comitffunc(obj, *args, **kw):
result = obj.Invoke(memid, _invkind=1, *args, **kw)
result = obj.Invoke(memid, _invkind=DISPATCH_METHOD, *args, **kw)
if result is None:
return
return result.QueryInterface(interface)

return comitffunc

def func(obj, *args, **kw):
return obj.Invoke(memid, _invkind=1, *args, **kw) # DISPATCH_METHOD
return obj.Invoke(memid, _invkind=DISPATCH_METHOD, *args, **kw)

return func

Expand Down
14 changes: 6 additions & 8 deletions comtypes/_vtbl.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@
_warning = logger.warning
_error = logger.error

################################################################
# COM object implementation

# so we don't have to import comtypes.automation
DISPATCH_METHOD = 1
DISPATCH_PROPERTYGET = 2
DISPATCH_PROPERTYPUT = 4
DISPATCH_PROPERTYPUTREF = 8
from comtypes.automation import (
DISPATCH_METHOD,
DISPATCH_PROPERTYGET,
DISPATCH_PROPERTYPUT,
DISPATCH_PROPERTYPUTREF,
)


class E_NotImplemented(Exception):
Expand Down
18 changes: 10 additions & 8 deletions comtypes/typeinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,16 @@
VARFLAG_FIMMEDIATEBIND = 4096
VARFLAGS = tagVARFLAGS

PARAMFLAG_NONE = 0
PARAMFLAG_FIN = 1
PARAMFLAG_FOUT = 2
PARAMFLAG_FLCID = 4
PARAMFLAG_FRETVAL = 8
PARAMFLAG_FOPT = 16
PARAMFLAG_FHASDEFAULT = 32
PARAMFLAG_FHASCUSTDATA = 64
from comtypes._memberspec import ( # noqa
PARAMFLAG_NONE,
PARAMFLAG_FIN,
PARAMFLAG_FOUT,
PARAMFLAG_FLCID,
PARAMFLAG_FRETVAL,
PARAMFLAG_FOPT,
PARAMFLAG_FHASDEFAULT,
PARAMFLAG_FHASCUSTDATA,
)

################################################################
# a helper
Expand Down