Fix Snapshot crash from comtypes VARIANT marshaling on non-ASCII UI elements#148
Merged
Jeomon merged 2 commits intoCursorTouch:mainfrom Mar 26, 2026
Merged
Conversation
added 2 commits
March 25, 2026 18:44
…lements When the UI Automation tree traversal encounters a UI element whose COM property triggers a TypeError in comtypes VARIANT marshaling (e.g. non-ASCII window titles or accessibility properties), the Snapshot tool crashes instead of returning a partial result. Add a defensive TypeError guard at both the per-child and per-node levels in tree_traversal so problematic elements are logged and skipped, allowing the rest of the desktop state capture to complete. Fixes CursorTouch#147 Made-with: Cursor
Member
|
Thanks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds a defensive guard in
tree_traversalso Snapshot skips the knowncomtypesord()VARIANT marshaling failure instead of aborting the entire traversal.Motivation
On some desktops, a UI Automation property can cause
comtypesto raiseTypeError: ord() expected a character, but string of length 5 foundwhile Snapshot is walking the tree. The problematic element or subtree can be skipped safely; crashing the entire Snapshot call is unnecessary.Changes
File:
src/windows_mcp/tree/service.pyTypeErrorguard in the per-child iteration loop to skip a single problematic child and continue with remaining siblingsTypeErrorguard at the outer node level to skip the current subtree when the current node itself triggers the same failurecomtypes/automation.pyord()TypeError; unrelatedTypeErrors still follow the existing error pathWARNINGlevel for diagnosticsFile:
tests/test_tree_service.pycomtypes/automation.pytracebackTesting
uv run --with pytest pytest tests/test_tree_service.pypython -m py_compile src/windows_mcp/tree/service.py tests/test_tree_service.pyuv run --with ruff ruff check src/windows_mcp/tree/service.py tests/test_tree_service.pysrc/windows_mcp/tree/service.py; the change adds no new lint findingsUpstream Context
Related Issues
Fixes #147