From 5453f0795d688fb6a8aa0b62a3c1d3815807b72c Mon Sep 17 00:00:00 2001 From: Amit Bawer Date: Jun 05 2019 13:12:40 +0000 Subject: python: Apply pypath_converter to parse_resource util function We should be able to parse resource paths as either unicode or bytes. Stub tests involving valid arguments parsing of resources paths are set with additional permutations with no xfails expected. --- diff --git a/python/sanlock.c b/python/sanlock.c index aaeaa86..1a983ee 100644 --- a/python/sanlock.c +++ b/python/sanlock.c @@ -160,6 +160,33 @@ pystring_as_cstring(PyObject *obj) } static int +parse_single_disk(PyObject* disk, struct sanlk_disk* res_disk) +{ + int rv = 0; + PyObject *path = NULL; + uint64_t offset; + + if (!PyTuple_Check(disk)) { + set_error(PyExc_ValueError, "Invalid disk %s", disk); + goto finally; + } + + if (!PyArg_ParseTuple(disk, "O&K", pypath_converter, &path, &offset)) { + /* Override the error since it confusing in this context. */ + set_error(PyExc_ValueError, "Invalid disk %s", disk); + goto finally; + } + + strncpy(res_disk->path, PyBytes_AsString(path), SANLK_PATH_LEN - 1); + res_disk->offset = offset; + rv = 1; + +finally: + Py_XDECREF(path); + return rv; +} + +static int __parse_resource(PyObject *obj, struct sanlk_resource **res_ret) { int i, num_disks, res_len; @@ -180,26 +207,11 @@ __parse_resource(PyObject *obj, struct sanlk_resource **res_ret) res->num_disks = num_disks; for (i = 0; i < num_disks; i++) { - PyObject *disk; - const char *path; - uint64_t offset; + PyObject *disk = PyList_GetItem(obj,i); - disk = PyList_GetItem(obj, i); - - if (!PyTuple_Check(disk)) { - set_error(PyExc_ValueError, "Invalid disk %s", disk); + if (!parse_single_disk(disk, &(res->disks[i]))) { goto exit_fail; - } - - if (!PyArg_ParseTuple(disk, "sK", &path, &offset)) { - /* Override the error since it confusing in this context. */ - set_error(PyExc_ValueError, "Invalid disk %s", disk); - goto exit_fail; - } - - strncpy(res->disks[i].path, path, SANLK_PATH_LEN - 1); - res->disks[i].offset = offset; } *res_ret = res; diff --git a/tests/python_test.py b/tests/python_test.py index 5218435..56d0c99 100644 --- a/tests/python_test.py +++ b/tests/python_test.py @@ -51,6 +51,14 @@ FILE_NAMES = [ reason="currently not supporting bytes paths")), ] +FILE_NAMES_NO_XFAILS = [ + #name, encoding + ("ascii", None), + (u"ascii", None), + (u"\u05d0", None), + (u"\u05d0", "utf-8"), +] + LOCKSPACE_OR_RESOURCE_NAMES = [ # Bytes are supported with python 2 and 3. pytest.param(b"\xd7\x90"), @@ -543,7 +551,7 @@ def test_write_lockspace_parse_args(no_sanlock_daemon, name, filename, encoding) @pytest.mark.parametrize("name", LOCKSPACE_OR_RESOURCE_NAMES) -@pytest.mark.parametrize("filename,encoding", FILE_NAMES) +@pytest.mark.parametrize("filename,encoding", FILE_NAMES_NO_XFAILS) def test_write_resource_parse_args(no_sanlock_daemon, name, filename, encoding): path = util.generate_path("/tmp/", filename, encoding) disks = [(path, 0)] @@ -555,7 +563,7 @@ def test_write_resource_parse_args(no_sanlock_daemon, name, filename, encoding): @pytest.mark.parametrize("name", LOCKSPACE_OR_RESOURCE_NAMES) -@pytest.mark.parametrize("filename,encoding", FILE_NAMES) +@pytest.mark.parametrize("filename,encoding", FILE_NAMES_NO_XFAILS) def test_release_resource_parse_args(no_sanlock_daemon, name, filename, encoding): path = util.generate_path("/tmp/", filename, encoding) disks = [(path, 0)] @@ -567,7 +575,7 @@ def test_release_resource_parse_args(no_sanlock_daemon, name, filename, encoding @pytest.mark.parametrize("name", LOCKSPACE_OR_RESOURCE_NAMES) -@pytest.mark.parametrize("filename,encoding", FILE_NAMES) +@pytest.mark.parametrize("filename,encoding", FILE_NAMES_NO_XFAILS) def test_read_resource_owners_parse_args(no_sanlock_daemon, name, filename, encoding): path = util.generate_path("/tmp/", filename, encoding) disks = [(path, 0)] @@ -619,7 +627,7 @@ def test_init_lockspace_parse_args(no_sanlock_daemon, name, filename, encoding): @pytest.mark.parametrize("name", LOCKSPACE_OR_RESOURCE_NAMES) -@pytest.mark.parametrize("filename,encoding", FILE_NAMES) +@pytest.mark.parametrize("filename,encoding", FILE_NAMES_NO_XFAILS) def test_init_resource_parse_args(no_sanlock_daemon, name, filename, encoding): path = util.generate_path("/tmp/", filename, encoding) disks = [(path, 0)]