Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python tempfile: broken or am I doing it wrong?

For a small python script I would like to use a temporary file with the tempfile module. Somehow it does not give the expected behavior and I don't know what I am doing wrong or if this is a bug:

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tmp = tempfile.TemporaryFile()
>>> tmp.read()
''
>>> tmp.write('test')
>>> tmp.read()
'P\xf6D\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ [ommitted]'

alternatively I tried with text mode only but the behavior is still strange:

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tmp = tempfile.TemporaryFile('w+t')
>>> tmp.read()
''
>>> tmp.write('test')
>>> tmp.read()
'\x00\xa5\x8b\x02int or long, hash(a) is used instead.\n    i\x10 [ommitted]'
>>> tmp.seek(0)
>>> tmp.readline()
'test\x00\xa5\x8b\x02int or long, hash(a) is used instead.\n'

Any help is appreciated!


Additional information: Python 2.7.2 (32 bit) from the current Python XY distribution running on a Windows 7 Enterprise x64 machine. In a test run python created the temporary file name "tmpvyocxj" in my temporary directory under D:\temp\myusername with several other python processes running. The commands where typed in, I have not tried to reproduce it in a script. The behavior is unchanged with no other python processes running.


Update: This behavior is not limited to the tempfile module but also for the normal file.read() and file.write() operations. According to the CPython guys both functions only call the underlying libc fread() routines. In the C-standard the exact behavior of read after write without a seek or flush in between is undefined, i.e. every implementation can lead to different results.

like image 382
Alexander Avatar asked Feb 23 '23 17:02

Alexander


1 Answers

I just reproduced this behavior in Python 2.7.1 on Windows XP.

This appears to be a bug that only occurs when you attempt to read without seeking first.

That is:

>>> tmp.write('test')
>>> tmp.seek(0)
>>> tmp.read()
'test'

vs.

>>> tmp.write('test')
>>> tmp.read()
'x\x01\x98\x00pfile.pyR\x05\x00\x00\x00G\x01\x00\x00s\x12 [blah blah blah]'
>>> tmp.seek(0)
>>> tmp.read()
'testx\x01\x98\x00pfile.pyR\x05\x00\x00\x00G\x01\x00\x00s\x12 [blah blah blah]'

EDIT:

For kicks, I also checked:

  1. Windows 7, again 2.7.1 (same build as my XP install), and the same behavior was seen as on XP.
  2. Cygwin version 2.6.5, and this bug was not present.
like image 136
Nate Avatar answered Mar 07 '23 16:03

Nate