Removing loaded pyd files

Viewed 1413

I'm writing an updater for an application. I need to remove the old files as they're running. This works fine until I hit dll files that I'm using. For various reasons I have to bundle the _ssl compiled library with my distribution.

I'm firstly freeing the library, which for whatever reason requires two calls to actually remove the handle (I'm assuming there is refcounting going on somewhere). After this, I try and remove the file. This results in Python dying with a 0xC0000005 which is apparently a very broad exit code.

import ssl
import ctypes
import _ctypes

print "Unloading SSL Module"
ssl_filename = ssl._ssl.__file__
dll = ctypes.CDLL(ssl_filename)
if ssl_filename.endswith("pyd"):
    _ctypes.FreeLibrary(dll._handle)
    _ctypes.FreeLibrary(dll._handle)
else:
    _ctypes.dlclose(dll._handle)
    _ctypes.dlclose(dll._handle)
print "Deleting current version..."
os.remove(ssl_filename)
print "Success"

Upon execution I get the following:

Unloading SSL Module

__builtin__ C:\path\to\_ssl.pyd 0 Deleting current version...

Process finished with exit code -1073741819 (0xC0000005)

What I'm expecting is for the code to remove the file and continue as normal.

0 Answers
Related