On Thu, Jan 15, 2015 at 10:35:39PM +0100, Jakub Wilk wrote:
> This is how wx.tools.img2py uses temporary files (with boring parts
> snipped):
> 
>    tfname = tempfile.mktemp()
>    try:
>        ok, msg = convert(image_file, maskClr, None, tfname, 
> wx.BITMAP_TYPE_PNG, ".png")
>        # ...
>    finally:
>        # ...

I've come up with a patch (attached), but I'm not really a Python
programmer, so I'd appreciate a review to make sure I'm not doing
something dumb.

Cheers,
    Olly
diff --git a/config.py b/config.py
index 08f8d6a..de42dd0 100644
--- a/config.py
+++ b/config.py
@@ -377,7 +377,7 @@ def run_swig(files, dir, gendir, package, USE_SWIG, force, swig_args,
                 #i_file = opj(i_file)     #'/'.join(i_file.split('\\'))
 
                 if BUILD_RENAMERS:
-                    xmltemp = tempfile.mktemp('.xml')
+                    xmltemp = tempfile.mkstemp('.xml')[1]
 
                     # First run swig to produce the XML file, adding
                     # an extra -D that prevents the old rename
diff --git a/distrib/build_renamers.py b/distrib/build_renamers.py
index e99acb3..3ab5963 100755
--- a/distrib/build_renamers.py
+++ b/distrib/build_renamers.py
@@ -92,12 +92,10 @@ def main(args):
     swigDest = os.path.join(destdir, "_"+modname+"_rename.i")
     pyDest = os.path.join(wxPythonDir, modname + '.py')
 
-    swigDestTemp = tempfile.mktemp('.tmp')
-    swigFile = open(swigDestTemp, "w")
+    (swigFile, swigDestTemp) = tempfile.mkstemp('.tmp')
     swigFile.write(renamerTemplateStart % sys.argv[0])
 
-    pyDestTemp = tempfile.mktemp('.tmp')
-    pyFile = open(pyDestTemp, "w")
+    (pyFile, pyDestTemp) = tempfile.mkstemp('.tmp')
     pyFile.write(wxPythonTemplateStart % (sys.argv[0], modname))
 
     try:
diff --git a/wx/lib/infoframe.py b/wx/lib/infoframe.py
index 14ab217..a67dcc0 100644
--- a/wx/lib/infoframe.py
+++ b/wx/lib/infoframe.py
@@ -68,7 +68,7 @@ The latter statement, of course, may be repeated arbitrarily often.
 The window will not appear until it is written to, and it may be
 manually closed by the user, after which it will reappear again until
 written to... Also note that all output is echoed to a file with a
-randomly-generated name [see the mktemp module in the standard
+randomly-generated name [see the tempfile module in the standard
 library], in the directory given as the 'dir' keyword argument to the
 InformationalMessagesFrame constructor [which has a default value of
 '.'), or set via the method SetOutputDirectory... This file will be
@@ -302,27 +302,26 @@ class PyInformationalMessagesFrame(object):
                         "below, allowing you to select a "
                         "new file...\n\n")
                 else:
-                    tempfile.tempdir = self.dir
-                    filename = os.path.abspath(tempfile.mktemp ())
-
-                    self.text.AppendText(
-                        "Please close this window (or select the "
-                        "'Dismiss' button below) when desired.  By "
-                        "default all messages written to this window "
-                        "will also be written to the file '%s'--you "
-                        "may close this file by selecting 'Close "
-                        "File' below, whereupon this button will be "
-                        "replaced with one allowing you to select a "
-                        "new file...\n\n" % filename)
                     try:
-                        self.f = open(filename, 'w')
+                        (self.f,filename) = tempfile.mkstemp(dir=self.dir)
+                        filename = os.path.abspath(filename)
+
+                        self.text.AppendText(
+                            "Please close this window (or select the "
+                            "'Dismiss' button below) when desired.  By "
+                            "default all messages written to this window "
+                            "will also be written to the file '%s'--you "
+                            "may close this file by selecting 'Close "
+                            "File' below, whereupon this button will be "
+                            "replaced with one allowing you to select a "
+                            "new file...\n\n" % filename)
                         self.frame.sb.SetStatusText("File '%s' opened..."
                                                     % filename,
                                                     0)
                     except EnvironmentError:
-                        self.frame.sb.SetStatusText("File creation failed "
-                                                    "(filename '%s')..."
-                                                    % filename,
+                        self.frame.sb.SetStatusText("Failed to create file "
+                                                    "in directory '%s'..."
+                                                    % self.dir,
                                                     0)
             self.text.AppendText(string)
 
diff --git a/wx/tools/img2py.py b/wx/tools/img2py.py
index 8299f76..2baa6aa 100644
--- a/wx/tools/img2py.py
+++ b/wx/tools/img2py.py
@@ -150,7 +150,7 @@ def img2py(image_file, python_file,
         app = wx.App()
         
     # convert the image file to a temporary file
-    tfname = tempfile.mktemp()
+    tfname = tempfile.mkstemp()[1]
     try:
         ok, msg = convert(image_file, maskClr, None, tfname, wx.BITMAP_TYPE_PNG, ".png")
         if not ok:
@@ -168,8 +168,7 @@ def img2py(image_file, python_file,
             lines.append(output)
         data = "\n".join(lines)
     finally:
-        if os.path.exists(tfname):
-            os.remove(tfname)
+        os.remove(tfname)
 
     old_index = []
     if catalog and append and python_file != '-':

Reply via email to