o aG@sDdZddlZddlmZddlmZddlmZGdddeZdS) z Wrapper class that takes a list of template loaders as an argument and attempts to load templates from them in order, caching the result. N)TemplateDoesNotExist)copy_exception)Loadercs\eZdZfddZddZddZdfdd Zd d Zdd d ZddZ ddZ Z S)rcs"i|_|||_t|dSN)get_template_cacheget_template_loadersloaderssuper__init__)selfenginer  __class__@/usr/lib/python3/dist-packages/django/template/loaders/cached.pyr s zLoader.__init__ccs*|jD]}t|dr|EdHqdS)Nget_dirs)r hasattrr)r loaderrrrrs   zLoader.get_dirscCs |j|Sr)r get_contents)r originrrrrs zLoader.get_contentsNc s|||}|j|}|r't|trt|tr||t|tr%t||Sz t ||}WntyJ}z|j j r@t|nt|j|<d}~ww||j|<|S)a  Perform the caching that gives this loader its name. Often many of the templates attempted will be missing, so memory use is of concern here. To keep it in check, caching behavior is a little complicated when a template is not found. See ticket #26306 for more details. With template debugging disabled, cache the TemplateDoesNotExist class for every missing template and raise a new instance of it after fetching it from the cache. With template debugging enabled, a unique TemplateDoesNotExist object is cached for each missing template to preserve debug data. When raising an exception, Python sets __traceback__, __context__, and __cause__ attributes on it. Those attributes can contain references to all sorts of objects up the call chain and caching them creates a memory leak. Thus, unraised copies of the exceptions are cached and copies of those copies are raised after they're fetched from the cache. N) cache_keyrget isinstancetype issubclassrrr get_templater debug)r template_nameskipkeycachedtemplateerrrrs"    zLoader.get_templateccs"|jD] }||EdHqdSr)r get_template_sources)r rrrrrr$Cs zLoader.get_template_sourcescsDd}|rfdd|D}|r||}dddt|fDS)aj Generate a cache key for the template name and skip. If skip is provided, only origins that match template_name are included in the cache key. This ensures each template is only parsed and cached once if contained in different extend chains like: x -> a -> a y -> a -> a z -> a -> a csg|] }|jkr|jqSr)rname).0rrrr Vsz$Loader.cache_key..-css|]}|r|VqdSrr)r'srrr Zsz#Loader.cache_key..) generate_hashjoinstr)r rr skip_prefixmatchingrr(rrGs   zLoader.cache_keycCstd|S)N|)hashlibsha1r.encode hexdigest)r valuesrrrr-\szLoader.generate_hashcCs|jdS)zEmpty the template cache.N)rclear)r rrrreset_sz Loader.resetr) __name__ __module__ __qualname__r rrrr$rr-r9 __classcell__rrrrrs & r) __doc__r3django.templaterdjango.template.backends.djangorbaser BaseLoaderrrrrs