Skip to content

Commit

Permalink
Closing all temporary file handles properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Pontus Stenetorp committed May 7, 2013
1 parent 5714e7a commit e4043b7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
5 changes: 3 additions & 2 deletions server/src/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from codecs import open as codecs_open
from functools import partial
from itertools import chain, takewhile
from os import utime
from os import close as os_close, utime
from time import time
from os.path import join as path_join
from os.path import basename, splitext
Expand Down Expand Up @@ -964,7 +964,8 @@ def __exit__(self, type, value, traceback):
# so we hack around it.
#with NamedTemporaryFile('w', suffix='.ann') as tmp_file:
# Grab the filename, but discard the handle
_, tmp_fname = mkstemp(suffix='.ann')
tmp_fh, tmp_fname = mkstemp(suffix='.ann')
os_close(tmp_fh)
try:
with open_textfile(tmp_fname, 'w') as tmp_file:
#XXX: Temporary hack to make sure we don't write corrupted
Expand Down
6 changes: 4 additions & 2 deletions server/src/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from __future__ import with_statement

from os import remove
from os import close as os_close, remove
from os.path import join as path_join, dirname, basename
from tempfile import mkstemp

Expand Down Expand Up @@ -42,7 +42,9 @@ def download_collection(collection, exclude_configs=False):

tmp_file_path = None
try:
_, tmp_file_path = mkstemp()
tmp_file_fh, tmp_file_path = mkstemp()
os_close(tmp_file_fh)

tar_cmd_split = ['tar', '--exclude=.stats_cache']
if exclude_configs:
tar_cmd_split.extend(['--exclude=annotation.conf',
Expand Down
3 changes: 2 additions & 1 deletion server/src/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ def load_conf():
init_session('127.0.0.1')
tmp_file_path = None
try:
_, tmp_file_path = mkstemp()
tmp_file_fh, tmp_file_path = mkstemp()
os_close(tmp_file_fh)
session = get_session()
session['foo'] = 'bar'
with open(tmp_file_path, 'wb') as tmp_file:
Expand Down

0 comments on commit e4043b7

Please sign in to comment.