[PEAK] DecoratorTools and python -OO

Jim Meier jim at dsdd.org
Fri Apr 30 18:35:23 EDT 2010


Hi folks, I hope this is the right list. If not, please direct me to the
best place for DecoratorTools development discussion.

We're trying to cut down the memory usage of our system, and it looks
like one area that might help is to use python's -OO flag to strip
docstrings from the bytecode. This works reasonable well as far as it
goes, but runs into an issue where DecoratorTools is using a function's
__doc__ attribute to store some code templates. Under -OO, __doc__ is
set to None, which causes a traceback when apply_template tried to
access the nonexistent replace method on it. Simply using an alternate
attribute resolves the problem.

Attached is a patch that uses the attribute "__template__".



-------------- next part --------------
--- DecoratorTools-1.7-orig-py2.5.egg/peak/util/decorators.py	2007-12-29 12:22:30.000000000 -0600
+++ DecoratorTools-1.7-py2.5.egg/peak/util/decorators.py	2010-04-30 15:59:51.000000000 -0600
@@ -56,7 +56,7 @@
 def apply_template(wrapper, func, *args, **kw):
     funcname, argspec  = name_and_spec(func)
     wrapname, wrapspec = name_and_spec(wrapper)
-    body = wrapper.__doc__.replace('%','%%').replace('$args','%(argspec)s')
+    body = wrapper.__template__.replace('%','%%').replace('$args','%(argspec)s')
     d ={}
     body = """
 def %(wrapname)s(%(wrapspec)s):
@@ -302,7 +302,9 @@
         return decorate_assignment(lambda f,k,v,o: synchronized(v))
 
     def wrap(__func):
-        '''
+        pass
+
+    wrap.__template__ = '''
         try:
             lock = $self.__lock__
         except AttributeError:
@@ -318,7 +320,7 @@
             lock.release()'''
     from inspect import getargspec
     first_arg = getargspec(func)[0][0]
-    wrap.__doc__ = wrap.__doc__.replace('$self', first_arg)
+    wrap.__template__ = wrap.__template__.replace('$self', first_arg)
     return apply_template(wrap, func)
 
 



More information about the PEAK mailing list