Skip to content

Conversation

@junkmd
Copy link
Collaborator

@junkmd junkmd commented Jul 19, 2025

Fixes #445.

Revives the util module by fixing a critical bug, and introduces a comprehensive test suite.

The util module provides essential helper functions for advanced COM interoperability, but it has lacked dedicated tests.

Fixing a RuntimeError in _calc_offset

The function was incorrectly comparing the PyCArgObject.tag (a bytes literal, b'P') with a str literal ("P"), causing the validation to fail and a RuntimeError to be raised.
This has been corrected by changing the literal to b'P', allowing the function to work as intended.

Adding comprehensive tests for util functions

byref_at

Tests now cover basic ctypes, array offsets, pointer arithmetic simulation, COM interfaces, large offsets, and memory safety.

cast_field

Tests validate behavior with basic ctypes, type reinterpretation, pointers, nested structures/unions, arrays, and v-table-like structures.

junkmd added 5 commits July 19, 2025 12:50
Adds a basic import test for the `comtypes.util` module to ensure it can be imported without errors.
This serves as a foundational test for future enhancements.
Fixes a `RuntimeError` in `comtypes.util._calc_offset` that occurred
due to a type mismatch when validating `PyCArgObject`.

The `argobj.tag` field, which is a `c_char`, is represented as a
`bytes` object in Python (e.g., `b'P'`).
However, the validation logic was comparing it to a `str` literal
(`"P"`).
This comparison always failed, leading to an incorrect `RuntimeError`
being raised.

This fix changes the literal to `b"P"`, ensuring the comparison is
performed correctly between two `bytes` objects.
This resolves the underlying issue and allows `_calc_offset` to
function as intended.
@junkmd junkmd added this to the 1.4.12 milestone Jul 19, 2025
@junkmd junkmd added bug Something isn't working tests enhance or fix tests labels Jul 19, 2025
@codecov
Copy link

codecov bot commented Jul 19, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.73%. Comparing base (61c7cd2) to head (36860a6).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #865      +/-   ##
==========================================
+ Coverage   84.39%   84.73%   +0.33%     
==========================================
  Files         124      125       +1     
  Lines       11442    11559     +117     
==========================================
+ Hits         9657     9795     +138     
+ Misses       1785     1764      -21     

☔ 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 marked this pull request as ready for review July 19, 2025 08:30
@junkmd junkmd merged commit 5e241d8 into enthought:main Jul 19, 2025
216 of 248 checks passed
@junkmd junkmd deleted the revive_util_and_add_tests branch July 19, 2025 08:31
@junkmd junkmd mentioned this pull request Jul 20, 2025
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.

Attempting to import comtypes.util raises RuntimeError

1 participant