#32316: Access __file__ lazily rather than at module level
-------------------------------------+-------------------------------------
     Reporter:  William Schwartz     |                    Owner:  William
         Type:                       |  Schwartz
  Cleanup/optimization               |                   Status:  closed
    Component:  Core (Other)         |                  Version:  dev
     Severity:  Normal               |               Resolution:  fixed
     Keywords:  freezers             |             Triage Stage:  Ready for
                                     |  checkin
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by William Schwartz):

 I'm cleaning up random files on my desktop and I came across these notes I
 left for myself while working on this bug. I'm leaving them here for
 posterity.

 = Appendix: Other modules that use `__file__`.
 From `master` at b41d38ae26b1da9519a6cd765bc2f2ce7d355007:
 {{{#!console
 $ git grep --files-with-matches -e "__file__" b41d38a -- django | wc -l
 b41d38a:django/apps/config.py
 b41d38a:django/conf/__init__.py
 b41d38a:django/conf/project_template/project_name/settings.py-tpl
 b41d38a:django/contrib/auth/password_validation.py
 b41d38a:django/core/management/commands/makemessages.py
 b41d38a:django/db/migrations/loader.py
 b41d38a:django/db/migrations/questioner.py
 b41d38a:django/forms/renderers.py
 b41d38a:django/utils/autoreload.py
 b41d38a:django/utils/module_loading.py
 b41d38a:django/utils/translation/trans_real.py
 b41d38a:django/utils/version.py
 b41d38a:django/views/debug.py
 }}}

 The remainder of the files listed in the `git grep` output above do
 **not** need to be addressed in this ticket at this time. This section
 explains why.

 The following modules access `__file__` only through proper `getattr` or
 `hasattr` guards:
 * `django.apps.config` to compute `AppConfig.path` from its `module`
 * `django.db.migrations.loader` (see #32302)
 * `django.db.migrations.questioner` in `MigrationQuestioner.ask_initial`
 * `django.utils.module_loading` in `module_dir`
 `django.utils.autoreload` doesn't access `__file__` at the module level.
 `iter_modules_and_files` has a proper `hasattr` guard but `is_django_path`
 would need different semantics. #32314 addresses `get_child_arguments`.

 `django.conf.LazySettings.PASSWORD_RESET_TIMEOUT_DAYS` uses `__file__`
 only to avoid warning about this deprecated setting. Frozen applications
 can avoid crashes simply by upgrading away from
 `PASSWORD_RESET_TIMEOUT_DAYS`.

 `django/conf/project_template/project_name/settings.py-tpl` is the
 template settings module. Django projects are supposed to edit this
 anyway.

 Finally the `makemessages` command has no use for projects with
 `USE_I18N=False`, which is necessary for frozen projects to work around
 the issue with `trans_real` described above.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32316#comment:17>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/068.6fd0403bfd06b261cb8e837d6b7adf19%40djangoproject.com.

Reply via email to