[pypy-svn] r11882 - pypy/dist/pypy/interpreter

arigo at codespeak.net arigo at codespeak.net
Tue May 3 19:36:35 CEST 2005


Author: arigo
Date: Tue May  3 19:36:34 2005
New Revision: 11882

Modified:
   pypy/dist/pypy/interpreter/interactive.py
   pypy/dist/pypy/interpreter/main.py
   pypy/dist/pypy/interpreter/py.py
Log:
Yet some more clean-ups, to allow sys.exitfunc to be introduced.



Modified: pypy/dist/pypy/interpreter/interactive.py
==============================================================================
--- pypy/dist/pypy/interpreter/interactive.py	(original)
+++ pypy/dist/pypy/interpreter/interactive.py	Tue May  3 19:36:34 2005
@@ -159,11 +159,13 @@
 
     def runcode(self, code):
         # 'code' is a CPython code object
-        self.settrace()
-        main.run_toplevel_program(self.space, code,
-                                  w_globals=self.w_globals,
-                                  verbose=self.verbose)
-        self.checktrace()
+        from pypy.interpreter.pycode import PyCode
+        pycode = PyCode(self.space)._from_code(code)
+        def doit():
+            self.settrace()
+            pycode.exec_code(self.space, self.w_globals, self.w_globals)
+            self.checktrace()
+        main.run_toplevel(self.space, doit, verbose=self.verbose)
 
     def runsource(self, source, ignored_filename="<input>", symbol="single"):
         hacked_filename = '<inline>' + source

Modified: pypy/dist/pypy/interpreter/main.py
==============================================================================
--- pypy/dist/pypy/interpreter/main.py	(original)
+++ pypy/dist/pypy/interpreter/main.py	Tue May  3 19:36:34 2005
@@ -17,13 +17,10 @@
     return mainmodule
 
 def compilecode(space, source, filename, cmd='exec'):
-    if isinstance(source, types.CodeType):
-        pycode = PyCode(space)._from_code(source)
-    else:
-        w = space.wrap
-        w_code = space.builtin.call('compile', 
-                 w(source), w(filename), w(cmd), w(0), w(0))
-        pycode = space.interpclass_w(w_code)
+    w = space.wrap
+    w_code = space.builtin.call('compile', 
+             w(source), w(filename), w(cmd), w(0), w(0))
+    pycode = space.interpclass_w(w_code)
     assert isinstance(pycode, eval.Code)
     return pycode
 
@@ -72,24 +69,16 @@
 
 # ____________________________________________________________
 
-def run_toplevel_program(space, source, filename='<program>',
-                         w_globals=None, verbose=False):
-    """Run the given source code in the given globals (or __main__ by default).
+def run_toplevel(space, f, verbose=False):
+    """Calls f() and handle all OperationErrors.
     Intended use is to run the main program or one interactive statement.
-    It handles details like forwarding exceptions to sys.excepthook(),
-    catching SystemExit, printing a newline after sys.stdout if needed, etc.
+    run_protected() handles details like forwarding exceptions to
+    sys.excepthook(), catching SystemExit, printing a newline after
+    sys.stdout if needed, etc.
     """
     try:
-        # build a valid w_globals
-        if w_globals is None:
-            w_globals = ensure__main__(space).w_dict
-        w1 = space.wrap('__builtins__')
-        if not space.is_true(space.contains(w_globals, w1)):
-            space.setitem(w_globals, w1, space.builtin)
-
-        # compile and execute the code
-        pycode = compilecode(space, source, filename)
-        pycode.exec_code(space, w_globals, w_globals)
+        # run it
+        f()
 
         # we arrive here if no exception is raised.  stdout cosmetics...
         try:
@@ -145,7 +134,7 @@
                 w_original = space.sys.getdictvalue(space, '__excepthook__')
                 if w_original is None or not space.is_w(w_hook, w_original):
                     space.call_function(w_hook, w_type, w_value, w_traceback)
-                    return 1   # done
+                    return False   # done
 
         except OperationError, err2:
             # XXX should we go through sys.get('stderr') ?
@@ -159,6 +148,6 @@
             operationerr.print_detailed_traceback(space)
         else:
             operationerr.print_application_traceback(space)
-        return 1
+        return False
 
-    return 0   # success
+    return True   # success

Modified: pypy/dist/pypy/interpreter/py.py
==============================================================================
--- pypy/dist/pypy/interpreter/py.py	(original)
+++ pypy/dist/pypy/interpreter/py.py	Tue May  3 19:36:34 2005
@@ -73,33 +73,42 @@
     for arg in args:
         space.call_method(space.sys.get('argv'), 'append', space.wrap(arg))
 
-    # compile the program given as command-line argument
+    # load the source of the program given as command-line argument
     if Options.command:
-        filename = '<string>'
-        fullsource = Options.command[0]
+        def doit():
+            main.run_string(Options.command[0], space=space)
     elif args:
-        filename = args[0]
-        fullsource = open(filename).read()
+        def doit():
+            main.run_file(args[0], space=space)
     else:
-        filename = fullsource = None
+        def doit():
+            pass
         space.call_method(space.sys.get('argv'), 'append', space.wrap(''))
         go_interactive = 1
         banner = None
 
-    # run it
-    if fullsource is not None:
-        exit_status = main.run_toplevel_program(space, fullsource, filename,
-                                                verbose=Options.verbose)
-
-    # start the interactive console
-    if go_interactive:
-        con = interactive.PyPyConsole(space, verbose=Options.verbose,
-                                             completer=Options.completer)
-        if banner == '':
-            banner = '%s / %s'%(con.__class__.__name__,
-                                repr(space))
-        con.interact(banner)
-        exit_status = 0
+    try:
+        # compile and run it
+        if not main.run_toplevel(space, doit, verbose=Options.verbose):
+            exit_status = 1
+
+        # start the interactive console
+        if go_interactive:
+            con = interactive.PyPyConsole(space, verbose=Options.verbose,
+                                                 completer=Options.completer)
+            if banner == '':
+                banner = '%s / %s'%(con.__class__.__name__,
+                                    repr(space))
+            con.interact(banner)
+            exit_status = 0
+    finally:
+        # call the sys.exitfunc()
+        w_exitfunc = space.sys.getdictvalue(space, 'exitfunc')
+        if w_exitfunc is not None:
+            def doit():
+                space.call_function(w_exitfunc)
+            main.run_toplevel(space, doit, verbose=Options.verbose)
+
     return exit_status
 
 ##def main_(argv=None):



More information about the Pypy-commit mailing list