mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-07 10:14:46 -04:00
Use a less failure prone implementation of temporary file cleanup
This commit is contained in:
parent
0f9ae27ee4
commit
c64b39ee95
@ -16,8 +16,7 @@
|
|||||||
Provides platform independent temporary files that persist even after
|
Provides platform independent temporary files that persist even after
|
||||||
being closed.
|
being closed.
|
||||||
"""
|
"""
|
||||||
import tempfile
|
import tempfile, os, atexit
|
||||||
import os
|
|
||||||
|
|
||||||
from libprs500 import __version__, __appname__
|
from libprs500 import __version__, __appname__
|
||||||
|
|
||||||
@ -32,7 +31,8 @@ class _TemporaryFileWrapper(object):
|
|||||||
|
|
||||||
def __init__(self, _file, name):
|
def __init__(self, _file, name):
|
||||||
self.file = _file
|
self.file = _file
|
||||||
self.name = name
|
self.name = name
|
||||||
|
atexit.register(cleanup, name)
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
_file = self.__dict__['file']
|
_file = self.__dict__['file']
|
||||||
@ -42,19 +42,20 @@ class _TemporaryFileWrapper(object):
|
|||||||
return a
|
return a
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
try:
|
self.close()
|
||||||
import os # Needs to be here as the main os may no longer exist
|
|
||||||
self.close()
|
def cleanup(path):
|
||||||
if self.name and os.access(self.name, os.F_OK):
|
try:
|
||||||
os.remove(self.name)
|
import os
|
||||||
except: # An error just means that deleting of temporary file failed
|
if os.path.exists(path):
|
||||||
pass
|
os.remove(path)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
def PersistentTemporaryFile(suffix="", prefix="", dir=None):
|
def PersistentTemporaryFile(suffix="", prefix="", dir=None):
|
||||||
"""
|
"""
|
||||||
Return a temporary file that is available even after being closed on
|
Return a temporary file that is available even after being closed on
|
||||||
all platforms. It is automatically deleted when this object is deleted.
|
all platforms. It is automatically deleted on normal program termination.
|
||||||
Uses tempfile.mkstemp to create the file. The file is opened in mode 'wb'.
|
Uses tempfile.mkstemp to create the file. The file is opened in mode 'wb'.
|
||||||
"""
|
"""
|
||||||
if prefix == None:
|
if prefix == None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user