Skip to content

Instantly share code, notes, and snippets.

@rtyler
Created May 7, 2009 20:06
Show Gist options
  • Select an option

  • Save rtyler/108321 to your computer and use it in GitHub Desktop.

Select an option

Save rtyler/108321 to your computer and use it in GitHub Desktop.
Cheetah v2.1.1 - v2.1.2 source changes
diff --git a/src/Compiler.py b/src/Compiler.py
index 71eeeb9..be3b63f 100644
--- a/src/Compiler.py
+++ b/src/Compiler.py
@@ -853,7 +853,8 @@ class MethodCompiler(GenUtils):
return self.nextCacheID()
def startCallRegion(self, functionName, args, lineCol, regionTitle='CALL'):
- class CallDetails: pass
+ class CallDetails(object):
+ pass
callDetails = CallDetails()
callDetails.ID = ID = self.nextCallRegionID()
callDetails.functionName = functionName
@@ -1023,6 +1024,8 @@ class AutoMethodCompiler(MethodCompiler):
MethodCompiler._setupState(self)
self._argStringList = [ ("self",None) ]
self._streamingEnabled = True
+ self._isClassMethod = None
+ self._isStaticMethod = None
def _useKWsDictArgForPassingTrans(self):
alreadyHasTransArg = [argname for argname,defval in self._argStringList
@@ -1030,6 +1033,16 @@ class AutoMethodCompiler(MethodCompiler):
return (self.methodName()!='respond'
and not alreadyHasTransArg
and self.setting('useKWsDictArgForPassingTrans'))
+
+ def isClassMethod(self):
+ if self._isClassMethod is None:
+ self._isClassMethod = '@classmethod' in self._decorators
+ return self._isClassMethod
+
+ def isStaticMethod(self):
+ if self._isStaticMethod is None:
+ self._isStaticMethod = '@staticmethod' in self._decorators
+ return self._isStaticMethod
def cleanupState(self):
MethodCompiler.cleanupState(self)
@@ -1071,7 +1084,7 @@ class AutoMethodCompiler(MethodCompiler):
if self._initialMethodComment:
self.addChunk(self._initialMethodComment)
- if self._streamingEnabled:
+ if self._streamingEnabled and not self.isClassMethod() and not self.isStaticMethod():
if self._useKWsDictArgForPassingTrans() and self._kwargsName:
self.addChunk('trans = %s.get("trans")'%self._kwargsName)
self.addChunk('if (not trans and not self._CHEETAH__isBuffering'
@@ -1099,9 +1112,11 @@ class AutoMethodCompiler(MethodCompiler):
pass
elif allowSearchListAsMethArg and 'searchList' in argNames:
self.addChunk('SL = searchList')
- else:
+ elif not self.isClassMethod() and not self.isStaticMethod():
self.addChunk('SL = self._CHEETAH__searchList')
- if self.setting('useFilters'):
+ else:
+ self.addChunk('SL = [KWS]')
+ if self.setting('useFilters') and not self.isClassMethod() and not self.isStaticMethod():
self.addChunk('_filter = self._CHEETAH__currentFilter')
self.addChunk('')
self.addChunk("#" *40)
@@ -1128,6 +1143,11 @@ class AutoMethodCompiler(MethodCompiler):
argStringChunks = []
for arg in self._argStringList:
chunk = arg[0]
+ if chunk == 'self' and self.isClassMethod():
+ chunk = 'cls'
+ if chunk == 'self' and self.isStaticMethod():
+ # Skip the "self" method for @staticmethod decorators
+ continue
if not arg[1] == None:
chunk += '=' + arg[1]
argStringChunks.append(chunk)
diff --git a/src/Tests/Template.py b/src/Tests/Template.py
index bca1b30..53e3a99 100644
--- a/src/Tests/Template.py
+++ b/src/Tests/Template.py
@@ -322,6 +322,36 @@ class TryExceptImportTest(TemplateTest):
klass = Template.compile(source=source, compilerSettings={'useLegacyImportMode' : False})
t = klass(namespaces={'foo' : 1234})
+class ClassMethodSupport(TemplateTest):
+ def test_BasicDecorator(self):
+ template = '''
+ #@classmethod
+ #def myClassMethod()
+ #return '$foo = %s' % $foo
+ #end def
+ '''
+ template = Template.compile(source=template)
+ try:
+ rc = template.myClassMethod(foo='bar')
+ assert rc == '$foo = bar', (rc, 'Template class method didn\'t return what I expected')
+ except AttributeError, ex:
+ self.fail(ex)
+
+class StaticMethodSupport(TemplateTest):
+ def test_BasicDecorator(self):
+ template = '''
+ #@staticmethod
+ #def myStaticMethod()
+ #return '$foo = %s' % $foo
+ #end def
+ '''
+ template = Template.compile(source=template)
+ try:
+ rc = template.myStaticMethod(foo='bar')
+ assert rc == '$foo = bar', (rc, 'Template class method didn\'t return what I expected')
+ except AttributeError, ex:
+ self.fail(ex)
+
diff --git a/src/Version.py b/src/Version.py
index 30bf439..390053d 100644
--- a/src/Version.py
+++ b/src/Version.py
@@ -1,5 +1,5 @@
-Version = '2.1.1'
-VersionTuple = (2,1,1,'final',0)
+Version = '2.1.2'
+VersionTuple = (2,1,2,'final',0)
MinCompatibleVersion = '2.0rc6'
MinCompatibleVersionTuple = (2,0,0,'candidate',6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment