[Django 1.7] horizon table summation can raise TypeError

Bug #1355939 reported by Akihiro Motoki
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
OpenStack Dashboard (Horizon)
Fix Released
Medium
Akihiro Motoki

Bug Description

https://review.openstack.org/#/c/111932/

With Django 1.7, the unit tests fail with the following error.
It is one of work towards django 1.7.

"average": lambda data: sum(data, 0.0) / len(data)

TypeError: unsupported operand type(s) for +: 'float' and 'str'

With Django 1.6, the template code that looked up the variable behind
get_summation was catching the TypeError exception:

    try: # method call (assuming no args required)
        current = current()
    except TypeError: # arguments *were* required
        # GOTCHA: This will also catch any TypeError
        # raised in the function itself.
        current = settings.TEMPLATE_STRING_IF_INVALID # invalid

With Django 1.7, the code has been refined to catch the exception only
when the function really requires argument (which get_summation()
doesn't):

    try: # method call (assuming no args required)
        current = current()
    except TypeError:
        try:
            getcallargs(current)
        except TypeError: # arguments *were* required
            current = settings.TEMPLATE_STRING_IF_INVALID # invalid
        else:
            raise

Note that we need to install Django 1.7rc2 to reproduce this.
So instead of blindly relying on sum(), I introduced a safe_sum() and
safe_average() functions which mimick the behaviour we got with Django
1.6 by returning an empty string when we have invalid input data.

Tags: django1.7
Akihiro Motoki (amotoki)
description: updated
Changed in horizon:
assignee: nobody → Akihiro Motoki (amotoki)
status: New → In Progress
Akihiro Motoki (amotoki)
Changed in horizon:
importance: Undecided → Medium
milestone: none → juno-3
Thierry Carrez (ttx)
Changed in horizon:
milestone: juno-3 → juno-rc1
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to horizon (master)

Reviewed: https://review.openstack.org/111932
Committed: https://git.openstack.org/cgit/openstack/horizon/commit/?id=d9c8a2e62fbf620eef4d1bdb292cb6ca6b830f17
Submitter: Jenkins
Branch: master

commit d9c8a2e62fbf620eef4d1bdb292cb6ca6b830f17
Author: Akihiro Motoki <email address hidden>
Date: Wed Aug 13 04:05:11 2014 +0900

    Handle TypeError from table column summation code

    This commit catches TypeError from horizon.tables.Column
    summation calculation. This TypeError is caught inside Django
    until Django 1.6, but Django 1.7 code is refined to catch
    more specific case and it leads to horizon unit test failure.

    Closes-Bug: #1355939
    Change-Id: I9d5b4565f1238a9880ccf117f2ea623fed466a44

Changed in horizon:
status: In Progress → Fix Committed
Thierry Carrez (ttx)
Changed in horizon:
status: Fix Committed → Fix Released
Thierry Carrez (ttx)
Changed in horizon:
milestone: juno-rc1 → 2014.2
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.