Issue 28811: Make pathlib.PurePath.str use shlex.quote (original) (raw)

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:40 admin set github: 72997
2016-11-28 10:25:38 cool-RR set messages: +
2016-11-27 10:31:01 serhiy.storchaka set messages: +
2016-11-27 10:11:28 cool-RR set messages: +
2016-11-27 10:09:11 serhiy.storchaka set status: open -> closednosy: + serhiy.storchakamessages: + resolution: rejectedstage: resolved
2016-11-27 09:33:13 cool-RR create