Browse files

Ensure we ignore __pycache__ PEP 3174 dirs in a few more places.

  • Loading branch information...
1 parent 69035b0 commit 2100da9dcdba7a8bc18f6180fd7e61640319b48a @ramiro ramiro committed Oct 6, 2012
Showing with 8 additions and 5 deletions.
  1. +2 −0
  2. +1 −1 django/core/management/
  3. +1 −1 django/db/
  4. +1 −1 django/template/
  5. +3 −2
@@ -32,3 +32,5 @@ recursive-include django/contrib/gis/tests/geogapp/fixtures *
recursive-include django/contrib/gis/tests/relatedapp/fixtures *
recursive-include django/contrib/sitemaps/templates *
recursive-include django/contrib/sitemaps/tests/templates *
+recursive-exclude * __pycache__
+recursive-exclude * *.py[co]
@@ -138,7 +138,7 @@ def handle(self, app_or_project, name, target=None, **options):
for dirname in dirs[:]:
- if dirname.startswith('.'):
+ if dirname.startswith('.') or dirname == '__pycache__':
for filename in files:
@@ -30,7 +30,7 @@ def load_backend(backend_name):
available_backends = [f for f in os.listdir(backend_dir)
if os.path.isdir(os.path.join(backend_dir, f))
- and not f.startswith('.')]
+ and not (f.startswith('.') or f == '__pycache__')]
except EnvironmentError:
available_backends = []
full_notation = backend_name.startswith('django.db.backends.')
@@ -96,7 +96,7 @@ def fix_ampersands_filter(value):
# Values for testing floatformat input against infinity and NaN representations,
# which differ across platforms and Python versions. Some (i.e. old Windows
# ones) are not recognized by Decimal but we want to return them unchanged vs.
-# returning an empty string as we do for completley invalid input. Note these
+# returning an empty string as we do for completely invalid input. Note these
# need to be built up from values that are not inf/nan, since inf/nan values do
# not reload properly from .pyc files on Windows prior to some level of Python 2.5
# (see Python Issue757815 and Issue1080440).
@@ -67,9 +67,10 @@ def fullsplit(path, result=None):
django_dir = 'django'
for dirpath, dirnames, filenames in os.walk(django_dir):
- # Ignore dirnames that start with '.'
+ # Ignore PEP 3147 cache dirs and those whose names start with '.'
for i, dirname in enumerate(dirnames):
- if dirname.startswith('.'): del dirnames[i]
+ if dirname.startswith('.') or dirname == '__pycache__':
marekbrzoska Feb 14, 2013

Never ever delete from list are iterating on:

dirnames = ['x', '.a', '.b', 'c']
for i, dirname in enumerate(dirnames):
... if dirname.startswith('.') or dirname == 'pycache':
... del dirnames[i]
['x', '.b', 'c']

And that is not the worst that can happen (IndexError can happen).

marekbrzoska Feb 14, 2013

You can do:
dirnames = filter(lambda x: not x.startwith('.') and not x == 'pycache', dirnames)

ramiro Feb 14, 2013 Member

Thank you very much for catching this old thinko!

Some notes:

  • dirnames is returned by os.walk and we can modifiy it in place. As you've found, problem is that we were doing that in place mod wrongly.
  • We can't use filter() because we need dirnames to still be a list and we need to be compatible with Python3.

I think I'm going to replace all the loop with:

    dirnames[:] = [d for d in dirnames if not d.startswith('.') and d != '__pycache__'] 
+ del dirnames[i]
if '' in filenames:
elif filenames:

0 comments on commit 2100da9

Please sign in to comment.