Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eb2884a

Browse files
committed
Close #15442: Expand the list of default directories ignored by filecmp.dircmp and expose it as a module attribute
1 parent 60a0c71 commit eb2884a

4 files changed

Lines changed: 32 additions & 12 deletions

File tree

Doc/library/filecmp.rst

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ The :class:`dircmp` class
5555

5656
.. class:: dircmp(a, b, ignore=None, hide=None)
5757

58-
Construct a new directory comparison object, to compare the directories *a* and
59-
*b*. *ignore* is a list of names to ignore, and defaults to ``['RCS', 'CVS',
60-
'tags']``. *hide* is a list of names to hide, and defaults to ``[os.curdir,
61-
os.pardir]``.
58+
Construct a new directory comparison object, to compare the directories *a*
59+
and *b*. *ignore* is a list of names to ignore, and defaults to
60+
:attr:`filecmp.DEFAULT_IGNORES`. *hide* is a list of names to hide, and
61+
defaults to ``[os.curdir, os.pardir]``.
6262

6363
The :class:`dircmp` class compares files by doing *shallow* comparisons
6464
as described for :func:`filecmp.cmp`.
@@ -164,6 +164,12 @@ The :class:`dircmp` class
164164
A dictionary mapping names in :attr:`common_dirs` to :class:`dircmp`
165165
objects.
166166

167+
.. attribute:: DEFAULT_IGNORES
168+
169+
.. versionadded:: 3.3
170+
171+
List of directories ignored by :class:`dircmp` by default.
172+
167173

168174
Here is a simplified example of using the ``subdirs`` attribute to search
169175
recursively through two directories to show common different files::

Lib/filecmp.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@
1313
import stat
1414
from itertools import filterfalse
1515

16-
__all__ = ["cmp", "dircmp", "cmpfiles"]
16+
__all__ = ['cmp', 'dircmp', 'cmpfiles', 'DEFAULT_IGNORES']
1717

1818
_cache = {}
1919
BUFSIZE = 8*1024
2020

21+
DEFAULT_IGNORES = [
22+
'RCS', 'CVS', 'tags', '.git', '.hg', '.bzr', '_darcs', '__pycache__']
23+
24+
2125
def cmp(f1, f2, shallow=True):
2226
"""Compare two files.
2327
@@ -80,7 +84,7 @@ class dircmp:
8084
dircmp(a, b, ignore=None, hide=None)
8185
A and B are directories.
8286
IGNORE is a list of names to ignore,
83-
defaults to ['RCS', 'CVS', 'tags'].
87+
defaults to DEFAULT_IGNORES.
8488
HIDE is a list of names to hide,
8589
defaults to [os.curdir, os.pardir].
8690
@@ -116,7 +120,7 @@ def __init__(self, a, b, ignore=None, hide=None): # Initialize
116120
else:
117121
self.hide = hide
118122
if ignore is None:
119-
self.ignore = ['RCS', 'CVS', 'tags'] # Names ignored in comparison
123+
self.ignore = DEFAULT_IGNORES
120124
else:
121125
self.ignore = ignore
122126

Lib/test/test_filecmp.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import os, filecmp, shutil, tempfile
32
import unittest
43
from test import support
@@ -46,9 +45,14 @@ def setUp(self):
4645
self.dir = os.path.join(tmpdir, 'dir')
4746
self.dir_same = os.path.join(tmpdir, 'dir-same')
4847
self.dir_diff = os.path.join(tmpdir, 'dir-diff')
48+
49+
# Another dir is created under dir_same, but it has a name from the
50+
# ignored list so it should not affect testing results.
51+
self.dir_ignored = os.path.join(self.dir_same, '.hg')
52+
4953
self.caseinsensitive = os.path.normcase('A') == os.path.normcase('a')
5054
data = 'Contents of file go here.\n'
51-
for dir in [self.dir, self.dir_same, self.dir_diff]:
55+
for dir in (self.dir, self.dir_same, self.dir_diff, self.dir_ignored):
5256
shutil.rmtree(dir, True)
5357
os.mkdir(dir)
5458
if self.caseinsensitive and dir is self.dir_same:
@@ -64,9 +68,11 @@ def setUp(self):
6468
output.close()
6569

6670
def tearDown(self):
67-
shutil.rmtree(self.dir)
68-
shutil.rmtree(self.dir_same)
69-
shutil.rmtree(self.dir_diff)
71+
for dir in (self.dir, self.dir_same, self.dir_diff):
72+
shutil.rmtree(dir)
73+
74+
def test_default_ignores(self):
75+
self.assertIn('.hg', filecmp.DEFAULT_IGNORES)
7076

7177
def test_cmpfiles(self):
7278
self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file']) ==

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,10 @@ Documentation
13871387
- Issue #15250: Document that `filecmp.dircmp()` compares files shallowly. Patch
13881388
contributed by Chris Jerdonek.
13891389

1390+
- Issue #15442: Expose the default list of directories ignored by
1391+
`filecmp.dircmp()` as a module attribute, and expand the list to more modern
1392+
values.
1393+
13901394
Tests
13911395
-----
13921396

0 commit comments

Comments
 (0)