Showing posts with label workaround. Show all posts
Showing posts with label workaround. Show all posts

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, 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'))