Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#27887 closed Bug (fixed)

TypeError when namespaced urlconf included from non-namespaced one

Reported by: Matt Westcott Owned by: nobody
Component: Core (System checks) Version: 1.11
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The fix for #27874 (on latest stable/1.11.x) causes TypeError: sequence item 0: expected str instance, NoneType found upon running ./manage.py check when a namespaced urlconf is included within a non-namespaced one. To replicate:

project/urls.py:

from django.conf.urls import url, include
from foo import urls as foo_urls
urlpatterns = [
    url(r'x/', include(foo_urls)),
]

foo/urls.py:

from django.conf.urls import url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
]

Traceback:

Traceback (most recent call last):
  File "./manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/vagrant/libs/django/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/home/vagrant/libs/django/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/vagrant/libs/django/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/vagrant/libs/django/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/vagrant/libs/django/django/core/management/commands/check.py", line 68, in handle
    fail_level=getattr(checks, options['fail_level']),
  File "/home/vagrant/libs/django/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/vagrant/libs/django/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/vagrant/libs/django/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/vagrant/libs/django/django/core/checks/urls.py", line 43, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "/home/vagrant/libs/django/django/core/checks/urls.py", line 67, in _load_all_namespaces
    namespaces.extend(_load_all_namespaces(pattern, current))
  File "/home/vagrant/libs/django/django/core/checks/urls.py", line 62, in _load_all_namespaces
    ':'.join(parents + (url.namespace,)) for url in url_patterns
  File "/home/vagrant/libs/django/django/core/checks/urls.py", line 63, in <listcomp>
    if getattr(url, 'namespace', None) is not None
TypeError: sequence item 0: expected str instance, NoneType found

It looks like this is due to a logic error in the line:

https://github.com/django/django/commit/0417bf47a6fd00d9a6a4c711780ce01f35f62d59#diff-2f7a72bccd989000993282b2a8c77b8aR66

If pattern.namespace is defined and equal to None, then getattr(pattern, 'namespace', ()), returns None, which fails on the subsequent join operation.

Change History (5)

comment:1 by Marten Kenbeek, 8 years ago

Component: UncategorizedCore (System checks)
Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

A RegexURLResolver always has a namespace attribute, but it may be None. RegexURLPattern doesn't have a namespace attribute.

It should be something like this:

if hasattr(pattern, 'namespace') and pattern.namespace is not None:
    current = parents + (pattern.namespace,)
else:
    current = parents

comment:2 by Simon Charette, 8 years ago

Small nitpick but getattr(pattern, 'namespace', None) is not None should be preferred over hasattr + getattr.

comment:3 by Tim Graham, 8 years ago

Has patch: set

comment:4 by GitHub <noreply@…>, 8 years ago

Resolution: fixed
Status: newclosed

In b23d2640:

Fixed #27887 -- Fixed URLs check crash with namespaced URLs inside non-namespaced URLs.

comment:5 by Tim Graham <timograham@…>, 8 years ago

In 1f20326d:

[1.11.x] Fixed #27887 -- Fixed URLs check crash with namespaced URLs inside non-namespaced URLs.

Backport of b23d264046db1c8f58a174b9be27ee760341ba06 from master

Note: See TracTickets for help on using tickets.
Back to Top