Skip to content

Commit

Permalink
prepare #566 part2 (#568)
Browse files Browse the repository at this point in the history
* remove duplicated stuffs
- remove from `comtypes\_base.py`

* revert temporary renaming
- rename from `comtypes\___init__.py` to `comtypes\__init__.py`
  • Loading branch information
junkmd authored Jun 10, 2024
1 parent 25fc2be commit 9820ced
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 220 deletions.
File renamed without changes.
234 changes: 14 additions & 220 deletions comtypes/_base.py
Original file line number Diff line number Diff line change
@@ -1,179 +1,25 @@
# comtypes version numbers follow semver (http://semver.org/) and PEP 440
__version__ = "1.4.4"

import atexit
from ctypes import *
from ctypes import _Pointer, _SimpleCData

try:
from _ctypes import COMError # noqa
except ImportError as e:
msg = "\n".join(
(
"COM technology not available (maybe it's the wrong platform).",
"Note that COM is only supported on Windows.",
"For more details, please check: "
"https://learn.microsoft.com/en-us/windows/win32/com",
)
)
raise ImportError(msg) from e
import logging
import sys
from ctypes import c_ulong, c_ushort, c_void_p, c_wchar_p, HRESULT, Structure
from ctypes import byref, cast, _Pointer, POINTER, pointer

# fmt: off
from typing import ( # noqa
Any, ClassVar, overload, TYPE_CHECKING, TypeVar,
Any, overload, TYPE_CHECKING, TypeVar,
# instead of `builtins`. see PEP585
Dict, List, Tuple, Type,
Type,
# instead of `collections.abc`. see PEP585
Callable, Iterable, Iterator,
Callable,
# instead of `A | B` and `None | A`. see PEP604
Optional, Union as _UnionT, # avoiding confusion with `ctypes.Union`
Optional,
)
# fmt: on
if TYPE_CHECKING:
from ctypes import _CData # only in `typeshed`, private in runtime
from comtypes import hints as hints # noqa # type: ignore
else:
_CData = _SimpleCData.__mro__[:-1][-1]

from comtypes.GUID import GUID
from comtypes import patcher # noqa
from comtypes._npsupport import interop as npsupport # noqa
from comtypes._memberspec import _encode_idl # noqa
from comtypes._tlib_version_checker import _check_version # noqa
from comtypes._bstr import BSTR # 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)


class NullHandler(logging.Handler):
"""A Handler that does nothing."""

def emit(self, record):
pass


logger = logging.getLogger(__name__)

# Add a NULL handler to the comtypes logger. This prevents getting a
# message like this:
# No handlers could be found for logger "comtypes"
# when logging is not configured and logger.error() is called.
logger.addHandler(NullHandler())


class ReturnHRESULT(Exception):
"""ReturnHRESULT(hresult, text)
Return a hresult code from a COM method implementation
without logging an error.
"""


# class IDLWarning(UserWarning):
# "Warn about questionable type information"

_GUID = GUID
IID = GUID
DWORD = c_ulong

wireHWND = c_ulong

################################################################
# About COM apartments:
# http://blogs.msdn.com/larryosterman/archive/2004/04/28/122240.aspx
################################################################

################################################################
# constants for object creation
CLSCTX_INPROC_SERVER = 1
CLSCTX_INPROC_HANDLER = 2
CLSCTX_LOCAL_SERVER = 4

CLSCTX_INPROC = 3
CLSCTX_SERVER = 5
CLSCTX_ALL = 7

CLSCTX_INPROC_SERVER16 = 8
CLSCTX_REMOTE_SERVER = 16
CLSCTX_INPROC_HANDLER16 = 32
CLSCTX_RESERVED1 = 64
CLSCTX_RESERVED2 = 128
CLSCTX_RESERVED3 = 256
CLSCTX_RESERVED4 = 512
CLSCTX_NO_CODE_DOWNLOAD = 1024
CLSCTX_RESERVED5 = 2048
CLSCTX_NO_CUSTOM_MARSHAL = 4096
CLSCTX_ENABLE_CODE_DOWNLOAD = 8192
CLSCTX_NO_FAILURE_LOG = 16384
CLSCTX_DISABLE_AAA = 32768
CLSCTX_ENABLE_AAA = 65536
CLSCTX_FROM_DEFAULT_CONTEXT = 131072

tagCLSCTX = c_int # enum
CLSCTX = tagCLSCTX

# Constants for security setups
SEC_WINNT_AUTH_IDENTITY_UNICODE = 0x2
RPC_C_AUTHN_WINNT = 10
RPC_C_AUTHZ_NONE = 0
RPC_C_AUTHN_LEVEL_CONNECT = 2
RPC_C_IMP_LEVEL_IMPERSONATE = 3
EOAC_NONE = 0


################################################################
# Initialization and shutdown
_ole32 = oledll.ole32
_ole32_nohresult = windll.ole32 # use this for functions that don't return a HRESULT

COINIT_MULTITHREADED = 0x0
COINIT_APARTMENTTHREADED = 0x2
COINIT_DISABLE_OLE1DDE = 0x4
COINIT_SPEED_OVER_MEMORY = 0x8


def CoInitialize():
return CoInitializeEx(COINIT_APARTMENTTHREADED)


def CoInitializeEx(flags=None):
if flags is None:
flags = getattr(sys, "coinit_flags", COINIT_APARTMENTTHREADED)
logger.debug("CoInitializeEx(None, %s)", flags)
_ole32.CoInitializeEx(None, flags)


# COM is initialized automatically for the thread that imports this
# module for the first time. sys.coinit_flags is passed as parameter
# to CoInitializeEx, if defined, otherwise COINIT_APARTMENTTHREADED
# (COINIT_MULTITHREADED on Windows CE) is used.
#
# A shutdown function is registered with atexit, so that
# CoUninitialize is called when Python is shut down.
CoInitializeEx()


# We need to have CoUninitialize for multithreaded model where we have
# to initialize and uninitialize COM for every new thread (except main)
# in which we are using COM
def CoUninitialize():
logger.debug("CoUninitialize()")
_ole32_nohresult.CoUninitialize()


################################################################
# global registries.

# allows to find interface classes by guid strings (iid)
com_interface_registry = {}

# allows to find coclasses by guid strings (clsid)
com_coclass_registry = {}
from comtypes import GUID
from comtypes._idl_stuff import COMMETHOD # noqa
from comtypes import CLSCTX_SERVER, CLSCTX_LOCAL_SERVER, CLSCTX_REMOTE_SERVER
from comtypes import _ole32, oledll, DWORD
from comtypes import IUnknown


def _is_object(obj):
Expand All @@ -192,14 +38,7 @@ def _is_object(obj):
return hasattr(obj, "_comobj")


################################################################
# IUnknown, the root of all evil...

_T_IUnknown = TypeVar("_T_IUnknown", bound="IUnknown")

from comtypes.unknwn import IUnknown, _shutdown # noqa

atexit.register(_shutdown)
_T_IUnknown = TypeVar("_T_IUnknown", bound=IUnknown)


################################################################
Expand All @@ -223,6 +62,7 @@ def GetClassID(self) -> GUID:
class IServiceProvider(IUnknown):
_iid_ = GUID("{6D5140C1-7436-11CE-8034-00AA006009FA}")
_QueryService: Callable[[Any, Any, Any], int]

# Overridden QueryService to make it nicer to use (passing it an
# interface and it returns a pointer to that interface)
def QueryService(
Expand Down Expand Up @@ -449,6 +289,7 @@ class tagBIND_OPTS2(Structure):
# XXX Add __init__ which sets cbStruct?
BINDOPTS2 = tagBIND_OPTS2


# Structures for security setups
#########################################
class _SEC_WINNT_AUTH_IDENTITY(Structure):
Expand Down Expand Up @@ -542,50 +383,3 @@ def CoCreateInstanceEx(
byref(clsid), None, clsctx, pServerInfo, 1, byref(multiqi)
)
return cast(multiqi.pItf, POINTER(interface)) # type: ignore


################################################################
from comtypes._comobject import COMObject

# What's a coclass?
# a POINTER to a coclass is allowed as parameter in a function declaration:
# http://msdn.microsoft.com/library/en-us/midl/midl/oleautomation.asp

from comtypes._meta import _coclass_meta


class CoClass(COMObject, metaclass=_coclass_meta):
pass


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


# fmt: off
__known_symbols__ = [
"BIND_OPTS", "tagBIND_OPTS", "BINDOPTS2", "tagBIND_OPTS2", "BSTR",
"_check_version", "CLSCTX", "tagCLSCTX", "CLSCTX_ALL",
"CLSCTX_DISABLE_AAA", "CLSCTX_ENABLE_AAA", "CLSCTX_ENABLE_CODE_DOWNLOAD",
"CLSCTX_FROM_DEFAULT_CONTEXT", "CLSCTX_INPROC", "CLSCTX_INPROC_HANDLER",
"CLSCTX_INPROC_HANDLER16", "CLSCTX_INPROC_SERVER",
"CLSCTX_INPROC_SERVER16", "CLSCTX_LOCAL_SERVER", "CLSCTX_NO_CODE_DOWNLOAD",
"CLSCTX_NO_CUSTOM_MARSHAL", "CLSCTX_NO_FAILURE_LOG",
"CLSCTX_REMOTE_SERVER", "CLSCTX_RESERVED1", "CLSCTX_RESERVED2",
"CLSCTX_RESERVED3", "CLSCTX_RESERVED4", "CLSCTX_RESERVED5",
"CLSCTX_SERVER", "_COAUTHIDENTITY", "COAUTHIDENTITY", "_COAUTHINFO",
"COAUTHINFO", "CoClass", "CoCreateInstance", "CoCreateInstanceEx",
"_CoGetClassObject", "CoGetClassObject", "CoGetObject",
"COINIT_APARTMENTTHREADED", "COINIT_DISABLE_OLE1DDE",
"COINIT_MULTITHREADED", "COINIT_SPEED_OVER_MEMORY", "CoInitialize",
"CoInitializeEx", "COMError", "COMMETHOD", "COMObject", "_COSERVERINFO",
"COSERVERINFO", "CoUninitialize", "dispid", "DISPMETHOD", "DISPPROPERTY",
"DWORD", "EOAC_NONE", "GetActiveObject", "_GUID", "GUID", "helpstring",
"IID", "IPersist", "IServiceProvider", "IUnknown", "MULTI_QI",
"ReturnHRESULT", "RPC_C_AUTHN_LEVEL_CONNECT", "RPC_C_AUTHN_WINNT",
"RPC_C_AUTHZ_NONE", "RPC_C_IMP_LEVEL_IMPERSONATE",
"_SEC_WINNT_AUTH_IDENTITY", "SEC_WINNT_AUTH_IDENTITY",
"SEC_WINNT_AUTH_IDENTITY_UNICODE", "_SOLE_AUTHENTICATION_INFO",
"SOLE_AUTHENTICATION_INFO", "_SOLE_AUTHENTICATION_LIST",
"SOLE_AUTHENTICATION_LIST", "STDMETHOD", "wireHWND",
]
# fmt: on

0 comments on commit 9820ced

Please sign in to comment.