windll exception
Gordon McMillan
gmcm at hypernet.com
Mon Dec 20 16:13:06 EST 1999
Mike Palmer wrote:
> Below is a simple test file I wrote. It opens bwcc32.dll, and
> runs the BWCCGetVersion() function to get the version, then
> prints the version as a hex value. All that works fine.
>
> As the output shows, an exception occurs when the program
> finishes, but ONLY if I run it from the command line (NT4, SP4).
> If open PythonWin and run it using execfile(), no exception is
> reported.
>
> I thought the problem might be related to the bwcc.unload() line,
> but it doesn't matter whether I include that line or not. The
> error message is the same.
>
> This leads me to believe that Python is generating the error on
> cleanup, and that somehow Python thinks None has been attached to
> a class in the same way that bwcc was when I called
> windll.module(). I suspect that I don't see the error from within
> PythonWin because it's only generated when PythonWin closes, and
> there probably isn't any mechanism to trap exceptions and report
> them on closing.
>
> Can anyone confirm this? Is there any known fix or workaround?
You got most of it.
The solution goes something like this:
def __del__(self)
xxx.free_library()
becomes:
def __del__(self, xxx=xxx):
xxx.free_library()
The __del__ method (or whatever cleanup function) needs to
squirrel away a reference to the module it is using. Otherwise,
Python's order-of-destruction will clean up the module and set
it to None, yielding the AttributeError.
> Thanks,
>
> -- Mike --
>
> ------- Python windll test file -------
> # Python windll test
> import windll
> bwcc = windll.module('bwcc32')
> ver = bwcc.BWCCGetVersion()
> verstr = "%x" % ver
> print verstr # should print '10200'
> bwcc.unload()
> ----------------------------------------
>
> ------- Output -------
> 10200
> Exception exceptions.AttributeError: "'None' object has no
> attribute 'free_library'" in <method module.__del__ of module
> instance at 7fd610> ignored
> ----------------------
>
>
>
>
> --
> http://www.python.org/mailman/listinfo/python-list
- Gordon
More information about the Python-list
mailing list