homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Make pathlib.PurePath.__str__ use shlex.quote
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: cool-RR, pitrou, serhiy.storchaka
Priority: normal Keywords:

Created on 2016年11月27日 09:33 by cool-RR, last changed 2022年04月11日 14:58 by admin. This issue is now closed.

Messages (5)
msg281812 - (view) Author: Ram Rachum (cool-RR) * Date: 2016年11月27日 09:33
I have a a PurePath object like so:
 path = PurePath('/home/my awesome user/file.txt')
I'm SSHing into a server and I want to remove the file. So I have to do this: 
 ssh_client.run(f'/bin/rm {shlex.quote(str(path))}')
 
Which is really long and ugly. (I might have been able to remove the str from there if #28623 wasn't rejected.)
I wish I could do this: 
 ssh_client.run(f'/bin/rm {path}')
 
But since my path has a space, that would only be possible if PurePath.__str__ were to use shlex.quote, and put quotes around my path (only if it includes a space).
What do you think about that?
msg281814 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016年11月27日 10:09
This will break any code that pass str(path) to API that doesn't support pathlib.
In your case you can introduce a short alias:
 q = shlex.quote
 ssh_client.run(f'/bin/rm {q(str(path))}')
Or add more convenient helper:
 def q(path):
 return shlex.quote(str(path))
 ssh_client.run(f'/bin/rm {q(path)}')
msg281815 - (view) Author: Ram Rachum (cool-RR) * Date: 2016年11月27日 10:11
"This will break any code that pass str(path) to API that doesn't support pathlib."
I don't understand. Can you give a concrete example of code it would break?
msg281818 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016年11月27日 10:31
open(str(path))
msg281856 - (view) Author: Ram Rachum (cool-RR) * Date: 2016年11月28日 10:25
I understand now, you're completely right. This change would break the entire world :)
History
Date User Action Args
2022年04月11日 14:58:40adminsetgithub: 72997
2016年11月28日 10:25:38cool-RRsetmessages: + msg281856
2016年11月27日 10:31:01serhiy.storchakasetmessages: + msg281818
2016年11月27日 10:11:28cool-RRsetmessages: + msg281815
2016年11月27日 10:09:11serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg281814

resolution: rejected
stage: resolved
2016年11月27日 09:33:13cool-RRcreate

AltStyle によって変換されたページ (->オリジナル) /