Opened 17 years ago

Closed 14 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: dev
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

Change History (9)

comment:1 by Jacob, 17 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Tomi Kyöstilä, 16 years ago

Cc: tomi.kyostila@… added

comment:3 by vizualbod, 16 years ago

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

comment:4 by Sayane, 16 years ago

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 by Sayane, 16 years ago

Cc: say4ne@… added

comment:6 by gptvnt, 14 years ago

Owner: changed from nobody to gptvnt
Status: newassigned

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 by Julien Phalip, 14 years ago

Type: Bug

comment:8 by Julien Phalip, 14 years ago

Severity: Normal

comment:9 by Dmitry Trofimov, 14 years ago

Easy pickings: unset
Resolution: worksforme
Status: assignedclosed
UI/UX: unset

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

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