Skip to content

Conversation

@junkmd
Copy link
Collaborator

@junkmd junkmd commented Jan 29, 2026

Overview

This pull request introduces comprehensive unit tests for the IGlobalInterfaceTable (GIT) functionalities within the git module.
These tests are verifying the handling of COM apartment boundaries which was an untested area previously.

Demonstrated Solution for Thread Boundary Violations

The tests specifically target and reproduce "thread boundary violations" by attempting direct access to STA (Single-Threaded Apartment) COM objects from different threads without proper marshaling.
By contrasting this failed direct access with successful access through GIT, the tests clearly demonstrate that GIT provides a mechanism for sharing interfaces across apartments.

Technical Background (for documentation integration)

The tests intentionally utilize out-of-process COM objects, such as Paint.Picture, because in-process objects can sometimes "accidentally work".

Add `test_git` to verify `GetInterfaceFromGlobal`, `RegisterInterfaceInGlobal`,
and `RevokeInterfaceFromGlobal` methods.
This test reflects the logic found in the `if __name__ == "__main__":` block
within `comtypes/git.py`.
…st_git`.

This change introduces a context manager `register_in_global` to streamline
the registration and revocation of COM objects in the Global Interface Table
(GIT) within `test_git.py`. This ensures proper cleanup, improving readability.
This adds `Test_ApartmentMarshaling` to `test_git.py` to verify the correct
marshaling of COM objects across different COM apartments using the Global
Interface Table (GIT). It uses `Paint.Picture`, an out-of-process, STA-only
COM object, to demonstrate object persistence and marshaling, including
thread management and message pumping to prevent deadlocks.
Enhance `Test_ApartmentMarshaling` in `test_git.py` by adding `Queue` to
retrieve the `IPersistFile.GetCurFile()` result from the marshaled object
in a separate thread. This allows asserting that the object's data remains
consistent after marshaling through the Global Interface Table (GIT).
…ling`.

Further refines `Test_ApartmentMarshaling` in `test_git.py` by adding an
immediate assertion to `IPersistFile.GetCurFile()` after loading the image
file. This ensures the file path is correctly retrieved and consistent
with the loaded file, even before cross-thread marshaling verification.
Further refines `Test_ApartmentMarshaling` in `test_git.py` by adding an
explicit check for the COM apartment type using `CoGetApartmentType`. This
assertion ensures that the test is executed within the expected main STA
(Single-Threaded Apartment) thread, which is crucial for valid COM
marshaling test results.
…arshaling`.

Enhance `Test_ApartmentMarshaling` in `test_git.py` by adding a test case to
explicitly verify that direct access to a `Paint.Picture` object (obtained from
an STA object created in the main thread) from a different apartment (worker
thread) fails as expected when NOT marshaled through the Global Interface Table
(GIT). This highlights the necessity of GIT for cross-apartment access.
Further refines `Test_ApartmentMarshaling` in `test_git.py` to explicitly
assert `RPC_E_WRONG_THREAD` when attempting to access a `Paint.Picture` object
directly from a different thread without proper COM marshaling. This verifies
the expected COM behavior where direct cross-apartment access to an STA object
fails with this specific error, underscoring the necessity of the Global
Interface Table (GIT) for such operations.
Enhance `test_git.py` by adding inline comments with direct links to the
Microsoft Learn documentation for COM API functions such as `PeekMessageA`,
`MsgWaitForMultipleObjects`, and `CoGetApartmentType`. This improves the
readability and maintainability of the test code by providing immediate
context and reference for the underlying Windows API calls.
@junkmd junkmd added this to the 1.4.16 milestone Jan 29, 2026
@junkmd junkmd added the tests enhance or fix tests label Jan 29, 2026
@codecov
Copy link

codecov bot commented Jan 29, 2026

Codecov Report

❌ Patch coverage is 97.97980% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.80%. Comparing base (4703e39) to head (fb1b616).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
comtypes/test/test_git.py 97.97% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #918      +/-   ##
==========================================
+ Coverage   87.53%   87.80%   +0.26%     
==========================================
  Files         136      137       +1     
  Lines       12975    13074      +99     
==========================================
+ Hits        11358    11479     +121     
+ Misses       1617     1595      -22     

☔ 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 338e34d into enthought:main Jan 29, 2026
81 of 101 checks passed
@junkmd junkmd deleted the test_git branch January 29, 2026 23:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests enhance or fix tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant