#23436: Should use abspath for default settings.BASE_DIR
-------------------------------------+-------------------------------------
     Reporter:  hjwp                 |                    Owner:  nobody
         Type:                       |                   Status:  new
  Cleanup/optimization               |                  Version:  1.6
    Component:  Uncategorized        |               Resolution:
     Severity:  Normal               |             Triage Stage:
     Keywords:                       |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by hjwp:

Old description:

> ref the default project template's settings.py, BASE_DIR is defined as:
>
> {{{
> BASE_DIR = os.path.dirname(os.path.dirname(__file__))
> }}}
>
> Because there's no abspath, you can get different results for BASE_DIR
> depending on how settings.py is run.  This is an edge case, since
> settings.py is almost always imported by django in the right context, but
> still -- compare:
>
> {{{
>     python manage.py shell
>     >>> from django.conf import settings
>     print(BASE_DIR)
> }}}
>
> With:
>
> {{{
>     python -i myproject/settings.py
>     >>> print(BASE_DIR)
> }}}
>
> As an aside, in general, in any path wrangling you should always call
> abspath first. Try running this script, saved to a file:
>
>     import os
>     print(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
>     print(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
>
> As to tests, the only one I can think of would be a pretty horrible one
> involving actually callling out using subprocess.Popen to a python -i of
> the project template settings file, so maybe ok not to bother?
>
> credit to @CleanCut for turning me onto this weirdness!

New description:

 ref the default project template's settings.py, BASE_DIR is defined as:

 {{{
 BASE_DIR = os.path.dirname(os.path.dirname(__file__))
 }}}

 Because there's no abspath, you can get different results for BASE_DIR
 depending on how settings.py is run.  This is an edge case, since
 settings.py is almost always imported by django in the right context, but
 still -- compare:

 {{{
 python manage.py shell
 >>> from django.conf import settings
 print(BASE_DIR)
 }}}

 With:

 {{{
 python -i myproject/settings.py
 >>> print(BASE_DIR)
 }}}

 As an aside, in general, in any path wrangling you should always call
 abspath first. Try running this script, saved to a file:

 {{{
 import os
 print(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
 print(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
 }}}

 As to tests, the only one I can think of would be a pretty horrible one
 involving actually callling out using subprocess.Popen to a python -i of
 the project template settings file, so maybe ok not to bother?

 credit to @CleanCut for turning me onto this weirdness!

--

--
Ticket URL: <https://code.djangoproject.com/ticket/23436#comment:2>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/062.f7eceab86d4972dfaa6840e02741f344%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to