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

remove the outdated bridge for GUID.binary #388

Closed
junkmd opened this issue Nov 28, 2022 · 2 comments · Fixed by #449
Closed

remove the outdated bridge for GUID.binary #388

junkmd opened this issue Nov 28, 2022 · 2 comments · Fixed by #449
Labels
drop_py2 dev based on supporting only Python3, see #392 good first issue Good for newcomers

Comments

@junkmd
Copy link
Collaborator

junkmd commented Nov 28, 2022

THIS IS FOR drop_py2 PLAN! Please see #392.

Since Release 1.0.0, comtypes dropped support for Python 2.5.

But still exists a bridge for GUID.binary.

if sys.version_info >= (2, 6):
def binary(obj):
return bytes(obj)
else:
def binary(obj):
return buffer(obj)

Please fix as follows.

def binary(obj: SupportsBytes) -> bytes:
    return bytes(obj)
@junkmd junkmd added the good first issue Good for newcomers label Nov 28, 2022
@junkmd junkmd added the drop_py2 dev based on supporting only Python3, see #392 label Dec 27, 2022
@junkmd
Copy link
Collaborator Author

junkmd commented Dec 29, 2022

Note for reminder of similar cases.

This *DOES NOT* mean to include them in the scope of this issue.
(This issue scope is only GUID.binary)

memo

No.

before #427

# comtypes version numbers follow semver (http://semver.org/) and PEP 440
__version__ = "1.1.14"

after #427

# comtypes version numbers follow semver (http://semver.org/) and PEP 440
__version__ = "1.1.14"


comtypes/comobject.py

No.1

before #427

elif what == "DISPPROPERTY":
# DISPPROPERTY have implicit "out"
if restype:
argspec += ((['out'], restype, ""),)
self.__make_dispentry(finder, interface,
"_get_" + mthname,
idlflags, argspec,
2 # DISPATCH_PROPERTYGET
)
if not 'readonly' in idlflags:
self.__make_dispentry(finder, interface,
"_set_" + mthname,
idlflags, argspec,
4) # DISPATCH_PROPERTYPUT
# Add DISPATCH_PROPERTYPUTREF also?

after #427

elif what == "DISPPROPERTY":
# DISPPROPERTY have implicit "out"
if restype:
argspec += ((["out"], restype, ""),)
self.__make_dispentry(
finder,
interface,
"_get_" + mthname,
idlflags,
argspec,
2, # DISPATCH_PROPERTYGET
)
if not "readonly" in idlflags:
self.__make_dispentry(
finder, interface, "_set_" + mthname, idlflags, argspec, 4
) # DISPATCH_PROPERTYPUT

comtypes/_memberspec.py

No.1

before #427

for item in (self.restype, self.name, self.argtypes, self.paramflags, self.idlflags, self.doc):
yield item

after #427

for item in (
self.restype,
self.name,
self.argtypes,
self.paramflags,
self.idlflags,
self.doc,
):
yield item

No.2

before #427

self._data = {} # type: Dict[Tuple[str, Optional[str], int], List[Optional[Callable[..., Any]]]]

after #427

self._data = (
{}
) # type: Dict[Tuple[str, Optional[str], int], List[Optional[Callable[..., Any]]]]

No.3

before #427

self._mths = [] # type: List[Tuple[str, Callable[..., Any], Callable[..., Any], bool]]

after #427

self._mths = (
[]
) # type: List[Tuple[str, Callable[..., Any], Callable[..., Any], bool]]

No.4

before #427

self._items = [] # type: List[Tuple[str, _UnionT[Callable[..., Any], property], bool]]

after #427

self._items = (
[]
) # type: List[Tuple[str, _UnionT[Callable[..., Any], property], bool]]

No.5

before #427

def _make_disp_method(self, m):
# type: (_DispMemberSpec) -> Callable[..., Any]
memid = m.memid
if "propget" in m.idlflags:
def getfunc(obj, *args, **kw):
return obj.Invoke(memid, _invkind=2, *args, **kw) # DISPATCH_PROPERTYGET
return getfunc
elif "propput" in m.idlflags:
def putfunc(obj, *args, **kw):
return obj.Invoke(memid, _invkind=4, *args, **kw) # DISPATCH_PROPERTYPUT
return putfunc
elif "propputref" in m.idlflags:
def putreffunc(obj, *args, **kw):
return obj.Invoke(memid, _invkind=8, *args, **kw) # DISPATCH_PROPERTYPUTREF
return putreffunc
# a first attempt to make use of the restype. Still, support for
# named arguments and default argument values should be added.
if hasattr(m.restype, "__com_interface__"):
interface = m.restype.__com_interface__ # type: ignore
def comitffunc(obj, *args, **kw):
result = obj.Invoke(memid, _invkind=1, *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 func

after #427

def _make_disp_method(self, m):
# type: (_DispMemberSpec) -> Callable[..., Any]
memid = m.memid
if "propget" in m.idlflags:
def getfunc(obj, *args, **kw):
return obj.Invoke(
memid, _invkind=2, *args, **kw
) # DISPATCH_PROPERTYGET
return getfunc
elif "propput" in m.idlflags:
def putfunc(obj, *args, **kw):
return obj.Invoke(
memid, _invkind=4, *args, **kw
) # DISPATCH_PROPERTYPUT
return putfunc
elif "propputref" in m.idlflags:
def putreffunc(obj, *args, **kw):
return obj.Invoke(
memid, _invkind=8, *args, **kw
) # DISPATCH_PROPERTYPUTREF
return putreffunc
# a first attempt to make use of the restype. Still, support for
# named arguments and default argument values should be added.
if hasattr(m.restype, "__com_interface__"):
interface = m.restype.__com_interface__ # type: ignore
def comitffunc(obj, *args, **kw):
result = obj.Invoke(memid, _invkind=1, *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 func

comtypes/client/_generate.py

No.1

before #427

with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, r"CLSID\%s\TypeLib" % clsid) as key:
libid = winreg.EnumValue(key, 0)[1]
with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, r"CLSID\%s\Version" % clsid) as key:
ver = winreg.EnumValue(key, 0)[1].split(".")

after #427

with winreg.OpenKey(
winreg.HKEY_CLASSES_ROOT, r"CLSID\%s\TypeLib" % clsid
) as key:
libid = winreg.EnumValue(key, 0)[1]
with winreg.OpenKey(
winreg.HKEY_CLASSES_ROOT, r"CLSID\%s\Version" % clsid
) as key:
ver = winreg.EnumValue(key, 0)[1].split(".")

comtypes/client/dynamic.py

No.1

before #427

self.__dict__["_ids"] = {} # Tiny optimization: trying not to use GetIDsOfNames more than once

after #427

self.__dict__[
"_ids"
] = {} # Tiny optimization: trying not to use GetIDsOfNames more than once

comtypes/test/test_excel.py

No.1

before #427

xl.Range["A1", "C1"].Value[()] = (10,"20",31.4) # XXX: in Python >= 3.8.x, cannot set values to A1:C1

after #427

xl.Range["A1", "C1"].Value[()] = (
10,
"20",
31.4,
) # XXX: in Python >= 3.8.x, cannot set values to A1:C1

related: python/cpython#99952, python/cpython#100169

comtypes/test/test_word.py

No.1

before #427

comtypes.client.GetModule(('{00020905-0000-0000-C000-000000000046}',)) # Word libUUID

after #427

comtypes.client.GetModule(
("{00020905-0000-0000-C000-000000000046}",)
) # Word libUUID

comtypes/tools/codegenerator.py

No.1

before #427

def name_wrapper_module(tlib):
"""Determine the name of a typelib wrapper module"""
libattr = tlib.GetLibAttr()
modname = "_%s_%s_%s_%s" % \
(str(libattr.guid)[1:-1].replace("-", "_"),
libattr.lcid,
libattr.wMajorVerNum,
libattr.wMinorVerNum)
return "comtypes.gen.%s" % modname

after #427

def name_wrapper_module(tlib):
"""Determine the name of a typelib wrapper module"""
libattr = tlib.GetLibAttr()
modname = "_%s_%s_%s_%s" % (
str(libattr.guid)[1:-1].replace("-", "_"),
libattr.lcid,
libattr.wMajorVerNum,
libattr.wMinorVerNum,
)
return "comtypes.gen.%s" % modname

No.2

before #427

def _make_withargs(self):
# type: () -> None
code = (
" COMMETHOD(\n"
" %r,\n"
" %s,\n"
" '%s',"
) % self._get_common_elms()
print(code, file=self._stream)
arglist = [_to_arg_definition(*i) for i in self._iter_args()]
print(",\n".join(arglist), file=self._stream)
print(" ),", file=self._stream)

after #427

def _make_withargs(self):
# type: () -> None
code = (
" COMMETHOD(\n" " %r,\n" " %s,\n" " '%s',"
) % self._get_common_elms()
print(code, file=self._stream)
arglist = [_to_arg_definition(*i) for i in self._iter_args()]
print(",\n".join(arglist), file=self._stream)
print(" ),", file=self._stream)

No.3

before #427

def _make_withargs(self):
# type: () -> None
code = (
" DISPMETHOD(\n"
" %r,\n"
" %s,\n"
" '%s',"
) % self._get_common_elms()
print(code, file=self._stream)
arglist = [_to_arg_definition(*i) for i in self._iter_args()]
print(",\n".join(arglist), file=self._stream)
print(" ),", file=self._stream)

after #427

def _make_withargs(self):
# type: () -> None
code = (
" DISPMETHOD(\n" " %r,\n" " %s,\n" " '%s',"
) % self._get_common_elms()
print(code, file=self._stream)
arglist = [_to_arg_definition(*i) for i in self._iter_args()]
print(",\n".join(arglist), file=self._stream)
print(" ),", file=self._stream)

No.4

before #427

print("################################################################", file=self.stream)

after #427

print(
"################################################################",
file=self.stream,
)

No.5

before #427

class ImportedNamespaces(object):
def __init__(self):
if sys.version_info >= (3, 7):
self.data = {}
else:
from collections import OrderedDict
self.data = OrderedDict()

after #427

class ImportedNamespaces(object):
def __init__(self):
if sys.version_info >= (3, 7):
self.data = {}
else:
from collections import OrderedDict
self.data = OrderedDict()

No.6

before #427

def _make_line(self, from_, imports, for_stub):
if for_stub:
import_ = ", ".join("%s as %s" % (n, n) for n in imports)
else:
import_ = ", ".join(imports)
code = "from %s import %s" % (from_, import_)
if len(code) <= 80:
return code
if for_stub:
import_ = "\n".join(" %s as %s," % (n, n) for n in imports)
else:
wrapper = textwrap.TextWrapper(subsequent_indent=" ",
initial_indent=" ",
break_long_words=False)
import_ = "\n".join(wrapper.wrap(import_))
code = "from %s import (\n%s\n)" % (from_, import_)
return code
def getvalue(self, for_stub=False):
ns = {}
lines = []
for key, val in self.data.items():
if val is None:
ns[key] = val
elif key == "*":
lines.append("from %s import *" % val)
else:
ns.setdefault(val, set()).add(key)
for key, val in ns.items():
if val is None:
lines.append("import %s" % key)
else:
names = sorted(val, key=lambda s: s.lower())
lines.append(self._make_line(key, names, for_stub=for_stub))
return "\n".join(lines)

after #427

def _make_line(self, from_, imports, for_stub):
if for_stub:
import_ = ", ".join("%s as %s" % (n, n) for n in imports)
else:
import_ = ", ".join(imports)
code = "from %s import %s" % (from_, import_)
if len(code) <= 80:
return code
if for_stub:
import_ = "\n".join(" %s as %s," % (n, n) for n in imports)
else:
wrapper = textwrap.TextWrapper(
subsequent_indent=" ", initial_indent=" ", break_long_words=False
)
import_ = "\n".join(wrapper.wrap(import_))
code = "from %s import (\n%s\n)" % (from_, import_)
return code
def getvalue(self, for_stub=False):
ns = {}
lines = []
for key, val in self.data.items():
if val is None:
ns[key] = val
elif key == "*":
lines.append("from %s import *" % val)
else:
ns.setdefault(val, set()).add(key)
for key, val in ns.items():
if val is None:
lines.append("import %s" % key)
else:
names = sorted(val, key=lambda s: s.lower())
lines.append(self._make_line(key, names, for_stub=for_stub))
return "\n".join(lines)

No.7

before #427

class DeclaredNamespaces(object):
def __init__(self):
if sys.version_info >= (3, 7):
self.data = {}
else:
from collections import OrderedDict
self.data = OrderedDict()

after #427

class DeclaredNamespaces(object):
def __init__(self):
if sys.version_info >= (3, 7):
self.data = {}
else:
from collections import OrderedDict
self.data = OrderedDict()

comtypes/tools/tlbparser.py

No.1

before #427

for i in range(ta.cVars):
vd = tinfo.GetVarDesc(i)
name = tinfo.GetDocumentation(vd.memid)[0]
offset = vd._.oInst * 8
assert vd.varkind == typeinfo.VAR_PERINSTANCE
typ = self.make_type(vd.elemdescVar.tdesc, tinfo)
field = typedesc.Field(name,
typ,
None, # bits
offset)
members.append(field)

after #427

" '%s',\n"
" )"
) % elms
return code
class ComMethodGenerator(object):
def __init__(self, m, isdual):

No.2

before #427

for i in range(ta.cVars):
vd = tinfo.GetVarDesc(i)
name = tinfo.GetDocumentation(vd.memid)[0]
offset = vd._.oInst * 8
assert vd.varkind == typeinfo.VAR_PERINSTANCE
typ = self.make_type(vd.elemdescVar.tdesc, tinfo)
field = typedesc.Field(name,
typ,
None, # bits
offset)
members.append(field)

after #427

for i in range(ta.cVars):
vd = tinfo.GetVarDesc(i)
name = tinfo.GetDocumentation(vd.memid)[0]
offset = vd._.oInst * 8
assert vd.varkind == typeinfo.VAR_PERINSTANCE
typ = self.make_type(vd.elemdescVar.tdesc, tinfo)
field = typedesc.Field(name, typ, None, offset) # bits

No.3

before #427

if 0 == windll.oleaut32.QueryPathOfRegTypeLib(byref(la.guid),
la.wMajorVerNum,
la.wMinorVerNum,
0, # lcid
byref(name)
):

after #427

if 0 == windll.oleaut32.QueryPathOfRegTypeLib(
byref(la.guid), la.wMajorVerNum, la.wMinorVerNum, 0, byref(name) # lcid
):

@junkmd
Copy link
Collaborator Author

junkmd commented Jan 4, 2023

I close this because #449 is merged.

@junkmd junkmd closed this as completed Jan 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
drop_py2 dev based on supporting only Python3, see #392 good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant