This is an automated email from the git hooks/post-receive script.
nsoffer pushed a commit to branch master in repository sanlock.
commit 89698171cf5555244f42851e2dd529cb848fb393 Author: Amit Bawer abawer@redhat.com AuthorDate: Sun Jun 2 14:56:05 2019 +0300
python: Use PyBytes converter for init_resource API
This API was deprecated by write_resource but we want the lockspace/resource name as bytes change to be uniform so we modify it as well for sake of old use cases.
Also added stub test for arguments parsing.
Implications:
- Py2 : no difference - Py3 : if used, only accept bytes for lockspace/resource name. --- python/sanlock.c | 31 ++++++++++++++++--------------- tests/python_test.py | 9 +++++++++ 2 files changed, 25 insertions(+), 15 deletions(-)
diff --git a/python/sanlock.c b/python/sanlock.c index f73c507..3d0bfb0 100644 --- a/python/sanlock.c +++ b/python/sanlock.c @@ -406,29 +406,29 @@ The disks must be in the format: [(path, offset), ... ]"); static PyObject * py_init_resource(PyObject *self __unused, PyObject *args, PyObject *keywds) { - int rv, max_hosts = 0, num_hosts = 0, use_aio = 1; - const char *lockspace, *resource; - struct sanlk_resource *res; + int rv = -1, max_hosts = 0, num_hosts = 0, use_aio = 1; + PyObject *lockspace = NULL, *resource = NULL; + struct sanlk_resource *res = NULL; PyObject *disks;
static char *kwlist[] = {"lockspace", "resource", "disks", "max_hosts", "num_hosts", "use_aio", NULL};
/* parse python tuple */ - if (!PyArg_ParseTupleAndKeywords(args, keywds, "ssO!|iii", - kwlist, &lockspace, &resource, &PyList_Type, &disks, &max_hosts, - &num_hosts, &use_aio)) { - return NULL; + if (!PyArg_ParseTupleAndKeywords(args, keywds, "O&O&O!|iii", + kwlist, convert_to_pybytes, &lockspace, convert_to_pybytes, &resource, + &PyList_Type, &disks, &max_hosts, &num_hosts, &use_aio)) { + goto finally; }
/* parse and check sanlock resource */ if (__parse_resource(disks, &res) < 0) { - return NULL; + goto finally; }
/* prepare sanlock names */ - strncpy(res->lockspace_name, lockspace, SANLK_NAME_LEN); - strncpy(res->name, resource, SANLK_NAME_LEN); + strncpy(res->lockspace_name, PyBytes_AsString(lockspace), SANLK_NAME_LEN); + strncpy(res->name, PyBytes_AsString(resource), SANLK_NAME_LEN);
/* init sanlock resource (gil disabled) */ Py_BEGIN_ALLOW_THREADS @@ -437,15 +437,16 @@ py_init_resource(PyObject *self __unused, PyObject *args, PyObject *keywds)
if (rv != 0) { __set_exception(rv, "Sanlock resource init failure"); - goto exit_fail; + goto finally; }
+finally: + Py_XDECREF(lockspace); + Py_XDECREF(resource); free(res); + if (rv != 0) + return NULL; Py_RETURN_NONE; - -exit_fail: - free(res); - return NULL; }
/* write_lockspace */ diff --git a/tests/python_test.py b/tests/python_test.py index 0d97cb9..7406bcb 100644 --- a/tests/python_test.py +++ b/tests/python_test.py @@ -610,3 +610,12 @@ def test_end_event_parse_args(no_sanlock_daemon, name): def test_init_lockspace_parse_args(no_sanlock_daemon, name): with raises_sanlock_errno(errno.ENODEV): sanlock.init_lockspace(name, "path") + + +@pytest.mark.parametrize("name", LOCKSPACE_OR_RESOURCE_NAMES) +def test_init_resource_parse_args(no_sanlock_daemon, name): + disks = [("path", 0)] + with raises_sanlock_errno(errno.ENOENT): + sanlock.init_resource(b"ls_name", name, disks) + with raises_sanlock_errno(errno.ENOENT): + sanlock.init_resource(name, b"res_name", disks)
sanlock-devel@lists.fedorahosted.org