[pypy-svn] r46188 - pypy/branch/pypy-more-rtti-inprogress/rpython/module

fijal at codespeak.net fijal at codespeak.net
Thu Aug 30 12:36:39 CEST 2007


Author: fijal
Date: Thu Aug 30 12:36:38 2007
New Revision: 46188

Modified:
   pypy/branch/pypy-more-rtti-inprogress/rpython/module/ll_os.py
Log:
Use more cool helpers


Modified: pypy/branch/pypy-more-rtti-inprogress/rpython/module/ll_os.py
==============================================================================
--- pypy/branch/pypy-more-rtti-inprogress/rpython/module/ll_os.py	(original)
+++ pypy/branch/pypy-more-rtti-inprogress/rpython/module/ll_os.py	Thu Aug 30 12:36:38 2007
@@ -51,6 +51,8 @@
 
 
 class RegisterOs(BaseLazyRegistering):
+    _stringpolicy_ = 'fullauto'
+    
     def __init__(self):
         self.configure(CConfig)
 
@@ -73,11 +75,9 @@
                                    rffi.INT)
 
         def execv_llimpl(path, args):
-            l_path = rffi.str2charp(path)
             l_args = rffi.liststr2charpp(args)
-            os_execv(l_path, l_args)
+            os_execv(path, l_args)
             rffi.free_charpp(l_args)
-            rffi.free_charp(l_path)
             raise OSError(rffi.get_errno(), "execv failed")
 
         return extdef([str, [str]], s_ImpossibleValue, llimpl=execv_llimpl,
@@ -91,11 +91,9 @@
 
         def spawnv_llimpl(mode, path, args):
             mode = rffi.cast(rffi.INT, mode)
-            l_path = rffi.str2charp(path)
             l_args = rffi.liststr2charpp(args)
             childpid = os_spawnv(mode, l_path, l_args)
             rffi.free_charpp(l_args)
-            rffi.free_charp(l_path)
             if childpid == -1:
                 raise OSError(rffi.get_errno(), "os_spawnv failed")
             return rffi.cast(lltype.Signed, childpid)
@@ -144,7 +142,6 @@
             # NB. this function is specialized; we get one version where
             # tp is known to be None, and one version where it is known
             # to be a tuple of 2 floats.
-            l_path = rffi.str2charp(path)
             if tp is None:
                 l_utimebuf = lltype.nullptr(UTIMEBUFP.TO)
             else:
@@ -155,7 +152,6 @@
             error = rffi.cast(lltype.Signed, os_utime(l_path, l_utimebuf))
             if tp is not None:
                 lltype.free(l_utimebuf, flavor='raw')
-            rffi.free_charp(l_path)
             if error == -1:
                 raise OSError(rffi.get_errno(), "os_utime failed")
         os_utime_llimpl._annspecialcase_ = 'specialize:argtype(1)'
@@ -290,11 +286,7 @@
                                   rffi.INT)
 
         def os_open_llimpl(path, flags, mode):
-            l_path = rffi.str2charp(path)
-            result = rffi.cast(lltype.Signed, os_open(l_path,
-                               rffi.cast(rffi.INT, flags),
-                               rffi.cast(mode_t, mode)))
-            rffi.free_charp(l_path)
+            result = os_open(path, flags, mode)
             if result == -1:
                 raise OSError(rffi.get_errno(), "os_open failed")
             return result
@@ -436,15 +428,11 @@
 
     @registering(os.access)
     def register_os_access(self):
-        os_access = self.llexternal('access',
-                                    [rffi.CCHARP, rffi.INT],
+        os_access = self.llexternal('access', [rffi.CCHARP, rffi.INT],
                                     rffi.INT)
 
         def access_llimpl(path, mode):
-            path = rffi.str2charp(path)
-            mode = rffi.cast(rffi.INT, mode)
-            error = rffi.cast(lltype.Signed, os_access(path, mode))
-            rffi.free_charp(path)
+            error = os_access(path, mode)
             return error == 0
 
         def os_access_oofakeimpl(path, mode):
@@ -458,7 +446,7 @@
     def register_os_getcwd(self):
         os_getcwd = self.llexternal('getcwd',
                                     [rffi.CCHARP, rffi.SIZE_T],
-                                    rffi.CCHARP)
+                                    rffi.CCHARP, stringpolicy='noauto')
 
         def os_getcwd_llimpl():
             bufsize = 256
@@ -503,9 +491,7 @@
             os_closedir = self.llexternal('closedir', [DIRP], rffi.INT)
 
             def os_listdir_llimpl(path):
-                path = rffi.str2charp(path)
                 dirp = os_opendir(path)
-                rffi.free_charp(path)
                 if not dirp:
                     raise OSError(rffi.get_errno(), "os_opendir failed")
                 rffi.set_errno(0)
@@ -556,34 +542,29 @@
     def register_os_readlink(self):
         os_readlink = self.llexternal('readlink',
                                    [rffi.CCHARP, rffi.CCHARP, rffi.SIZE_T],
-                                   rffi.INT)  # XXX SSIZE_T in POSIX.1-2001
+                                   rffi.INT)
+        # XXX SSIZE_T in POSIX.1-2001
 
         def os_readlink_llimpl(path):
-            l_path = rffi.str2charp(path)
-            try:
-                bufsize = 1023
-                while True:
-                    buf = lltype.malloc(rffi.CCHARP.TO, bufsize,
-                                        flavor='raw')
-                    res = os_readlink(l_path, buf,
-                                      rffi.cast(rffi.SIZE_T, bufsize))
-                    if res < 0:
-                        error = rffi.get_errno()    # failed
-                        lltype.free(buf, flavor='raw')
-                        raise OSError(error, "readlink failed")
-                    elif res < bufsize:
-                        break                       # ok
-                    else:
-                        # buf too small, try again with a larger buffer
-                        lltype.free(buf, flavor='raw')
-                        bufsize *= 4
-                # convert the result to a string
-                res = rffi.cast(lltype.Signed, res)
-                l = [buf[i] for i in range(res)]
-                result = ''.join(l)
-                lltype.free(buf, flavor='raw')
-            finally:
-                rffi.free_charp(l_path)
+            bufsize = 1023
+            while True:
+                buf = lltype.malloc(rffi.CCHARP.TO, bufsize,
+                                    flavor='raw')
+                res = os_readlink(path, buf, bufsize)
+                if res < 0:
+                    error = rffi.get_errno()    # failed
+                    lltype.free(buf, flavor='raw')
+                    raise OSError(error, "readlink failed")
+                elif res < bufsize:
+                    break                       # ok
+                else:
+                    # buf too small, try again with a larger buffer
+                    lltype.free(buf, flavor='raw')
+                    bufsize *= 4
+            # convert the result to a string
+            l = [buf[i] for i in range(res)]
+            result = ''.join(l)
+            lltype.free(buf, flavor='raw')
             return result
 
         return extdef([str], str,
@@ -655,9 +636,7 @@
         os_system = self.llexternal('system', [rffi.CCHARP], rffi.INT)
 
         def system_llimpl(command):
-            l_command = rffi.str2charp(command)
-            res = os_system(l_command)
-            rffi.free_charp(l_command)
+            res = os_system(command)
             return res
 
         return extdef([str], int, llimpl=system_llimpl,
@@ -668,9 +647,7 @@
         os_unlink = self.llexternal('unlink', [rffi.CCHARP], rffi.INT)
 
         def unlink_llimpl(pathname):
-            l_pathname = rffi.str2charp(pathname)
-            res = os_unlink(l_pathname)
-            rffi.free_charp(l_pathname)
+            res = os_unlink(pathname)
             if res < 0:
                 raise OSError(rffi.get_errno(), "os_unlink failed")
 
@@ -682,9 +659,7 @@
         os_chdir = self.llexternal('chdir', [rffi.CCHARP], rffi.INT)
 
         def chdir_llimpl(path):
-            l_path = rffi.str2charp(path)
-            res = os_chdir(l_path)
-            rffi.free_charp(l_path)
+            res = os_chdir(path)
             if res < 0:
                 raise OSError(rffi.get_errno(), "os_chdir failed")
 
@@ -702,11 +677,9 @@
         os_mkdir = self.llexternal('mkdir', [rffi.CCHARP]+ARG2, rffi.INT)
 
         def mkdir_llimpl(pathname, *opt_mode):
-            l_pathname = rffi.str2charp(pathname)
             if opt_mode:
-                opt_mode = (rffi.cast(rffi.MODE_T, opt_mode[0]),)
-            res = os_mkdir(l_pathname, *opt_mode)
-            rffi.free_charp(l_pathname)
+                opt_mode = opt_mode[0]
+            res = os_mkdir(pathname, *opt_mode)
             if res < 0:
                 raise OSError(rffi.get_errno(), "os_mkdir failed")
 
@@ -718,9 +691,7 @@
         os_rmdir = self.llexternal('rmdir', [rffi.CCHARP], rffi.INT)
 
         def rmdir_llimpl(pathname):
-            l_pathname = rffi.str2charp(pathname)
-            res = os_rmdir(l_pathname)
-            rffi.free_charp(l_pathname)
+            res = os_rmdir(pathname)
             if res < 0:
                 raise OSError(rffi.get_errno(), "os_rmdir failed")
 
@@ -736,9 +707,7 @@
         os_chmod = self.llexternal('chmod', [rffi.CCHARP, MODE_T], rffi.INT)
 
         def chmod_llimpl(path, mode):
-            l_path = rffi.str2charp(path)
-            res = os_chmod(l_path, rffi.cast(MODE_T, mode))
-            rffi.free_charp(l_path)
+            res = os_chmod(path, rffi.cast(MODE_T, mode))
             if res < 0:
                 raise OSError(rffi.get_errno(), "os_chmod failed")
 
@@ -751,11 +720,7 @@
                                     rffi.INT)
 
         def rename_llimpl(oldpath, newpath):
-            l_oldpath = rffi.str2charp(oldpath)
-            l_newpath = rffi.str2charp(newpath)
-            res = os_rename(l_oldpath, l_newpath)
-            rffi.free_charp(l_newpath)
-            rffi.free_charp(l_oldpath)
+            res = os_rename(oldpath, newpath)
             if res < 0:
                 raise OSError(rffi.get_errno(), "os_rename failed")
 
@@ -797,11 +762,7 @@
                                   rffi.INT)
 
         def link_llimpl(oldpath, newpath):
-            l_oldpath = rffi.str2charp(oldpath)
-            l_newpath = rffi.str2charp(newpath)
-            res = os_link(l_oldpath, l_newpath)
-            rffi.free_charp(l_newpath)
-            rffi.free_charp(l_oldpath)
+            res = os_link(oldpath, newpath)
             if res < 0:
                 raise OSError(rffi.get_errno(), "os_link failed")
 
@@ -814,11 +775,7 @@
                                      rffi.INT)
 
         def symlink_llimpl(oldpath, newpath):
-            l_oldpath = rffi.str2charp(oldpath)
-            l_newpath = rffi.str2charp(newpath)
-            res = os_symlink(l_oldpath, l_newpath)
-            rffi.free_charp(l_newpath)
-            rffi.free_charp(l_oldpath)
+            res = os_symlink(oldpath, newpath)
             if res < 0:
                 raise OSError(rffi.get_errno(), "os_symlink failed")
 



More information about the Pypy-commit mailing list