[pypy-svn] r18830 - in pypy/dist/pypy: module/posix module/posix/test translator/c/src translator/c/test

arigo at codespeak.net arigo at codespeak.net
Fri Oct 21 19:51:09 CEST 2005


Author: arigo
Date: Fri Oct 21 19:51:05 2005
New Revision: 18830

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
   pypy/dist/pypy/translator/c/src/ll_os.h
   pypy/dist/pypy/translator/c/test/test_extfunc.py
Log:
Exported os.strerror(), which was already mostly there at RPython-level.


Modified: pypy/dist/pypy/module/posix/__init__.py
==============================================================================
--- pypy/dist/pypy/module/posix/__init__.py	(original)
+++ pypy/dist/pypy/module/posix/__init__.py	Fri Oct 21 19:51:05 2005
@@ -37,6 +37,7 @@
     'rmdir'     : 'interp_posix.rmdir',
     'environ'   : 'interp_posix.get(space).w_environ',
     'listdir'   : 'interp_posix.listdir',
+    'strerror'  : 'interp_posix.strerror',
     }
     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	Fri Oct 21 19:51:05 2005
@@ -8,8 +8,11 @@
 
 def wrap_oserror(space, e): 
     assert isinstance(e, OSError) 
-    errno = e.errno 
-    msg = os.strerror(errno) 
+    errno = e.errno
+    try:
+        msg = os.strerror(errno)
+    except ValueError:
+        msg = 'error %d' % errno
     w_error = space.call_function(space.w_OSError,
                                   space.wrap(errno),
                                   space.wrap(msg))
@@ -180,6 +183,16 @@
         raise wrap_oserror(space, e) 
 rmdir.unwrap_spec = [ObjSpace, str]
 
+def strerror(space, errno):
+    'strerror(code) -> string\n\nTranslate an error code to a message string.'
+    try:
+        text = os.strerror(errno)
+    except ValueError:
+        raise OperationError(space.w_ValueError,
+                             space.wrap("strerror() argument out of range"))
+    return space.wrap(text)
+strerror.unwrap_spec = [ObjSpace, int]
+
 
 # this is a particular case, because we need to supply
 # the storage for the environment variables, at least
@@ -245,6 +258,14 @@
     return space.newlist(result)
 
 def listdir(space, dirname):
+    """listdir(path) -> list_of_strings
+
+Return a list containing the names of the entries in the directory.
+
+\tpath: path of directory to list
+
+The list is in arbitrary order.  It does not include the special
+entries '.' and '..' even if they are present in the directory."""
     dir = ros.opendir(dirname)
     try:
         # sub-function call to make sure that 'try:finally:' will catch

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	Fri Oct 21 19:51:05 2005
@@ -90,6 +90,10 @@
                           'file1',
                           'file2']
 
+    def test_strerror(self):
+        assert isinstance(self.posix.strerror(0), str)
+        assert isinstance(self.posix.strerror(1), str)
+
 class AppTestEnvironment(object):
     def setup_class(cls): 
         cls.space = space 

Modified: pypy/dist/pypy/translator/c/src/ll_os.h
==============================================================================
--- pypy/dist/pypy/translator/c/src/ll_os.h	(original)
+++ pypy/dist/pypy/translator/c/src/ll_os.h	Fri Oct 21 19:51:05 2005
@@ -185,6 +185,11 @@
 RPyString *LL_os_strerror(int errnum) {
 	char *res;
 	res = strerror(errnum);
+	if (res == NULL) {
+		RPyRaiseSimpleException(PyExc_ValueError,
+					"strerror() argument out of range");
+		return NULL;
+	}
 	return RPyString_FromString(res);
 }
 

Modified: pypy/dist/pypy/translator/c/test/test_extfunc.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_extfunc.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_extfunc.py	Fri Oct 21 19:51:05 2005
@@ -435,6 +435,14 @@
     f1(dirname, True)
     assert not os.path.exists(dirname)
 
+def test_strerror():
+    def does_stuff(n):
+        return os.strerror(n)
+    f1 = compile(does_stuff, [int])
+    for i in range(4):
+        res = f1(i)
+        assert res == os.strerror(i)
+
 # ____________________________________________________________
 
 def _real_getenv(var):



More information about the Pypy-commit mailing list