Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#19829 closed Bug (fixed)

Index lookup fails for NumPy arrays in templates

Reported by: julien Owned by: nobody
Component: Template system Version: master
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 Changed 3 years ago by claudep

  • Triage Stage changed from Unreviewed to Ready for checkin
  • Version changed from 1.4 to master

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

  • Resolution set to fixed
  • Status changed from new to closed

In 7d5e35cdb46124e2471833b9570add1a00a1d9e0:

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

comment:4 Changed 3 years ago by Julien Phalip <jphalip@…>

In 400fba9ca847c40310491c560c9ce71144deec20:

Merge pull request #728 from jphalip/ticket-19829

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

comment:5 Changed 3 years ago by Julien Phalip <jphalip@…>

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