From fd5901b00e21d191f045b81ac6fa8922d4a9d0d8 Mon Sep 17 00:00:00 2001 From: Laurent Bachelier Date: Sat, 27 Jul 2013 15:42:23 +0200 Subject: [PATCH] Use NamedTemporaryFile(delete=False) instead of mkstemp --- weboob/applications/webcontentedit/webcontentedit.py | 5 ++--- weboob/tools/config/yamlconfig.py | 7 ++----- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/weboob/applications/webcontentedit/webcontentedit.py b/weboob/applications/webcontentedit/webcontentedit.py index ec9f0992..3b8ed05d 100644 --- a/weboob/applications/webcontentedit/webcontentedit.py +++ b/weboob/applications/webcontentedit/webcontentedit.py @@ -66,8 +66,7 @@ class WebContentEdit(ReplApplication): tmpdir = os.path.join(tempfile.gettempdir(), "weboob") if not os.path.isdir(tmpdir): os.makedirs(tmpdir) - fd, path = tempfile.mkstemp(prefix='%s_' % content.id.replace(os.path.sep, '_'), dir=tmpdir) - with os.fdopen(fd, 'w') as f: + with tempfile.NamedTemporaryFile(prefix='%s_' % content.id.replace(os.path.sep, '_'), dir=tmpdir, delete=False) as f: data = content.content if isinstance(data, unicode): data = data.encode('utf-8') @@ -75,7 +74,7 @@ class WebContentEdit(ReplApplication): content.content = u'' data = '' f.write(data) - paths[path.encode('utf-8')] = content + paths[f.name.encode('utf-8')] = content params = '' editor = os.environ.get('EDITOR', 'vim') diff --git a/weboob/tools/config/yamlconfig.py b/weboob/tools/config/yamlconfig.py index a9f4fea9..16015bb7 100644 --- a/weboob/tools/config/yamlconfig.py +++ b/weboob/tools/config/yamlconfig.py @@ -18,8 +18,6 @@ # along with weboob. If not, see . - - import os import tempfile import logging @@ -60,10 +58,9 @@ class YamlConfig(IConfig): def save(self): # write in a temporary file to avoid corruption problems - fd, path = tempfile.mkstemp(dir=os.path.dirname(self.path)) - with os.fdopen(fd, 'w') as f: + with tempfile.NamedTemporaryFile(dir=os.path.dirname(self.path), delete=False) as f: yaml.dump(self.values, f, Dumper=Dumper) - os.rename(path, self.path) + os.rename(f.name, self.path) def get(self, *args, **kwargs): default = None