Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 5 additions & 60 deletions comtypes/logutil.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# logutil.py
import functools
import logging
import warnings
from collections.abc import Callable
from ctypes import WinDLL
from ctypes.wintypes import LPCSTR, LPCWSTR

Expand All @@ -19,65 +18,11 @@
class NTDebugHandler(logging.Handler):
def emit(
self,
record,
writeW=_OutputDebugStringW,
):
record: logging.LogRecord,
writeW: Callable[[str], None] = _OutputDebugStringW,
) -> None:
text = self.format(record)
writeW(text + "\n")


logging.NTDebugHandler = NTDebugHandler


def deprecated(reason: str):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
warnings.warn(reason, category=DeprecationWarning, stacklevel=2)
return func(*args, **kwargs)

return wrapper

return decorator


@deprecated("Deprecated. See https://github.com/enthought/comtypes/issues/920.")
def setup_logging(*pathnames):
import configparser

parser = configparser.ConfigParser()
parser.optionxform = str # use case sensitive option names!

parser.read(pathnames)

DEFAULTS = {
"handler": "StreamHandler()",
"format": "%(levelname)s:%(name)s:%(message)s",
"level": "WARNING",
}

def get(section, option):
try:
return parser.get(section, option, True)
except (configparser.NoOptionError, configparser.NoSectionError):
return DEFAULTS[option]

levelname = get("logging", "level")
format = get("logging", "format")
handlerclass = get("logging", "handler")

# convert level name to level value
level = getattr(logging, levelname)
# create the handler instance
handler = eval(handlerclass, vars(logging))
formatter = logging.Formatter(format)
handler.setFormatter(formatter)
logging.root.addHandler(handler)
logging.root.setLevel(level)

try:
for name, value in parser.items("logging.levels", True):
value = getattr(logging, value)
logging.getLogger(name).setLevel(value)
except configparser.NoSectionError:
pass
logging.NTDebugHandler = NTDebugHandler # type: ignore
16 changes: 1 addition & 15 deletions comtypes/test/test_logutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from typing import Union as _UnionT

from comtypes.client._events import SECURITY_ATTRIBUTES
from comtypes.logutil import NTDebugHandler, deprecated
from comtypes.logutil import NTDebugHandler
from comtypes.logutil import (
_OutputDebugStringW as OutputDebugStringW,
)
Expand All @@ -21,20 +21,6 @@
from ctypes import _CArgObject, _Pointer


class Test_deprecated(ut.TestCase):
def test_warning_is_raised(self):
reason_text = "This is deprecated."

@deprecated(reason_text)
def test_func():
return "success"

with self.assertWarns(DeprecationWarning) as cm:
result = test_func()
self.assertEqual(result, "success")
self.assertEqual(reason_text, str(cm.warning))


_kernel32 = WinDLL("kernel32", use_last_error=True)

# https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-createeventw
Expand Down
Loading