Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#18983 closed Bug (wontfix)

django.contrib.staticfiles.finders.find can return [None] if a file is not found with a custom finder

Reported by: Paul Swartz (PCF) Owned by: nobody
Component: contrib.staticfiles Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If a custom Finder returns None when it can't find a file (when all=True), the find() function wraps it in a list and adds it to the possible return values instead of recognizing it as a not-found file.

It results in this not-too-useful exception:

Traceback (most recent call last):

  File "/home/ec2-user/AdmitCloud/virtualenv/lib/python2.6/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)

  File "/home/ec2-user/AdmitCloud/virtualenv/lib/python2.6/site-packages/django/contrib/staticfiles/views.py", line 38, in serve
    document_root, path = os.path.split(absolute_path)

  File "/home/ec2-user/AdmitCloud/virtualenv/lib64/python2.6/posixpath.py", line 82, in split
    i = p.rfind('/') + 1

AttributeError: 'list' object has no attribute 'rfind'

If you're writing the custom Finder, the quick fix is to return [] instead of None regardless of the all setting. I noticed it on 1.3, but it looks like it's still a problem in master. I'll throw a patch up on GitHub shortly.

Change History (5)

comment:2 by Preston Holmes, 12 years ago

While the patch looks technically sound, I have to point out:

https://github.com/django/django/blob/master/docs/ref/contrib/staticfiles.txt#L124
"Static file finders are currently considered a private interface, and this interface is thus undocumented."

While I can't think of any reason why [] and None would be meaningfully different ATM, I'm a bit hesitant to accept this without some input from Jannis on the matter.

comment:3 by Jannis Leidel, 12 years ago

Well, it's one of those things, if it doesn't work when returning None just don't return None.

Besides, the finder API is private (see the docs), so technical there is no such thing as a "custom finder". Closing as wontfix.

comment:4 by Jannis Leidel, 12 years ago

Resolution: wontfix
Status: newclosed

comment:5 by Paul Swartz (PCF), 12 years ago

Thanks for the quick review! It's certainly an easy fix in my own code, but it feels weird to be returning [] when the other cases (including find() itself) return None when looking for a single file and nothing is found.

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