Author: Marc Abramowitz <[email protected]>
Branch: add_PyErr_SetFromErrnoWithFilenameObject_try_2
Changeset: r70224:0bc8a372d4b2
Date: 2014-03-23 18:12 -0700
http://bitbucket.org/pypy/pypy/changeset/0bc8a372d4b2/

Log:    Add PyErr_SetFromErrnoWithFilenameObject to cpyext

diff --git a/pypy/module/cpyext/pyerrors.py b/pypy/module/cpyext/pyerrors.py
--- a/pypy/module/cpyext/pyerrors.py
+++ b/pypy/module/cpyext/pyerrors.py
@@ -164,6 +164,17 @@
                                       space.wrap(msg))
     raise OperationError(w_type, w_error)
 
+@cpython_api([PyObject, PyObject], PyObject)
+def PyErr_SetFromErrnoWithFilenameObject(space, w_type, filename_object):
+    """Similar to PyErr_SetFromErrno(), with the additional behavior that if
+    filename_object 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."""
+    from pypy.module.cpyext.stringobject import PyString_AsString
+    PyErr_SetFromErrnoWithFilename(space, w_type,
+                                   PyString_AsString(space, filename_object))
+
 @cpython_api([], rffi.INT_real, error=-1)
 def PyErr_CheckSignals(space):
     """
diff --git a/pypy/module/cpyext/test/test_pyerrors.py 
b/pypy/module/cpyext/test/test_pyerrors.py
--- a/pypy/module/cpyext/test/test_pyerrors.py
+++ b/pypy/module/cpyext/test/test_pyerrors.py
@@ -215,6 +215,25 @@
         assert exc_info.value.errno == errno.EBADF
         assert exc_info.value.strerror == os.strerror(errno.EBADF)
 
+    def test_SetFromErrnoWithFilenameObject(self):
+        import errno, os
+
+        module = self.import_extension('foo', [
+                ("set_from_errno", "METH_NOARGS",
+                 '''
+                 errno = EBADF;
+                 PyObject *filenameObject = 
PyString_FromString("/path/to/file");
+                 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, 
filenameObject);
+                 Py_DECREF(filenameObject);
+                 return NULL;
+                 '''),
+                ],
+                prologue="#include <errno.h>")
+        exc_info = raises(OSError, module.set_from_errno)
+        assert exc_info.value.filename == "/path/to/file"
+        assert exc_info.value.errno == errno.EBADF
+        assert exc_info.value.strerror == os.strerror(errno.EBADF)
+
     def test_PyErr_Display(self):
         module = self.import_extension('foo', [
             ("display_error", "METH_VARARGS",
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to