[pypy-svn] r46468 - in pypy/dist/pypy/translator/jvm: . src/pypy

antocuni at codespeak.net antocuni at codespeak.net
Tue Sep 11 14:15:37 CEST 2007


Author: antocuni
Date: Tue Sep 11 14:15:36 2007
New Revision: 46468

Added:
   pypy/dist/pypy/translator/jvm/src/pypy/ll_os.java
Modified:
   pypy/dist/pypy/translator/jvm/generator.py
   pypy/dist/pypy/translator/jvm/genjvm.py
   pypy/dist/pypy/translator/jvm/src/pypy/PyPy.java
   pypy/dist/pypy/translator/jvm/typesystem.py
Log:
move ll_os_* to their own class.

Wrap std{out,in,err} into file descriptors.



Modified: pypy/dist/pypy/translator/jvm/generator.py
==============================================================================
--- pypy/dist/pypy/translator/jvm/generator.py	(original)
+++ pypy/dist/pypy/translator/jvm/generator.py	Tue Sep 11 14:15:36 2007
@@ -10,7 +10,7 @@
      jObject, jByteArray, jPyPyExcWrap, jIntegerClass, jLongClass, \
      jDoubleClass, jCharClass, jStringBuilder, JvmScalarType, jArrayList, \
      jObjectArray, jPyPyInterlink, jPyPyCustomDict, jPyPyEquals, \
-     jPyPyHashCode, jMap, jWeakRef, jSystem
+     jPyPyHashCode, jMap, jWeakRef, jSystem, jll_os
 
 # ___________________________________________________________________________
 # Miscellaneous helper functions
@@ -1020,7 +1020,11 @@
     def call_primitive(self, op, module, name):
         callee = op.args[0].value
         argtypes, rettype = self.db.types_for_signature(callee._TYPE.ARGS, callee._TYPE.RESULT)
-        mthd = Method.s(jPyPy, name, argtypes, rettype)
+        if module == 'll_os':
+            jcls = jll_os
+        else:
+            jcls = jPyPy
+        mthd = Method.s(jcls, name, argtypes, rettype)
         self.emit(mthd)
 
     def call_oostring(self, OOTYPE):

Modified: pypy/dist/pypy/translator/jvm/genjvm.py
==============================================================================
--- pypy/dist/pypy/translator/jvm/genjvm.py	(original)
+++ pypy/dist/pypy/translator/jvm/genjvm.py	Tue Sep 11 14:15:36 2007
@@ -176,6 +176,7 @@
                               'Constants',
                               'StatResult',
                               'PyPy',
+                              'll_os',
                               ))
 
     def _make_str(self, a):

Modified: pypy/dist/pypy/translator/jvm/src/pypy/PyPy.java
==============================================================================
--- pypy/dist/pypy/translator/jvm/src/pypy/PyPy.java	(original)
+++ pypy/dist/pypy/translator/jvm/src/pypy/PyPy.java	Tue Sep 11 14:15:36 2007
@@ -792,65 +792,6 @@
         return System.currentTimeMillis()/1000.0;
     }
 
-    public static int ll_os_write(int fd, String text) {
-        // TODO: file descriptors, etc
-        if (fd == 1)
-            System.out.print(text);
-        else if (fd == 2)
-            System.err.print(text);
-        else
-            throw new RuntimeException("Invalid FD");
-        return text.length();
-    }
-
-    public static ArrayList ll_os_envitems()
-    {
-        return new ArrayList(); // XXX
-    }
-
-    public static String ll_os_getcwd()
-    {
-        return System.getProperty("user.dir");
-    }
-
-    public static StatResult ll_os_stat(String path)
-    {
-        if (path.equals(""))
-            throwOSError(ENOENT, "No such file or directory: ''");
-
-        File f = new File(path);
-        
-        if (f.exists()) {
-            StatResult res = new StatResult();
-            if (f.isDirectory())
-                res.setMode(S_IFDIR);
-            else {
-                res.setMode(S_IFREG);
-                res.setSize(f.length());
-                res.setMtime((int)f.lastModified());
-            }
-            return res;
-        }
-
-        throwOSError(ENOENT, "No such file or directory: '"+path+"'");
-        return null; // never reached
-    }
-
-    public static int ll_os_open(String path, int flags, int mode)
-    {
-        throwOSError(ENOENT, "DUMMY: No such file or directory: '"+path+"'"); // XXX
-        return -1; // never reached
-    }
-
-    public static StatResult ll_os_lstat(String path)
-    {
-        return ll_os_stat(path); // XXX
-    }
-
-    public static String ll_os_strerror(int errno)
-    {
-        return "errno: " + errno;
-    }
 
     public static String ll_join(String a, String b)
     {
@@ -980,10 +921,6 @@
     public static void throwValueError() {
         interlink.throwValueError();
     }
-
-    public static void throwOSError(int errCode, String errText) {
-        interlink.throwOSError(errCode); // errText currently ignored... fix?
-    }
     
     // ----------------------------------------------------------------------
     // Self Test

Added: pypy/dist/pypy/translator/jvm/src/pypy/ll_os.java
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/jvm/src/pypy/ll_os.java	Tue Sep 11 14:15:36 2007
@@ -0,0 +1,157 @@
+package pypy;
+
+import java.io.*;
+import java.util.HashMap;
+import java.util.ArrayList;
+import java.util.Map;
+
+abstract class FileWrapper
+{
+    public abstract void write(String buffer);
+    public abstract String read(int count);
+}
+
+class PrintStreamWrapper extends FileWrapper
+{
+    private PrintStream stream;
+
+    public PrintStreamWrapper(PrintStream stream)
+    {
+        this.stream = stream;
+    }
+
+    public void write(String buffer)
+    {
+        this.stream.print(buffer);
+    }
+
+    public String read(int count)
+    {
+        ll_os.throwOSError(PyPy.EBADF, "Write-only fd");
+        return null; // never reached
+    }
+}
+
+class InputStreamWrapper extends FileWrapper
+{
+    private InputStream stream;
+
+    public InputStreamWrapper(InputStream stream)
+    {
+        this.stream = stream;
+    }
+
+    public void write(String buffer)
+    {
+        ll_os.throwOSError(PyPy.EBADF, "Read-only fd");
+    }
+
+    public String read(int count)
+    {
+        try {
+            byte[] buf = new byte[count];
+            int n = stream.read(buf, 0, count);
+            return new String(buf);
+        }
+        catch(IOException e) {
+            ll_os.throwOSError(PyPy.EIO, e.getMessage());
+            return null; // never reached
+        }
+    }
+}
+
+
+public class ll_os {
+
+    // NB: these values are those used by Windows and they differs
+    // from the Unix ones; the os module is patched with these
+    // values before flowgraphing to make sure we get the very
+    // same values on each platform we do the compilation.
+    private static final int O_RDONLY = 0x0000;
+    private static final int O_WRONLY = 0x0001;
+    private static final int O_RDWR   = 0x0002;
+    private static final int O_APPEND = 0x0008;
+    private static final int O_CREAT  = 0x0100;
+    private static final int O_TRUNC  = 0x0200;
+    private static final int O_TEXT   = 0x4000;
+    private static final int O_BINARY = 0x8000;
+    
+    private static final int S_IFMT = 61440;
+    private static final int S_IFDIR = 16384;
+    private static final int S_IFREG = 32768;
+
+    private static Map<Integer, FileWrapper> FileDescriptors = new HashMap<Integer, FileWrapper>();
+    private static Map<Integer, String> ErrorMessages = new HashMap<Integer, String>();
+
+    static {
+        FileDescriptors.put(new Integer(0), new PrintStreamWrapper(System.out));
+        FileDescriptors.put(new Integer(1), new InputStreamWrapper(System.in));
+        FileDescriptors.put(new Integer(2), new PrintStreamWrapper(System.err));
+    }
+
+    public static void throwOSError(int errno, String errText) {
+        ErrorMessages.put(new Integer(errno), errText);
+        PyPy.interlink.throwOSError(errno);
+    }
+
+    public static int ll_os_open(String name, int flags, int mode)
+    {
+        throwOSError(PyPy.ENOENT, "DUMMY ll_os_open");
+        return -1;
+    }
+
+    public static StatResult ll_os_lstat(String path)
+    {
+        return ll_os_stat(path); // XXX
+    }
+
+    public static String ll_os_strerror(int errno)
+    {
+        String msg = ErrorMessages.remove(new Integer(errno));
+        if (msg == null)
+            return "errno: " + errno;
+        else
+            return msg;
+    }
+
+    public static int ll_os_write(int fd, String text) {
+        FileWrapper f = FileDescriptors.get(new Integer(fd));
+        if (f == null)
+            throwOSError(PyPy.EBADF, "Invalid fd: " + fd);
+        f.write(text);
+        return text.length();
+    }
+
+    public static ArrayList ll_os_envitems()
+    {
+        return new ArrayList(); // XXX
+    }
+
+    public static String ll_os_getcwd()
+    {
+        return System.getProperty("user.dir");
+    }
+
+    public static StatResult ll_os_stat(String path)
+    {
+        if (path.equals(""))
+            ll_os.throwOSError(PyPy.ENOENT, "No such file or directory: ''");
+
+        File f = new File(path);
+        
+        if (f.exists()) {
+            StatResult res = new StatResult();
+            if (f.isDirectory())
+                res.setMode(S_IFDIR);
+            else {
+                res.setMode(S_IFREG);
+                res.setSize(f.length());
+                res.setMtime((int)f.lastModified());
+            }
+            return res;
+        }
+
+        ll_os.throwOSError(PyPy.ENOENT, "No such file or directory: '"+path+"'");
+        return null; // never reached
+    }
+}

Modified: pypy/dist/pypy/translator/jvm/typesystem.py
==============================================================================
--- pypy/dist/pypy/translator/jvm/typesystem.py	(original)
+++ pypy/dist/pypy/translator/jvm/typesystem.py	Tue Sep 11 14:15:36 2007
@@ -178,6 +178,7 @@
 jPyPyInterlink = JvmClassType('pypy.Interlink')
 jPyPyCustomDict = JvmClassType('pypy.CustomDict')
 jPyPyStatResult = JvmClassType('pypy.StatResult')
+jll_os = JvmClassType('pypy.ll_os')
 
 jArithmeticException = JvmClassType('java.lang.ArithmeticException', throwable=True)
 


More information about the pypy-svn mailing list