[pypy-svn] r45516 - in pypy/dist/pypy: rlib rpython/module rpython/module/test

fijal at codespeak.net fijal at codespeak.net
Mon Aug 6 15:55:44 CEST 2007


Author: fijal
Date: Mon Aug  6 15:55:43 2007
New Revision: 45516

Modified:
   pypy/dist/pypy/rlib/ros.py
   pypy/dist/pypy/rpython/module/ll_os.py
   pypy/dist/pypy/rpython/module/test/test_ll_os.py
   pypy/dist/pypy/rpython/module/test/test_posix.py
Log:
os.tmpfile, RPython level implementation


Modified: pypy/dist/pypy/rlib/ros.py
==============================================================================
--- pypy/dist/pypy/rlib/ros.py	(original)
+++ pypy/dist/pypy/rlib/ros.py	Mon Aug  6 15:55:43 2007
@@ -3,6 +3,7 @@
 """
 
 import os
+from pypy.rlib.streamio import fdopen_as_stream
 
 def putenv(name_eq_value):
     # we fake it with the real one
@@ -62,3 +63,18 @@
 
 def utime_tuple(path, tp):
     os.utime(path, tp)
+
+# ARGH! strange hack to allow os.tmpfile not to be deleted
+# (it should guess it based on getting file desc)
+# this is only when run on top of cpython
+# this will eventually lead to two closes (when this list is deleted), but
+# well.., unsure we can do anything with that.
+KEEP_ME_ALIVE_PLEASE = []
+
+def _tmpfile():
+    tmpfile = os.tmpfile()
+    KEEP_ME_ALIVE_PLEASE.append(tmpfile)
+    return tmpfile.fileno()
+
+def tmpfile():
+    return fdopen_as_stream(_tmpfile(), "w+b", True)

Modified: pypy/dist/pypy/rpython/module/ll_os.py
==============================================================================
--- pypy/dist/pypy/rpython/module/ll_os.py	(original)
+++ pypy/dist/pypy/rpython/module/ll_os.py	Mon Aug  6 15:55:43 2007
@@ -12,7 +12,8 @@
 from pypy.rlib.rarithmetic import r_longlong
 from pypy.tool.staticmethods import ClassMethods
 import stat
-from pypy.rpython.extfunc import BaseLazyRegistering, registering
+from pypy.rpython.extfunc import BaseLazyRegistering, registering,\
+     lazy_register, _register_external
 from pypy.annotation.model import SomeString, SomeInteger
 from pypy.annotation.model import s_ImpossibleValue, s_None, s_Bool
 from pypy.annotation.listdef import s_list_of_strings
@@ -472,6 +473,28 @@
             self.register(os.ttyname, [int], str, "ll_os.ttyname",
                           llimpl=ttyname_lltypeimpl)
 
+def register_tmpfile():
+    from pypy.rpython.lltypesystem import rffi
+    includes = ['stdio.h']
+
+    FILEP = rffi.COpaque('FILE', includes=includes)
+
+    c_tmpfile = rffi.llexternal('tmpfile', [], FILEP, includes=includes)
+    c_fileno = rffi.llexternal('fileno', [FILEP], rffi.INT, includes=includes)
+
+    def _tmpfile_llimpl():
+        fileobj = c_tmpfile()
+        if not fileobj:
+            raise OSError(rffi.get_errno(), "tmpfile failed")
+        ret = c_fileno(fileobj)
+        if ret == -1:
+            raise OSError(rffi.get_errno(), "fileno failed")
+        return ret
+
+    _register_external(ros._tmpfile, [], int, export_name='ros._tmpfile',
+                       llimpl=_tmpfile_llimpl)
+lazy_register(ros._tmpfile, register_tmpfile)
+
 class BaseOS:
     __metaclass__ = ClassMethods
 

Modified: pypy/dist/pypy/rpython/module/test/test_ll_os.py
==============================================================================
--- pypy/dist/pypy/rpython/module/test/test_ll_os.py	(original)
+++ pypy/dist/pypy/rpython/module/test/test_ll_os.py	Mon Aug  6 15:55:43 2007
@@ -114,7 +114,7 @@
 
         fn = compile(fun, [int])
         for value in [0, 1, 127, 128, 255]:
-            assert fn(value) == fun(value)
+            assert fn(value) == fun(value)        
 
 class ExpectTestOs:
     def setup_class(cls):

Modified: pypy/dist/pypy/rpython/module/test/test_posix.py
==============================================================================
--- pypy/dist/pypy/rpython/module/test/test_posix.py	(original)
+++ pypy/dist/pypy/rpython/module/test/test_posix.py	Mon Aug  6 15:55:43 2007
@@ -104,6 +104,46 @@
             return os.getuid()
         assert self.interpret(f, []) == f()
 
+    def test_tmpfile(self):
+        py.test.skip("XXX get_errno() does not work with ll2ctypes")
+        from pypy.rlib import ros
+        def f():
+            f = ros.tmpfile()
+            f.write('xxx')
+            f.flush()
+            return f.try_to_find_file_descriptor()
+
+        def f2(num):
+            os.close(num)
+
+        fd = self.interpret(f, [])
+        realfile = os.fdopen(fd)
+        realfile.seek(0)
+        assert realfile.read() == 'xxx'
+        self.interpret(f2, [fd])
+
+def test_tmpfile_translate():
+    from pypy.rlib import ros
+    def f():
+        f = ros.tmpfile()
+        f.write('xxx')
+        f.flush()
+        return f.try_to_find_file_descriptor()
+    
+    def f2(num):
+        os.close(num)
+
+    from pypy.translator.c.test.test_genc import compile
+
+    fn1 = compile(f, [])
+    fn2 = compile(f2, [int])
+
+    fd = fn1()
+    realfile = os.fdopen(fd)
+    realfile.seek(0)
+    assert realfile.read() == 'xxx'
+    fn2(fd)
+
 if not hasattr(os, 'ftruncate'):
     del BaseTestPosix.test_ftruncate
 
@@ -118,4 +158,3 @@
 
 class TestOOtype(BaseTestPosix, OORtypeMixin):
     pass
-



More information about the Pypy-commit mailing list