classification
Title: pathlib p.match('') should return False rather than raising exception
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, jitterman, pitrou
Priority: normal Keywords:

Created on 2016-01-14 20:16 by jitterman, last changed 2016-01-15 00:56 by gvanrossum. This issue is now closed.

Files
File name Uploaded Description Edit
example.py jitterman, 2016-01-14 20:16 Simple example that illustrates the issue.
Messages (4)
msg258216 - (view) Author: JitterMan (jitterman) Date: 2016-01-14 20:16
One can use '*' as an 'accept all' pattern to match(). It would be nice to also use '' as a 'reject all' pattern. These 'accept all' and 'reject all' rules are useful as defaults. Currently passing '' to match() causes an exception. While it is easy enough to catch the exception, the need to constrains the way match() must be called and makes the calling code needlessly complicated and fussy.
msg258217 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2016-01-14 20:34
What's the use case for a "reject all" pattern? Why do you want to call glob with an argument that causes it do do a bunch of pointless work but then return an empty result set?
msg258257 - (view) Author: JitterMan (jitterman) Date: 2016-01-15 00:42
I don't know that passing '' as a pattern to glob() makes much sense, but it is useful when passed to match(). Doing so allows me to build a filter that can easily and naturally be disabled. For example, the following allows me to get all the items in a directory that both match a select pattern and do not match a reject pattern:

def ls(dir, select='*', reject='.*'):
    return (p for p in dir.glob(select) if not p.match(reject))

By default this function does not return hidden files or directories. It would seem like this restriction could be removed by passing reject='', but that generates an exception. Working around the exception makes the code noticeably more complicated.

Perhaps the question should really be 'what is the benefit of raising an exception when an empty glob string is encountered?'. I cannot think of any.
msg258258 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2016-01-15 00:56
A benefit is that it catches certain bugs early. It's not really worth a fight, so status quo wins.
History
Date User Action Args
2016-01-15 00:56:51gvanrossumsetstatus: open -> closed
resolution: wont fix
messages: + msg258258
2016-01-15 00:42:36jittermansetmessages: + msg258257
2016-01-14 20:34:11gvanrossumsetmessages: + msg258217
2016-01-14 20:19:04SilentGhostsetnosy: + gvanrossum, pitrou

components: + Library (Lib)
versions: + Python 3.6, - Python 3.5
2016-01-14 20:16:18jittermancreate