[py-svn] r7911 - in py/dist/py: . code magic

arigo at codespeak.net arigo at codespeak.net
Fri Dec 17 20:32:43 CET 2004


Author: arigo
Date: Fri Dec 17 20:32:42 2004
New Revision: 7911

Added:
   py/dist/py/code/frame.py   (contents, props changed)
Modified:
   py/dist/py/__init__.py
   py/dist/py/magic/exprinfo.py
Log:
moved RunnerFrame to its own file in the new 'py.code' directory.


Modified: py/dist/py/__init__.py
==============================================================================
--- py/dist/py/__init__.py	(original)
+++ py/dist/py/__init__.py	Fri Dec 17 20:32:42 2004
@@ -46,6 +46,7 @@
     'magic.View'             : ('./magic/viewtype.py', 'View'),
     'magic.AssertionError'   : ('./magic/assertion.py', 'AssertionError'),
     'code.Source'            : ('./code/source.py', 'Source'),
+    'code.RunnerFrame'       : ('./code/frame.py', 'RunnerFrame'),
     'execnet.SocketGateway'  : ('./execnet/register.py', 'SocketGateway'),
     'execnet.PopenGateway'   : ('./execnet/register.py', 'PopenGateway'),
 })

Added: py/dist/py/code/frame.py
==============================================================================
--- (empty file)
+++ py/dist/py/code/frame.py	Fri Dec 17 20:32:42 2004
@@ -0,0 +1,23 @@
+
+class RunnerFrame:
+    """Wrapper around a Python frame holding f_locals and f_globals
+    in which expressions can be evaluated."""
+    
+    def __init__(self, frame):
+        self.f_globals = frame.f_globals
+        self.f_locals = getattr(frame, 'f_locals', {})
+        self.f_code = getattr(frame, 'f_code', None)      # for inspection
+
+    def eval(self, code, **vars):
+        self.f_locals.update(vars)
+        return eval(code, self.f_globals, self.f_locals)
+
+    def exec_(self, code, **vars):
+        self.f_locals.update(vars)
+        exec code in self.f_globals, self.f_locals
+
+    def repr(self, object):
+        return repr(object)
+
+    def is_true(self, object):
+        return object

Modified: py/dist/py/magic/exprinfo.py
==============================================================================
--- py/dist/py/magic/exprinfo.py	(original)
+++ py/dist/py/magic/exprinfo.py	Fri Dec 17 20:32:42 2004
@@ -1,6 +1,7 @@
 from compiler import parse, ast, pycodegen
 from py import magic 
 from py.__impl__.magic import dyncode
+from py.code import RunnerFrame
 import __builtin__, sys
 
 passthroughex = (KeyboardInterrupt, SystemExit, MemoryError) 
@@ -12,29 +13,6 @@
         #import traceback
         #traceback.print_exc()
 
-class RunnerFrame:
-    """Wrapper around a Python frame holding f_locals and f_globals
-    in which expressions can be evaluated."""
-    
-    def __init__(self, f_globals, f_locals):
-        self.f_globals = f_globals
-        self.f_locals = f_locals
-
-    def eval(self, code, **vars):
-        self.f_locals.update(vars)
-        return eval(code, self.f_globals, self.f_locals)
-
-    def exec_(self, code, **vars):
-        self.f_locals.update(vars)
-        exec code in self.f_globals, self.f_locals
-
-    def repr(self, object):
-        return repr(object)
-
-    def is_true(self, object):
-        return object
-
-
 class Interpretable(magic.View):
     """A parse tree node with a few extra methods."""
     explanation = None
@@ -390,7 +368,7 @@
     if frame is None:
         import sys
         frame = sys._getframe(1)
-        frame = RunnerFrame(frame.f_globals, frame.f_locals)
+        frame = RunnerFrame(frame)
     expr = parse(s, 'eval')
     assert isinstance(expr, ast.Expression)
     node = Interpretable(expr.node)
@@ -425,7 +403,7 @@
 def interpret(source, frame):
     module = Interpretable(parse(source, 'exec').node)
     #print "got module", module 
-    frame = RunnerFrame(frame.f_globals, frame.f_locals)
+    frame = RunnerFrame(frame)
     try:
         return module.run(frame)  # None if no exception generated
     except Failure, e:
@@ -443,13 +421,13 @@
 
 def getmsg((typ, val, tb)):
     #frame, line = gettbline(tb)
-    #frame = RunnerFrame(frame.f_globals, frame.f_locals)
+    #frame = RunnerFrame(frame)
     #return interpret(line, frame)
 
     tb = magic.dyncode.listtb(tb)[-1] 
     source = dyncode.getparseablestartingblock(tb)
     frame = tb.tb_frame 
-    frame = RunnerFrame(frame.f_globals, frame.f_locals) 
+    frame = RunnerFrame(frame)
     x = interpret(source, frame)
     if not isinstance(x, str):
         raise TypeError, "interpret returned non-string %r" % (x,)
@@ -466,7 +444,7 @@
     if frame is None:
         import sys
         frame = sys._getframe(1)
-        frame = RunnerFrame(frame.f_globals, frame.f_locals)
+        frame = RunnerFrame(frame)
     module = Interpretable(parse(s, 'exec').node)
     try:
         module.run(frame)



More information about the pytest-commit mailing list