[pypy-svn] r46339 - in pypy/dist/pypy: rpython/module rpython/module/test translator/c/test

arigo at codespeak.net arigo at codespeak.net
Wed Sep 5 13:41:47 CEST 2007


Author: arigo
Date: Wed Sep  5 13:41:46 2007
New Revision: 46339

Modified:
   pypy/dist/pypy/rpython/module/ll_os.py
   pypy/dist/pypy/rpython/module/test/test_posix.py
   pypy/dist/pypy/translator/c/test/test_extfunc.py
Log:
Reimplement os.uname().


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	Wed Sep  5 13:41:46 2007
@@ -302,46 +302,40 @@
         return extdef([], int, export_name="ll_os.ll_os_setsid",
                       llimpl=setsid_llimpl)
 
-    if False:
-        @registering_if(os, 'uname')
-        def register_os_uname(self):
-            lgt = platform.intdefined('_UTSNAME_LENGTH',
-                                      includes=['sys/utsname.h'])
-            UTCHARP = lltype.FixedSizeArray(lltype.Char, lgt)
-            fields = [('sysname', UTCHARP),
-                      ('nodename', UTCHARP),
-                      ('release', UTCHARP),
-                      ('version', UTCHARP),
-                      ('machine', UTCHARP),
-                      ('domainname', UTCHARP)]
-            UTSNAMEP = rffi.CStructPtr('utsname', *fields)
-    
-            os_uname = self.llexternal('uname', [UTSNAMEP], rffi.INT,
-                                       includes=['sys/utsname.h'])
-
-            def utcharp2str(cp):
-                l = []
-                i = 0
-                while cp[i] != '\x00' and i < lgt:
-                    l.append(cp[i])
-                    i += 1
-                return "".join(l)
-
-            def uname_llimpl():
-                l_utsbuf = lltype.malloc(UTSNAMEP.TO, flavor='raw')
-                result = os_uname(l_utsbuf)
-                if result == -1:
-                    raise OSError(rffi.get_errno(), "os_uname failed")
-                fields = [l_utsbuf.c_sysname, l_utsbuf.c_nodename,
-                          l_utsbuf.c_release, l_utsbuf.c_version,
-                          l_utsbuf.c_machine]
-                l = [utcharp2str(i) for i in fields]
-                retval = (l[0], l[1], l[2], l[3], l[4])
-                lltype.free(l_utsbuf, flavor='raw')
-                return retval
+    @registering_if(os, 'uname')
+    def register_os_uname(self):
+        CHARARRAY = lltype.FixedSizeArray(lltype.Char, 1)
+        class CConfig:
+            _includes_ = ['sys/utsname.h']
+            UTSNAME = platform.Struct('struct utsname', [
+                ('sysname',  CHARARRAY),
+                ('nodename', CHARARRAY),
+                ('release',  CHARARRAY),
+                ('version',  CHARARRAY),
+                ('machine',  CHARARRAY)])
+        config = platform.configure(CConfig)
+        UTSNAMEP = lltype.Ptr(config['UTSNAME'])
+
+        os_uname = self.llexternal('uname', [UTSNAMEP], rffi.INT,
+                                   includes=CConfig._includes_)
+
+        def uname_llimpl():
+            l_utsbuf = lltype.malloc(UTSNAMEP.TO, flavor='raw')
+            result = os_uname(l_utsbuf)
+            if result == -1:
+                raise OSError(rffi.get_errno(), "os_uname failed")
+            retval = (
+                rffi.charp2str(rffi.cast(rffi.CCHARP, l_utsbuf.c_sysname)),
+                rffi.charp2str(rffi.cast(rffi.CCHARP, l_utsbuf.c_nodename)),
+                rffi.charp2str(rffi.cast(rffi.CCHARP, l_utsbuf.c_release)),
+                rffi.charp2str(rffi.cast(rffi.CCHARP, l_utsbuf.c_version)),
+                rffi.charp2str(rffi.cast(rffi.CCHARP, l_utsbuf.c_machine)),
+                )
+            lltype.free(l_utsbuf, flavor='raw')
+            return retval
 
-            return extdef([], (str, str, str, str, str),
-                          "ll_os.ll_uname", llimpl=uname_llimpl)
+        return extdef([], (str, str, str, str, str),
+                      "ll_os.ll_uname", llimpl=uname_llimpl)
 
     @registering_if(os, 'getuid')
     def register_os_getuid(self):

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	Wed Sep  5 13:41:46 2007
@@ -131,13 +131,7 @@
                 assert res == fun(value)
 
 class TestLLtype(BaseTestPosix, LLRtypeMixin):
-    if False and hasattr(os, 'uname'):
-        def test_os_uname(self):
-            for num in range(5):
-                def fun():
-                    return os.uname()[num]
-                res = self.interpret(fun, [])
-                assert self.ll_to_string(res) == os.uname()[num]
+    pass
 
 class TestOOtype(BaseTestPosix, OORtypeMixin):
     pass

Modified: pypy/dist/pypy/translator/c/test/test_extfunc.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_extfunc.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_extfunc.py	Wed Sep  5 13:41:46 2007
@@ -918,3 +918,14 @@
     assert os.stat(path).st_atime > t0
     func(0)
     assert int(os.stat(path).st_atime) == int(t0)
+
+if hasattr(os, 'uname'):
+    def test_os_uname():
+        def does_stuff(num):
+            tup = os.uname()
+            lst = [tup[0], tup[1], tup[2], tup[3], tup[4]]
+            return lst[num]
+        func = compile(does_stuff, [int])
+        for i in range(5):
+            res = func(i)
+            assert res == os.uname()[i]



More information about the Pypy-commit mailing list