This file is indexed.

/usr/share/pyshared/Cheetah/Tests/Regressions.py is in python-cheetah 2.4.4-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env python

import Cheetah.NameMapper 
import Cheetah.Template

import sys
import unittest


majorVer, minorVer = sys.version_info[0], sys.version_info[1]
versionTuple = (majorVer, minorVer)

def isPython23():
    ''' Python 2.3 is still supported by Cheetah, but doesn't support decorators '''
    return majorVer == 2 and minorVer < 4

class GetAttrException(Exception):
    pass

class CustomGetAttrClass(object):
    def __getattr__(self, name):
        raise GetAttrException('FAIL, %s' % name)

class GetAttrTest(unittest.TestCase):
    '''
        Test for an issue occurring when __getatttr__() raises an exception
        causing NameMapper to raise a NotFound exception
    '''
    def test_ValidException(self):
        o = CustomGetAttrClass()
        try:
            print(o.attr)
        except GetAttrException, e:
            # expected
            return
        except:
            self.fail('Invalid exception raised: %s' % e)
        self.fail('Should have had an exception raised')

    def test_NotFoundException(self):
        template = '''
            #def raiseme()
                $obj.attr
            #end def'''

        template = Cheetah.Template.Template.compile(template, compilerSettings={}, keepRefToGeneratedCode=True)
        template = template(searchList=[{'obj' : CustomGetAttrClass()}])
        assert template, 'We should have a valid template object by now'

        self.failUnlessRaises(GetAttrException, template.raiseme)


class InlineImportTest(unittest.TestCase):
    def test_FromFooImportThing(self):
        '''
            Verify that a bug introduced in v2.1.0 where an inline:
                #from module import class
            would result in the following code being generated:
                import class
        '''
        template = '''
            #def myfunction()
                #if True
                    #from os import path
                    #return 17
                    Hello!
                #end if
            #end def
        '''
        template = Cheetah.Template.Template.compile(template, compilerSettings={'useLegacyImportMode' : False}, keepRefToGeneratedCode=True)
        template = template(searchList=[{}])

        assert template, 'We should have a valid template object by now'

        rc = template.myfunction()
        assert rc == 17, (template, 'Didn\'t get a proper return value')

    def test_ImportFailModule(self):
        template = '''
            #try
                #import invalidmodule
            #except
                #set invalidmodule = dict(FOO='BAR!')
            #end try

            $invalidmodule.FOO
        '''
        template = Cheetah.Template.Template.compile(template, compilerSettings={'useLegacyImportMode' : False}, keepRefToGeneratedCode=True)
        template = template(searchList=[{}])

        assert template, 'We should have a valid template object by now'
        assert str(template), 'We weren\'t able to properly generate the result from the template'

    def test_ProperImportOfBadModule(self):
        template = '''
            #from invalid import fail
                
            This should totally $fail
        '''
        self.failUnlessRaises(ImportError, Cheetah.Template.Template.compile, template, compilerSettings={'useLegacyImportMode' : False}, keepRefToGeneratedCode=True)

    def test_AutoImporting(self):
        template = '''
            #extends FakeyTemplate

            Boo!
        '''
        self.failUnlessRaises(ImportError, Cheetah.Template.Template.compile, template)

    def test_StuffBeforeImport_Legacy(self):
        template = '''
###
### I like comments before import
###
#extends Foo
Bar
'''
        self.failUnlessRaises(ImportError, Cheetah.Template.Template.compile, template, compilerSettings={'useLegacyImportMode' : True}, keepRefToGeneratedCode=True)


class Mantis_Issue_11_Regression_Test(unittest.TestCase):
    ''' 
        Test case for bug outlined in Mantis issue #11:
            
        Output:
        Traceback (most recent call last):
          File "test.py", line 12, in <module>
            t.respond()
          File "DynamicallyCompiledCheetahTemplate.py", line 86, in respond
          File "/usr/lib64/python2.6/cgi.py", line 1035, in escape
            s = s.replace("&", "&") # Must be done first! 
    '''
    def test_FailingBehavior(self):
        import cgi
        template = Cheetah.Template.Template("$escape($request)", searchList=[{'escape' : cgi.escape, 'request' : 'foobar'}])
        assert template
        self.failUnlessRaises(AttributeError, template.respond)


    def test_FailingBehaviorWithSetting(self):
        import cgi
        template = Cheetah.Template.Template("$escape($request)", 
                searchList=[{'escape' : cgi.escape, 'request' : 'foobar'}], 
                compilerSettings={'prioritizeSearchListOverSelf' : True})
        assert template
        assert template.respond()

class Mantis_Issue_21_Regression_Test(unittest.TestCase):
    ''' 
        Test case for bug outlined in issue #21

        Effectively @staticmethod and @classmethod
        decorated methods in templates don't 
        properly define the _filter local, which breaks
        when using the NameMapper
    '''
    def runTest(self):
        if isPython23():
            return
        template = '''
            #@staticmethod
            #def testMethod()
                This is my $output
            #end def
        '''
        template = Cheetah.Template.Template.compile(template)
        assert template
        assert template.testMethod(output='bug') # raises a NameError: global name '_filter' is not defined


class Mantis_Issue_22_Regression_Test(unittest.TestCase):
    ''' 
        Test case for bug outlined in issue #22

        When using @staticmethod and @classmethod
        in conjunction with the #filter directive
        the generated code for the #filter is reliant
        on the `self` local, breaking the function
    '''
    def test_NoneFilter(self):
        # XXX: Disabling this test for now
        return
        if isPython23():
            return
        template = '''
            #@staticmethod
            #def testMethod()
                #filter None
                    This is my $output
                #end filter
            #end def
        '''
        template = Cheetah.Template.Template.compile(template)
        assert template
        assert template.testMethod(output='bug')

    def test_DefinedFilter(self):
        # XXX: Disabling this test for now
        return
        if isPython23():
            return
        template = '''
            #@staticmethod
            #def testMethod()
                #filter Filter
                    This is my $output
                #end filter
            #end def
        '''
        # The generated code for the template's testMethod() should look something
        # like this in the 'error' case:
        '''
        @staticmethod
        def testMethod(**KWS):
            ## CHEETAH: generated from #def testMethod() at line 3, col 13.
            trans = DummyTransaction()
            _dummyTrans = True
            write = trans.response().write
            SL = [KWS]
            _filter = lambda x, **kwargs: unicode(x)

            ########################################
            ## START - generated method body

            _orig_filter_18517345 = _filter
            filterName = u'Filter'
            if self._CHEETAH__filters.has_key("Filter"):
                _filter = self._CHEETAH__currentFilter = self._CHEETAH__filters[filterName]
            else:
                _filter = self._CHEETAH__currentFilter = \
                            self._CHEETAH__filters[filterName] = getattr(self._CHEETAH__filtersLib, filterName)(self).filter
            write(u'                    This is my ')
            _v = VFFSL(SL,"output",True) # u'$output' on line 5, col 32
            if _v is not None: write(_filter(_v, rawExpr=u'$output')) # from line 5, col 32.

            ########################################
            ## END - generated method body

            return _dummyTrans and trans.response().getvalue() or ""
        '''
        template = Cheetah.Template.Template.compile(template)
        assert template
        assert template.testMethod(output='bug')


if __name__ == '__main__':
    unittest.main()