[pypy-svn] r45198 - in pypy/dist/pypy: rpython rpython/lltypesystem/module rpython/module rpython/ootypesystem/module translator/c translator/c/src

arigo at codespeak.net arigo at codespeak.net
Thu Jul 19 10:38:00 CEST 2007


Author: arigo
Date: Thu Jul 19 10:37:59 2007
New Revision: 45198

Modified:
   pypy/dist/pypy/rpython/extfunctable.py
   pypy/dist/pypy/rpython/lltypesystem/module/ll_os.py
   pypy/dist/pypy/rpython/module/ll_os.py
   pypy/dist/pypy/rpython/ootypesystem/module/ll_os.py
   pypy/dist/pypy/translator/c/extfunc.py
   pypy/dist/pypy/translator/c/node.py
   pypy/dist/pypy/translator/c/src/ll_os.h
Log:
Move os.read,write,close() to the new-style rffi.


Modified: pypy/dist/pypy/rpython/extfunctable.py
==============================================================================
--- pypy/dist/pypy/rpython/extfunctable.py	(original)
+++ pypy/dist/pypy/rpython/extfunctable.py	Thu Jul 19 10:37:59 2007
@@ -179,9 +179,6 @@
 
 # external function declarations
 posix = __import__(os.name)
-declare(os.read     , str           , 'll_os/read')
-declare(os.write    , posannotation , 'll_os/write')
-declare(os.close    , noneannotation, 'll_os/close')
 declare(os.access   , int           , 'll_os/access')
 declare(os.lseek    , r_longlong    , 'll_os/lseek')
 declare(os.isatty   , bool          , 'll_os/isatty')

Modified: pypy/dist/pypy/rpython/lltypesystem/module/ll_os.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/module/ll_os.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/module/ll_os.py	Thu Jul 19 10:37:59 2007
@@ -34,18 +34,6 @@
         return tup
     ll_pipe_result = staticmethod(ll_pipe_result)
 
-    def ll_os_read(cls, fd, count):
-        from pypy.rpython.lltypesystem.rstr import mallocstr
-        if count < 0:
-            raise OSError(errno.EINVAL, None)
-        buffer = mallocstr(count)
-        n = cls.ll_read_into(fd, buffer)
-        if n != count:
-            s = mallocstr(n)
-            ll_strcpy(s, buffer, n)
-            buffer = s
-        return buffer
-
     def ll_os_readlink(cls, path):
         from pypy.rpython.lltypesystem.rstr import mallocstr
         bufsize = 1023

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	Thu Jul 19 10:37:59 2007
@@ -19,8 +19,8 @@
 # 'suggested_primitive' flag is set to another function, if the conversion
 # and buffer preparation stuff is not useful.
 
-import os
-from pypy.rpython.module.support import ll_strcpy, _ll_strfill
+import os, errno
+from pypy.rpython.module.support import ll_strcpy, _ll_strfill, OOSupport
 from pypy.rpython.module.support import to_opaque_object, from_opaque_object
 from pypy.rlib import ros
 from pypy.rlib.rarithmetic import r_longlong
@@ -37,7 +37,7 @@
 if hasattr(os, 'execv'):
 
     os_execv = rffi.llexternal('execv', [rffi.CCHARP, rffi.CCHARPP],
-                               lltype.Signed)
+                               rffi.INT)
 
     def execv_lltypeimpl(path, args):
         l_path = rffi.str2charp(path)
@@ -52,11 +52,11 @@
 
 # ------------------------------- os.dup --------------------------------
 
-os_dup = rffi.llexternal('dup', [lltype.Signed], lltype.Signed,
+os_dup = rffi.llexternal('dup', [rffi.INT], rffi.INT,
                          _callable=os.dup)
 
 def dup_lltypeimpl(fd):
-    newfd = os_dup(fd)
+    newfd = rffi.cast(lltype.Signed, os_dup(rffi.cast(rffi.INT, fd)))
     if newfd == -1:
         raise OSError(rffi.c_errno, "dup failed")
     return newfd
@@ -65,10 +65,11 @@
 
 # ------------------------------- os.dup2 -------------------------------
 
-os_dup2 = rffi.llexternal('dup2', [lltype.Signed, lltype.Signed], lltype.Signed)
+os_dup2 = rffi.llexternal('dup2', [rffi.INT, rffi.INT], rffi.INT)
 
 def dup2_lltypeimpl(fd, newfd):
-    error = os_dup2(fd, newfd)
+    error = rffi.cast(lltype.Signed, os_dup2(rffi.cast(rffi.INT, fd),
+                                             rffi.cast(rffi.INT, newfd)))
     if error == -1:
         raise OSError(rffi.c_errno, "dup2 failed")
 register_external(os.dup2, [int, int], s_None, llimpl=dup2_lltypeimpl,
@@ -81,12 +82,13 @@
                                     ('modtime', TIME_T))
 
 # XXX sys/types.h is not portable at all
-ros_utime = rffi.llexternal('utime', [rffi.CCHARP, UTIMEBUFP], lltype.Signed,
+ros_utime = rffi.llexternal('utime', [rffi.CCHARP, UTIMEBUFP], rffi.INT,
                             includes=['utime.h', 'sys/types.h'])
 
 def utime_null_lltypeimpl(path):
     l_path = rffi.str2charp(path)
-    error = ros_utime(l_path, lltype.nullptr(UTIMEBUFP.TO))
+    error = rffi.cast(lltype.Signed, ros_utime(l_path,
+                                               lltype.nullptr(UTIMEBUFP.TO)))
     rffi.free_charp(l_path)
     if error == -1:
         raise OSError(rffi.c_errno, "utime_null failed")
@@ -100,7 +102,7 @@
     l_utimebuf = lltype.malloc(UTIMEBUFP.TO, flavor='raw')
     actime, modtime = tp
     l_utimebuf.c_actime, l_utimebuf.c_modtime = int(actime), int(modtime)
-    error = ros_utime(l_path, l_utimebuf)
+    error = rffi.cast(lltype.Signed, ros_utime(l_path, l_utimebuf))
     rffi.free_charp(l_path)
     lltype.free(l_utimebuf, flavor='raw')
     if error == -1:
@@ -115,17 +117,18 @@
     return os.open(path, flags, mode)
 
 if os.name == 'nt':
-    mode_t = lltype.Signed
+    mode_t = rffi.INT
 else:
     mode_t = rffi.MODE_T
 
-os_open = rffi.llexternal('open', [rffi.CCHARP, lltype.Signed, mode_t],
-                          lltype.Signed, _callable=fake_os_open)
+os_open = rffi.llexternal('open', [rffi.CCHARP, rffi.INT, mode_t],
+                          rffi.INT, _callable=fake_os_open)
 
 def os_open_lltypeimpl(path, flags, mode):
     l_path = rffi.str2charp(path)
-    mode = lltype.cast_primitive(mode_t, mode)
-    result = os_open(l_path, flags, mode)
+    result = rffi.cast(lltype.Signed, os_open(l_path,
+                                              rffi.cast(rffi.INT, flags),
+                                              rffi.cast(mode_t, mode)))
     rffi.free_charp(l_path)
     if result == -1:
         raise OSError(rffi.c_errno, "os_open failed")
@@ -137,6 +140,71 @@
 register_external(os.open, [str, int, int], int, "ll_os.ll_os_open",
                   llimpl=os_open_lltypeimpl, oofakeimpl=os_open_oofakeimpl)
 
+# ------------------------------- os.read -------------------------------
+
+os_read = rffi.llexternal('read', [rffi.INT, rffi.VOIDP, rffi.SIZE_T],
+                          rffi.SIZE_T)
+
+def os_read_lltypeimpl(fd, count):
+    if count < 0:
+        raise OSError(errno.EINVAL, None)
+    inbuf = lltype.malloc(rffi.CCHARP.TO, count, flavor='raw')
+    try:
+        got = rffi.cast(lltype.Signed, os_read(rffi.cast(rffi.INT, fd),
+                                               inbuf,
+                                               rffi.cast(rffi.SIZE_T, count)))
+        if got < 0:
+            raise OSError(rffi.c_errno, "os_read failed")
+        # XXX too many copies of the data!
+        l = [inbuf[i] for i in range(got)]
+    finally:
+        lltype.free(inbuf, flavor='raw')
+    return ''.join(l)
+
+def os_read_oofakeimpl(fd, count):
+    return OOSupport.to_rstr(os.read(fd, count))
+
+register_external(os.read, [int, int], str, "ll_os.ll_os_read",
+                  llimpl=os_read_lltypeimpl, oofakeimpl=os_read_oofakeimpl)
+
+# ------------------------------- os.write ------------------------------
+
+os_write = rffi.llexternal('write', [rffi.INT, rffi.VOIDP, rffi.SIZE_T],
+                           rffi.SIZE_T)
+
+def os_write_lltypeimpl(fd, data):
+    count = len(data)
+    outbuf = lltype.malloc(rffi.CCHARP.TO, count, flavor='raw')
+    try:
+        for i in range(count):
+            outbuf[i] = data[i]
+        written = rffi.cast(lltype.Signed, os_write(rffi.cast(rffi.INT, fd),
+                                                    outbuf,
+                                                rffi.cast(rffi.SIZE_T, count)))
+        if written < 0:
+            raise OSError(rffi.c_errno, "os_write failed")
+    finally:
+        lltype.free(outbuf, flavor='raw')
+    return written
+
+def os_write_oofakeimpl(fd, data):
+    return os.write(fd, OOSupport.from_rstr(data))
+
+register_external(os.write, [int, str], int, "ll_os.ll_os_write",
+                  llimpl=os_write_lltypeimpl, oofakeimpl=os_write_oofakeimpl)
+
+# ------------------------------- os.close ------------------------------
+
+os_close = rffi.llexternal('close', [rffi.INT], rffi.INT)
+
+def close_lltypeimpl(fd):
+    error = rffi.cast(lltype.Signed, os_close(rffi.cast(rffi.INT, fd)))
+    if error == -1:
+        raise OSError(rffi.c_errno, "close failed")
+
+register_external(os.close, [int], s_None, llimpl=close_lltypeimpl,
+                  export_name="ll_os.ll_os_close", oofakeimpl=os.close)
+
 # ------------------------------- os.* ----------------------------------
 
 w_star = ['WCOREDUMP', 'WIFCONTINUED', 'WIFSTOPPED',
@@ -196,25 +264,10 @@
 class BaseOS:
     __metaclass__ = ClassMethods
 
-    def ll_os_write(cls, fd, astring):
-        return os.write(fd, cls.from_rstr(astring))
-    ll_os_write.suggested_primitive = True
-
     def ll_os_getcwd(cls):
         return cls.to_rstr(os.getcwd())
     ll_os_getcwd.suggested_primitive = True
 
-    def ll_read_into(fd, buffer):
-        data = os.read(fd, len(buffer.chars))
-        _ll_strfill(buffer, data, len(data))
-        return len(data)
-    ll_read_into.suggested_primitive = True
-    ll_read_into = staticmethod(ll_read_into)
-
-    def ll_os_close(cls, fd):
-        os.close(fd)
-    ll_os_close.suggested_primitive = True
-
     def ll_os_access(cls, path, mode):
         return os.access(cls.from_rstr(path), mode)
     ll_os_access.suggested_primitive = True

Modified: pypy/dist/pypy/rpython/ootypesystem/module/ll_os.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/module/ll_os.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/module/ll_os.py	Thu Jul 19 10:37:59 2007
@@ -32,10 +32,6 @@
         return tup
     ll_stat_result = staticmethod(ll_stat_result)
 
-    def ll_os_read(cls, fd, count):
-        return cls.to_rstr(os.read(fd, count))
-    ll_os_read.suggested_primitive = True
-
     def ll_pipe_result(fd1, fd2):
         tup = ootype.new(PIPE_RESULT)
         tup.item0 = fd1

Modified: pypy/dist/pypy/translator/c/extfunc.py
==============================================================================
--- pypy/dist/pypy/translator/c/extfunc.py	(original)
+++ pypy/dist/pypy/translator/c/extfunc.py	Thu Jul 19 10:37:59 2007
@@ -23,9 +23,6 @@
 # references to functions, so we cannot insert classmethods here.
 
 EXTERNALS = {
-    impl.ll_read_into:          'LL_read_into', # it's a staticmethod
-    impl.ll_os_write.im_func:   'LL_os_write',
-    impl.ll_os_close.im_func:   'LL_os_close',
     impl.ll_os_access.im_func:  'LL_os_access',
     impl.ll_os_stat.im_func:    'LL_os_stat',
     impl.ll_os_fstat.im_func:   'LL_os_fstat',

Modified: pypy/dist/pypy/translator/c/node.py
==============================================================================
--- pypy/dist/pypy/translator/c/node.py	(original)
+++ pypy/dist/pypy/translator/c/node.py	Thu Jul 19 10:37:59 2007
@@ -701,6 +701,7 @@
         db.externalfuncs[fnobj._external_name] = fnobj
         return []
     elif fnobj._callable in extfunc.EXTERNALS:
+        # -- deprecated case --
         # 'fnobj' is one of the ll_xyz() functions with the suggested_primitive
         # flag in pypy.rpython.module.*.  The corresponding C wrappers are
         # written by hand in src/ll_*.h, and declared in extfunc.EXTERNALS.

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	Thu Jul 19 10:37:59 2007
@@ -51,9 +51,6 @@
 
 /* prototypes */
 
-long LL_read_into(int fd, RPyString *buffer);
-long LL_os_write(int fd, RPyString *buffer);
-void LL_os_close(int fd);
 int LL_os_access(RPyString *filename, int mode);
 RPySTAT_RESULT* _stat_construct_result_helper(STRUCT_STAT st);
 RPySTAT_RESULT* LL_os_stat(RPyString * fname);
@@ -102,28 +99,6 @@
 
 #include "ll_osdefs.h"
 
-long LL_read_into(int fd, RPyString *buffer)
-{
-	long n = read(fd, RPyString_AsString(buffer), RPyString_Size(buffer));
-	if (n < 0)
-		RPYTHON_RAISE_OSERROR(errno);
-	return n;
-}
-
-long LL_os_write(int fd, RPyString *buffer)
-{
-	long n = write(fd, RPyString_AsString(buffer), RPyString_Size(buffer));
-	if (n < 0)
-		RPYTHON_RAISE_OSERROR(errno);
-	return n;
-}
-
-void LL_os_close(int fd)
-{
-	if (close(fd) < 0)
-		RPYTHON_RAISE_OSERROR(errno);
-}
-
 int LL_os_access(RPyString *filename, int mode) {
 	int n = access(RPyString_AsString(filename), mode);
 	return (n == 0);



More information about the Pypy-commit mailing list