Commits

Stephen Tu committed b659f34

shares FP for writing

  • Participants
  • Parent commits b99efe3

Comments (0)

Files changed (2)

File lib-python/2.7/instrumentation.py

+
+def get_output_file_object():
+  import os
+  if not hasattr(get_output_file_object, '_fobj'):
+      trace_file = os.getenv('PYTHON_TRACE')
+      if not trace_file:
+          trace_file = os.path.join(os.path.expanduser('~'), 'py.trace')
+      get_output_file_object._fobj = open(trace_file, 'w')
+  return get_output_file_object._fobj

File pypy/interpreter/astcompiler/astbuilder.py

 from pypy.interpreter.pyparser import future, pyparse, parsestring
 from pypy.rlib.objectmodel import specialize
 
+import os
+
 ### XXX: mutates AST **in place**, instead of returning a copy
 class LineNumberVisitor(ast.ASTVisitor):
     def __init__(self, lineno):
     _EXCLUDES_ = ('pypy', 'lib_pypy', 'lib-python')
 
     def do_instrument_file(self):
-        import os
         fname = os.path.basename(self.compile_info.filename)
         if fname == '?':
           return False
             if not self.do_instrument_file():
               return ast.Module(stmts)
 
-            my_source = \
-('def %s' % (self._HIDDEN_NAME_)) + '''(filename, lineno, column, varname, var):
-  print "filename=%s, lineno=%d, column=%d, varname=%s, type=%s.%s" % (
-    filename, lineno, column, varname, type(var).__module__, type(var).__name__)
+#            my_source = \
+#('def %s' % (self._HIDDEN_NAME_)) + '''(filename, lineno, column, varname, var):
+#  print "filename=%s, lineno=%d, column=%d, varname=%s, type=%s.%s" % (
+#    filename, lineno, column, varname, type(var).__module__, type(var).__name__)
+#  return var
+#'''
+
+            my_source = '''
+def %s(filename, lineno, column, varname, var):
+  import instrumentation
+  import json
+  rec = {
+    'filename' : filename,
+    'lineno'   : lineno,
+    'column'   : column,
+    'varname'  : varname,
+    'type'     : type(var).__module__ + '.' + type(var).__name__,
+  }
+  fp = instrumentation.get_output_file_object()
+  print >>fp, json.dumps(rec)
   return var
-'''
+''' % (self._HIDDEN_NAME_)
+
             ci = pyparse.CompileInfo(self.compile_info.filename,
                                      self.compile_info.mode,
                                      self.compile_info.flags)