[pypy-svn] r15133 - in pypy/dist/pypy: . lib module/__builtin__ module/sys module/sys/test objspace/std objspace/std/test tool translator/goal

hpk at codespeak.net hpk at codespeak.net
Tue Jul 26 18:56:40 CEST 2005


Author: hpk
Date: Tue Jul 26 18:56:37 2005
New Revision: 15133

Added:
   pypy/dist/pypy/module/__builtin__/app_file_stub.py
   pypy/dist/pypy/module/__builtin__/state.py
Removed:
   pypy/dist/pypy/objspace/std/test/test_nonfakefile.py
   pypy/dist/pypy/translator/goal/targetpypy.py
Modified:
   pypy/dist/pypy/conftest.py
   pypy/dist/pypy/lib/_file.py
   pypy/dist/pypy/module/__builtin__/__init__.py
   pypy/dist/pypy/module/sys/__init__.py
   pypy/dist/pypy/module/sys/state.py
   pypy/dist/pypy/module/sys/test/test_sysmodule.py
   pypy/dist/pypy/objspace/std/fake.py
   pypy/dist/pypy/objspace/std/objspace.py
   pypy/dist/pypy/tool/option.py
   pypy/dist/pypy/translator/goal/targetpypymain.py
Log:
(arfigo, hufpk)

- completely got rid of "faking <type 'file'>" when we start
  py.py --file.

- lib-python/conftest.py is a bit broken now.  More clean-up
  is needed regarding the way the options are applied: the
  object spaces should do their initialization completely
  based on the 'options' they get instead of being patched
  from the outside.

- another problem is that applevel tests print output 
  is not captured anymore (conftest.py needs enhancements)

- removed targetpypy.py because it isn't used anymore 



Modified: pypy/dist/pypy/conftest.py
==============================================================================
--- pypy/dist/pypy/conftest.py	(original)
+++ pypy/dist/pypy/conftest.py	Tue Jul 26 18:56:37 2005
@@ -42,10 +42,13 @@
     try:
         return _spacecache[name]
     except KeyError:
+        import pypy.tool.option
+        spaceoptions = pypy.tool.option.Options()
+        spaceoptions.uselibfile = option.uselibfile
         #py.magic.invoke(compile=True)
         module = __import__("pypy.objspace.%s" % name, None, None, ["Space"])
         try: 
-            space = module.Space()
+            space = module.Space(spaceoptions)
         except KeyboardInterrupt: 
             raise 
         except OperationError, e: 
@@ -63,11 +66,6 @@
         _spacecache[name] = space
         if name == 'std' and option.oldstyle: 
             space.enable_old_style_classes_as_default_metaclass()
-        if option.uselibfile:
-            space.appexec([], '''():
-                from _file import file
-                __builtins__.file = __builtins__.open = file
-            ''')
         if name != 'flow': # not sensible for flow objspace case
             space.setitem(space.builtin.w_dict, space.wrap('AssertionError'), 
                           appsupport.build_pytest_assertion(space))

Modified: pypy/dist/pypy/lib/_file.py
==============================================================================
--- pypy/dist/pypy/lib/_file.py	(original)
+++ pypy/dist/pypy/lib/_file.py	Tue Jul 26 18:56:37 2005
@@ -85,13 +85,14 @@
         
     def fdopen(cls, fd, mode='r', buffering=None):
         f = cls.__new__(cls)
-
-        f.fd = fd
-        f._name = "<fdopen>"
-        f._inithelper(mode, buffering)
+        f._fdopen(fd, mode, buffering, '<fdopen>')
         return f
-
     fdopen = classmethod(fdopen)
+
+    def _fdopen(self, fd, mode, buffering, name): 
+        self.fd = fd
+        self._name = name 
+        self._inithelper(mode, buffering)
         
     def _inithelper(self, mode, buffering):
         self._mode = mode

Modified: pypy/dist/pypy/module/__builtin__/__init__.py
==============================================================================
--- pypy/dist/pypy/module/__builtin__/__init__.py	(original)
+++ pypy/dist/pypy/module/__builtin__/__init__.py	Tue Jul 26 18:56:37 2005
@@ -52,6 +52,8 @@
 
         'set'           : 'app_sets.set',
         'frozenset'     : 'app_sets.frozenset',
+
+        '__filestub'    : 'app_file_stub.file',
     }
 
     interpleveldefs = {
@@ -64,10 +66,11 @@
         '__debug__'     : '(space.w_True)',      # XXX
         'type'          : '(space.w_type)',
         'object'        : '(space.w_object)',
-        'file'          : '(space.wrap(file))',
-        'open'          : '(space.wrap(file))',
         'unicode'       : '(space.w_unicode)',
 
+        'file'          : 'state.get(space).w_file', 
+        'open'          : 'state.get(space).w_file', 
+
         # old-style classes dummy support
         '_classobj'     : 'space.w_classobj',
         '_instance'     : 'space.w_instance',

Added: pypy/dist/pypy/module/__builtin__/app_file_stub.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/__builtin__/app_file_stub.py	Tue Jul 26 18:56:37 2005
@@ -0,0 +1,20 @@
+# NOT_RPYTHON
+
+class file(object): 
+    """file(name[, mode[, buffering]]) -> file object
+
+Open a file.  The mode can be 'r', 'w' or 'a' for reading (default),
+writing or appending.  The file will be created if it doesn't exist
+when opened for writing or appending; it will be truncated when
+opened for writing.  Add a 'b' to the mode for binary files.
+Add a '+' to the mode to allow simultaneous reading and writing.
+If the buffering argument is given, 0 means unbuffered, 1 means line
+buffered, and larger numbers specify the buffer size.
+Add a 'U' to mode to open the file for input with universal newline
+support.  Any line ending in the input file will be seen as a '\n'
+in Python.  Also, a file so opened gains the attribute 'newlines';
+the value for this attribute is one of None (no newline read yet),
+'\r', '\n', '\r\n' or a tuple containing all the newline types seen.
+
+Note:  open() is an alias for file().
+"""

Added: pypy/dist/pypy/module/__builtin__/state.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/__builtin__/state.py	Tue Jul 26 18:56:37 2005
@@ -0,0 +1,10 @@
+
+class State: 
+    def __init__(self, space): 
+        if space.options.uselibfile:
+            self.w_file = space.builtin.get('__filestub')
+        else: 
+            self.w_file = space.wrap(file) 
+        
+def get(space): 
+    return space.fromcache(State)

Modified: pypy/dist/pypy/module/sys/__init__.py
==============================================================================
--- pypy/dist/pypy/module/sys/__init__.py	(original)
+++ pypy/dist/pypy/module/sys/__init__.py	Tue Jul 26 18:56:37 2005
@@ -20,12 +20,12 @@
         'prefix'                : 'space.wrap(sys.prefix)', 
         'maxunicode'            : 'space.wrap(sys.maxunicode)',
         'maxint'                : 'space.wrap(sys.maxint)',
-        'stdin'                 : 'space.wrap(sys.stdin)',
-        '__stdin__'             : 'space.wrap(sys.stdin)',
-        'stdout'                : 'space.wrap(sys.stdout)',
-        '__stdout__'            : 'space.wrap(sys.stdout)',
-        'stderr'                : 'space.wrap(sys.stderr)', 
-        '__stderr__'            : 'space.wrap(sys.stderr)',
+        'stdin'                 : 'state.getio(space).w_stdin', 
+        '__stdin__'             : 'state.getio(space).w_stdin', 
+        'stdout'                : 'state.getio(space).w_stdout', 
+        '__stdout__'            : 'state.getio(space).w_stdout', 
+        'stderr'                : 'state.getio(space).w_stderr', 
+        '__stderr__'            : 'state.getio(space).w_stderr', 
         'pypy_objspaceclass'    : 'space.wrap(repr(space))',
 
         'path'                  : 'state.get(space).w_path', 

Modified: pypy/dist/pypy/module/sys/state.py
==============================================================================
--- pypy/dist/pypy/module/sys/state.py	(original)
+++ pypy/dist/pypy/module/sys/state.py	Tue Jul 26 18:56:37 2005
@@ -76,6 +76,21 @@
 def get(space): 
     return space.fromcache(State)
 
+class IOState: 
+    def __init__(self, space): 
+        self.space = space
+        if space.options.uselibfile: 
+            self.w_stdout = space.call_function(space.builtin.get('file'))
+            self.w_stderr = space.call_function(space.builtin.get('file'))
+            self.w_stdin = space.call_function(space.builtin.get('file'))
+        else: 
+            self.w_stdout = space.wrap(sys.__stdout__) 
+            self.w_stderr = space.wrap(sys.__stderr__) 
+            self.w_stdin = space.wrap(sys.__stdin__) 
+
+def getio(space): 
+    return space.fromcache(IOState) 
+
 def _pypy_getudir(space):
     """NOT_RPYTHON"""
     from pypy.tool.udir import udir

Modified: pypy/dist/pypy/module/sys/test/test_sysmodule.py
==============================================================================
--- pypy/dist/pypy/module/sys/test/test_sysmodule.py	(original)
+++ pypy/dist/pypy/module/sys/test/test_sysmodule.py	Tue Jul 26 18:56:37 2005
@@ -96,6 +96,16 @@
         else:
             raise AssertionError, "ZeroDivisionError not caught"
 
+def app_test_io(): 
+    #space.appexec([], """(): 
+        import sys
+        assert isinstance(sys.stdout, file)
+        assert isinstance(sys.__stdout__, file)
+        assert isinstance(sys.stderr, file)
+        assert isinstance(sys.__stderr__, file)
+        assert isinstance(sys.stdin, file)
+        assert isinstance(sys.__stdin__, file)
+    #""")
 
 class AppTestSysModulePortedFromCPython:
 

Modified: pypy/dist/pypy/objspace/std/fake.py
==============================================================================
--- pypy/dist/pypy/objspace/std/fake.py	(original)
+++ pypy/dist/pypy/objspace/std/fake.py	Tue Jul 26 18:56:37 2005
@@ -12,9 +12,12 @@
 
 
 def fake_object(space, x):
+    if isinstance(x, file): 
+        debug_print("fake-wrapping interp file %s" % x)
     if isinstance(x, type):
         ft = fake_type(x)
         return space.gettypeobject(ft.typedef)
+    #debug_print("faking obj %s" % x)
     ft = fake_type(type(x))
     return ft(space, x)
 fake_object._annspecialcase_ = "override:fake_object"
@@ -56,6 +59,7 @@
 
 def really_build_fake_type(cpy_type):
     "NOT_RPYTHON (not remotely so!)."
+    #assert not issubclass(cpy_type, file), cpy_type
     debug_print('faking %r'%(cpy_type,))
     kw = {}
     

Modified: pypy/dist/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/std/objspace.py	(original)
+++ pypy/dist/pypy/objspace/std/objspace.py	Tue Jul 26 18:56:37 2005
@@ -1,6 +1,6 @@
 from pypy.objspace.std.register_all import register_all
 from pypy.interpreter.baseobjspace import ObjSpace, BaseWrappable
-from pypy.interpreter.error import OperationError
+from pypy.interpreter.error import OperationError, debug_print
 from pypy.interpreter.typedef import get_unique_interplevel_subclass
 from pypy.interpreter.typedef import instantiate
 from pypy.interpreter.gateway import PyPyCacheDir
@@ -92,6 +92,9 @@
                 dict.fromkeys = classmethod(fromkeys)
         """) 
 
+        if self.options.uselibfile:
+            self.setuselibfile() 
+
     def enable_old_style_classes_as_default_metaclass(self):
         self.setitem(self.builtin.w_dict, self.wrap('__metaclass__'), self.w_classobj)
 
@@ -124,26 +127,34 @@
         self.w_classobj = w_classobj
         self.w_instance = w_instance
 
-    def unfakefile(self): 
+    def setuselibfile(self): 
         """ NOT_RPYTHON use our application level file implementation
             including re-wrapping sys.stdout/err/in
         """ 
+        assert self.options.uselibfile 
+        space = self
+        # nice print helper if the below does not work 
+        # (we dont have prints working at applevel before
+        # setuselibfile is complete)
+        #from pypy.interpreter import gateway 
+        #def printit(space, w_msg): 
+        #    s = space.str_w(w_msg) 
+        #    print "$", s, 
+        #w_p = space.wrap(gateway.interp2app(printit))
+        #self.appexec([w_p], '''(p):
         self.appexec([], '''():
-            from _file import file
-            __builtins__.file = __builtins__.open = file
-            import sys
-            sys.stdout = file.fdopen(sys.stdout.fileno(),
-                                     sys.stdout.mode,
-                                     buffering=1)
-            sys.stdin = file.fdopen(sys.stdin.fileno(),
-                                    sys.stdin.mode,
-                                    buffering=1)                                         
-            sys.stderr = file.fdopen(sys.stderr.fileno(),
-                                     sys.stderr.mode,
-                                     buffering=0)
-            sys.__stdout__ = sys.stdout
-            sys.__stderr__ = sys.stderr
-            sys.__stdin__ = sys.stdin
+            import sys 
+            sys.stdin
+            sys.stdout
+            sys.stderr    # force unlazifying from mixedmodule 
+            from _file import file as libfile 
+            for name, value in libfile.__dict__.items(): 
+                if (name != '__dict__' and name != '__doc__'
+                    and name != '__module__'):
+                    setattr(file, name, value) 
+            sys.stdin._fdopen(0, "r", 1, '<stdin>') 
+            sys.stdout._fdopen(1, "w", 1, '<stdout>') 
+            sys.stderr._fdopen(2, "w", 0, '<stderr>') 
         ''')
 
     def setup_exceptions(self):

Deleted: /pypy/dist/pypy/objspace/std/test/test_nonfakefile.py
==============================================================================
--- /pypy/dist/pypy/objspace/std/test/test_nonfakefile.py	Tue Jul 26 18:56:37 2005
+++ (empty file)
@@ -1,24 +0,0 @@
-
-import autopath
-from pypy.tool.udir import udir
-import py
-import sys
-
-pypypath = str(py.path.local(autopath.pypydir).join('bin', 'py.py'))
-
-def test_nonfake_stdfile():
-    """  """
-    uselibfile = udir.join('uselibfile.py')
-    uselibfile.write("""if 1:
-    import sys
-    assert not _isfake(sys.stdin)
-    assert not _isfake(sys.stdout)
-    assert not _isfake(sys.stderr)
-    assert not _isfake(sys.__stdin__)
-    assert not _isfake(sys.__stdout__)
-    assert not _isfake(sys.__stderr__)
-    print "ok"
-    """)
-    output = py.process.cmdexec( '''"%s" "%s" --file "%s"''' %
-                                (sys.executable, pypypath, uselibfile) )
-    assert output.splitlines()[-1] == "ok"

Modified: pypy/dist/pypy/tool/option.py
==============================================================================
--- pypy/dist/pypy/tool/option.py	(original)
+++ pypy/dist/pypy/tool/option.py	Tue Jul 26 18:56:37 2005
@@ -89,7 +89,4 @@
         space = Space( Options() )
         if name == 'std' and Options.oldstyle:
             space.enable_old_style_classes_as_default_metaclass()
-        if Options.uselibfile:
-            space.unfakefile()
-            
         return _spacecache.setdefault(name, space)

Deleted: /pypy/dist/pypy/translator/goal/targetpypy.py
==============================================================================
--- /pypy/dist/pypy/translator/goal/targetpypy.py	Tue Jul 26 18:56:37 2005
+++ (empty file)
@@ -1,32 +0,0 @@
-import buildcache2
-from pypy.objspace.std.objspace import StdObjSpace, W_Object
-from pypy.objspace.std.intobject import W_IntObject
-
-# __________  Entry point  __________
-
-def entry_point():
-    w_a = W_IntObject(space, -6)
-    w_b = W_IntObject(space, -7)
-    return space.mul(w_a, w_b)
-
-# _____ Define and setup target ___
-
-def target():
-    global space
-    # disable translation of the whole of classobjinterp.py
-    StdObjSpace.setup_old_style_classes = lambda self: None
-    space = StdObjSpace()
-    # call cache filling code
-    buildcache2.buildcache(space)    
-    # further call the entry_point once to trigger building remaining
-    # caches (as far as analyzing the entry_point is concerned)
-    entry_point()
-
-    return entry_point, []
-
-# _____ Run translated _____
-def run(c_entry_point):
-    w_result = c_entry_point()
-    print w_result
-    print w_result.intval
-    assert w_result.intval == 42

Modified: pypy/dist/pypy/translator/goal/targetpypymain.py
==============================================================================
--- pypy/dist/pypy/translator/goal/targetpypymain.py	(original)
+++ pypy/dist/pypy/translator/goal/targetpypymain.py	Tue Jul 26 18:56:37 2005
@@ -38,8 +38,10 @@
     # XXX why can't I enable this? crashes the annotator!
     gateway.ApplevelClass.use_geninterp = False
 
-    space = StdObjSpace()
-    space.unfakefile()
+    from pypy.tool import Options
+    options = Options()
+    options.uselibfile = True
+    space = StdObjSpace(options)
 
     # manually imports app_main.py
     filename = os.path.join(this_dir, 'app_main.py')



More information about the Pypy-commit mailing list