[pypy-svn] r9282 - pypy/dist/pypy/tool

tismer at codespeak.net tismer at codespeak.net
Thu Feb 17 18:39:10 CET 2005


Author: tismer
Date: Thu Feb 17 18:39:10 2005
New Revision: 9282

Modified:
   pypy/dist/pypy/tool/sourcetools.py
Log:
added a feature that shows true source code for our generated method stuff.
I think something alike could go into py.code, but this needs to be
discussed. Here the effect of the patch on the source output of _classobj.py:

After running of _classobj.py, this can be done:

>>> import inspect
>>> print inspect.getsource(instance.__div__)
    def __%(op)s__(self, other):
        coerced = _coerce(self, other)
        if coerced is None or coerced[0] is self:
            func = instance_getattr1(self, '__%(op)s__', False)
            if func:
                return func(other)
            return NotImplemented
        else:
            return %(opref)s(self, other)

>>> from pypy.tool import sourcetools
>>> print sourcetools.getsource(instance.__div__)
    def __div__(self, other):
        coerced = _coerce(self, other)
        if coerced is None or coerced[0] is self:
            func = instance_getattr1(self, '__div__', False)
            if func:
                return func(other)
            return NotImplemented
        else:
            return operator.div(self, other)

>>> 


Modified: pypy/dist/pypy/tool/sourcetools.py
==============================================================================
--- pypy/dist/pypy/tool/sourcetools.py	(original)
+++ pypy/dist/pypy/tool/sourcetools.py	Thu Feb 17 18:39:10 2005
@@ -1,7 +1,7 @@
 # a couple of support functions which
 # help with generating Python source.
 
-import sys, os
+import sys, os, inspect
 
 def render_docstr(func, indent_str='', closing_str=''):
     """ Render a docstring as a string of lines.
@@ -70,4 +70,64 @@
             src = 'if 1:\n' + src
         src = '\n' * prelines + src % args
         c = compile(src, self.srcname, "exec")
+        # preserve the arguments of the code in an attribute
+        # of the code's co_filename
+        if self.srcname:
+            srcname = MyStr(self.srcname)
+            srcname.__sourceargs__ = args
+            c = newcode_withfilename(c, srcname)
         return c
+
+def getsource(object):
+    """ similar to inspect.getsource, but trying to
+    find the parameters of formatting generated methods and
+    functions.
+    """
+    src = inspect.getsource(object)
+    name = inspect.getfile(object)
+    if hasattr(name, "__sourceargs__"):
+        return src % name.__sourceargs__
+    return src
+
+## the following is stolen frompy.code.source.py for now.
+## XXX discuss whether and how to put this functionality
+## into py.code.source.
+#
+# various helper functions
+#
+class MyStr(str):
+    """ custom string which allows to add attributes. """
+
+def newcode(fromcode, **kwargs):
+    names = [x for x in dir(fromcode) if x[:3] == 'co_']
+    for name in names:
+        if name not in kwargs:
+            kwargs[name] = getattr(fromcode, name)
+    import new
+    return new.code(
+             kwargs['co_argcount'],
+             kwargs['co_nlocals'],
+             kwargs['co_stacksize'],
+             kwargs['co_flags'],
+             kwargs['co_code'],
+             kwargs['co_consts'],
+             kwargs['co_names'],
+             kwargs['co_varnames'],
+             kwargs['co_filename'],
+             kwargs['co_name'],
+             kwargs['co_firstlineno'],
+             kwargs['co_lnotab'],
+             kwargs['co_freevars'],
+             kwargs['co_cellvars'],
+    )
+
+def newcode_withfilename(co, co_filename):
+    newconstlist = []
+    cotype = type(co)
+    for c in co.co_consts:
+        if isinstance(c, cotype):
+            c = newcode_withfilename(c, co_filename)
+        newconstlist.append(c)
+    return newcode(co, co_consts = tuple(newconstlist),
+                       co_filename = co_filename)
+



More information about the Pypy-commit mailing list