mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
More explicit closing of resources
This commit is contained in:
parent
6e3b948bc9
commit
89f9b14fc2
@ -70,6 +70,7 @@ def pdftotext(path):
|
|||||||
cmd = [PDFTOTEXT] + '-enc UTF-8 -nodiag -eol unix'.split() + [os.path.basename(path), '-']
|
cmd = [PDFTOTEXT] + '-enc UTF-8 -nodiag -eol unix'.split() + [os.path.basename(path), '-']
|
||||||
p = popen(cmd, cwd=os.path.dirname(path), stdout=subprocess.PIPE, stdin=subprocess.DEVNULL)
|
p = popen(cmd, cwd=os.path.dirname(path), stdout=subprocess.PIPE, stdin=subprocess.DEVNULL)
|
||||||
raw = p.stdout.read()
|
raw = p.stdout.read()
|
||||||
|
p.stdout.close()
|
||||||
if p.wait() != 0:
|
if p.wait() != 0:
|
||||||
return ''
|
return ''
|
||||||
return clean_ascii_chars(raw).decode('utf-8', 'replace')
|
return clean_ascii_chars(raw).decode('utf-8', 'replace')
|
||||||
|
@ -32,9 +32,14 @@ def parse_opf_version(raw):
|
|||||||
|
|
||||||
def parse_opf(stream_or_path):
|
def parse_opf(stream_or_path):
|
||||||
stream = stream_or_path
|
stream = stream_or_path
|
||||||
if not hasattr(stream, 'read'):
|
needs_close = not hasattr(stream, 'read')
|
||||||
|
if needs_close:
|
||||||
stream = open(stream, 'rb')
|
stream = open(stream, 'rb')
|
||||||
raw = stream.read()
|
try:
|
||||||
|
raw = stream.read()
|
||||||
|
finally:
|
||||||
|
if needs_close:
|
||||||
|
stream.close()
|
||||||
if not raw:
|
if not raw:
|
||||||
raise ValueError('Empty file: '+getattr(stream, 'name', 'stream'))
|
raise ValueError('Empty file: '+getattr(stream, 'name', 'stream'))
|
||||||
raw, encoding = xml_to_unicode(raw, strip_encoding_pats=True, resolve_entities=True, assume_utf8=True)
|
raw, encoding = xml_to_unicode(raw, strip_encoding_pats=True, resolve_entities=True, assume_utf8=True)
|
||||||
|
@ -1478,7 +1478,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
|
|||||||
f = open(path, 'rb')
|
f = open(path, 'rb')
|
||||||
except OSError:
|
except OSError:
|
||||||
time.sleep(0.2)
|
time.sleep(0.2)
|
||||||
f = open(path, 'rb')
|
f = open(path, 'rb')
|
||||||
with f:
|
with f:
|
||||||
if hasattr(dest, 'write'):
|
if hasattr(dest, 'write'):
|
||||||
shutil.copyfileobj(f, dest)
|
shutil.copyfileobj(f, dest)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user