From 47493a0fc86ea0798a68cd644058fa345fa6861c Mon Sep 17 00:00:00 2001 From: Kota Tsuyuzaki Date: Thu, 1 Dec 2016 21:47:51 -0800 Subject: [PATCH] Change the version reference Now, if liberasurecode.so exposes a liberasurecode_get_version function, we'll use that; otherwise, we'll fall back to reporting the version that PyECLib was built against. Note that if your liberasurecode.so doesn't support the liberasurecode_get_verion function, pyeclib will the version at built as well as older behavior. Co-Authored-By: Tim Burke Change-Id: I54823183cce6775a83e913baf6bb1eeb94aabc13 --- src/c/pyeclib_c/pyeclib_c.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/c/pyeclib_c/pyeclib_c.c b/src/c/pyeclib_c/pyeclib_c.c index 8e1a23c..5ba5770 100644 --- a/src/c/pyeclib_c/pyeclib_c.c +++ b/src/c/pyeclib_c/pyeclib_c.c @@ -1213,7 +1213,32 @@ pyeclib_c_check_backend_available(PyObject *self, PyObject *args) static PyObject* pyeclib_c_liberasurecode_version(PyObject *self, PyObject *args) { - return PyInt_FromLong(LIBERASURECODE_VERSION); + void *hLib; + char *err; + uint32_t (*hGetVersion)(void); + + dlerror(); + hLib = dlopen("liberasurecode.so", RTLD_LAZY); + /* It's important that we clear the last error before calling dysym */ + err = dlerror(); + if (err) { + /* This should never actually get hit; since we're using various + symbols already, liberasurecode.so should *already* be loaded. */ + return PyInt_FromLong(LIBERASURECODE_VERSION); + } + + hGetVersion = dlsym(hLib, "liberasurecode_get_version"); + err = dlerror(); + if (err) { + /* This is the important bit. Old version, doesn't have get_version + support; fall back to old behavior. */ + dlclose(hLib); + return PyInt_FromLong(LIBERASURECODE_VERSION); + } + + uint32_t version = (*hGetVersion)(); + dlclose(hLib); + return Py_BuildValue("k", version); } static PyMethodDef PyECLibMethods[] = {