Author Kevin Bonham
Recipients Kevin Bonham
Date 2017-01-11.19:44:31
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1484163871.38.0.591576246688.issue29245@psf.upfronthosting.co.za>
In-reply-to
Content
Python 3.6.0 (default, Dec 24 2016, 08:01:42) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from tempfile import NamedTemporaryFile
>>> tmp = NamedTemporaryFile()
>>> tmp.write("hello world!")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tempfile.py", line 483, in func_wrapper
    return func(*args, **kwargs)
TypeError: a bytes-like object is required, not 'str'
>>> type(tmp)
<class 'tempfile._TemporaryFileWrapper'>

The more verbose error points to issue #18879 (http://bugs.python.org/issue18879), which seems very similar to my problem, but is marked as resolved.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-23-b9a4238a79b7> in <module>()
----> 1 tmp_file.write("blah")

/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tempfile.py in func_wrapper(*args, **kwargs)
    481             @_functools.wraps(func)
    482             def func_wrapper(*args, **kwargs):
--> 483                 return func(*args, **kwargs)
    484             # Avoid closing the file as long as the wrapper is alive,
    485             # see issue #18879.

TypeError: a bytes-like object is required, not 'str'

This also seems like it might be related to http://bugs.python.org/issue28867, though I'm getting the same behavior with TemporaryFile:

>>> from tempfile import TemporaryFile
>>> tmp2 = TemporaryFile()
>>> tmp2.write("Hello World")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'


I can do:

>>> with open(tmp.name, "w") as t:
...     t.write("Hello world!")


Is this intended behavior? The docs still say that these functions should return file-like objects.
History
Date User Action Args
2017-01-11 19:44:31Kevin Bonhamsetrecipients: + Kevin Bonham
2017-01-11 19:44:31Kevin Bonhamsetmessageid: <1484163871.38.0.591576246688.issue29245@psf.upfronthosting.co.za>
2017-01-11 19:44:31Kevin Bonhamlinkissue29245 messages
2017-01-11 19:44:31Kevin Bonhamcreate