Suraj Barkale schrieb:
> Hi,
> I am trying to automate Lotus Notes using comtypes and want to handle
> error conditions (e.g. wrong password) by catching COM exceptions.
> Unfortunately, Lotus Notes always gives error code -2147217504. So I
> decided to check the error description. Following is a sample COMError
> object:
>
> COMError(-2147217504, None, (u'Notes error: Wrong Password. (Passwords
> are case sensitive - be sure to use correct upper and lower case.)',
> u'NotesSession', None, 0, None))
>
> I checked the code of COMError in
> http://svn.python.org/view/python/tags/r251/Modules/_ctypes/_ctypes.c?rev=54864&view=auto
> and is seems to me that COMError objects should have "hresult, text,
> details" attributes. However, the COMError object I caught had only
> args attribute.
>
> My questions are:
> 1. err.args[2][0] returns description. Can I get away with
> using err.args[2][0] without checking for error bounds (assuming only
> COMError is caught)?
I think so; at least that is the intent.
> 2. Will COMError grow "hresult, text, details" attributes in future?
It is a bug in Python 2.5 and 2.5.1 _ctypes module that COMError does not
have these attributes. The bug is not present in Python 2.4.
I'll fix this in Python SVN asap; fortunately it seems that it is possible
to monkeypatch comtypes' COMError to add a workaround for the bug.
If you add the following code snippet to comtypes\__init__.py you can try
it out; I have already committed this to comtypes svn and added a test.
Please try it out if it works for you.
Thanks,
Thomas
<snippet>
try:
COMError()
except TypeError:
pass
else:
# Python 2.5 and 2.5.1 have a bug in the COMError implementation:
# The type has no __init__ method, and no hresult, text, and
# details instance vars. Work around this bug by monkeypatching
# COMError.
def monkeypatch_COMError():
def __init__(self, hresult, text, details):
self.hresult = hresult
self.text = text
self.details = details
super(COMError, self).__init__(hresult, text, details)
COMError.__init__ = __init__
def __repr__(self):
return "%s(%r, %r, %r)" % (self.__class__.__name__, self.hresult,
self.text, self.details)
COMError.__repr__ = __repr__
monkeypatch_COMError()
del monkeypatch_COMError
</snippet>
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
comtypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/comtypes-users