[New-bugs-announce] [issue40701] tempfile mixes str and bytes in an inconsistent manner

Eric L. report at bugs.python.org
Wed May 20 15:20:24 EDT 2020


New submission from Eric L. <ewl+python at lavar.de>:

tempfile fails on mixed str and bytes when setting tempfile.tempdir to a non-existent bytes path but succeeds when set to an existing bytes path.

$ python3.9
Python 3.9.0a6 (default, Apr 28 2020, 00:00:00) 
[GCC 10.0.1 20200430 (Red Hat 10.0.1-0.14)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tempfile.tempdir = b'/doesntexist'
>>> tempfile.TemporaryFile()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.9/tempfile.py", line 615, in TemporaryFile
    (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags, output_type)
  File "/usr/lib64/python3.9/tempfile.py", line 248, in _mkstemp_inner
    file = _os.path.join(dir, pre + name + suf)
  File "/usr/lib64/python3.9/posixpath.py", line 90, in join
    genericpath._check_arg_types('join', a, *p)
  File "/usr/lib64/python3.9/genericpath.py", line 155, in _check_arg_types
    raise TypeError("Can't mix strings and bytes in path components") from None
TypeError: Can't mix strings and bytes in path components
>>> tempfile.tempdir = b'/tmp'
>>> tempfile.TemporaryFile()
<_io.BufferedRandom name=3>
>>> tempfile.mktemp()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.9/tempfile.py", line 400, in mktemp
    file = _os.path.join(dir, prefix + name + suffix)
  File "/usr/lib64/python3.9/posixpath.py", line 90, in join
    genericpath._check_arg_types('join', a, *p)
  File "/usr/lib64/python3.9/genericpath.py", line 155, in _check_arg_types
    raise TypeError("Can't mix strings and bytes in path components") from None
TypeError: Can't mix strings and bytes in path components

It seems to me that the case of `tempfile.tempdir` being of type bytes hasn't been completely considered and is handled inconsistently. My suggestion would be to manage the paths as bytes if there is no other indication like suffix or prefix.

----------
messages: 369469
nosy: ericzolf
priority: normal
severity: normal
status: open
title: tempfile mixes str and bytes in an inconsistent manner
type: behavior
versions: Python 3.5, Python 3.6, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40701>
_______________________________________


More information about the New-bugs-announce mailing list