Skip to content

Commit dc51912

Browse files
committed
Check for PermissionError on temporary files
1 parent 80b926a commit dc51912

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/sscg/__init__.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
from __future__ import print_function
3333
import os
34+
import sys
3435
import tempfile
3536
import gettext
3637

@@ -65,15 +66,20 @@ def write_secure_file(options, destination, data):
6566
# Create the temporary file in the same directory as the destination
6667
# This ensures that we can atomically move it to the final name.
6768

68-
f = tempfile.NamedTemporaryFile(dir=os.path.dirname(destination),
69-
delete=False)
69+
try:
70+
f = tempfile.NamedTemporaryFile(dir=os.path.dirname(destination),
71+
delete=False)
72+
except PermissionError:
73+
raise SSCGIOError(_("Could not create tempfile in {0}. Error: {1}").format(
74+
os.path.dirname(destination), sys.exc_info()[1]))
75+
7076
try:
7177
f.write(data)
7278
f.flush()
7379
except IOError as e:
7480
f.close()
7581
os.unlink(f.name)
76-
raise Exception(_("Could not write to {0}. Error: {1}").format(f.name, e))
82+
raise SSCGIOError(_("Could not write to {0}. Error: {1}").format(f.name, e))
7783

7884
# Now atomically move the temporary file into place.
7985
# We use os.rename because this is guaranteed to be atomic if it succeeds

0 commit comments

Comments
 (0)