how to switch from os.tmpnam to os.tmpfile

  • Thread starter Harold Fellermann
  • Start date
H

Harold Fellermann

Hi,

I need to create a temporary file and I need to retrieve the path of
that file.
os.tmpnam() would do the job quite well if it wasn't for the
RuntimeWarning
"tmpnam is a potential security risk to your program". I would like to
switch
to os.tmpfile() which is supposed to be safer, but I do not know how to
get
the path information from the file object returned by tmpfile(). any
clues?
thanks!

- harold -
 
M

Maric Michaud

Le Jeudi 08 Juin 2006 15:30, Harold Fellermann a écrit :
to os.tmpfile() which is supposed to be safer, but I do not know how to
get
the path information from the file object returned by tmpfile(). any
clues?
There is no path for tmpfile, once it's closed, the file and its content are
lost. from the doc :
" The file has no directory entries associated with it and will be
automatically deleted once there are no file descriptors for the file."

You must maintain a reference to it in your program untill you don't need it
anymore.

--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
 
H

Harold Fellermann

Maric said:
Le Jeudi 08 Juin 2006 15:30, Harold Fellermann a écrit :
There is no path for tmpfile, once it's closed, the file and its content are
lost. from the doc :
" The file has no directory entries associated with it and will be
automatically deleted once there are no file descriptors for the file."

You must maintain a reference to it in your program untill you don't need it
anymore.

I am doing so. But still, I need its path. To give you some context:
I have an app built on Tk that uses gnuplot behind the scenes.
My application creates a temporary file where which gnuplot writes its
results to (using the tkcanvas terminal). Later, I load the contents of
that file into the a tk canvas. I don't care about the temporary file
after my app is closed, so I have its reference all the time. But I
need
its path to tell both gnuplot and tk where to read/write data to/from.

class PlotWindow(Tk.Canvas) :
def plot(self,commands) :
tmp = os.tmpnam()
gnuplot = subprocess.Popen(
"gnuplot", shell=True,
stdin=subprocess.PIPE, stdout=file(tmp,"w")
)
stdout,stderr = gnuplot.communicate("""
set terminal tkcanvas interact
set output "%s"
""" % tmp + commands)
assert not stderr
self.tk.call("source",tmp)
self.tk.call("gnuplot",self._w)

Of course, I could just use matplotlib or Gnuplot.py but the problem
is not necessary enough to make any refacturing. If there is no way
to use os.tmpfile(), I just go ahead with the security warning. Its
only
a small personal app, anyway.

- harold -
 
M

Maric Michaud

Well, I never used gnuplot and I didn't use Tkinter for a while, but :

Le Jeudi 08 Juin 2006 16:44, Harold Fellermann a écrit :
        tmp = os.tmpnam()
        gnuplot = subprocess.Popen(
            "gnuplot", shell=True,
            stdin=subprocess.PIPE, stdout=file(tmp,"w")
        )
        stdout,stderr = gnuplot.communicate("""
            set terminal tkcanvas interact
            set output "%s"
            """ % tmp + commands)
assuming tmp is a os.tmpfile and you connect it to the the stdout of your
gnuplot command, something like :

gnuplot = subprocess.Popen(
"gnuplot", shell=True,
stdin=subprocess.PIPE, stdout=tmp
)
stdout,stderr = gnuplot.communicate("""
set terminal tkcanvas interact
set output /dev/stdout
""" % tmp + commands)

should do the job.
        assert not stderr
        self.tk.call("source",tmp)
You still need to find a way to pass directly a file object to this call.

Also note if that works, and if you don't need to flush the datas on disk (if
they fit in memory), you can use a file-like buffer (StringIO) instead of a
tmpfile, this will save resources.

This said, you should consider writing your temporary file in a directory
owned by you and not world writeable, which is perfectly safe.

--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
 
S

Steve Holden

Harold said:
I am doing so. But still, I need its path. To give you some context:
I have an app built on Tk that uses gnuplot behind the scenes.
My application creates a temporary file where which gnuplot writes its
results to (using the tkcanvas terminal). Later, I load the contents of
that file into the a tk canvas. I don't care about the temporary file
after my app is closed, so I have its reference all the time. But I
need
its path to tell both gnuplot and tk where to read/write data to/from.

class PlotWindow(Tk.Canvas) :
def plot(self,commands) :
tmp = os.tmpnam()
gnuplot = subprocess.Popen(
"gnuplot", shell=True,
stdin=subprocess.PIPE, stdout=file(tmp,"w")
)
stdout,stderr = gnuplot.communicate("""
set terminal tkcanvas interact
set output "%s"
""" % tmp + commands)
assert not stderr
self.tk.call("source",tmp)
self.tk.call("gnuplot",self._w)

Of course, I could just use matplotlib or Gnuplot.py but the problem
is not necessary enough to make any refacturing. If there is no way
to use os.tmpfile(), I just go ahead with the security warning. Its
only
a small personal app, anyway.

Looks like the only way you could handle this is using inherited file
descriptors between processes and have Gnuplot write to its standard
output. In other words, build a pipeline like the shell does.

Of course this only works if the process that has the handle on the temp
file is the same one that starts Gnuplot, but it looks from your code
like that's the case.

regards
Steve
 
C

Chris Lambacher

You should be able to find exactly what you need in the tempfile module.
http://docs.python.org/lib/module-tempfile.html

os.tmpfile() is no good whether you want the filename or not since on Windows
it is likely to break if you are not a privileged user. Its a windows
problem, not an actual bug in Python, the call depends on a broken windows
command that creates its files in C:\


-Chris
 
N

Nick Craig-Wood

Harold Fellermann said:
I need to create a temporary file and I need to retrieve the path
of that file. os.tmpnam() would do the job quite well if it wasn't
for the RuntimeWarning "tmpnam is a potential security risk to your
program". I would like to switch to os.tmpfile() which is supposed
to be safer, but I do not know how to get the path information from
the file object returned by tmpfile(). any clues?

You can't. The file opened doesn't have a name

tmpfile(...)
tmpfile() -> file object

Create a temporary file with no directory entries.

Have a look at the functions in the tempfile module, mkstemp() in
particular (the posix way), or NamedTemporaryFile()

http://docs.python.org/lib/module-tempfile.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top