Debugging Python COM Objects
When you use COM clients such as Excel from Python, you can employ the same debugging techniques as for any Python code; you are simply calling Python objects. However, when you implement COM objects in Python, things become more difficult. In this case, the caller of your Python code isn’t Python, but another application, such as Visual Basic or Delphi. These applications obviously have no concept of a Python exception or a Python traceback, so finding bugs in your Python code can be a problem.
In a nutshell: register your COM objects using
-debug on the
command line. Then use the Trace Collector
Debugging Tool item on the PythonWin Tools menu to see any print
statements or Python exceptions. The rest of this section is devoted
to how this works.
To assist with the debugging problem, the Python COM framework has
the concept of a
dispatcher
.
A dispatcher is similar to a policy object, but dispatches calls to
the policy. The
win32com
package provides a number of useful dispatchers.
When you register the COM Server with
--debug (note the double hyphen), the registration
mechanism also registers a dispatcher for your object. The default
dispatcher is known as
DispatcherWin32trace,
although you can specify a
different dispatcher using the
_reg_debug_dispatcher_spec_ attribute on your
object, as described in the earlier section Registering Your COM Server.
The default DispatcherWin32trace uses the
win32trace
module to display its output. To see the output ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access