[python-win32] python com server exceptions don't make it to .NET / CLR app

martin at martindengler.com martin at martindengler.com
Wed Mar 29 18:45:06 CEST 2006


Hi,

Has anyone seen exceptions raised from their python com server code get across to a .NET application (C# especially) with its *args somewhere in the C# COMException (probably the Exception.InnerException)?

Couple of specific questions below, but the general gist is that the following code lets VB see the IErrorInfo.text field, but although the stack trace makes it over to a C# exception, none of the *args or IErrorInfo members do:

----

import inspect
import pythoncom
import winerror

[...]

def _create_com_ex(e, frames):
    #see O'Reilly's Python programming for win32
    #google cache: http://72.14.203.104/search?q=cache:UHfawJfSAP0J:ngharo.com/~nick/oreilly/books/pythonwin32/ch12.htm+win32com+isupporterrorinfo&hl=en&ct=clnk&cd=21

    errorcode = winerror.DISP_E_EXCEPTION #now we've committed to create an IErrorInfo
    errorcode = winerror.DISP_E_MEMBERNOTFOUND

    #some debugging sugar
    desc = "%s: -- %s" % (type(e), str(e))
    frames.reverse()
    pretty_frames = "....".join(["%s:%s" % info[1:3] for info in frames])

    #IErrorInfo members
    wCode = 0   #one of wCode or sCode must be zero
    source = str(frames[0])
    text = "%s\r\n%s" % (desc, pretty_frames)
    helpFile = "comutils.py"
    helpId = 0
    sCode = winerror.E_FAIL #from win32com's errorSemantics.py best practice

    ierrorinfo = (wCode, source, text, helpFile, helpId, sCode)

    err = pythoncom.com_error(errorcode, desc, ierrorinfo, None)

    return err

[....inside a pythoncom IDispath subclass...]

    def test_exception(msg):
	raise _create_com_ex(msg, inspect.trace())

[...]

----



The "text" variable comes over fine to VB, but the following C#...


----

using System;

using MyPythonCOMServer;

namespace ConsoleApplication1
{
	class Class1
	{
		[STAThread]       
		static void Main(string[] args)
		{
	            IMyServer s = new MyPythonCOMServer.CServer();
	            try {
	                s.test_exception("goodbye cruel world");
	            } catch (Exception e) {
	                Console.WriteLine(String.Format(@"

C# stuff visible here....
Type:           {0}
Message:        {1}
InnerException: {2}
StackTrace:
{3}

TargetSite:     {4}
HelpLink:       {5}
",
	                    e.GetType(),
        	            e.Message,
                	    e.InnerException,
	                    e.StackTrace,
	                    e.TargetSite,
	                    e.HelpLink));
	                Console.WriteLine("Success");
	            }
		}
	}
}

----


...prints:



----

pythoncom error: Failed to call the universal dispatcher
Traceback (most recent call last):
  File "C:\Python23\Lib\site-packages\win32com\universal.py", line 173, in dispatch
    retVal = ob._InvokeEx_(meth.dispid, 0, meth.invkind, args, None, None)
  File "C:\Python23\Lib\site-packages\win32com\server\policy.py", line 322, in _InvokeEx_
    return self._invokeex_(dispid, lcid, wFlags, args, kwargs, serviceProvider)
  File "...MyPythonCOMServer.py", line 791, in _invokeex_
    kwargs, serviceProvider)
  File "...MyPythonCOMServer.py", line 653, in _invokeex_
    raise _create_com_ex(e, inspect.trace())
pywintypes.com_error: (-2147352573, "<type 'instance'>: -- goodbye cruel world", (0, '(<frame object at 0x00939810>, \'....\', 92, \'test_exception\', ...... , "C:\\Python23\\Lib\\site-packages\\win32com\\server\\policy.py:541...., 0, -2147467259), None)


C# stuff visible here....
Type:           System.Runtime.InteropServices.COMException
Message:        Unspecified error
InnerException:
StackTrace:
   at MyPythonCOMServer.CServer.test_exception(String msg)
   at ConsoleApplication1.Class1.Main(String[] args) in c:\temp\consoleapplication1\class1.cs:line 23

TargetSite:     Void test_exception(System.String)
HelpLink:

----



So:

1) What is pythoncom.com_error() and the code that handles it doing to get VB to see the (presumably) IErrorInfo?  I'm sorry if I've missed where I can get the source as presumably it's the C unmarshaling code that's in the C# process calling SetLastError() or something, but it'd be nice to know how it's done.  All the (nice) presentations / books by the experts basically stop at "it'll get done if you use win32com.server.exception.COMException, don't worry about how".

2) Has anyone gotten any win32com.server.exception.COMExceptions across to C# with the string/Exception passed to raise()?

Thanks,
Martin



-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 191 bytes
Desc: not available
Url : http://mail.python.org/pipermail/python-win32/attachments/20060329/89aa7a00/attachment.pgp 


More information about the Python-win32 mailing list