[pypy-svn] r79822 - in pypy/branch/cpyext-darwin/pypy/module/cpyext: . test

danchr at codespeak.net danchr at codespeak.net
Sat Dec 4 20:51:07 CET 2010


Author: danchr
Date: Sat Dec  4 20:51:05 2010
New Revision: 79822

Modified:
   pypy/branch/cpyext-darwin/pypy/module/cpyext/pyerrors.py
   pypy/branch/cpyext-darwin/pypy/module/cpyext/stubs.py
   pypy/branch/cpyext-darwin/pypy/module/cpyext/test/test_pyerrors.py
Log:
Implement PyErr_SetFromErrnoWithFilename()

Modified: pypy/branch/cpyext-darwin/pypy/module/cpyext/pyerrors.py
==============================================================================
--- pypy/branch/cpyext-darwin/pypy/module/cpyext/pyerrors.py	(original)
+++ pypy/branch/cpyext-darwin/pypy/module/cpyext/pyerrors.py	Sat Dec  4 20:51:05 2010
@@ -129,10 +129,29 @@
     function around a system call can write return PyErr_SetFromErrno(type);
     when the system call returns an error.
     Return value: always NULL."""
+    PyErr_SetFromErrnoWithFilename(space, w_type,
+                                   lltype.nullptr(rffi.CCHARP.TO))
+
+ at cpython_api([PyObject, rffi.CCHARP], PyObject)
+def PyErr_SetFromErrnoWithFilename(space, w_type, llfilename):
+    """Similar to PyErr_SetFromErrno(), with the additional behavior that if
+    filename is not NULL, it is passed to the constructor of type as a third
+    parameter.  In the case of exceptions such as IOError and OSError,
+    this is used to define the filename attribute of the exception instance.
+    Return value: always NULL."""
     # XXX Doesn't actually do anything with PyErr_CheckSignals.
     errno = get_errno()
     msg = os.strerror(errno)
-    w_error = space.call_function(w_type, space.wrap(errno), space.wrap(msg))
+    if llfilename:
+        w_filename = rffi.charp2str(llfilename)
+        w_error = space.call_function(w_type,
+                                      space.wrap(errno),
+                                      space.wrap(msg),
+                                      space.wrap(w_filename))
+    else:
+        w_error = space.call_function(w_type,
+                                      space.wrap(errno),
+                                      space.wrap(msg))
     raise OperationError(w_type, w_error)
 
 @cpython_api([], rffi.INT_real, error=-1)

Modified: pypy/branch/cpyext-darwin/pypy/module/cpyext/stubs.py
==============================================================================
--- pypy/branch/cpyext-darwin/pypy/module/cpyext/stubs.py	(original)
+++ pypy/branch/cpyext-darwin/pypy/module/cpyext/stubs.py	Sat Dec  4 20:51:05 2010
@@ -668,15 +668,6 @@
     """
     raise NotImplementedError
 
- at cpython_api([PyObject, rffi.CCHARP], PyObject)
-def PyErr_SetFromErrnoWithFilename(space, type, filename):
-    """Similar to PyErr_SetFromErrno(), with the additional behavior that if
-    filename is not NULL, it is passed to the constructor of type as a third
-    parameter.  In the case of exceptions such as IOError and OSError,
-    this is used to define the filename attribute of the exception instance.
-    Return value: always NULL."""
-    raise NotImplementedError
-
 @cpython_api([rffi.INT_real], PyObject)
 def PyErr_SetFromWindowsErr(space, ierr):
     """This is a convenience function to raise WindowsError. If called with

Modified: pypy/branch/cpyext-darwin/pypy/module/cpyext/test/test_pyerrors.py
==============================================================================
--- pypy/branch/cpyext-darwin/pypy/module/cpyext/test/test_pyerrors.py	(original)
+++ pypy/branch/cpyext-darwin/pypy/module/cpyext/test/test_pyerrors.py	Sat Dec  4 20:51:05 2010
@@ -185,3 +185,26 @@
         except OSError, e:
             assert e.errno == errno.EBADF
             assert e.strerror == os.strerror(errno.EBADF)
+            assert e.filename == None
+
+    def test_SetFromErrnoWithFilename(self):
+        import sys
+        if sys.platform != 'win32':
+            skip("callbacks through ll2ctypes modify errno")
+        import errno, os
+
+        module = self.import_extension('foo', [
+                ("set_from_errno", "METH_NOARGS",
+                 '''
+                 errno = EBADF;
+                 PyErr_SetFromErrnoWithFilename(PyExc_OSError, "blyf");
+                 return NULL;
+                 '''),
+                ],
+                prologue="#include <errno.h>")
+        try:
+            module.set_from_errno()
+        except OSError, e:
+            assert e.filename == "blyf"
+            assert e.errno == errno.EBADF
+            assert e.strerror == os.strerror(errno.EBADF)



More information about the Pypy-commit mailing list