From 86aee02d485a3caf75559a4d641d6af03212e016 Mon Sep 17 00:00:00 2001 From: junkmd Date: Tue, 13 Oct 2026 23:47:07 +0900 Subject: [PATCH 1/2] refactor: Remove deprecated `setup_logging` function and related `deprecated` decorator. --- comtypes/logutil.py | 56 ----------------------------------- comtypes/test/test_logutil.py | 16 +--------- 2 files changed, 1 insertion(+), 71 deletions(-) diff --git a/comtypes/logutil.py b/comtypes/logutil.py index a3db8ef1f..4e667057c 100644 --- a/comtypes/logutil.py +++ b/comtypes/logutil.py @@ -1,7 +1,5 @@ # logutil.py -import functools import logging -import warnings from ctypes import WinDLL from ctypes.wintypes import LPCSTR, LPCWSTR @@ -27,57 +25,3 @@ def emit( 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 diff --git a/comtypes/test/test_logutil.py b/comtypes/test/test_logutil.py index 4d116daf0..3414c3776 100644 --- a/comtypes/test/test_logutil.py +++ b/comtypes/test/test_logutil.py @@ -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, ) @@ -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 From 82293806b1296c5d7e87a875474fb22123113f33 Mon Sep 17 00:00:00 2001 From: junkmd Date: Tue, 13 Oct 2026 23:47:07 +0900 Subject: [PATCH 2/2] refactor: Add type hints to `NTDebugHandler.emit`. --- comtypes/logutil.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/comtypes/logutil.py b/comtypes/logutil.py index 4e667057c..b3f2e7aad 100644 --- a/comtypes/logutil.py +++ b/comtypes/logutil.py @@ -1,5 +1,6 @@ # logutil.py import logging +from collections.abc import Callable from ctypes import WinDLL from ctypes.wintypes import LPCSTR, LPCWSTR @@ -17,11 +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 +logging.NTDebugHandler = NTDebugHandler # type: ignore