classification
Title: tempfile.TemporaryDirectory() name string is incorrect
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: eosborne, eric.smith, serhiy.storchaka, terry.reedy
Priority: normal Keywords:

Created on 2021-01-27 13:06 by eosborne, last changed 2021-01-29 20:46 by eric.smith. This issue is now closed.

Messages (5)
msg385768 - (view) Author: Eric Osborne (eosborne) Date: 2021-01-27 13:06
When I create a temporary directory, the name doesn't come back in a way the application expects it.  I ran into this using gitPython and I'm not entirely clear whether it's a tmpfile issue or a gitpython issue, but I think it's tempfile.  

Repro:

In [12]: td = tempfile.TemporaryDirectory()

In [13]: td.name
Out[13]: '/var/folders/kd/bjhw84ss2x734frx98qh1mw40000gn/T/tmpgralfh9m'

In [14]: str(td)
Out[14]: "<TemporaryDirectory '/var/folders/kd/bjhw84ss2x734frx98qh1mw40000gn/T/tmpgralfh9m'>"

The problem with this is that gitPython takes a temporary directory as an argument and effectively does a mkdir(str(td)), which creates a weird directory in /var/tmp:

eosborne@eosborne-salt-master:/tmp$ ls -alhd *Temp*
drwxrwxr-x 3 eosborne eosborne 4.0K Jan 27 12:42 '<TemporaryDirectory '\'''



I believe that str(td) should return just the directory name and that repr(td) can return the extra bit of text with "<TemporaryDirectory ..>" but that's just my two cents.

I have thrown this issue up on the gitPython list too: https://github.com/gitpython-developers/GitPython/issues/1115
msg385771 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-01-27 13:23
It is a bug in gitPython.

As for adding a new feature, making TemporaryDirectory.__str__() returning just a name, I am not sure that it is needed, and it has drawbacks. If print() for TemporaryDirectory object will output just a directory name, it can create a false assumption that TemporaryDirectory() returns a string. Also, there are problems with bytes paths. You always can use the name attribute, str(td) is not better than td.name.
msg385774 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-01-27 13:46
I agree with Serhiy: changing TemporaryDirectory.__str__() definitely has drawbacks.

I assume gitPython is using str() where they might want to use os.fspath(). Maybe tempfile.TemporaryDirectory (and similar) could then support __fspath__.
msg385933 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-01-29 20:19
Making str(td) == td.name does not seem useful.  I think this should be closed as 'Not a bug', because it isn't.  If changed to an enhancement request, reject as insufficiently warranted against the likely breakage.
msg385936 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-01-29 20:46
I've been thinking about this some more, and I agree with Terry. Changing str() seems dangerous, and __fspath__ wouldn't be backwardly compatible. If you want the name, use .name. So I'm going to close this.
History
Date User Action Args
2021-01-29 20:46:49eric.smithsetstatus: open -> closed
type: enhancement
messages: + msg385936

resolution: rejected
stage: resolved
2021-01-29 20:19:25terry.reedysetnosy: + terry.reedy
messages: + msg385933
2021-01-27 13:46:06eric.smithsetnosy: + eric.smith
messages: + msg385774
2021-01-27 13:23:49serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg385771
2021-01-27 13:06:25eosbornecreate