Skip to content

Fix a code path in logutil.NTDebugHandler and add a deprecation decorator to logutil.setup_logging.#921

Merged
junkmd merged 14 commits intoenthought:mainfrom
junkmd:update_logutil
Feb 1, 2026
Merged

Fix a code path in logutil.NTDebugHandler and add a deprecation decorator to logutil.setup_logging.#921
junkmd merged 14 commits intoenthought:mainfrom
junkmd:update_logutil

Conversation

@junkmd
Copy link
Collaborator

@junkmd junkmd commented Feb 1, 2026

Part of #920

Summary

This pull request improves the logutil module by fixing NTDebugHandler's Python 2/3 compatibility issues and deprecating setup_logging to streamline the codebase.

Enhanced Reliability

logutil.NTDebugHandler is now available

Previously, logutil.NTDebugHandler suffered from Python 2/3 str vs bytes incompatibility, leading to runtime errors when handling debug output.
This change eliminates the problematic type branching, ensuring that _OutputDebugStringW is consistently used.
This makes NTDebugHandler reliable for debugging in modern Python environments, where logging.Formatter.format always returns Unicode strings.

Debug output capture is now testable

To ensure the correct functioning of NTDebugHandler, a new mechanism to capture OutputDebugStringW output has been introduced.
This enables robust testing of Windows debug output, allowing for verification of what was previously an untestable component.
This new capability not only validates the current fix but also provides a foundation for future debugging-related enhancements.

Streamlined Codebase

Reduce unnecessary responsibilities in logutil

The setup_logging function, which handled general logging configuration unrelated to COM or Windows-specific functionalities, has been deprecated.
This change clarifies the scope of logutil by removing excessive responsibilities that are better handled by application-level logging configurations, thus simplifying the library's overall purpose and reducing its maintenance burden.

junkmd added 14 commits February 1, 2026 18:58
- Introduce `@deprecated` decorator for marking functions as deprecated.
- Apply `@deprecated` to `comtypes.logutil.setup_logging` due to its excessive
  responsibility and limited use.
- Add `test_logutil.py` to verify the `DeprecationWarning` mechanism.
- Introduce `capture_debug_strings` context manager to capture debug output.
- Add `Test_OutputDebugStringW` to verify the functionality of
  `OutputDebugStringW`.
- This test lays the groundwork for fixing `logutil.NTDebugHandler`'s Python
  2/3 `str` vs `bytes` incompatibility.
- Add inline comments to `test_logutil.py` with direct links to Microsoft Learn
  documentation for relevant Windows API functions.
- Introduce detailed comments to context managers.
- Clarify their roles in handling Windows API objects and interprocess
  communication for `OutputDebugString` capture.
- Enhance understanding of test utility functions and shared resources.
- Add detailed comments to `capture_debug_strings` in `test_logutil.py`.
- Clarify the role of shared events (`DBWIN_BUFFER_READY`, `DBWIN_DATA_READY`)
  and shared memory (`DBWIN_BUFFER`) in interprocess communication for
  capturing `OutputDebugStringW` output.
- Improve understanding of the test's underlying mechanism.
- Replaces `ctypes.windll.kernel32` with `ctypes.WinDLL("kernel32")`.
- This prevents unintended modification of shared `ctypes.windll` objects.
- Aligns `test_logutil` with recommended `ctypes` practices.
- Introduce `assert` statements with test helpers in `test_logutil.py`.
- These assertions ensure that resource creation failures are immediately
  caught and reported with detailed error information.
…py`.

- Update `argtypes` to use `POINTER(SECURITY_ATTRIBUTES)`.
- This change enhances type safety and clarity by explicitly defining the
  expected argument types for these Windows API functions, making the test
  code more robust and easier to understand.
…py`.

- Introduce type hints to the `capture_debug_strings` context manager in
  `test_logutil.py`.
- This improves code readability and maintainability by explicitly the expected
  types of its parameters and return value, facilitating better static analysis
  and developer understanding.
…anagers.

- Introduce type hints to `create_file_mapping`, `map_view_of_file`, and
  `create_event` in `test_logutil.py`.
- This improves code readability and maintainability by making explicit the
  expected types of arguments and return values for these Windows API wrapper
  functions, enabling better static analysis.
….py`

- Replace magic numbers with descriptive named constants for Windows API calls
  within `test_logutil.py`.
- This improves the readability, maintainability, and clarity of the test code,
  making the intent of API parameters more explicit.
- Introduce `open_dbwin_debug_channels` context manager in `test_logutil.py` to
  encapsulate the creation and management of Windows debug output channels
  (events and shared memory).
- This refactoring improves the readability and reusability of the debug string
  capturing mechanism, centralizing resource handling.
- Modify `capture_debug_strings` context manager in `test_logutil.py` to use
  `queue.Queue` instead of a list for collecting debug output.
- This ensures thread-safe communication between the listener thread and the main
  test thread, preventing race conditions when capturing `OutputDebugString`
  messages.
- Update `Test_OutputDebugStringW` assertions to correctly retrieve messages
  from the `Queue`.
…tibility.

- Modify `logutil.NTDebugHandler.emit` to consistently use `_OutputDebugStringW`.
- Remove `str` vs `bytes` type branching, simplifying implementation and
  resolving compatibility issues.
- Add `Test_NTDebugHandler` to verify proper functioning of the fixed handler.
@junkmd junkmd added this to the 1.4.16 milestone Feb 1, 2026
@junkmd junkmd added bug Something isn't working tests enhance or fix tests labels Feb 1, 2026
@codecov
Copy link

codecov bot commented Feb 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.20%. Comparing base (06879ab) to head (50494f7).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #921      +/-   ##
==========================================
+ Coverage   87.94%   88.20%   +0.25%     
==========================================
  Files         138      139       +1     
  Lines       13153    13284     +131     
==========================================
+ Hits        11568    11717     +149     
+ Misses       1585     1567      -18     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@junkmd junkmd merged commit c21fd13 into enthought:main Feb 1, 2026
52 checks passed
@junkmd junkmd deleted the update_logutil branch February 1, 2026 10:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working tests enhance or fix tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proposal: Fix NTDebugHandler and Deprecate setup_logging in logutil.py.

1 participant