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

arigo at codespeak.net arigo at codespeak.net
Fri Aug 3 09:50:53 CEST 2007


Author: arigo
Date: Fri Aug  3 09:50:51 2007
New Revision: 45471

Modified:
   pypy/dist/pypy/rpython/extfunctable.py
   pypy/dist/pypy/rpython/module/ll_os.py
   pypy/dist/pypy/rpython/module/test/test_ll_os.py
   pypy/dist/pypy/translator/c/extfunc.py
   pypy/dist/pypy/translator/c/src/ll_os.h
Log:
os.getcwd()...


Modified: pypy/dist/pypy/rpython/extfunctable.py
==============================================================================
--- pypy/dist/pypy/rpython/extfunctable.py	(original)
+++ pypy/dist/pypy/rpython/extfunctable.py	Fri Aug  3 09:50:51 2007
@@ -189,7 +189,6 @@
 declare(os.system   , int           , 'll_os/system')
 declare(os.strerror , str           , 'll_os/strerror')
 declare(os.unlink   , noneannotation, 'll_os/unlink')
-declare(os.getcwd   , str           , 'll_os/getcwd')
 declare(os.chdir    , noneannotation, 'll_os/chdir')
 declare(os.mkdir    , noneannotation, 'll_os/mkdir')
 declare(os.rmdir    , noneannotation, 'll_os/rmdir')

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	Fri Aug  3 09:50:51 2007
@@ -324,6 +324,55 @@
                       export_name="ll_os.ll_os_access",
                       oofakeimpl=os_access_oofakeimpl)
 
+    @registering(os.getcwd)
+    def register_os_getcwd(self):
+        os_getcwd = rffi.llexternal('getcwd',
+                                    [rffi.CCHARP, rffi.SIZE_T],
+                                    rffi.CCHARP)
+
+        def os_getcwd_lltypeimpl():
+            bufsize = 256
+            while True:
+                buf = lltype.malloc(rffi.CCHARP.TO, bufsize, flavor='raw')
+                res = os_getcwd(buf, rffi.cast(rffi.SIZE_T, bufsize))
+                if res:
+                    break   # ok
+                error = rffi.c_errno
+                lltype.free(buf, flavor='raw')
+                if error != errno.ERANGE:
+                    raise OSError(error, "getcwd failed")
+                # else try again with a larger buffer, up to some sane limit
+                bufsize *= 4
+                if bufsize > 1024*1024:  # xxx hard-coded upper limit
+                    raise OSError(error, "getcwd result too large")
+            result = rffi.charp2str(res)
+            lltype.free(buf, flavor='raw')
+            return result
+
+        def os_getcwd_oofakeimpl():
+            return OOSupport.to_rstr(os.getcwd())
+
+        self.register(os.getcwd, [], SomeString(),
+                      "ll_os.ll_os_getcwd", llimpl=os_getcwd_lltypeimpl,
+                      oofakeimpl=os_getcwd_oofakeimpl)
+
+        # '--sandbox' support
+        def os_getcwd_marshal_input(msg, buf, bufsize):
+            msg.packsize_t(bufsize)
+        def os_getcwd_unmarshal_output(msg, buf, bufsize):
+            # the outside process should not send a result larger than
+            # the requested 'bufsize'
+            result = msg.nextstring()
+            n = len(result)
+            if rffi.cast(rffi.SIZE_T, n) >= bufsize:
+                raise OverflowError
+            for i in range(n):
+                buf[i] = result[i]
+            buf[n] = '\x00'
+            return buf
+        os_getcwd._obj._marshal_input = os_getcwd_marshal_input
+        os_getcwd._obj._unmarshal_output = os_getcwd_unmarshal_output
+
     # ------------------------------- os.W* ---------------------------------
 
     w_star = ['WCOREDUMP', 'WIFCONTINUED', 'WIFSTOPPED',
@@ -378,10 +427,6 @@
 class BaseOS:
     __metaclass__ = ClassMethods
 
-    def ll_os_getcwd(cls):
-        return cls.to_rstr(os.getcwd())
-    ll_os_getcwd.suggested_primitive = True
-
     def ll_os_lseek(cls, fd,pos,how):
         return r_longlong(os.lseek(fd,pos,how))
     ll_os_lseek.suggested_primitive = True

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	Fri Aug  3 09:50:51 2007
@@ -22,8 +22,8 @@
 
 
 def test_getcwd():
-    data = impl.ll_os_getcwd()
-    assert impl.from_rstr(data) == os.getcwd()
+    data = getllimpl(os.getcwd)()
+    assert data == os.getcwd()
 
 def test_strerror():
     data = impl.ll_os_strerror(2)

Modified: pypy/dist/pypy/translator/c/extfunc.py
==============================================================================
--- pypy/dist/pypy/translator/c/extfunc.py	(original)
+++ pypy/dist/pypy/translator/c/extfunc.py	Fri Aug  3 09:50:51 2007
@@ -32,7 +32,6 @@
     impl.ll_os_strerror.im_func: 'LL_os_strerror',
     impl.ll_os_system.im_func:  'LL_os_system',
     impl.ll_os_unlink.im_func:  'LL_os_unlink',
-    impl.ll_os_getcwd.im_func:  'LL_os_getcwd',
     impl.ll_os_chdir.im_func:   'LL_os_chdir',
     impl.ll_os_mkdir.im_func:   'LL_os_mkdir',
     impl.ll_os_rmdir.im_func:   'LL_os_rmdir',

Modified: pypy/dist/pypy/translator/c/src/ll_os.h
==============================================================================
--- pypy/dist/pypy/translator/c/src/ll_os.h	(original)
+++ pypy/dist/pypy/translator/c/src/ll_os.h	Fri Aug  3 09:50:51 2007
@@ -61,7 +61,6 @@
 RPyString *LL_os_strerror(int errnum);
 long LL_os_system(RPyString * fname);
 void LL_os_unlink(RPyString * fname);
-RPyString *LL_os_getcwd(void);
 void LL_os_chdir(RPyString * path);
 void LL_os_mkdir(RPyString * path, int mode);
 void LL_os_rmdir(RPyString * path);
@@ -237,17 +236,6 @@
     }
 }
 
-RPyString *LL_os_getcwd(void) {
-	char buf[PATH_MAX];
-	char *res;
-	res = getcwd(buf, sizeof buf);
-	if (res == NULL) {
-		RPYTHON_RAISE_OSERROR(errno);
-		return NULL;
-	}
-	return RPyString_FromString(buf);
-}
-
 void LL_os_chdir(RPyString * path) {
     int error = chdir(RPyString_AsString(path));
     if (error != 0) {



More information about the Pypy-commit mailing list