Ticket #1194: filesystem.diff

File filesystem.diff, 2.1 KB (added by tbarta@…, 12 years ago)

patch to generate a different exception type when a file is found but unreadable

  • __init__.py

     
    116116class TemplateDoesNotExist(Exception):
    117117    pass
    118118
     119class TemplateIsNotReadable(TemplateDoesNotExist):
     120    # subclassed to provide backwards-compatibility in catch blocks
     121    pass
     122
    119123class VariableDoesNotExist(Exception):
    120124
    121125    def __init__(self, msg, params=()):
  • loaders/filesystem.py

     
    11# Wrapper for loading templates from the filesystem.
    22
    33from django.conf import settings
    4 from django.template import TemplateDoesNotExist
     4from django.template import TemplateDoesNotExist, TemplateIsNotReadable
    55import os
     6import errno
    67
    78def get_template_sources(template_name, template_dirs=None):
    89    if not template_dirs:
     
    1213
    1314def load_template_source(template_name, template_dirs=None):
    1415    tried = []
     16    found = []
     17    error_class = TemplateDoesNotExist
    1518    for filepath in get_template_sources(template_name, template_dirs):
    1619        try:
    1720            return (open(filepath).read(), filepath)
    18         except IOError:
    19             tried.append(filepath)
    20     if tried:
     21        except IOError, (code, message):
     22            if code == errno.EACCES:
     23                # file exists but is unreadable
     24                tried.append(filepath)
     25                found.append(filepath)
     26                error_class = TemplateIsNotReadable
     27            else:
     28                # file didn't exist, or something else happened
     29                tried.append(filepath)
     30
     31    if found:
     32        # if we found something, but it was unreadable, make a special note
     33        error_msg = "Tried %s, Found unreadable %s" % (tried, found)
     34    elif tried:
    2135        error_msg = "Tried %s" % tried
    2236    else:
    2337        error_msg = "Your TEMPLATE_DIRS setting is empty. Change it to point to at least one template directory."
    24     raise TemplateDoesNotExist, error_msg
     38    raise error_class, error_msg
    2539load_template_source.is_usable = True
Back to Top