How do I avoid "pure virtual function call" errors when programming Adobe Illustrator via WIN32COM with Python?

51 Views Asked by At

While writing python (3.12.2) code to interact with Adobe Illustrator via WIN32COM (Windows 10), I sometimes crash Illustrator with "pure virtual function call" errors.

Here is an example line of code that causes such a crash:

font_size = text_frame.TextRange.CharacterAttributes.Size

The mere act of trying to access the Size property triggers the crash. However, these two lines of semantically-equivalent code manage to avoid any crashing:

text_range = text_frame.TextRange
font_size = text_range.CharacterAttributes.Size

I'm hoping someone who knows the intricacies of WIN32COM programming can take me on a medium-depth dive into the technical guts of interacting with COM objects and perhaps explain why breaking up the access chain into two parts makes the code work. I'd like to be able to anticipate when this will happen so I can write working code without having to stumble into this minefield every time I try to access something new from the Illustrator object model.

The full exception traceback is as follows:

Traceback (most recent call last):
  File "C:\Users\John\PycharmProjects\Adobe API\Short Grain 500 Hex Coordinates.py", line 83, in <module>
    main()
  File "C:\Users\John\PycharmProjects\Adobe API\Short Grain 500 Hex Coordinates.py", line 21, in main
    original = OriginalTextInfo(item)
               ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\John\PycharmProjects\Adobe API\hex_coord.py", line 10, in __init__
    self.font_size = item.TextRange.CharacterAttributes.Size
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\John\PycharmProjects\venv\Lib\site-packages\win32com\client\__init__.py", line 585, in __getattr__
    return self._ApplyTypes_(*args)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\John\PycharmProjects\venv\Lib\site-packages\win32com\client\__init__.py", line 574, in _ApplyTypes_
    self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.com_error: (-2147023170, 'The remote procedure call failed.', None, None)
0

There are 0 best solutions below