From 589445de5875f6f41bc94b314faf2016aa0e3856 Mon Sep 17 00:00:00 2001 From: junkmd Date: Wed, 4 Jan 2023 20:23:33 +0900 Subject: [PATCH] remove `sys.version_info` bridges --- comtypes/GUID.py | 27 +++------- comtypes/__init__.py | 87 ++++++--------------------------- comtypes/_comobject.py | 12 +---- comtypes/automation.py | 37 +++----------- comtypes/client/_constants.py | 7 +-- comtypes/client/_generate.py | 16 ++---- comtypes/connectionpoints.py | 40 ++++----------- comtypes/errorinfo.py | 7 +-- comtypes/server/inprocserver.py | 5 +- comtypes/server/localserver.py | 6 +-- comtypes/server/register.py | 9 ++-- comtypes/tools/codegenerator.py | 20 ++------ 12 files changed, 56 insertions(+), 217 deletions(-) diff --git a/comtypes/GUID.py b/comtypes/GUID.py index 44b81c3c5..f44875b86 100644 --- a/comtypes/GUID.py +++ b/comtypes/GUID.py @@ -1,23 +1,10 @@ from ctypes import * import sys -if sys.version_info >= (2, 6): - def binary(obj): - return bytes(obj) +def binary(obj): + return bytes(obj) -else: - - def binary(obj): - return buffer(obj) - - -if sys.version_info >= (3, 0): - text_type = str - base_text_type = str -else: - text_type = unicode - base_text_type = basestring BYTE = c_byte WORD = c_ushort @@ -41,10 +28,10 @@ class GUID(Structure): def __init__(self, name=None): if name is not None: - _CLSIDFromString(text_type(name), byref(self)) + _CLSIDFromString(str(name), byref(self)) def __repr__(self): - return 'GUID("%s")' % text_type(self) + return 'GUID("%s")' % str(self) def __unicode__(self): p = c_wchar_p() @@ -71,7 +58,7 @@ def __hash__(self): return hash(binary(self)) def copy(self): - return GUID(text_type(self)) + return GUID(str(self)) @classmethod def from_progid(cls, progid): @@ -80,11 +67,11 @@ def from_progid(cls, progid): progid = progid._reg_clsid_ if isinstance(progid, cls): return progid - elif isinstance(progid, base_text_type): + elif isinstance(progid, str): if progid.startswith("{"): return cls(progid) inst = cls() - _CLSIDFromProgID(text_type(progid), byref(inst)) + _CLSIDFromProgID(str(progid), byref(inst)) return inst else: raise TypeError("Cannot construct guid from %r" % progid) diff --git a/comtypes/__init__.py b/comtypes/__init__.py index 73f8567c8..d6d564465 100644 --- a/comtypes/__init__.py +++ b/comtypes/__init__.py @@ -52,53 +52,6 @@ ) -################################################################ - -# fmt: off -def add_metaclass(metaclass): - """Class decorator from six.py for creating a class with a metaclass. - - Copyright (c) 2010-2020 Benjamin Peterson - - Permission is hereby granted, free of charge, to any person obtaining a copy of - this software and associated documentation files (the "Software"), to deal in - the Software without restriction, including without limitation the rights to - use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - the Software, and to permit persons to whom the Software is furnished to do so, - subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS - FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR - COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER - IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - """ - def wrapper(cls): - orig_vars = cls.__dict__.copy() - slots = orig_vars.get('__slots__') - if slots is not None: - if isinstance(slots, text_type): - slots = [slots] - for slots_var in slots: - orig_vars.pop(slots_var) - orig_vars.pop('__dict__', None) - orig_vars.pop('__weakref__', None) - if hasattr(cls, '__qualname__'): - orig_vars['__qualname__'] = cls.__qualname__ - return metaclass(cls.__name__, cls.__bases__, orig_vars) - return wrapper -# fmt: on - - -################################################################ -if sys.version_info >= (3, 0): - text_type = str -else: - text_type = unicode _all_slice = slice(None, None, None) @@ -134,21 +87,16 @@ def _check_version(actual, tlib_cached_mtime=None): raise ImportError("Typelib different than module") -if sys.version_info >= (3, 0): - pythonapi.PyInstanceMethod_New.argtypes = [py_object] - pythonapi.PyInstanceMethod_New.restype = py_object - PyInstanceMethod_Type = type(pythonapi.PyInstanceMethod_New(id)) +pythonapi.PyInstanceMethod_New.argtypes = [py_object] +pythonapi.PyInstanceMethod_New.restype = py_object +PyInstanceMethod_Type = type(pythonapi.PyInstanceMethod_New(id)) - def instancemethod(func, inst, cls): - mth = PyInstanceMethod_Type(func) - if inst is None: - return mth - return mth.__get__(inst) -else: - - def instancemethod(func, inst, cls): - return types.MethodType(func, inst, cls) +def instancemethod(func, inst, cls): + mth = PyInstanceMethod_Type(func) + if inst is None: + return mth + return mth.__get__(inst) class ReturnHRESULT(Exception): @@ -587,7 +535,7 @@ def _make_methods(self, methods): except KeyError: raise AttributeError("this class must define an _iid_") else: - com_interface_registry[text_type(iid)] = self + com_interface_registry[str(iid)] = self # create members vtbl_offset = self.__get_baseinterface_methodcount() member_gen = ComMemberGenerator(self.__name__, vtbl_offset, self._iid_) @@ -627,8 +575,7 @@ class _compointer_meta(type(c_void_p), _cominterface_meta): # no functionality, but needed to avoid a metaclass conflict -@add_metaclass(_compointer_meta) -class _compointer_base(c_void_p): +class _compointer_base(c_void_p, metaclass=_compointer_meta): "base class for COM interface pointer classes" def __del__(self, _debug=logger.debug): @@ -757,7 +704,7 @@ def from_param(cls, value): # IDL stuff -class helpstring(text_type): +class helpstring(str): "Specifies the helpstring for a COM method or property." @@ -830,7 +777,7 @@ def COMMETHOD(idlflags, restype, methodname, *argspec): if TYPE_CHECKING: - class _IUnknown_Base(c_void_p): + class _IUnknown_Base(c_void_p, metaclass=_cominterface_meta): """This is workaround to avoid false-positive of static type checking. `IUnknown` behaves as a ctypes type, and `POINTER` can take it. @@ -848,8 +795,7 @@ class _IUnknown_Base(c_void_p): _IUnknown_Base = object -@add_metaclass(_cominterface_meta) -class IUnknown(_IUnknown_Base): +class IUnknown(_IUnknown_Base, metaclass=_cominterface_meta): """The most basic COM interface. Each subclasses of IUnknown must define these class attributes: @@ -963,9 +909,7 @@ def CoGetObject(displayname, interface): interface = IUnknown punk = POINTER(interface)() # Do we need a way to specify the BIND_OPTS parameter? - _ole32.CoGetObject( - text_type(displayname), None, byref(interface._iid_), byref(punk) - ) + _ole32.CoGetObject(str(displayname), None, byref(interface._iid_), byref(punk)) return punk # type: ignore @@ -1236,8 +1180,7 @@ def CoCreateInstanceEx( from comtypes._meta import _coclass_meta -@add_metaclass(_coclass_meta) -class CoClass(COMObject): +class CoClass(COMObject, metaclass=_coclass_meta): pass diff --git a/comtypes/_comobject.py b/comtypes/_comobject.py index d042267c8..c9e234413 100644 --- a/comtypes/_comobject.py +++ b/comtypes/_comobject.py @@ -13,6 +13,7 @@ from _ctypes import CopyComPointer import logging import os +import queue import sys from comtypes import COMError, ReturnHRESULT, instancemethod, _encode_idl @@ -37,11 +38,6 @@ _warning = logger.warning _error = logger.error -if sys.version_info >= (3, 0): - int_types = (int,) -else: - int_types = (int, long) - ################################################################ # COM object implementation @@ -72,7 +68,7 @@ def winerror(exc): return exc.hresult elif isinstance(exc, WindowsError): code = exc.winerror - if isinstance(code, int_types): + if isinstance(code, int): return code # Sometimes, a WindowsError instance has no error code. An access # violation raised by ctypes has only text, for example. In this @@ -381,10 +377,6 @@ def run_sta(self): messageloop.run() def run_mta(self): - if sys.version_info >= (3, 0): - import queue - else: - import Queue as queue self._queue = queue.Queue() self._queue.get() diff --git a/comtypes/automation.py b/comtypes/automation.py index ab2206ea0..1f15ba842 100644 --- a/comtypes/automation.py +++ b/comtypes/automation.py @@ -34,15 +34,6 @@ class _safearray(object): tagSAFEARRAY = None -if sys.version_info >= (3, 0): - int_types = (int,) - str_types = (str,) - base_text_type = str -else: - int_types = (int, long) - str_types = (unicode, str) - base_text_type = basestring - LCID = DWORD DISPID = LONG SCODE = LONG @@ -262,9 +253,7 @@ def _set_value(self, value): if value is None: self.vt = VT_NULL elif ( - hasattr(value, "__len__") - and len(value) == 0 - and not isinstance(value, base_text_type) + hasattr(value, "__len__") and len(value) == 0 and not isinstance(value, str) ): self.vt = VT_NULL # since bool is a subclass of int, this check must come before @@ -275,7 +264,7 @@ def _set_value(self, value): elif isinstance(value, (int, c_int)): self.vt = VT_I4 self._.VT_I4 = value - elif isinstance(value, int_types): + elif isinstance(value, int): u = self._ # try VT_I4 first. u.VT_I4 = value @@ -310,7 +299,7 @@ def _set_value(self, value): elif isinstance(value, (float, c_double)): self.vt = VT_R8 self._.VT_R8 = value - elif isinstance(value, str_types): + elif isinstance(value, str): self.vt = VT_BSTR # do the c_wchar_p auto unicode conversion self._.c_void_p = _SysAllocStringLen(value, len(value)) @@ -633,21 +622,11 @@ class IEnumVARIANT(IUnknown): def __iter__(self): return self - if sys.version_info >= (3, 0): - - def __next__(self): - item, fetched = self.Next(1) - if fetched: - return item - raise StopIteration - - else: - - def next(self): - item, fetched = self.Next(1) - if fetched: - return item - raise StopIteration + def __next__(self): + item, fetched = self.Next(1) + if fetched: + return item + raise StopIteration def __getitem__(self, index): self.Reset() diff --git a/comtypes/client/_constants.py b/comtypes/client/_constants.py index f20222d61..bc9f6e918 100644 --- a/comtypes/client/_constants.py +++ b/comtypes/client/_constants.py @@ -10,11 +10,6 @@ import comtypes.automation import comtypes.typeinfo -if sys.version_info >= (3, 0): - base_text_type = str -else: - base_text_type = basestring - class _frozen_attr_dict(dict): __slots__ = () @@ -78,7 +73,7 @@ class Constants(object): __slots__ = ("alias", "consts", "enums", "tcomp") def __init__(self, obj): - if isinstance(obj, base_text_type): + if isinstance(obj, str): tlib = comtypes.typeinfo.LoadTypeLibEx(obj) else: obj = obj.QueryInterface(comtypes.automation.IDispatch) diff --git a/comtypes/client/_generate.py b/comtypes/client/_generate.py index 841463fb1..9fc0e2305 100644 --- a/comtypes/client/_generate.py +++ b/comtypes/client/_generate.py @@ -6,13 +6,7 @@ import sys import types from typing import Any, Tuple, List, Optional, Dict, Union as _UnionT - -if sys.version_info >= (3, 0): - base_text_type = str - import winreg -else: - base_text_type = basestring - import _winreg as winreg +import winreg from comtypes import GUID, typeinfo import comtypes.client @@ -46,7 +40,7 @@ def _resolve_filename(tlib_string, dirpath): (abspath, True) or (relpath, False): where relpath is an unresolved path. """ - assert isinstance(tlib_string, base_text_type) + assert isinstance(tlib_string, str) # pathname of type library if os.path.isabs(tlib_string): # a specific location @@ -107,7 +101,7 @@ def GetModule(tlib): UIAutomation. The former module contains all the code, the latter is a short stub loading the former. """ - if isinstance(tlib, base_text_type): + if isinstance(tlib, str): tlib_string = tlib # if a relative pathname is used, we try to interpret it relative to # the directory of the calling module (if not from command line) @@ -136,8 +130,6 @@ def GetModule(tlib): modulename = codegenerator.name_friendly_module(tlib) if modulename is None: return mod - if sys.version_info < (3, 0): - modulename = modulename.encode("mbcs") # create and import the friendly-named module return _create_friendly_module(tlib, modulename) @@ -146,7 +138,7 @@ def _load_tlib(obj): # type: (Any) -> typeinfo.ITypeLib """Load a pointer of ITypeLib on demand.""" # obj is a filepath or a ProgID - if isinstance(obj, base_text_type): + if isinstance(obj, str): # in any case, attempt to load and if tlib_string is not valid, then raise # as "OSError: [WinError -2147312566] Error loading type library/DLL" return typeinfo.LoadTypeLibEx(obj) diff --git a/comtypes/connectionpoints.py b/comtypes/connectionpoints.py index 955347da2..94be91a82 100644 --- a/comtypes/connectionpoints.py +++ b/comtypes/connectionpoints.py @@ -34,21 +34,11 @@ class IEnumConnections(IUnknown): def __iter__(self): return self - if sys.version_info >= (3, 0): - - def __next__(self): - cp, fetched = self.Next(1) - if fetched == 0: - raise StopIteration - return cp - - else: - - def next(self): - cp, fetched = self.Next(1) - if fetched == 0: - raise StopIteration - return cp + def __next__(self): + cp, fetched = self.Next(1) + if fetched == 0: + raise StopIteration + return cp class IEnumConnectionPoints(IUnknown): @@ -58,21 +48,11 @@ class IEnumConnectionPoints(IUnknown): def __iter__(self): return self - if sys.version_info >= (3, 0): - - def __next__(self): - cp, fetched = self.Next(1) - if fetched == 0: - raise StopIteration - return cp - - else: - - def next(self): - cp, fetched = self.Next(1) - if fetched == 0: - raise StopIteration - return cp + def __next__(self): + cp, fetched = self.Next(1) + if fetched == 0: + raise StopIteration + return cp ################################################################ diff --git a/comtypes/errorinfo.py b/comtypes/errorinfo.py index 181596b83..55e6ee62f 100644 --- a/comtypes/errorinfo.py +++ b/comtypes/errorinfo.py @@ -6,11 +6,6 @@ LPCOLESTR = c_wchar_p DWORD = c_ulong -if sys.version_info >= (3, 0): - base_text_type = str -else: - base_text_type = basestring - class ICreateErrorInfo(IUnknown): _iid_ = GUID("{22F03340-547D-101B-8E65-08002B2BD119}") @@ -84,7 +79,7 @@ def ReportError( if helpcontext is not None: ei.SetHelpContext(helpcontext) if clsid is not None: - if isinstance(clsid, base_text_type): + if isinstance(clsid, str): clsid = GUID(clsid) try: progid = clsid.as_progid() diff --git a/comtypes/server/inprocserver.py b/comtypes/server/inprocserver.py index 38e4a199c..60fe440d3 100644 --- a/comtypes/server/inprocserver.py +++ b/comtypes/server/inprocserver.py @@ -5,11 +5,8 @@ import sys import logging +import winreg -if sys.version_info >= (3, 0): - import winreg -else: - import _winreg as winreg logger = logging.getLogger(__name__) _debug = logger.debug diff --git a/comtypes/server/localserver.py b/comtypes/server/localserver.py index 15e4b78ba..1f50d21a5 100644 --- a/comtypes/server/localserver.py +++ b/comtypes/server/localserver.py @@ -4,11 +4,7 @@ from comtypes.hresult import * from comtypes.server import IClassFactory import logging - -if sys.version_info >= (3, 0): - import queue -else: - import Queue as queue +import queue logger = logging.getLogger(__name__) _debug = logger.debug diff --git a/comtypes/server/register.py b/comtypes/server/register.py index 0fb7ce43b..c6b509df5 100644 --- a/comtypes/server/register.py +++ b/comtypes/server/register.py @@ -35,12 +35,9 @@ python mycomobj.py /nodebug """ -import sys, os - -if sys.version_info >= (3, 0): - import winreg -else: - import _winreg as winreg +import sys +import os +import winreg import logging import comtypes diff --git a/comtypes/tools/codegenerator.py b/comtypes/tools/codegenerator.py index d0beb2f12..903e113a1 100644 --- a/comtypes/tools/codegenerator.py +++ b/comtypes/tools/codegenerator.py @@ -8,11 +8,7 @@ import sys import textwrap from typing import Any, Dict, Iterator, List, Optional, Tuple, Union as _UnionT - -if sys.version_info >= (3, 0): - import io -else: - import cStringIO as io +import io import comtypes from comtypes import typeinfo @@ -1340,12 +1336,7 @@ def _inspect_PointerType(self, t, count=0): class ImportedNamespaces(object): def __init__(self): - if sys.version_info >= (3, 7): - self.data = {} - else: - from collections import OrderedDict - - self.data = OrderedDict() + self.data = {} def add(self, name1, name2=None, symbols=None): """Adds a namespace will be imported. @@ -1460,12 +1451,7 @@ def getvalue(self, for_stub=False): class DeclaredNamespaces(object): def __init__(self): - if sys.version_info >= (3, 7): - self.data = {} - else: - from collections import OrderedDict - - self.data = OrderedDict() + self.data = {} def add(self, alias, definition, comment=None): """Adds a namespace will be declared.