Skip to content

Commit

Permalink
Use tempfile.mkstemp to stay compatible with Python 2.5 (it does …
Browse files Browse the repository at this point in the history
…not have the ``delete`` option on ``NamedTemporaryFile``).
  • Loading branch information
malthe committed Mar 3, 2011
1 parent f05d44a commit 2c5e94b
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/chameleon/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,19 @@ def build(self, source, filename):
base, ext = os.path.splitext(filename)
name = os.path.join(self.path, base + ".py")
log.debug("writing source to disk (%d bytes)." % len(source))
temp = tempfile.NamedTemporaryFile(
prefix=base, suffix='.tmp', dir=self.path, delete=False)
fd, fn = tempfile.mkstemp(prefix=base, suffix='.tmp', dir=self.path)
temp = open(fn, 'w')
try:
try:
temp.write("%s\n" % '# -*- coding: utf-8 -*-')
temp.write(source)
finally:
temp.close()
except:
os.remove(temp.name)
os.remove(fn)
raise

os.rename(temp.name, name)
os.rename(fn, name)
log.debug("compiling %s into byte-code..." % filename)
py_compile.compile(name)

Expand Down

0 comments on commit 2c5e94b

Please sign in to comment.