[pypy-svn] r18798 - in pypy/dist/pypy/module/posix: . test

arigo at codespeak.net arigo at codespeak.net
Thu Oct 20 16:48:54 CEST 2005


Author: arigo
Date: Thu Oct 20 16:48:53 2005
New Revision: 18798

Modified:
   pypy/dist/pypy/module/posix/__init__.py
   pypy/dist/pypy/module/posix/interp_posix.py
   pypy/dist/pypy/module/posix/test/test_posix2.py
Log:
os.listdir() at app-level.


Modified: pypy/dist/pypy/module/posix/__init__.py
==============================================================================
--- pypy/dist/pypy/module/posix/__init__.py	(original)
+++ pypy/dist/pypy/module/posix/__init__.py	Thu Oct 20 16:48:53 2005
@@ -35,7 +35,8 @@
     'chdir'     : 'interp_posix.chdir',
     'mkdir'     : 'interp_posix.mkdir',
     'rmdir'     : 'interp_posix.rmdir',
-    'environ'   : 'interp_posix.get(space).w_environ'
+    'environ'   : 'interp_posix.get(space).w_environ',
+    'listdir'   : 'interp_posix.listdir',
     }
     if hasattr(os, 'ftruncate'):
         interpleveldefs['ftruncate'] = 'interp_posix.ftruncate'

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	Thu Oct 20 16:48:53 2005
@@ -232,3 +232,24 @@
         # old value was still accessible until then.
         del get(space).posix_putenv_garbage[name]
 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):
+    dir = ros.opendir(dirname)
+    try:
+        # sub-function call to make sure that 'try:finally:' will catch
+        # everything including MemoryErrors
+        return enumeratedir(space, dir)
+    finally:
+        dir.closedir()
+listdir.unwrap_spec = [ObjSpace, str]

Modified: pypy/dist/pypy/module/posix/test/test_posix2.py
==============================================================================
--- pypy/dist/pypy/module/posix/test/test_posix2.py	(original)
+++ pypy/dist/pypy/module/posix/test/test_posix2.py	Thu Oct 20 16:48:53 2005
@@ -6,12 +6,18 @@
     mod.space = StdObjSpace(usemodules=['posix'])
     mod.path = udir.join('posixtestfile.txt') 
     mod.path.write("this is a test")
+    pdir = udir.ensure('posixtestdir', dir=True)
+    pdir.join('file1').write("test1")
+    pdir.join('file2').write("test2")
+    pdir.join('another_longer_file_name').write("test3")
+    mod.pdir = pdir
 
 class AppTestPosix: 
     def setup_class(cls): 
         cls.space = space 
         cls.w_posix = space.appexec([], "(): import %s as m ; return m" % os.name)
         cls.w_path = space.wrap(str(path))
+        cls.w_pdir = space.wrap(str(pdir))
     
     def test_posix_is_pypy_s(self): 
         assert self.posix.__file__ 
@@ -75,6 +81,15 @@
         else:
             raise "did not raise"
 
+    def test_listdir(self):
+        pdir = self.pdir
+        posix = self.posix 
+        result = posix.listdir(pdir)
+        result.sort()
+        assert result == ['another_longer_file_name',
+                          'file1',
+                          'file2']
+
 class AppTestEnvironment(object):
     def setup_class(cls): 
         cls.space = space 



More information about the Pypy-commit mailing list