Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

automation.VARIANT._set_value assumes c_char.value has length 1

Aperta
#935 10 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@haoxuanjng-lang ci sta già lavorando.

Dal 9/10/2026.

  • #962 di @haoxuanjng-lang — aperta

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
58/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Inizia in comtypes/automation.py, in VARIANT._set_value, e riproduci il ramo c_char con l’esempio sintetico WeirdChar dell’issue. Esamina i test VARIANT nelle vicinanze, se presenti, e considera completato il lavoro quando l’errore generico di ord() sarà sostituito dal comportamento validato e contestuale previsto per gli input c_char.value multi-byte.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

question

Environment data

  • OS: Windows Server 2022 / Windows 10
  • Python version: 3.13.12
  • comtypes version: 1.4.16
  • COM type libraries involved in the real-world trigger: UI Automation (IUIAutomation)

Code snippet

This synthetic reproducer exercises the exact failing branch in comtypes/automation.py:

from ctypes import c_char
from comtypes.automation import VARIANT


class WeirdChar(c_char):
    @property
    def value(self):
        return b"hello"


VARIANT(WeirdChar(b"h"))

Traceback on comtypes 1.4.16:

Traceback (most recent call last):
  File "<stdin>", line 10, in <module>
  File "...\site-packages\comtypes\automation.py", line 199, in __init__
    self.value = args[0]
  File "...\site-packages\comtypes\automation.py", line 347, in _set_value
    self._.VT_UI1 = ord(value.value)
TypeError: ord() expected a character, but string of length 5 found

The current branch is:

elif isinstance(value, c_char):
    self._.VT_UI1 = ord(value.value)
    self.vt = VT_UI1

Situation reproducing steps

  1. Import VARIANT from comtypes.automation
  2. Pass a c_char instance whose .value is longer than one byte to VARIANT(...)
  3. automation.py calls ord(value.value)
  4. ord() raises TypeError because the input length is greater than 1

In a real application, I hit this through COM/UI Automation property marshaling. During tree traversal, a COM property eventually reached VARIANT._set_value as a c_char whose .value was unexpectedly multi-byte, which caused the same traceback above.

Expected behavior

comtypes should not assume that c_char.value is always length 1 before calling ord().

At minimum, this branch should validate the length explicitly and raise a more direct error with context. If treating the underlying data as a byte is the intended behavior, raw[0] after validation would also avoid the current ord() failure path.

Actual behavior

comtypes raises a generic TypeError from ord():

TypeError: ord() expected a character, but string of length 5 found

When this happens in COM-driven code, the error bubbles out of a deep marshaling path and is difficult for downstream callers to diagnose or handle precisely.

Additional context

I do not yet have a minimal pure-COM reproducer outside the larger application, so the code snippet above is synthetic. I am filing this because it demonstrates the exact failing branch and matches the traceback seen in the real COM/UI Automation scenario.

Lingua principale
Python
Stelle
346
Fork
107
Merge medio
22h 5m
PR unite (30g)
15

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di enthought/comtypes

Tutte le issue di enthought/comtypes

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.