[pypy-svn] pypy default: implement os.fstat for the jvm backend

antocuni commits-noreply at bitbucket.org
Mon Apr 11 17:37:39 CEST 2011


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: 
Changeset: r43301:f70561d31d68
Date: 2011-04-11 17:30 +0200
http://bitbucket.org/pypy/pypy/changeset/f70561d31d68/

Log:	implement os.fstat for the jvm backend

diff --git a/pypy/translator/jvm/src/pypy/ll_os.java b/pypy/translator/jvm/src/pypy/ll_os.java
--- a/pypy/translator/jvm/src/pypy/ll_os.java
+++ b/pypy/translator/jvm/src/pypy/ll_os.java
@@ -14,10 +14,22 @@
 
 abstract class FileWrapper
 {
+    private final String name;
+
+    public FileWrapper(String name)
+    {
+        this.name = name;
+    }
+
     public abstract void write(String buffer);
     public abstract String read(int count);
     public abstract void close();
     public abstract RandomAccessFile getFile();
+
+    public String getName()
+    {
+        return this.name;
+    }
 }
 
 class PrintStreamWrapper extends FileWrapper
@@ -25,8 +37,9 @@
     private final PrintStream stream;
     private final ll_os os;
 
-    public PrintStreamWrapper(PrintStream stream, ll_os os)
+    public PrintStreamWrapper(String name, PrintStream stream, ll_os os)
     {
+        super(name);
         this.stream = stream;
         this.os = os;
     }
@@ -58,8 +71,9 @@
     private final InputStream stream;
     private final ll_os os;
 
-    public InputStreamWrapper(InputStream stream, ll_os os)
+    public InputStreamWrapper(String name, InputStream stream, ll_os os)
     {
+        super(name);
         this.stream = stream;
         this.os = os;
     }
@@ -102,11 +116,13 @@
     private final boolean canWrite;
     private final ll_os os;
 
-    public RandomAccessFileWrapper(RandomAccessFile file, 
+    public RandomAccessFileWrapper(String name,
+                                   RandomAccessFile file, 
                                    boolean canRead, 
                                    boolean canWrite,
                                    ll_os os)
     {
+        super(name);
         this.file = file;
         this.canRead = canRead;
         this.canWrite = canWrite;
@@ -228,9 +244,9 @@
 
     public ll_os(Interlink interlink) {
         this.interlink = interlink;
-        FileDescriptors.put(0, new InputStreamWrapper(System.in, this));
-        FileDescriptors.put(1, new PrintStreamWrapper(System.out, this));
-        FileDescriptors.put(2, new PrintStreamWrapper(System.err, this));
+        FileDescriptors.put(0, new InputStreamWrapper("<stdin>", System.in, this));
+        FileDescriptors.put(1, new PrintStreamWrapper("<stdout>", System.out, this));
+        FileDescriptors.put(2, new PrintStreamWrapper("<stderr>", System.err, this));
         fdcount = 2;
     }
 
@@ -339,7 +355,7 @@
         // XXX: we ignore O_CREAT
         RandomAccessFile file = open_file(name, javaMode, flags);
         RandomAccessFileWrapper wrapper = 
-          new RandomAccessFileWrapper(file, canRead, canWrite, this);
+            new RandomAccessFileWrapper(name, file, canRead, canWrite, this);
 
         fdcount++;
         FileDescriptors.put(fdcount, wrapper);
@@ -418,6 +434,12 @@
         return ll_os_stat(path); // XXX
     }
 
+    public StatResult ll_os_fstat(int fd)
+    {
+        String name = getfd(fd).getName();
+        return ll_os_stat(name);
+    }
+
     public String ll_os_strerror(int errno)
     {
         String msg = ErrorMessages.remove(errno);

diff --git a/pypy/translator/jvm/test/test_builtin.py b/pypy/translator/jvm/test/test_builtin.py
--- a/pypy/translator/jvm/test/test_builtin.py
+++ b/pypy/translator/jvm/test/test_builtin.py
@@ -37,6 +37,15 @@
     def test_cast_primitive(self):
         py.test.skip('fixme!')
 
+    def test_os_fstat(self):
+        import os, stat
+        def fn():
+            fd = os.open(__file__, os.O_RDONLY, 0)
+            st = os.fstat(fd)
+            os.close(fd)
+            return st.st_mode
+        res = self.interpret(fn, [])
+        assert stat.S_ISREG(res)
 
 class TestJvmTime(JvmTest, BaseTestTime):
 


More information about the Pypy-commit mailing list