Mercurial > cpython-lru_cache-weakref
annotate Lib/glob.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | d7aff4423172 |
| children | b0935ef48186 |
| rev | line source |
|---|---|
| 5242 | 1 """Filename globbing utility.""" |
| 107 | 2 |
|
41050
850c32db27e6
Patch #1001604: glob.glob() now returns unicode filenames if it was
Georg Brandl <georg@python.org>
parents:
37545
diff
changeset
|
3 import sys |
| 570 | 4 import os |
|
41050
850c32db27e6
Patch #1001604: glob.glob() now returns unicode filenames if it was
Georg Brandl <georg@python.org>
parents:
37545
diff
changeset
|
5 import re |
|
41103
5978e07291fe
Revert rev. 54198, it's not really backwards compatible.
Georg Brandl <georg@python.org>
parents:
41050
diff
changeset
|
6 import fnmatch |
| 107 | 7 |
|
77072
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
68255
diff
changeset
|
8 try: |
|
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
68255
diff
changeset
|
9 _unicode = unicode |
|
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
68255
diff
changeset
|
10 except NameError: |
|
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
68255
diff
changeset
|
11 # If Python is built without Unicode support, the unicode type |
|
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
68255
diff
changeset
|
12 # will not exist. Fake one. |
|
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
68255
diff
changeset
|
13 class _unicode(object): |
|
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
68255
diff
changeset
|
14 pass |
|
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
68255
diff
changeset
|
15 |
| 34365 | 16 __all__ = ["glob", "iglob"] |
| 570 | 17 |
| 107 | 18 def glob(pathname): |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
19 """Return a list of paths matching a pathname pattern. |
| 5242 | 20 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
21 The pattern may contain simple shell-style wildcards a la fnmatch. |
| 5242 | 22 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
23 """ |
| 34365 | 24 return list(iglob(pathname)) |
| 25 | |
| 26 def iglob(pathname): | |
|
52900
4acbbb3e65e2
Merged revisions 70866-70868,70870-70871,70893,70896,70902,70905,70907,70912,70915,70927,70933,70940,70944,70954,70963,70998,71056 via svnmerge from
Georg Brandl <georg@python.org>
parents:
41250
diff
changeset
|
27 """Return an iterator which yields the paths matching a pathname pattern. |
| 34365 | 28 |
| 29 The pattern may contain simple shell-style wildcards a la fnmatch. | |
| 30 | |
| 31 """ | |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
32 if not has_magic(pathname): |
|
33332
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
18040
diff
changeset
|
33 if os.path.lexists(pathname): |
| 34365 | 34 yield pathname |
| 35 return | |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
36 dirname, basename = os.path.split(pathname) |
|
18040
fb93dfbd8052
Patch #409973: Speedup glob.glob, add fnmatch.filter.
Martin v. Löwis <martin@v.loewis.de>
parents:
16072
diff
changeset
|
37 if not dirname: |
| 34365 | 38 for name in glob1(os.curdir, basename): |
| 39 yield name | |
| 40 return | |
| 41 if has_magic(dirname): | |
| 42 dirs = iglob(dirname) | |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
43 else: |
| 34365 | 44 dirs = [dirname] |
| 45 if has_magic(basename): | |
| 46 glob_in_dir = glob1 | |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
47 else: |
| 34365 | 48 glob_in_dir = glob0 |
| 49 for dirname in dirs: | |
| 50 for name in glob_in_dir(dirname, basename): | |
| 51 yield os.path.join(dirname, name) | |
| 52 | |
| 53 # These 2 helper functions non-recursively glob inside a literal directory. | |
| 54 # They return a list of basenames. `glob1` accepts a pattern while `glob0` | |
| 55 # takes a literal basename (so it only has to check for its existence). | |
| 107 | 56 |
| 57 def glob1(dirname, pattern): | |
| 34365 | 58 if not dirname: |
| 59 dirname = os.curdir | |
|
77072
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
68255
diff
changeset
|
60 if isinstance(pattern, _unicode) and not isinstance(dirname, unicode): |
|
41250
9d0c05411734
Fix for glob.py if filesystem encoding is None.
Georg Brandl <georg@python.org>
parents:
41050
diff
changeset
|
61 dirname = unicode(dirname, sys.getfilesystemencoding() or |
|
9d0c05411734
Fix for glob.py if filesystem encoding is None.
Georg Brandl <georg@python.org>
parents:
41050
diff
changeset
|
62 sys.getdefaultencoding()) |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
63 try: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
64 names = os.listdir(dirname) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
65 except os.error: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
66 return [] |
|
41050
850c32db27e6
Patch #1001604: glob.glob() now returns unicode filenames if it was
Georg Brandl <georg@python.org>
parents:
37545
diff
changeset
|
67 if pattern[0] != '.': |
|
850c32db27e6
Patch #1001604: glob.glob() now returns unicode filenames if it was
Georg Brandl <georg@python.org>
parents:
37545
diff
changeset
|
68 names = filter(lambda x: x[0] != '.', names) |
|
850c32db27e6
Patch #1001604: glob.glob() now returns unicode filenames if it was
Georg Brandl <georg@python.org>
parents:
37545
diff
changeset
|
69 return fnmatch.filter(names, pattern) |
| 107 | 70 |
| 34365 | 71 def glob0(dirname, basename): |
| 72 if basename == '': | |
| 73 # `os.path.split()` returns an empty basename for paths ending with a | |
| 74 # directory separator. 'q*x/' should match only directories. | |
|
37545
a8d4928292d8
glob('anything*/') would fail because isdir is in os.path, not os.
Neal Norwitz <nnorwitz@gmail.com>
parents:
34365
diff
changeset
|
75 if os.path.isdir(dirname): |
| 34365 | 76 return [basename] |
| 77 else: | |
| 78 if os.path.lexists(os.path.join(dirname, basename)): | |
| 79 return [basename] | |
| 80 return [] | |
| 81 | |
|
574
03b9f152adb6
Never return a non-existing pathname.
Guido van Rossum <guido@python.org>
parents:
570
diff
changeset
|
82 |
|
6519
24da035ceb6e
Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents:
5242
diff
changeset
|
83 magic_check = re.compile('[*?[]') |
|
574
03b9f152adb6
Never return a non-existing pathname.
Guido van Rossum <guido@python.org>
parents:
570
diff
changeset
|
84 |
| 107 | 85 def has_magic(s): |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
86 return magic_check.search(s) is not None |
