[pypy-svn] r71870 - in pypy/trunk/pypy/translator/sandbox: . test

arigo at codespeak.net arigo at codespeak.net
Mon Mar 8 10:32:31 CET 2010


Author: arigo
Date: Mon Mar  8 10:32:29 2010
New Revision: 71870

Modified:
   pypy/trunk/pypy/translator/sandbox/pypy_interact.py
   pypy/trunk/pypy/translator/sandbox/test/test_pypy_interact.py
   pypy/trunk/pypy/translator/sandbox/test/test_vfs.py
   pypy/trunk/pypy/translator/sandbox/vfs.py
Log:
In sandboxing mode, don't deliver .pyc files.
The .py files only are enough, and the .pyc files
were not considered anyway because the .py files
had a mtime of 0.  (Which caused troubles because
of the bug fixed in r71869.)


Modified: pypy/trunk/pypy/translator/sandbox/pypy_interact.py
==============================================================================
--- pypy/trunk/pypy/translator/sandbox/pypy_interact.py	(original)
+++ pypy/trunk/pypy/translator/sandbox/pypy_interact.py	Mon Mar  8 10:32:29 2010
@@ -45,18 +45,21 @@
         # * can access its own executable
         # * can access the pure Python libraries
         # * can access the temporary usession directory as /tmp
+        exclude = ['.pyc', '.pyo']
         if self.tmpdir is None:
             tmpdirnode = Dir({})
         else:
-            tmpdirnode = RealDir(self.tmpdir)
+            tmpdirnode = RealDir(self.tmpdir, exclude=exclude)
         pypydist = os.path.dirname(os.path.abspath(autopath.pypydir))
 
         return Dir({
             'bin': Dir({
                 'pypy-c': RealFile(self.executable),
-                'lib-python': RealDir(os.path.join(pypydist, 'lib-python')),
+                'lib-python': RealDir(os.path.join(pypydist, 'lib-python'),
+                                      exclude=exclude),
                 'pypy': Dir({
-                    'lib': RealDir(os.path.join(pypydist, 'pypy', 'lib')),
+                    'lib': RealDir(os.path.join(pypydist, 'pypy', 'lib'),
+                                   exclude=exclude),
                     }),
                 }),
              'tmp': tmpdirnode,

Modified: pypy/trunk/pypy/translator/sandbox/test/test_pypy_interact.py
==============================================================================
--- pypy/trunk/pypy/translator/sandbox/test/test_pypy_interact.py	(original)
+++ pypy/trunk/pypy/translator/sandbox/test/test_pypy_interact.py	Mon Mar  8 10:32:29 2010
@@ -36,6 +36,12 @@
         assert_(False, "os.stat('site') should have failed")
     st = os.stat('/bin/lib-python/modified-2.5.2/site.py')
     assert_(stat.S_ISREG(st.st_mode), "bad st_mode for .../site.py")
+    try:
+        os.stat('/bin/lib-python/modified-2.5.2/site.pyc')
+    except OSError:
+        pass
+    else:
+        assert_(False, "os.stat('....pyc') should have failed")
     fd = os.open('/bin/lib-python/modified-2.5.2/site.py', os.O_RDONLY, 0666)
     length = 8192
     ofs = 0

Modified: pypy/trunk/pypy/translator/sandbox/test/test_vfs.py
==============================================================================
--- pypy/trunk/pypy/translator/sandbox/test/test_vfs.py	(original)
+++ pypy/trunk/pypy/translator/sandbox/test/test_vfs.py	Mon Mar  8 10:32:29 2010
@@ -90,3 +90,19 @@
             else:
                 py.test.raises(OSError, v_test_vfs.join, '.hidden')
                 py.test.raises(OSError, v_test_vfs.join, '.subdir2')
+
+def test_realdir_exclude():
+    xdir = udir.ensure('test_realdir_exclude', dir=1)
+    xdir.ensure('test_realdir_exclude.yes')
+    xdir.ensure('test_realdir_exclude.no')
+    v_udir = RealDir(str(udir), exclude=['.no'])
+    v_xdir = v_udir.join('test_realdir_exclude')
+    assert 'test_realdir_exclude.yes' in v_xdir.keys()
+    assert 'test_realdir_exclude.no' not in v_xdir.keys()
+    v_xdir.join('test_realdir_exclude.yes')    # works
+    py.test.raises(OSError, v_xdir.join, 'test_realdir_exclude.no')
+    # Windows and Mac tests, for the case
+    py.test.raises(OSError, v_xdir.join, 'Test_RealDir_Exclude.no')
+    py.test.raises(OSError, v_xdir.join, 'test_realdir_exclude.No')
+    py.test.raises(OSError, v_xdir.join, 'test_realdir_exclude.nO')
+    py.test.raises(OSError, v_xdir.join, 'test_realdir_exclude.NO')

Modified: pypy/trunk/pypy/translator/sandbox/vfs.py
==============================================================================
--- pypy/trunk/pypy/translator/sandbox/vfs.py	(original)
+++ pypy/trunk/pypy/translator/sandbox/vfs.py	Mon Mar  8 10:32:29 2010
@@ -64,21 +64,30 @@
     # with '.' simply don't exist.  If follow_links=True, then symlinks are
     # transparently followed (they look like a regular file or directory to
     # the sandboxed process).  If follow_links=False, the subprocess is
-    # not allowed to access them at all.
-    def __init__(self, path, show_dotfiles=False, follow_links=False):
+    # not allowed to access them at all.  Finally, exclude is a list of
+    # file endings that we filter out (note that we also filter out files
+    # with the same ending but a different case, to be safe).
+    def __init__(self, path, show_dotfiles=False, follow_links=False,
+                 exclude=[]):
         self.path = path
         self.show_dotfiles = show_dotfiles
         self.follow_links  = follow_links
+        self.exclude       = [excl.lower() for excl in exclude]
     def __repr__(self):
         return '<RealDir %s>' % (self.path,)
     def keys(self):
         names = os.listdir(self.path)
         if not self.show_dotfiles:
             names = [name for name in names if not name.startswith('.')]
+        for excl in self.exclude:
+            names = [name for name in names if not name.lower().endswith(excl)]
         return names
     def join(self, name):
         if name.startswith('.') and not self.show_dotfiles:
             raise OSError(errno.ENOENT, name)
+        for excl in self.exclude:
+            if name.lower().endswith(excl):
+                raise OSError(errno.ENOENT, name)
         path = os.path.join(self.path, name)
         if self.follow_links:
             st = os.stat(path)
@@ -86,7 +95,8 @@
             st = os.lstat(path)
         if stat.S_ISDIR(st.st_mode):
             return RealDir(path, show_dotfiles = self.show_dotfiles,
-                                 follow_links  = self.follow_links)
+                                 follow_links  = self.follow_links,
+                                 exclude       = self.exclude)
         elif stat.S_ISREG(st.st_mode):
             return RealFile(path)
         else:



More information about the Pypy-commit mailing list