[pypy-svn] r45498 - in pypy/dist/pypy: module/posix translator/c/src translator/cli/src translator/cli/test

arigo at codespeak.net arigo at codespeak.net
Sun Aug 5 14:38:59 CEST 2007


Author: arigo
Date: Sun Aug  5 14:38:57 2007
New Revision: 45498

Modified:
   pypy/dist/pypy/module/posix/interp_posix.py
   pypy/dist/pypy/translator/c/src/ll_os.h
   pypy/dist/pypy/translator/cli/src/   (props changed)
   pypy/dist/pypy/translator/cli/src/ll_os.cs
   pypy/dist/pypy/translator/cli/test/test_builtin.py
Log:
Move away from ros.opendir/readdir/closedir and use os.listdir() directly
in PyPy's Posix module - now supported by the CLI backend too.


Modified: pypy/dist/pypy/module/posix/interp_posix.py
==============================================================================
--- pypy/dist/pypy/module/posix/interp_posix.py	(original)
+++ pypy/dist/pypy/module/posix/interp_posix.py	Sun Aug  5 14:38:57 2007
@@ -287,16 +287,6 @@
 unsetenv.unwrap_spec = [ObjSpace, str]
 
 
-def enumeratedir(space, dir):
-    result = []
-    while True:
-        nextentry = dir.readdir()
-        if nextentry is None:
-            break
-        if nextentry not in ('.' , '..'):
-            result.append(space.wrap(nextentry))
-    return space.newlist(result)
-
 def listdir(space, dirname):
     """Return a list containing the names of the entries in the directory.
 
@@ -305,15 +295,11 @@
 The list is in arbitrary order.  It does not include the special
 entries '.' and '..' even if they are present in the directory."""
     try:
-        dir = ros.opendir(dirname)
-    except OSError, e: 
-        raise wrap_oserror(space, e) 
-    try:
-        # sub-function call to make sure that 'try:finally:' will catch
-        # everything including MemoryErrors
-        return enumeratedir(space, dir)
-    finally:
-        dir.closedir()
+        result = os.listdir(dirname)
+    except OSError, e:
+        raise wrap_oserror(space, e)
+    result_w = [space.wrap(s) for s in result]
+    return space.newlist(result_w)
 listdir.unwrap_spec = [ObjSpace, str]
 
 def pipe(space):

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	Sun Aug  5 14:38:57 2007
@@ -423,6 +423,7 @@
 }
 
 /******************** opendir/readdir/closedir ********************/
+/* XXX old interface no longer used in PyPy, will be removed at some point */
 #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
 
 /* emulation of opendir, readdir, closedir */
@@ -450,6 +451,7 @@
     char arg[1]; /*also used as flag */
 } DIR;
 
+/* XXX old interface no longer used in PyPy, will be removed at some point */
 static DIR *opendir(char *dirname)
 {
     int lng = strlen(dirname);
@@ -473,6 +475,7 @@
     return d;
 }
 
+/* XXX old interface no longer used in PyPy, will be removed at some point */
 static struct dirent *readdir(DIR *d)
 {
     if (d->arg[0])
@@ -489,6 +492,7 @@
     return d->d_name ? d : NULL;
 }
 
+/* XXX old interface no longer used in PyPy, will be removed at some point */
 static int closedir(DIR *d)
 {
     HANDLE hFind = d->hFind;
@@ -503,6 +507,7 @@
 
 #endif /* defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) */
 
+/* XXX old interface no longer used in PyPy, will be removed at some point */
 struct RPyOpaque_DIR *LL_os_opendir(RPyString *dirname)
 {
     DIR *dir = opendir(RPyString_AsString(dirname));
@@ -511,6 +516,7 @@
     return (struct RPyOpaque_DIR *) dir;
 }
 
+/* XXX old interface no longer used in PyPy, will be removed at some point */
 RPyString *LL_os_readdir(struct RPyOpaque_DIR *dir)
 {
     struct dirent *d;
@@ -523,6 +529,7 @@
     return NULL;
 }
 
+/* XXX old interface no longer used in PyPy, will be removed at some point */
 void LL_os_closedir(struct RPyOpaque_DIR *dir)
 {
     if (closedir((DIR *) dir) < 0)

Modified: pypy/dist/pypy/translator/cli/src/ll_os.cs
==============================================================================
--- pypy/dist/pypy/translator/cli/src/ll_os.cs	(original)
+++ pypy/dist/pypy/translator/cli/src/ll_os.cs	Sun Aug  5 14:38:57 2007
@@ -463,6 +463,25 @@
             Helpers.raise_OSError(Errno.EPERM); // this is only a stub
         }
 
+        public static pypy.runtime.List<string> ll_os_listdir(string path)
+        {
+            if (path == "")
+                Helpers.raise_OSError(Errno.ENOENT);
+
+            DirectoryInfo dir = new DirectoryInfo(path);
+            if (!dir.Exists)
+                Helpers.raise_OSError(Errno.ENOENT);
+
+            pypy.runtime.List<string> names = new pypy.runtime.List<string>();
+            foreach(DirectoryInfo d in dir.GetDirectories())
+                names.Add(d.Name);
+            foreach(FileInfo f in dir.GetFiles())
+                names.Add(f.Name);
+
+            return names;
+        }
+
+        /* XXX old interface, will be removed at some point */
         public static object ll_os_opendir(string path)
         {
             if (path == "")
@@ -481,6 +500,7 @@
             return names.GetEnumerator();
         }
 
+        /* XXX old interface, will be removed at some point */
         public static string ll_os_readdir(object obj)
         {
             IEnumerator<string> names = (IEnumerator<string>)obj;
@@ -490,6 +510,7 @@
                 return null;
         }
 
+        /* XXX old interface, will be removed at some point */
         public static void ll_os_closedir(object obj)
         {
         }

Modified: pypy/dist/pypy/translator/cli/test/test_builtin.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/test_builtin.py	(original)
+++ pypy/dist/pypy/translator/cli/test/test_builtin.py	Sun Aug  5 14:38:57 2007
@@ -1,6 +1,7 @@
 import platform
 import os, stat, errno
 import py
+from py.builtin import sorted
 from pypy.tool import udir
 from pypy.translator.cli.test.runtest import CliTest
 from pypy.rpython.test.test_rbuiltin import BaseTestRbuiltin
@@ -105,18 +106,9 @@
         res = self.ll_to_string(self.interpret(fn, []))
         # XXX assert something about res
 
-    def test_os_opendir(self):
-        from pypy.rlib import ros
+    def test_os_listdir(self):
         def fn():
-            d = ros.opendir('.')
-            res = []
-            while True:
-                current = d.readdir()
-                if current is None:
-                    break
-                res.append(current)
-            d.closedir()
-            return res
+            return os.listdir('.')
         res = self.ll_to_list(self.interpret(fn, []))
         res = [self.ll_to_string(s) for s in res]
         res.sort()



More information about the Pypy-commit mailing list