Permalink
Browse files

Support difference between types.MethodType constructors in Python2 v…

…s. Python3
  • Loading branch information...
1 parent 189eaa5 commit dd56eea7375f24b782c136cfacca30d4022e9132 @rtyler committed Jan 3, 2010
Showing with 13 additions and 12 deletions.
  1. +13 −12 cheetah/Template.py
View
@@ -31,18 +31,19 @@ def release(self):
pass
filetype = None
-try:
- # Python3
+
+if isinstance(sys.version_info, tuple):
+ # Python 2.xx
+ filetype = types.FileType
+ def createMethod(func, cls):
+ return types.MethodType(func, None, cls)
+else:
import io
filetype = io.IOBase
-except ImportError:
- filetype = types.FileType
+ def createMethod(func, cls):
+ return types.MethodType(func, cls)
+
-try:
- x = set()
-except NameError:
- # Python 2.3 compatibility
- from sets import Set as set
from Cheetah.Version import convertVersionStringToTuple, MinCompatibleVersionTuple
from Cheetah.Version import MinCompatibleVersion
@@ -980,7 +981,7 @@ def _addCheetahPlumbingCodeToClass(klass, concreteTemplateClass):
for methodname in klass._CHEETAH_requiredCheetahMethods:
if not hasattr(concreteTemplateClass, methodname):
method = getattr(Template, methodname)
- newMethod = types.MethodType(method.im_func, None, concreteTemplateClass)
+ newMethod = createMethod(method.im_func, concreteTemplateClass)
setattr(concreteTemplateClass, methodname, newMethod)
for classMethName in klass._CHEETAH_requiredCheetahClassMethods:
@@ -1036,8 +1037,8 @@ def __unicode__(self):
else:
return super(self.__class__, self).__unicode__()
- __str__ = types.MethodType(__str__, None, concreteTemplateClass)
- __unicode__ = types.MethodType(__unicode__, None, concreteTemplateClass)
+ __str__ = createMethod(__str__, concreteTemplateClass)
+ __unicode__ = createMethod(__unicode__, concreteTemplateClass)
setattr(concreteTemplateClass, '__str__', __str__)
setattr(concreteTemplateClass, '__unicode__', __unicode__)

0 comments on commit dd56eea

Please sign in to comment.