Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Friday, October 21, 2011

IndexError: tuple index out of range in get_template_exception_info

Sometimes people do not specify messages, when they're firing exceptions. In that case, one day we can get something like this in http server stdout:
  File "/Users/pythonaut/.virtualenvs/dev/src/django/django/core/handlers/base.py", line 221, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/Users/pythonaut/.virtualenvs/dev/src/django/django/views/debug.py", line 66, in technical_500_response
    html = reporter.get_traceback_html()
  File "/Users/pythonaut/.virtualenvs/dev/src/django/django/views/debug.py", line 275, in get_traceback_html
    c = Context(self.get_traceback_data())
  File "/Users/pythonaut/.virtualenvs/dev/src/django/django/views/debug.py", line 231, in get_traceback_data
    self.get_template_exception_info()
  File "/Users/pythonaut/.virtualenvs/dev/src/django/django/views/debug.py", line 306, in get_template_exception_info
    'message': self.exc_value.args[0],
IndexError: tuple index out of range
And in a browser
A server error has occurred. Please contact the administrator
Quick workaround
  1. Open /Users/pythonaut/.virtualenvs/dev/src/django/django/views/debug.py(use your own path from error trace) in your favourite editor
  2. Goto line 306, find this text
  3.     'message': self.exc_value.args[0],
    
  4. Replace it with
  5.     'message': self.exc_value.args[0] if self.exc_value.args else None,
    
Now you can restart http server(if it wasn't done automagically) and see detailed debug information.

Thursday, October 20, 2011

tddspry, twill and UnicodeDecodeError

Problem was caused by this naive code, which tries to find string "http://www.fedex.com/Tracking?cntry_code=us&tracknumber_list=123465789&language=english" in plain HTML
class ShipmentsListTest(DatabaseTestCase):
    def test_tracking_link_presence(self):
        self.go200('members:shipments_list')
        self.find(self.rentorder.outgoing_tracking_link, flat=True, escape=True)
Which causes this:
  File "/Users/zomg/.virtualenvs/dev/src/tddspry/tddspry/django/cases.py", line 497, in find
    real_count = html.count(what)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 277: ordinal not in range(128)
Follownig tddspry code we can found that on escape flag it applies django escape method, which returns an unicode string.
from django.utils.html import escape as real_escape
.. skipped ..

def find(self, what, flags='', flat=False, count=None, escape=False):
    if escape:
        what = real_escape(what)

Unfortunately, in twill won't work with unicode strings. So here is quick workaround, make string escaping manually and force it to string.
from django.utils.html import escape as real_escape

class ShipmentsListTest(DatabaseTestCase):
    def test_tracking_link_presence(self):
        self.go200('members:shipments_list')
        link = str(real_escape(self.rentorder.outgoing_tracking_link))
        self.find(link, flat=True)

Voila!

Wednesday, October 19, 2011

How to clear SUDS cache

At suds/cache.py we can follow the way how is the cache path is generated.
    if location is None:
            location = os.path.join(tmp(), 'suds')
        log.debug("cache dir is %s" % location)
        self.location = location
Here is the code that wipes all chache data in default place.
import os
import shutil
from tempfile import gettempdir as tmp
shutil.rmtree(os.path.join(tmp(), 'suds'), True)

Wednesday, March 23, 2011

AttributeError: 'module' object has no attribute 'parse_vars'

UPD recipe author has fixed this problem, so if you only should update your recipe egg

Usually I use buildout for my django projects, which builds complete environment to run project.
One of environment parts is uwsgi application container server, for installing which i use shaunsephton.recipe.uwsgi(to be honest, i use self-patched version of this recipe, but it's other story).

Recently, when I tried to rebuild my project, I was surprise on building uwsgi part:
original exception was skipped
...
File "/package/develop/recipes/uwsgi/shaunsephton/recipe/uwsgi.py", line 93, in build_uwsgi
uwsgiconfig.parse_vars()
AttributeError: 'module' object has no attribute 'parse_vars'
...

As we see, the problem is in call of non-existent attribute(method) parse_vars of uwsgiconfig module.

After few minutes groundless looking in sources, i recall that this buildout recipe always get latest uwsgi version to build it. No doubt, this method was refactored in 'new' latest tarball, so the exception was raised in cause of backward incompatible changes.

To find out what exactly was changed, and see how to use it now i had to find changeset that made uwsgiconfig.py incompatible, so i went to uwsgi trac site and lurked in http://projects.unbit.it/uwsgi/log/uwsgiconfig.py and found changeset
where uwsgiconfig.parse_vars was removed.

When we know how to use new code we can solve recipe problem in this part :
uwsgiconfig = __import__('uwsgiconfig')
uwsgiconfig.parse_vars()
uwsgiconfig.build_uwsgi('uwsgi')

To fix this, and be compatible with older versions(if you want be able to build older versions), i had to replace that piece of code with this:
uwsgiconfig = __import__('uwsgiconfig')
try:
uwsgiconfig.parse_vars()
uwsgiconfig.build_uwsgi('uwsgi')
except AttributeError:
uwsgiconfig.build_uwsgi(uwsgiconfig.uConf('buildconf/default.ini'))