Skip to content

"a foreign function that will call a COM method" generated by ctypes.WINFUNCTYPE works in Python3.7, does not work as same in newer Python #99952

Closed
@junkmd

Description

@junkmd

Bug report

I'm contributing on enthought/comtypes.

In comtypes, there is a test for the behavior of Excel that is currently skipped. If I comment out the unittest.skip marker in that test, it works in Python 3.7 and fails in Python 3.11.

PS ...\comtypes> py -3.7 -m unittest comtypes.test.test_excel -vv
test (comtypes.test.test_excel.Test_EarlyBind) ... ok
test (comtypes.test.test_excel.Test_LateBind) ... ok
----------------------------------------------------------------------
Ran 2 tests in 10.576s
OK
PS ...\comtypes> py -3.7 -m clear_comtypes_cache -y  # <- clear caches, required!
Removed directory "...\comtypes\comtypes\gen"
Removed directory "...\AppData\Roaming\Python\Python37\comtypes_cache"
PS ...\comtypes> py -3.11 -m unittest comtypes.test.test_excel -vv       
test (comtypes.test.test_excel.Test_EarlyBind.test) ... FAIL
test (comtypes.test.test_excel.Test_LateBind.test) ... ok

======================================================================
FAIL: test (comtypes.test.test_excel.Test_EarlyBind.test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "...\comtypes\comtypes\test\test_excel.py", line 62, in test
    self.assertEqual(xl.Range["A1:C3"].Value(),
AssertionError: Tuples differ: ((None, None, 10.0), ('x', 'y', 'z'), (3.0, 2.0, 1.0)) != ((10.0, 20.0, 31.4), ('x', 'y', 'z'), (3.0, 2.0, 1.0))

First differing element 0:
(None, None, 10.0)
(10.0, 20.0, 31.4)

- ((None, None, 10.0), ('x', 'y', 'z'), (3.0, 2.0, 1.0))
?   ------------

+ ((10.0, 20.0, 31.4), ('x', 'y', 'z'), (3.0, 2.0, 1.0))
?       ++++++++++++


----------------------------------------------------------------------
Ran 2 tests in 9.640s

FAILED (failures=1)

xl.Range[... calls a prototype-function generated by ctypes.WinFunctionType and ctypes.WINFUNCTYPE.
This is also reported in enthought/comtypes#212 and the test fails in Python 3.8 as well.
A strange callback behavior also occurs with simple COM libraries.
Therefore, I think that this may not be caused by the Excel specification.

There may be other regressions in ctypes callbacks that have also been reported in #82929.
Also, is #97513 a possible solution to this problem?

Any opinions would be appreciated.

Your environment

  • CPython versions tested on: Python 3.7.1 and Python 3.11.0
  • Windows 10

Linked PRs

Metadata

Metadata

Assignees

Labels

3.10only security fixes3.11only security fixes3.12only security fixestopic-ctypestype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions