how to switch from os.tmpnam to os.tmpfile

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Harold Fellermann

    #1

    how to switch from os.tmpnam to os.tmpfile

    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 -

  • Maric Michaud

    #2
    Re: how to switch from os.tmpnam to os.tmpfile

    Le Jeudi 08 Juin 2006 15:30, Harold Fellermann a écrit :[color=blue]
    > 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?[/color]
    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

    Comment

    • Harold Fellermann

      #3
      Re: how to switch from os.tmpnam to os.tmpfile


      Maric Michaud wrote:[color=blue]
      > Le Jeudi 08 Juin 2006 15:30, Harold Fellermann a écrit :[color=green]
      > > 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?[/color]
      > 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 needit
      > anymore.[/color]

      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.C anvas) :
      def plot(self,comma nds) :
      tmp = os.tmpnam()
      gnuplot = subprocess.Pope n(
      "gnuplot", shell=True,
      stdin=subproces s.PIPE, stdout=file(tmp ,"w")
      )
      stdout,stderr = gnuplot.communi cate("""
      set terminal tkcanvas interact
      set output "%s"
      """ % tmp + commands)
      assert not stderr
      self.tk.call("s ource",tmp)
      self.tk.call("g nuplot",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 -

      Comment

      • Maric Michaud

        #4
        Re: how to switch from os.tmpnam to os.tmpfile

        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 :[color=blue]
        >         tmp = os.tmpnam()
        >         gnuplot = subprocess.Pope n(
        >             "gnuplot", shell=True,
        >             stdin=subproces s.PIPE, stdout=file(tmp ,"w")
        >         )
        >         stdout,stderr = gnuplot.communi cate("""
        >             set terminal tkcanvas interact
        >             set output "%s"
        >             """ % tmp + commands)[/color]
        assuming tmp is a os.tmpfile and you connect it to the the stdout of your
        gnuplot command, something like :

        gnuplot = subprocess.Pope n(
        "gnuplot", shell=True,
        stdin=subproces s.PIPE, stdout=tmp
        )
        stdout,stderr = gnuplot.communi cate("""
        set terminal tkcanvas interact
        set output /dev/stdout
        """ % tmp + commands)

        should do the job.
        [color=blue]
        >         assert not stderr
        >         self.tk.call("s ource",tmp)[/color]
        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

        Comment

        • Chris Lambacher

          #5
          Re: how to switch from os.tmpnam to os.tmpfile

          You should be able to find exactly what you need in the tempfile module.
          Source code: Lib/tempfile.py This module creates temporary files and directories. It works on all supported platforms. TemporaryFile, NamedTemporaryFile, TemporaryDirectory, and SpooledTemporaryFil...


          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

          On Thu, Jun 08, 2006 at 07:44:38AM -0700, Harold Fellermann wrote:[color=blue]
          >
          > Maric Michaud wrote:[color=green]
          > > Le Jeudi 08 Juin 2006 15:30, Harold Fellermann a ?crit :[color=darkred]
          > > > 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?[/color]
          > > 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.[/color]
          >
          > 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.C anvas) :
          > def plot(self,comma nds) :
          > tmp = os.tmpnam()
          > gnuplot = subprocess.Pope n(
          > "gnuplot", shell=True,
          > stdin=subproces s.PIPE, stdout=file(tmp ,"w")
          > )
          > stdout,stderr = gnuplot.communi cate("""
          > set terminal tkcanvas interact
          > set output "%s"
          > """ % tmp + commands)
          > assert not stderr
          > self.tk.call("s ource",tmp)
          > self.tk.call("g nuplot",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 -
          >
          > --
          > http://mail.python.org/mailman/listinfo/python-list[/color]

          Comment

          • Steve Holden

            #6
            Re: how to switch from os.tmpnam to os.tmpfile

            Harold Fellermann wrote:[color=blue]
            > Maric Michaud wrote:
            >[color=green]
            >>Le Jeudi 08 Juin 2006 15:30, Harold Fellermann a écrit :
            >>[color=darkred]
            >>>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?[/color]
            >>
            >>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
            >>automatical ly 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.[/color]
            >
            >
            > 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.C anvas) :
            > def plot(self,comma nds) :
            > tmp = os.tmpnam()
            > gnuplot = subprocess.Pope n(
            > "gnuplot", shell=True,
            > stdin=subproces s.PIPE, stdout=file(tmp ,"w")
            > )
            > stdout,stderr = gnuplot.communi cate("""
            > set terminal tkcanvas interact
            > set output "%s"
            > """ % tmp + commands)
            > assert not stderr
            > self.tk.call("s ource",tmp)
            > self.tk.call("g nuplot",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.[/color]

            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
            --
            Steve Holden +44 150 684 7255 +1 800 494 3119
            Holden Web LLC/Ltd http://www.holdenweb.com
            Love me, love my blog http://holdenweb.blogspot.com
            Recent Ramblings http://del.icio.us/steve.holden

            Comment

            • Harold Fellermann

              #7
              Re: how to switch from os.tmpnam to os.tmpfile


              Chris Lambacher wrote:[color=blue]
              > You should be able to find exactly what you need in the tempfile module.
              > http://docs.python.org/lib/module-tempfile.html[/color]

              thanks! tempfile.NamedT emporaryFile() is exaclty what I have been
              looking
              for. Using python for such a long time now, and still there are unknown
              goodies
              in the library, great :-)

              - harold -

              Comment

              • Nick Craig-Wood

                #8
                Re: how to switch from os.tmpnam to os.tmpfile

                Harold Fellermann <dadapapa@googl email.com> wrote:[color=blue]
                > 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?[/color]

                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 NamedTemporaryF ile()

                Source code: Lib/tempfile.py This module creates temporary files and directories. It works on all supported platforms. TemporaryFile, NamedTemporaryFile, TemporaryDirectory, and SpooledTemporaryFil...


                --
                Nick Craig-Wood <nick@craig-wood.com> -- http://www.craig-wood.com/nick

                Comment

                Working...