[Python-modules-team] Bug#525770: python-django-tagging: _calculate_tag_weight() may return value which is greater than max_weight.

Krzysztof Klimonda kklimonda at syntaxhighlighted.com
Mon Apr 27 00:23:20 UTC 2009


Package: python-django-tagging
Version: 0.2.1+svn147-1
Severity: important
Tags: patch
User: ubuntu-devel at lists.ubuntu.com
Usertags: origin-ubuntu jaunty ubuntu-patch

Current version of package ``python-django-tagging'' has a long standing bug
in a _calculate_tag_weight() method.

When a programmer creates tag cloud using method util.calculate_cloud()
or Tag.objects.cloud_for_model() and sets distribution argument to
``LOGARITHMIC'' (or leave it empty as it is default value)
_calculate_tag_weight() in some cases may return result which is greater
than max_weight - because of that Tag object (the one most frequently
used in model) will be missing argument ``font_size'' and throw AttributeError
exception. It happens because of the way a floating point division is done
in Python.

The proposed patch address this issue by returning ``max_weight'' in case when
result of computation is greater than it.

A link to upstream bugreport: http://code.google.com/p/django-tagging/issues/detail?id=91

In LP bug there is a simple testcase which shows this issue.

*** /tmp/tmpRSdnGr
In Ubuntu, we've applied the attached patch to achieve the following:

  * debian/patches/02_fix_calculate_tag_weight.patch: Fix error in
    _calculate_tag_weight method which in some cases may lead 
    to font_size attribute of most frequently used tag to be left unset.
    (LP: #367214)

We thought you might be interested in doing the same. 


-- System Information:
Debian Release: 5.0
  APT prefers jaunty-updates
  APT policy: (500, 'jaunty-updates'), (500, 'jaunty-security'), (500, 'jaunty')
Architecture: amd64 (x86_64)

Kernel: Linux 2.6.28-11-generic (SMP w/2 CPU cores)
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash
-------------- next part --------------
--- python-django-tagging-0.2.1+svn154.orig/debian/patches/02_fix_calculate_tag_weight.patch
+++ python-django-tagging-0.2.1+svn154/debian/patches/02_fix_calculate_tag_weight.patch
@@ -0,0 +1,15 @@
+Comment: When logarithmic distribution is used _calculate_tag_weight may
+return weight greater than maximum threshold. Because of this the attribute
+``font_size'' of the most frequently used tag may be left unset.
+--- django-tagging.old/tagging/utils.py	(revision 132)
++++ django-tagging/tagging/utils.py	(working copy)
+@@ -230,7 +230,7 @@
+     if distribution == LINEAR or max_weight == 1:
+         return weight
+     elif distribution == LOGARITHMIC:
+-        return math.log(weight) * max_weight / math.log(max_weight)
++        return min((max_weight, math.log(weight) * max_weight / math.log(max_weight)))
+     raise ValueError(_('Invalid distribution algorithm specified: %s.') % distribution)
+ 
+ def calculate_cloud(tags, steps=4, distribution=LOGARITHMIC):
+


More information about the Python-modules-team mailing list