Skip to content

Commit

Permalink
[2.2.x] Fixed #30647 -- Fixed crash of autoreloader when extra direct…
Browse files Browse the repository at this point in the history
…ory cannot be resolved.

Backport of fc75694 from master.
  • Loading branch information
orf authored and felixxm committed Jul 24, 2019
1 parent 61d4a15 commit 4d6449e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
11 changes: 9 additions & 2 deletions django/utils/autoreload.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,15 @@ def __init__(self):

def watch_dir(self, path, glob):
path = Path(path)
if not path.is_absolute():
raise ValueError('%s must be absolute.' % path)
try:
path = path.absolute()
except FileNotFoundError:
logger.debug(
'Unable to watch directory %s as it cannot be resolved.',
path,
exc_info=True,
)
return
logger.debug('Watching dir %s with glob %s.', path, glob)
self.directory_globs[path].add(glob)

Expand Down
3 changes: 1 addition & 2 deletions django/utils/translation/reloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ def watch_for_translation_changes(sender, **kwargs):
directories.extend(Path(config.path) / 'locale' for config in apps.get_app_configs())
directories.extend(Path(p) for p in settings.LOCALE_PATHS)
for path in directories:
absolute_path = path.absolute()
sender.watch_dir(absolute_path, '**/*.mo')
sender.watch_dir(path, '**/*.mo')


def translation_file_changed(sender, file_path, **kwargs):
Expand Down
3 changes: 3 additions & 0 deletions docs/releases/2.2.4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ Bugfixes

* Fixed a regression in Django 2.2 where auto-reloader crashes if a file path
contains nulls characters (``'\x00'``) (:ticket:`30506`).

* Fixed a regression in Django 2.2 where auto-reloader crashes if a translation
directory cannot be resolved (:ticket:`30647`).
6 changes: 6 additions & 0 deletions tests/utils_tests/test_autoreload.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,12 @@ def test_watch_with_single_file(self):
watched_files = list(self.reloader.watched_files())
self.assertIn(self.existing_file, watched_files)

def test_watch_dir_with_unresolvable_path(self):
path = Path('unresolvable_directory')
with mock.patch.object(Path, 'absolute', side_effect=FileNotFoundError):
self.reloader.watch_dir(path, '**/*.mo')
self.assertEqual(list(self.reloader.directory_globs), [])

def test_watch_with_glob(self):
self.reloader.watch_dir(self.tempdir, '*.py')
watched_files = list(self.reloader.watched_files())
Expand Down

0 comments on commit 4d6449e

Please sign in to comment.