Exception holes, again.

David Bolen db3l at fitlinxx.com
Tue Nov 6 16:43:54 EST 2001


Dale Strickland-Clark <dale at riverhall.NOTHANKS.co.uk> writes:

> Traceback (most recent call last):
>   File "I:\BATCH\tests\testreg2.py", line 6, in ?
>     print win32api.RegQueryValueEx(key, 'Spam')
> pywintypes.api_error: (2, 'RegQueryValueEx', 'The system cannot find
> the file specified.')
> 
> Doesn't tell you much, does it!

Well, the error is clear, but yes, the error object name can be
misleading.

The confusing part I think is that the underlying object always
displays itself as pywintypes.api_error, but is referenced as the
"error" object from all of the win32 modules (including pywintypes
itself) and not "api_error".

Thus, if you import win32api, you can reference the error object with
"win32api.error", but during a traceback it still prints as
pywintypes.api_error.

    >>> import win32api
    >>> print win32api.error
    pywintypes.api_error

But the object is also defined in pywintypes:

    >>> import pywintypes
    >>> print pywintypes.error
    pywintypes.api_error

and you can use pywintypes.error as an exception class to trap errors
in any of the win32 modules.

I think (but haven't looked at the source for a while) that there's
really only a single API error object, and the other modules just
contain references to it, thus they all print as the same object, and
it's primary existence is in the pywintypes module.  Yep, that seems
to be the case:

    >>> import pywintypes, win32api
    >>> print id(pywintypes.error), id(win32api.error)
    8358240 8358240

> I've tried to find pywintypes but can't and I had a hunt through the
> Python directories to see if I could find the definition but came up
> with nothing.

This and pythoncom is a strange case.  Both exist as DLLs in your
system directory, but based on a Python release (e.g.,
PyWinTypes15.dll or PyWinTypes21.dll).  There is then a registry entry
(under HKLM\Software\Python\PythonCore\#.#\Modules\pythoncom) that
references the appropriate DLL module out of the system directory.
It's the registry key name that is used as the module name, thus just
"pywintypes" can be used rather than "pywintypes15".

I'm not positive why things were structured this way (the win32
extensions are the only uses of that Modules key that I've seen, but
I'm sure Mark had a good reason), but it may have something to do with
how the code may be invoked via COM or services, or whatever.  This
also can confuse packagers like installer or py2exe too so it's sort
of a pain.  But it's this translation that occurs when pywintypes or
pythoncom is imported.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list