Difference between tempfile and spooled tempfile?

Discussion in 'Python' started by Alex van der Spek, Apr 5, 2012.

  1. I do not understand why the spooled write gives an error. See below.
    The normal tempfile works just fine. They are supposed to behave equal?

    All insight you can provide is welcome.
    Alex van der Spek

    +++++++++++++++++++++++++++++++++++++++++++++++++


    Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)]
    on win32
    Type "copyright", "credits" or "license()" for more information.
    >>> import array
    >>> import tempfile
    >>> stf = tempfile.SpooledTemporaryFile(max_size=1024)
    >>> ptf = tempfile.TemporaryFile()
    >>> fff = [float(x) for x in range(2048)]
    >>> ffa = array.array('f',fff)
    >>> ptf.write(ffa)
    >>> stf.write(ffa)


    Traceback (most recent call last):
    File "<pyshell#7>", line 1, in <module>
    stf.write(ffa)
    File "C:\Python27\lib\tempfile.py", line 595, in write
    rv = file.write(s)
    TypeError: must be string or read-only character buffer, not array.array
    >>>
    Alex van der Spek, Apr 5, 2012
    #1
    1. Advertising

  2. Alex van der Spek

    Steve Howell Guest

    On Apr 5, 7:50 am, "Alex van der Spek" <> wrote:
    > I do not understand why the spooled write gives an error. See below.
    > The normal tempfile works just fine. They are supposed to behave equal?
    >
    > All insight you can provide is welcome.
    > Alex van der Spek
    >
    > +++++++++++++++++++++++++++++++++++++++++++++++++
    >
    > Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)]
    > on win32
    > Type "copyright", "credits" or "license()" for more information.
    >
    > >>> import array
    > >>> import tempfile
    > >>> stf = tempfile.SpooledTemporaryFile(max_size=1024)
    > >>> ptf = tempfile.TemporaryFile()
    > >>> fff = [float(x) for x in range(2048)]
    > >>> ffa = array.array('f',fff)
    > >>> ptf.write(ffa)
    > >>> stf.write(ffa)

    >
    > Traceback (most recent call last):
    >   File "<pyshell#7>", line 1, in <module>
    >     stf.write(ffa)
    >   File "C:\Python27\lib\tempfile.py", line 595, in write
    >     rv = file.write(s)
    > TypeError: must be string or read-only character buffer, not array.array
    >


    I think the docs are slightly misleading. While SpooledTemporaryFile
    allows you to write(), it's more finicky about serializing arrays,
    hence the error message.

    If you look under the hood, you'll see that it's mostly a limitation
    of StringIO.

    http://hg.python.org/cpython/file/2.7/Lib/tempfile.py

    494 """Temporary file wrapper, specialized to switch from
    495 StringIO to a real file when it exceeds a certain size or
    496 when a fileno is needed.
    497 """
    498 _rolled = False
    499
    500 def __init__(self, max_size=0, mode='w+b', bufsize=-1,
    501 suffix="", prefix=template, dir=None):
    502 self._file = _StringIO()
    503 self._max_size = max_size
    504 self._rolled = False
    505 self._TemporaryFileArgs = (mode, bufsize, suffix,
    prefix, dir)

    (See line 502.)

    600 def write(self, s):
    601 file = self._file
    602 rv = file.write(s)
    603 self._check(file)
    604 return rv

    (See line 602.)

    I'm looking at a slightly different version of the module than you,
    but hopefully you get the idea.
    Steve Howell, Apr 5, 2012
    #2
    1. Advertising

  3. Alex van der Spek

    Steve Howell Guest

    On Apr 5, 8:10 am, Steve Howell <> wrote:
    > On Apr 5, 7:50 am, "Alex van der Spek" <> wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > I do not understand why the spooled write gives an error. See below.
    > > The normal tempfile works just fine. They are supposed to behave equal?

    >
    > > All insight you can provide is welcome.
    > > Alex van der Spek

    >
    > > +++++++++++++++++++++++++++++++++++++++++++++++++

    >
    > > Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)]
    > > on win32
    > > Type "copyright", "credits" or "license()" for more information.

    >
    > > >>> import array
    > > >>> import tempfile
    > > >>> stf = tempfile.SpooledTemporaryFile(max_size=1024)
    > > >>> ptf = tempfile.TemporaryFile()
    > > >>> fff = [float(x) for x in range(2048)]
    > > >>> ffa = array.array('f',fff)
    > > >>> ptf.write(ffa)
    > > >>> stf.write(ffa)

    >
    > > Traceback (most recent call last):
    > >   File "<pyshell#7>", line 1, in <module>
    > >     stf.write(ffa)
    > >   File "C:\Python27\lib\tempfile.py", line 595, in write
    > >     rv = file.write(s)
    > > TypeError: must be string or read-only character buffer, not array.array

    >
    > I think the docs are slightly misleading.  While SpooledTemporaryFile
    > allows you to write(), it's more finicky about serializing arrays,
    > hence the error message.
    >
    > If you look under the hood, you'll see that it's mostly a limitation
    > of StringIO.
    >
    > http://hg.python.org/cpython/file/2.7/Lib/tempfile.py
    >
    >    494     """Temporary file wrapper, specialized to switch from
    >    495     StringIO to a real file when it exceeds a certain sizeor
    >    496     when a fileno is needed.
    >    497     """
    >    498     _rolled = False
    >    499
    >    500     def __init__(self, max_size=0, mode='w+b', bufsize=-1,
    >    501                  suffix="", prefix=template, dir=None):
    >    502         self._file = _StringIO()
    >    503         self._max_size = max_size
    >    504         self._rolled = False
    >    505         self._TemporaryFileArgs = (mode, bufsize, suffix,
    > prefix, dir)
    >
    > (See line 502.)
    >
    >    600     def write(self, s):
    >    601         file = self._file
    >    602         rv = file.write(s)
    >    603         self._check(file)
    >    604         return rv
    >
    > (See line 602.)
    >
    > I'm looking at a slightly different version of the module than you,
    > but hopefully you get the idea.


    P.S. The problems the OP is encountering may be a side effect of this
    bug:

    http://bugs.python.org/issue1730114
    Steve Howell, Apr 5, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,049
  2. Santa
    Replies:
    1
    Views:
    1,062
    Mark A. Odell
    Jul 17, 2003
  3. arnuld
    Replies:
    33
    Views:
    1,166
  4. PerlFAQ Server
    Replies:
    0
    Views:
    346
    PerlFAQ Server
    Jan 6, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    250
    PerlFAQ Server
    Apr 15, 2011
Loading...

Share This Page