[pypy-svn] r40280 - in pypy/dist/pypy/interpreter: . test

arigo at codespeak.net arigo at codespeak.net
Mon Mar 12 10:49:00 CET 2007


Author: arigo
Date: Mon Mar 12 10:48:57 2007
New Revision: 40280

Modified:
   pypy/dist/pypy/interpreter/main.py
   pypy/dist/pypy/interpreter/test/test_py.py
Log:
Starting from CPython 2.3, the __main__ module also has a __file__ if it
comes from a file.  Fix this for py.py.


Modified: pypy/dist/pypy/interpreter/main.py
==============================================================================
--- pypy/dist/pypy/interpreter/main.py	(original)
+++ pypy/dist/pypy/interpreter/main.py	Mon Mar 12 10:48:57 2007
@@ -36,12 +36,14 @@
 
         w = space.wrap
 
-        pycode = compilecode(space, source, filename, cmd)
+        pycode = compilecode(space, source, filename or '<string>', cmd)
 
         mainmodule = ensure__main__(space)
         w_globals = mainmodule.w_dict
 
         space.setitem(w_globals, w('__builtins__'), space.builtin)
+        if filename is not None:
+            space.setitem(w_globals, w('__file__'), w(filename))
 
         retval = pycode.exec_code(space, w_globals, w_globals)
         if eval:
@@ -53,10 +55,10 @@
         operationerr.record_interpreter_traceback()
         raise
 
-def run_string(source, filename='<string>', space=None):
+def run_string(source, filename=None, space=None):
     _run_eval_string(source, filename, space, False)
 
-def eval_string(source, filename='<string>', space=None):
+def eval_string(source, filename=None, space=None):
     return _run_eval_string(source, filename, space, True)
 
 def run_file(filename, space=None):

Modified: pypy/dist/pypy/interpreter/test/test_py.py
==============================================================================
--- pypy/dist/pypy/interpreter/test/test_py.py	(original)
+++ pypy/dist/pypy/interpreter/test/test_py.py	Mon Mar 12 10:48:57 2007
@@ -15,13 +15,30 @@
 
 def test_prefix():
     """Make sure py.py sys.prefix and exec_prefix are the same as C Python's"""
-    output = py.process.cmdexec( '''"%s" "%s" -c "import sys;print sys.prefix" ''' %
-                                 (sys.executable, pypypath) )
-    assert output.splitlines()[-1] == sys.prefix
-    output = py.process.cmdexec( '''"%s" "%s" -c "import sys;print sys.exec_prefix" ''' %
-                                 (sys.executable, pypypath) )
+    cmd = "import sys;print sys.prefix;print sys.exec_prefix"
+    output = py.process.cmdexec( '''"%s" "%s" -c "%s" ''' %
+                                 (sys.executable, pypypath, cmd) )
+    assert output.splitlines()[-2] == sys.prefix
     assert output.splitlines()[-1] == sys.exec_prefix
 
+def test_special_names():
+    """Test the __name__ and __file__ special global names"""
+    cmd = "print __name__; print '__file__' in globals()"
+    output = py.process.cmdexec( '''"%s" "%s" -c "%s" ''' %
+                                 (sys.executable, pypypath, cmd) )
+    assert output.splitlines()[-2] == '__main__'
+    assert output.splitlines()[-1] == 'False'
+
+    tmpfilepath = str(udir.join("test_py_script_1.py"))
+    tmpfile = file( tmpfilepath, "w" )
+    tmpfile.write("print __name__; print __file__\n")
+    tmpfile.close()
+
+    output = py.process.cmdexec( '''"%s" "%s" "%s" ''' %
+                                 (sys.executable, pypypath, tmpfilepath) )
+    assert output.splitlines()[-2] == '__main__'
+    assert output.splitlines()[-1] == str(tmpfilepath)
+
 def test_argv_command():
     """Some tests on argv"""
     # test 1 : no arguments
@@ -91,16 +108,3 @@
         pass
     assert e," expected failure"
     assert e.err.splitlines()[-1] == 'KeyError: <normalized>'
-
-
-def test_no__file__in_main():
-    tmpfilepath = udir.join("test_py_script.py")
-    tmpfilepath.write(str(py.code.Source("""
-        try:
-            print __file__
-        except NameError:
-            print 'no __file__.'
-    """)))
-    output = py.process.cmdexec( '''"%s" "%s" "%s" ''' %
-                                 (sys.executable, pypypath, tmpfilepath) )
-    assert 'no __file__.\n' in output



More information about the Pypy-commit mailing list