Edgewall Software

Changeset 704

Show
Ignore:
Timestamp:
08/10/07 11:44:00 (2 years ago)
Author:
cmlenz
Message:

Fix includes so that they again raise an exception when the included template is not found and no fallback has been provided.

Location:
trunk
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/ChangeLog

    r702 r704  
    2727 * Fixed handling of nested function and class definitions in Python code 
    2828   blocks. 
     29 * Includes were not raising `TemplateNotFound` exceptions even when no 
     30   fallback has been specified. That has been corrected. 
     31 * The template loader now raises a `TemplateNotFound` error when a previously 
     32   cached template is removed or renamed, where it previously was passing up 
     33   an `OSError`. 
    2934 
    3035 
  • trunk/genshi/template/base.py

    r675 r704  
    393393                                yield event 
    394394                        continue 
    395                     else: 
     395                    elif fallback: 
    396396                        # Otherwise the include is performed at run time 
    397397                        data = href, list(self._prepare(fallback)) 
  • trunk/genshi/template/loader.py

    r657 r704  
    175175                        os.path.getmtime(tmpl.filepath) == self._mtime[filename]: 
    176176                    return tmpl 
    177             except KeyError: 
     177            except KeyError, OSError: 
    178178                pass 
    179179 
  • trunk/genshi/template/markup.py

    r661 r704  
    8484        ns_prefix = {} 
    8585        depth = 0 
    86         in_fallback = 0 
    87         include_href = None 
     86        fallbacks = [] 
     87        includes = [] 
    8888 
    8989        if not isinstance(source, Stream): 
     
    156156                                                      'attribute "href"', 
    157157                                                      self.filepath, *pos[1:]) 
     158                        includes.append(include_href) 
    158159                        streams.append([]) 
    159160                    elif tag.localname == 'fallback': 
    160                         in_fallback += 1 
     161                        streams.append([]) 
     162                        fallbacks.append(streams[-1]) 
    161163 
    162164                else: 
     
    168170                depth -= 1 
    169171 
    170                 if in_fallback and data == self.XINCLUDE_NAMESPACE['fallback']: 
    171                     in_fallback -= 1 
     172                if fallbacks and data == self.XINCLUDE_NAMESPACE['fallback']: 
     173                    assert streams.pop() is fallbacks[-1] 
    172174                elif data == self.XINCLUDE_NAMESPACE['include']: 
    173                     fallback = streams.pop() 
     175                    fallback = None 
     176                    if len(fallbacks) == len(includes): 
     177                        fallback = fallbacks.pop() 
     178                    streams.pop() # discard anything between the include tags 
     179                                  # and the fallback element 
    174180                    stream = streams[-1] 
    175                     stream.append((INCLUDE, (include_href, fallback), pos)) 
     181                    stream.append((INCLUDE, (includes.pop(), fallback), pos)) 
    176182                else: 
    177183                    stream.append((kind, data, pos)) 
  • trunk/genshi/template/tests/markup.py

    r657 r704  
    2323from genshi.input import XML 
    2424from genshi.template.base import BadDirectiveError, TemplateSyntaxError 
    25 from genshi.template.loader import TemplateLoader 
     25from genshi.template.loader import TemplateLoader, TemplateNotFound 
    2626from genshi.template.markup import MarkupTemplate 
    2727 
     
    352352            shutil.rmtree(dirname) 
    353353 
     354    def test_error_when_include_not_found(self): 
     355        dirname = tempfile.mkdtemp(suffix='genshi_test') 
     356        try: 
     357            file2 = open(os.path.join(dirname, 'tmpl2.html'), 'w') 
     358            try: 
     359                file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude"> 
     360                  <xi:include href="tmpl1.html"/> 
     361                </html>""") 
     362            finally: 
     363                file2.close() 
     364 
     365            loader = TemplateLoader([dirname], auto_reload=True) 
     366            tmpl = loader.load('tmpl2.html') 
     367            self.assertRaises(TemplateNotFound, tmpl.generate().render) 
     368        finally: 
     369            shutil.rmtree(dirname) 
     370 
    354371    def test_fallback_when_include_not_found(self): 
    355372        dirname = tempfile.mkdtemp(suffix='genshi_test') 
     
    398415            tmpl = loader.load('tmpl3.html') 
    399416            self.assertEqual("""<html> 
    400                   <div>Included</div> 
     417                      <div>Included</div> 
    401418                </html>""", tmpl.generate().render()) 
    402419        finally: 
     
    423440            tmpl = loader.load('tmpl3.html') 
    424441            self.assertEqual("""<html> 
    425                         Missing 
     442                      Missing 
     443                </html>""", tmpl.generate().render()) 
     444        finally: 
     445            shutil.rmtree(dirname) 
     446 
     447    def test_nested_include_in_fallback(self): 
     448        dirname = tempfile.mkdtemp(suffix='genshi_test') 
     449        try: 
     450            file1 = open(os.path.join(dirname, 'tmpl2.html'), 'w') 
     451            try: 
     452                file1.write("""<div>Included</div>""") 
     453            finally: 
     454                file1.close() 
     455 
     456            file2 = open(os.path.join(dirname, 'tmpl3.html'), 'w') 
     457            try: 
     458                file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude"> 
     459                  <xi:include href="tmpl2.html"> 
     460                    <xi:fallback> 
     461                      <xi:include href="tmpl1.html" /> 
     462                    </xi:fallback> 
     463                  </xi:include> 
     464                </html>""") 
     465            finally: 
     466                file2.close() 
     467 
     468            loader = TemplateLoader([dirname]) 
     469            tmpl = loader.load('tmpl3.html') 
     470            self.assertEqual("""<html> 
     471                  <div>Included</div> 
    426472                </html>""", tmpl.generate().render()) 
    427473        finally: