Code

Opened 6 years ago

Closed 3 years ago

#6442 closed Bug (worksforme)

base.py: TypeError: 'str' object is not callable when importing a module fails

Reported by: miohtama Owned by: gptvnt
Component: Core (Other) Version: master
Severity: Normal Keywords:
Cc: tomi.kyostila@…, say4ne@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If you specify a module which does not import (it has syntax error or missing dependendies), base.py will raise mysterious TypeError. This is because urlresolver does not raise an exception when callback is not callable, but returns the original string instead.

This makes it very difficult for a developer to track down the real cause of the problem, since the orignal ImportError is silently eaten.

I suggest that urlresolver would raise an exception on invalid look-ups. I can create test case and patch if there is no reason why the current behavior should kept.

Steps to reproduce.

  1. Create a views.py module with any function
  2. Make views.py have an invalid import
    import x
    
    def any_function(request, *args, **kwargs):
         pass
    
  3. Add views.py to urlconf, e.g.
         (r'^sampleurl/(.*)$', 'myproduct.views.any_function'),
    
  4. Use test client to get the URI
c = Client()
response = c.get("/sampleurl/")     

Sample result below:

Traceback (most recent call last):
  File "/home/moo/workspace/web/dependencies/django-go/scaler/tests/scaletest.py", line 102, in testServeScaledImage
    response = c.get(uri)
  File "/home/moo/workspace/web/dependencies/django-trunk/django/test/client.py", line 219, in get
    return self.request(**r)
  File "/home/moo/workspace/web/dependencies/django-trunk/django/core/handlers/base.py", line 82, in _real_get_response
    response = callback(request, *callback_args, **callback_kwargs)
TypeError: 'str' object is not callable

Attachments (0)

Change History (9)

comment:1 Changed 6 years ago by jacob

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 6 years ago by dob

  • Cc tomi.kyostila@… added

comment:3 Changed 5 years ago by vizualbod

Miohtama,
Please create a patch and test case, this is very annoying and it eats so much time.

comment:4 Changed 5 years ago by sayane

I had the same error.
I found that function get_callable() is ignoring UnicodeEncodeError here:
http://code.djangoproject.com/browser/django/trunk/django/core/urlresolvers.py#L64

I think it's here to catch errors from line 55, but it is also catching errors from import (line 58).

I was testing it on django 1.0.2 final, but code in trunk is almost the same.

Solution is to catch UnicodeEncodeError only from line 55.

comment:5 Changed 5 years ago by sayane

  • Cc say4ne@… added

comment:6 Changed 4 years ago by gptvnt

  • Owner changed from nobody to gptvnt
  • Status changed from new to assigned

Looks like this is no longer an issue. I just tried with latest trunk and for given view and url config

# views.py
from django.http import HttpResponse
import x

def index(request):
    return HttpResponse("Hello, world. This is the main page.")

def sampleurl(request, *args, **kwargs):
    pass

# urls.py
urlpatterns = patterns('',
    (r'^$', 'polls.views.index'),
    (r'^sampleurl/(.*)$', 'polls.views.sampleurl'),

I got this

>>> from django.test.client import Client
>>> c = Client()
>>> r = c.get('/sampleurl/')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "F:\code\django_src\django\test\client.py", line 436, in get
    response = super(Client, self).get(path, data=data, **extra)
  File "F:\code\django_src\django\test\client.py", line 220, in get
    return self.request(**r)
  File "F:\code\django_src\django\core\handlers\base.py", line 95, in get_response
    request.path_info)
  File "F:\code\django_src\django\core\urlresolvers.py", line 251, in resolve
    sub_match = pattern.resolve(new_path)
  File "F:\code\django_src\django\core\urlresolvers.py", line 157, in resolve
    return ResolverMatch(self.callback, args, kwargs, self.name)
  File "F:\code\django_src\django\core\urlresolvers.py", line 166, in _get_callback
    raise ViewDoesNotExist("Could not import %s. Error was: %s" % (mod_name, str(e)))
ViewDoesNotExist: Could not import polls.views. Error was: No module named x

Which correctly shows import error.

comment:7 Changed 3 years ago by julien

  • Type set to Bug

comment:8 Changed 3 years ago by julien

  • Severity set to Normal

comment:9 Changed 3 years ago by traff

  • Easy pickings unset
  • Resolution set to worksforme
  • Status changed from assigned to closed
  • UI/UX unset

I've verified this also. No longer an issue.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.