[Python-ideas] Add OS-dependent automatic glob support

random832 at fastmail.us random832 at fastmail.us
Mon Jan 5 21:33:08 CET 2015

On Sun, Jan 4, 2015, at 23:19, Stephen J. Turnbull wrote:
> By "platform-specific", do you mean one module that handles all
> platforms appropriately (what this thread is about), or do you mean
> per-platform modules (what "platform-specific" means to me)?

Windows technically requires calls to the actual filesystem driver for
correct behavior - different filesystems may have different case
folding, theoretically other different rules, etc. Plus you've got to
examine both the long and short filenames - if you have foobar.html,
*.htm will match it because the short filename is FOOBAR~1.HTM (it,
however, _returns_ the long filename.)

This means that the equivalent function to glob can't simply be the
equivalent of listdir filtered by fnmatch.

On Mon, Jan 5, 2015, at 05:10, Andrew Barnert wrote:
> It seems to me that if you want auto-globbing support on Windows, the
> "right" way to do it is to link in the fancy setargv instead of the
> default one. This handles wildcards exactly the same way the
> command.com/cmd.exe builtin commands do, which I suspect is what Windows
> users would actually be expecting of they passed a wildcard on the
> command line. (I think the docs no longer guarantee that this is true,
> but it's probably still true, and certainly closer to true than if you
> try to do it manually.)

The problem with that is that if you do that you can no longer pass in
_non_-filename arguments that contain a question mark or asterisk (and
happen to match a file). Better to do it inside the program, when you
actually know the argument you're looking at is intended to be a
filename spec. Which, I assume, is _why_ it's not done by default when
you compile a C program.

A program like grep "a.*b" "Some Directory\*.txt" should handle
wildcards in the filename and not in the regex. That is the user
expectation on windows, and that is something that windows programs (and
not unix programs) can and do deliver. Setargv doesn't, which is why you
have to opt in to it (if you know your program doesn't need non-filename
arguments that look like wildcards). It should be possible to write
Python programs that can do the same, in a way that is relatively
painless. A setargv-like solution is insufficient for that.

On Mon, Jan 5, 2015, at 13:45, Skip Montanaro wrote:
> I only discovered this "shortcoming" (or "Bourne Shell dependency")
> relatively recently. I've been using bash for so long it never even
> occurred to me that {...} notation wasn't available in all shells.

Brace expansion is technically not globbing. yen{2,3}.txt will return
yen2.txt yen3.txt even if one or both of those files don't exist. The
reason it works with globs is that yen{2,3}.* literally expands to
yen2.* yen3.* which is then globbed.

More information about the Python-ideas mailing list