[pypy-svn] r33217 - pypy/dist/pypy/translator/cli/src

antocuni at codespeak.net antocuni at codespeak.net
Thu Oct 12 11:34:54 CEST 2006


Author: antocuni
Date: Thu Oct 12 11:34:52 2006
New Revision: 33217

Modified:
   pypy/dist/pypy/translator/cli/src/ll_os.cs
Log:
- be more robust against various I/O errors that can occours

- after an OSError has been raised, the *first* (and only that) call
  to ll_os_strerror will return an informative error message, instead of
  the old "error XXX"



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	Thu Oct 12 11:34:52 2006
@@ -143,6 +143,7 @@
     {
         private static Dictionary<int, IFile> FileDescriptors;
         private static int fdcount;
+        private static Dictionary<int, string> ErrorMessages;
         
         // NB: these values are those used by Windows and they differs
         // from the Unix ones; the os module is patched with these
@@ -163,6 +164,7 @@
 
         static ll_os()
         {
+            ErrorMessages = new Dictionary<int, string>();
             FileDescriptors = new Dictionary<int, IFile>();
             // XXX: what about CRLF conversion for stdin, stdout and stderr?
             // It seems that Posix let you read from stdout and
@@ -173,6 +175,23 @@
             fdcount = 2; // 0, 1 and 2 are already used by stdin, stdout and stderr
         }
 
+        private static void raise_OSError(int errno, string msg)
+        {
+            ErrorMessages[errno] = msg;
+            Helpers.raise_OSError(errno);
+        }
+
+        public static string ll_os_strerror(int errno)
+        {
+            string msg = ErrorMessages[errno];
+            if (msg != null) {
+                ErrorMessages.Remove(errno);
+                return msg;
+            }
+            else
+                return "error " + errno;
+        }
+
         public static string ll_os_getcwd()
         {
             return System.IO.Directory.GetCurrentDirectory();
@@ -181,7 +200,9 @@
         private static IFile getfd(int fd)
         {
             IFile f = FileDescriptors[fd];
-            Debug.Assert(f != null, string.Format("Invalid file descriptor: {0}", fd));
+            if (f == null)
+                raise_OSError(Errno.EBADF, string.Format("Invalid file descriptor: {0}", fd));
+
             return f;
         }
 
@@ -210,7 +231,11 @@
                 stream = new FileStream(name, f_mode, f_access, FileShare.ReadWrite);
             }
             catch(FileNotFoundException e) {
-                Helpers.raise_OSError(Errno.ENOENT);
+                raise_OSError(Errno.ENOENT, e.Message);
+                return -1;
+            }
+            catch(IOException e) {
+                raise_OSError(Errno.EIO, e.Message);
                 return -1;
             }
 
@@ -266,7 +291,7 @@
         public static Record_Stat_Result ll_os_stat(string path)
         {
             if (path == "")
-                Helpers.raise_OSError(Errno.ENOENT);
+                raise_OSError(Errno.ENOENT, "No such file or directory: ''");
 
             FileInfo f = new FileInfo(path);
             if (f.Exists) {
@@ -285,7 +310,7 @@
                 return res;
             }
             // path is not a file nor a dir, raise OSError
-            Helpers.raise_OSError(Errno.ENOENT);
+            raise_OSError(Errno.ENOENT, string.Format("No such file or directory: '{0}'", path));
             return null; // never reached
         }
 
@@ -329,11 +354,6 @@
             return stream.Seek(offset, origin);
         }
 
-        public static string ll_os_strerror(int errno)
-        {
-            return "error " + errno;     // TODO
-        }
-
         public static void ll_os__exit(int x)
         {
             Helpers.raise_OSError(Errno.EPERM); // this is only a stub



More information about the Pypy-commit mailing list