home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_win32comext_axdebug_util.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  3.3 KB  |  116 lines

  1. # Utility function for wrapping objects.  Centralising allows me to turn
  2. # debugging on and off for the entire package in a single spot.
  3.  
  4. import sys
  5. import win32com.server.util
  6. from win32com.server.exception import Exception
  7. import winerror
  8. import win32api
  9. import os
  10.  
  11. try:
  12.     os.environ["DEBUG_AXDEBUG"]
  13.     debugging = 1
  14. except KeyError:
  15.     debugging = 0
  16.  
  17. def trace(*args):
  18.     if not debugging: return
  19.     print str(win32api.GetCurrentThreadId()) + ":",
  20.     for arg in args:
  21.         print arg,
  22.     print
  23.  
  24. # The AXDebugging implementation assumes that the returned COM pointers are in
  25. # some cases identical.  Eg, from a C++ perspective:
  26. # p->GetSomeInterface( &p1 );
  27. # p->GetSomeInterface( &p2 );
  28. # p1==p2
  29. # By default, this is _not_ true for Python.
  30. # (Now this is only true for Document objects, and Python
  31. # now does ensure this.
  32.  
  33. all_wrapped = {} 
  34.  
  35. def _wrap_nodebug(object, iid):
  36.     return win32com.server.util.wrap(object, iid)
  37.  
  38. def _wrap_debug(object, iid):
  39.     import win32com.server.policy
  40.     dispatcher = win32com.server.policy.DispatcherWin32trace
  41.     return win32com.server.util.wrap(object, iid, useDispatcher = dispatcher)
  42.  
  43. if debugging:
  44.     _wrap = _wrap_debug
  45. else:
  46.     _wrap = _wrap_nodebug
  47.  
  48. def _wrap_remove(object, iid = None):
  49.     # Old - no longer used or necessary!
  50.     return
  51.  
  52. def _dump_wrapped():
  53.     from win32com.server.util import unwrap
  54.     print "Wrapped items:"
  55.     for key, items in all_wrapped.items():
  56.         print key,
  57.         try:
  58.             ob = unwrap(key)
  59.             print ob, sys.getrefcount(ob)
  60.         except:
  61.             print "<error>"
  62.  
  63.  
  64. def RaiseNotImpl(who = None):
  65.     if who is not None:
  66.         print "********* Function %s Raising E_NOTIMPL  ************" % (who)
  67.  
  68.     # Print a sort-of "traceback", dumping all the frames leading to here.        
  69.     try:
  70.         1/0
  71.     except:
  72.         frame = sys.exc_info()[2].tb_frame
  73.     while frame:
  74.         print "File: %s, Line: %d" % (frame.f_code.co_filename, frame.f_lineno)
  75.         frame = frame.f_back
  76.     
  77.     # and raise the exception for COM
  78.     raise Exception(scode=winerror.E_NOTIMPL)
  79.         
  80.  
  81. import win32com.server.policy
  82. class Dispatcher(win32com.server.policy.DispatcherWin32trace):
  83.     def __init__(self, policyClass, object):
  84.         win32com.server.policy.DispatcherTrace.__init__(self, policyClass, object)
  85.         import win32traceutil # Sets up everything.
  86. #        print "Object with win32trace dispatcher created (object=%s)" % `object`
  87.  
  88.     def _QueryInterface_(self, iid):
  89.         rc = win32com.server.policy.DispatcherBase._QueryInterface_(self, iid)
  90. #        if not rc:
  91. #            self._trace_("in _QueryInterface_ with unsupported IID %s (%s)\n" % (IIDToInterfaceName(iid),iid))
  92.         return rc
  93.  
  94.     def _Invoke_(self, dispid, lcid, wFlags, args):
  95.         print "In Invoke with", dispid, lcid, wFlags, args, "with object",self.policy._obj_
  96.         try:
  97.             rc = win32com.server.policy.DispatcherBase._Invoke_(self, dispid, lcid, wFlags, args)
  98. #            print "Invoke of", dispid, "returning", rc
  99.             return rc
  100.         except Exception:
  101.             t, v, tb = sys.exc_info()
  102.             tb = None # A cycle
  103.             scode = v.scode
  104.             try:
  105.                 desc = " (" + str(v.description) + ")"
  106.             except AttributeError:
  107.                 desc = ""
  108.             print "*** Invoke of %s raised COM exception 0x%x%s" % (dispid, scode, desc)
  109.         except:
  110.             print "*** Invoke of %s failed:" % dispid
  111.             typ, val, tb = sys.exc_info()
  112.             import traceback
  113.             traceback.print_exception(typ, val, tb)
  114.             raise typ, val, tb
  115.  
  116.