From 76bb5a0ba8126952a5a3cf7338639d7fddb274c7 Mon Sep 17 00:00:00 2001 From: Iustin Pop Date: Wed, 11 Dec 2019 21:18:46 +0100 Subject: [PATCH] Add file path to error message on ACL initialisation --- acl.c | 17 ++++++++++++++++- tests/test_acls.py | 2 ++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/acl.c b/acl.c index c280967..f96b679 100644 --- a/acl.c +++ b/acl.c @@ -155,6 +155,7 @@ static int ACL_init(PyObject* obj, PyObject* args, PyObject *keywds) { ACL_Object* thesrc = NULL; const void *buf = NULL; Py_ssize_t bufsize; + int set_err = 0; if(!PyTuple_Check(args) || PyTuple_Size(args) != 0 || (keywds != NULL && PyDict_Check(keywds) && PyDict_Size(keywds) > 1)) { @@ -179,6 +180,12 @@ static int ACL_init(PyObject* obj, PyObject* args, PyObject *keywds) { fprintf(stderr, "foobar!\n"); char *path = PyBytes_AS_STRING(file); new = acl_get_file(path, ACL_TYPE_ACCESS); + // Set custom exception on this failure path which includes + // the filename. + if (new == NULL) { + PyErr_SetFromErrnoWithFilename(PyExc_IOError, path); + set_err = 1; + } Py_DECREF(file); } else if(text != NULL) new = acl_from_text(text); @@ -192,6 +199,12 @@ static int ACL_init(PyObject* obj, PyObject* args, PyObject *keywds) { else if(filedef != NULL) { char *path = PyBytes_AS_STRING(filedef); new = acl_get_file(path, ACL_TYPE_DEFAULT); + // Set custom exception on this failure path which includes + // the filename. + if (new == NULL) { + PyErr_SetFromErrnoWithFilename(PyExc_IOError, path); + set_err = 1; + } Py_DECREF(path); } #ifdef HAVE_LINUX @@ -207,7 +220,9 @@ static int ACL_init(PyObject* obj, PyObject* args, PyObject *keywds) { new = acl_init(0); if(new == NULL) { - PyErr_SetFromErrno(PyExc_IOError); + if (!set_err) { + PyErr_SetFromErrno(PyExc_IOError); + } return -1; } diff --git a/tests/test_acls.py b/tests/test_acls.py index cc9a2e2..55ba9e1 100644 --- a/tests/test_acls.py +++ b/tests/test_acls.py @@ -256,6 +256,8 @@ class TestLoad: _, fname = get_file(testdir) with pytest.raises(IOError): posix1e.ACL(file="fname"+".no-such-file") + with pytest.raises(IOError): + posix1e.ACL(filedef="fname"+".no-such-file") def test_from_invalid_fd(self, testdir): fd, _ = get_file(testdir) -- 2.39.5