Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#19829 closed Bug (fixed)

Index lookup fails for NumPy arrays in templates

Reported by: Julien Phalip Owned by: nobody
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The following will crash with a ValueError when using a NumPy array:

{{ my_numpy_array.0 }}

This is because currently Django only handles index lookups for regular Python lists, by catching a TypeError. See how differently regular lists and NumPy arrays behave:

>>> regular_list = [1,2,3,4]
>>> regular_list['0']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str

>>> import numpy
>>> numpy_array = numpy.array([1,2,3,4])
>>> numpy_array['0']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: field named 0 not found.

The attached patch fixes this issue by handling ValueError too.

Change History (5)

comment:2 by Claude Paroz, 12 years ago

Triage Stage: UnreviewedReady for checkin
Version: 1.4master

comment:3 by Julien Phalip <jphalip@…>, 12 years ago

Resolution: fixed
Status: newclosed

In 7d5e35cdb46124e2471833b9570add1a00a1d9e0:

Fixed #19829 -- Fixed index lookups for NumPy arrays in templates.

comment:4 by Julien Phalip <jphalip@…>, 12 years ago

In 400fba9ca847c40310491c560c9ce71144deec20:

Merge pull request #728 from jphalip/ticket-19829

Fixed #19829 -- Fixed index lookups for NumPy arrays in templates.

comment:5 by Julien Phalip <jphalip@…>, 12 years ago

In 42e87c17f27f68ea43c78938d4972211b141ede3:

[1.5.x] Fixed #19829 -- Fixed index lookups for NumPy arrays in templates.
Backport of 7d5e35cdb46124e2471

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