From 0a8fb18781fcf221600b12b0d7bba0b4007dcec0 Mon Sep 17 00:00:00 2001 From: dangtrungtin Date: Thu, 1 Jul 2021 14:35:07 +0700 Subject: [PATCH 1/4] Support for Swig 4.0.2 and Python 3.9 - Update typemap code for support Swig 4.0.2 and Python 3.9 --- src/gstype_python.i | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/gstype_python.i b/src/gstype_python.i index 79a857a..086b18f 100755 --- a/src/gstype_python.i +++ b/src/gstype_python.i @@ -136,7 +136,7 @@ char* convertObjToStr(PyObject *string) { %#if PY_MAJOR_VERSION < 3 return PyString_AsString(string); %#else - return PyUnicode_AsUTF8(string); + return (char*) PyUnicode_AsUTF8(string); %#endif } } @@ -2265,12 +2265,14 @@ static bool getRowFields(GSRow* row, int columnCount, GSType* typeList, bool tim %attribute(griddb::ExpirationInfo, int, division_count, get_division_count, set_division_count); //Attribute ContainerInfo::columnInfoList +#if SWIG_VERSION < 0x040000 %extend griddb::ContainerInfo{ %pythoncode %{ __swig_getmethods__["column_info_list"] = get_column_info_list __swig_setmethods__["column_info_list"] = set_column_info_list %} }; +#endif /** * Typemap for Container::multi_put From a844e6bf22811fffde6893d40dbf8ad91d5f51ce Mon Sep 17 00:00:00 2001 From: dangtrungtin Date: Wed, 14 Jul 2021 15:26:08 +0700 Subject: [PATCH 2/4] Fix error with ContainerInfo after version up - Correct error : missing attribute ContainerInfo.column_info_list after update to SWIG 4.0.2 - Update python to 3.9.5 in Makefile - Update CmakeLists.txt to build in Windows --- CMakeLists.txt | 13 +++++--- Makefile | 4 +-- src/gstype_python.i | 77 ++++++++++++++++++++++----------------------- 3 files changed, 47 insertions(+), 47 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ed6447c..a32da27 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,9 +4,10 @@ project(griddb_python) set(C_LIB E:/c_client-master/c_client-master/bin/x64/Release) set(C_HEADER E:/c_client-master/c_client-master/client/c/include) -set(SOURCE_HEADER E:/cmake-fun/SourceCode/src) -set(PYTHON_RESOURCE C:/Users/griddb/AppData/Local/Programs/Python/Python36/) -set(SWIG_RESOURCE E:/swigwin-3.0.12) +set(SOURCE_HEADER E:/griddb/python_client/SourceCode/src) +set(PYTHON_RESOURCE C:/Users/griddb/AppData/Local/Programs/Python/Python39/) +set(NUMPY_RESOURCE C:/Users/griddb/AppData/Local/Programs/Python/Python39/Lib/site-packages/numpy/core/include) +set(SWIG_RESOURCE E:/swigwin-4.0.2) set(PYTHON_LIBRARIES ${PYTHON_RESOURCE}/libs) set(PYTHON_INCLUDE_DIRS ${PYTHON_RESOURCE}/include) @@ -23,8 +24,8 @@ include(${SWIG_USE_FILE}) set(CMAKE_SWIG_FLAGS -c++) set(CMAKE_CXX_FLAGS "/DLL /LD /D_USRDLL /D_WINDLL /MT /MD") -include_directories(${C_HEADER} ${PYTHON_RESOURCE}/include ${SOURCE_HEADER}) -link_directories(${PYTHON_RESOURCE}/libs ${C_LIB}) +include_directories(${C_HEADER} ${PYTHON_RESOURCE}/include ${SOURCE_HEADER} ${NUMPY_RESOURCE}) +link_directories(${PYTHON_RESOURCE}/libs ${C_LIB} ${NUMPY_RESOURCE}) set_property(SOURCE ${SOURCE_HEADER}/griddb.i PROPERTY CPLUSPLUS ON) swig_add_library(griddb_python TYPE SHARED LANGUAGE python SOURCES ${SOURCE_HEADER}/griddb.i @@ -41,6 +42,8 @@ ${SOURCE_HEADER}/RowKeyPredicate.cpp ${SOURCE_HEADER}/RowSet.cpp ${SOURCE_HEADER}/TimestampUtils.cpp ${SOURCE_HEADER}/Field.cpp +${SOURCE_HEADER}/RowList.cpp +${SOURCE_HEADER}/RowSet.cpp ${SOURCE_HEADER}/Util.cpp) target_link_libraries(griddb_python gridstore_c) diff --git a/Makefile b/Makefile index 86a53dc..c654fec 100755 --- a/Makefile +++ b/Makefile @@ -9,8 +9,8 @@ CPPFLAGS = -fPIC -std=c++0x -g -O2 INCLUDES = -Iinclude -Isrc INCLUDES_PYTHON = $(INCLUDES) \ - -I/usr/include/python3.6 \ - -I$(HOME)/.pyenv/versions/3.6.4/lib/python3.6/site-packages/numpy/core/include + -I/usr/include/python3.9/ \ + -I$(HOME)/.pyenv/versions/3.9.5/lib/python3.9/site-packages/numpy/core/include PROGRAM = _griddb_python.so EXTRA = griddb_python.py griddb_python.pyc diff --git a/src/gstype_python.i b/src/gstype_python.i index 086b18f..739d0af 100755 --- a/src/gstype_python.i +++ b/src/gstype_python.i @@ -2265,14 +2265,7 @@ static bool getRowFields(GSRow* row, int columnCount, GSType* typeList, bool tim %attribute(griddb::ExpirationInfo, int, division_count, get_division_count, set_division_count); //Attribute ContainerInfo::columnInfoList -#if SWIG_VERSION < 0x040000 -%extend griddb::ContainerInfo{ - %pythoncode %{ - __swig_getmethods__["column_info_list"] = get_column_info_list - __swig_setmethods__["column_info_list"] = set_column_info_list - %} -}; -#endif +%attributeval(griddb::ContainerInfo, ColumnInfoList, column_info_list, get_column_info_list, set_column_info_list); /** * Typemap for Container::multi_put @@ -2337,32 +2330,29 @@ static bool getRowFields(GSRow* row, int columnCount, GSType* typeList, bool tim %typemap(doc, name = "row_list") (GSRow** listRowdata, int rowCount) "list[list[object]]"; //attribute ContainerInfo::column_info_list -%typemap(in, fragment = "SWIG_AsCharPtrAndSize", fragment = "cleanString") (ColumnInfoList columnInfoList) (int* alloc = NULL){ +%typemap(in, fragment = "SWIG_AsCharPtrAndSize", fragment = "cleanString") (ColumnInfoList*) (int* alloc = NULL){ if (!PyList_Check($input)) { PyErr_SetString(PyExc_ValueError, "Expected a List"); SWIG_fail; } - int res; - char* v = 0; - bool vbool; - size_t size = 0; - size = (size_t)PyInt_AsLong(PyLong_FromSsize_t(PyList_Size($input))); - $1.columnInfo = NULL; - $1.size = size; - size_t stringSize = 0; + + $1 = NULL; + size_t size = (size_t)PyInt_AsLong(PyLong_FromSsize_t(PyList_Size($input))); if (size) { try { - alloc = new int[size](); - $1.columnInfo = new GSColumnInfo[size](); + $1 = new ColumnInfoList(); + alloc = new int[size](); + $1->columnInfo = new GSColumnInfo[size](); } catch (bad_alloc& ba) { PyErr_SetString(PyExc_ValueError, "Memory allocation for set column_info_list is error"); SWIG_fail; } + $1->size = size; PyObject* columInfoList; int option; for (int i = 0; i < size; i++) { - $1.columnInfo[i] = GS_COLUMN_INFO_INITIALIZER; + $1->columnInfo[i] = GS_COLUMN_INFO_INITIALIZER; columInfoList = PyList_GetItem($input, i); if (!PyList_Check(columInfoList)) { PyErr_SetString(PyExc_ValueError, "Expected a List"); @@ -2371,55 +2361,62 @@ static bool getRowFields(GSRow* row, int columnCount, GSType* typeList, bool tim size_t sizeColumn = (size_t)PyInt_AsLong(PyLong_FromSsize_t(PyList_Size(columInfoList))); if (sizeColumn == 3) { option = PyInt_AsLong(PyList_GetItem(columInfoList, 2)); - $1.columnInfo[i].options = option; + $1->columnInfo[i].options = option; if (option != GS_TYPE_OPTION_NULLABLE && option != GS_TYPE_OPTION_NOT_NULL) { PyErr_SetString(PyExc_ValueError, "Invalid value for column option"); SWIG_fail; } } else if (sizeColumn == 2) { - if (i == 0) { - $1.columnInfo[i].options = GS_TYPE_OPTION_NOT_NULL; - } else { - $1.columnInfo[i].options = GS_TYPE_OPTION_NULLABLE; - } + if (i == 0) { + $1->columnInfo[i].options = GS_TYPE_OPTION_NOT_NULL; + } else { + $1->columnInfo[i].options = GS_TYPE_OPTION_NULLABLE; + } } else { PyErr_SetString(PyExc_ValueError, "Invalid element number for List"); SWIG_fail; } + char* v = 0; + size_t stringSize = 0; + int res; res = SWIG_AsCharPtrAndSize(PyList_GetItem(columInfoList, 0), &v, &stringSize, &alloc[i]); if (!SWIG_IsOK(res)) { PyErr_SetString(PyExc_ValueError, "Can't convert field to string"); SWIG_fail; } - $1.columnInfo[i].name = v; - $1.columnInfo[i].type = PyInt_AsLong(PyList_GetItem(columInfoList, 1)); + $1->columnInfo[i].name = v; + $1->columnInfo[i].type = PyInt_AsLong(PyList_GetItem(columInfoList, 1)); } } } -%typemap(freearg, fragment = "cleanString") (ColumnInfoList columnInfoList) { +%typemap(freearg, fragment = "cleanString") (ColumnInfoList*) { size_t size = 0; - if ($1.size) { - size = $1.size; - } - if ($1.columnInfo != NULL) { - if (alloc$argnum) { - for (int i = 0; i < size; i++) { - if ($1.columnInfo[i].name) { - cleanString($1.columnInfo[i].name, alloc$argnum[i]); + if ($1) { + if ($1->size) { + size = $1->size; + } + if ($1->columnInfo != NULL) { + if (alloc$argnum) { + for (int i = 0; i < size; i++) { + if ($1->columnInfo[i].name) { + cleanString($1->columnInfo[i].name, alloc$argnum[i]); + } } } + delete [] $1->columnInfo; } - delete [] $1.columnInfo; + + delete $1; } if (alloc$argnum) { delete [] alloc$argnum; } } -%typemap(out, fragment = "convertStrToObj") (ColumnInfoList) { - ColumnInfoList data = $1; +%typemap(out, fragment = "convertStrToObj") (ColumnInfoList*) { + ColumnInfoList data = *$1; size_t size = data.size; $result = PyList_New(size); if ($result == NULL) { From 431566c6d53a10c83267257545a34aeac1022f9f Mon Sep 17 00:00:00 2001 From: dangtrungtin Date: Wed, 14 Jul 2021 15:31:11 +0700 Subject: [PATCH 3/4] Update README.md after version up - Change from SWIG 3.0.12 to 4.0.2 - Update install SWIG manual - Change from Python 3.6 to 3.9 --- README.md | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index f0f06d8..a513d30 100644 --- a/README.md +++ b/README.md @@ -9,26 +9,26 @@ GridDB Python Client is developed using GridDB C Client and [SWIG](http://www.sw Building of the library and execution of the sample programs have been checked in the following environment. OS: CentOS 7.6(x64) (GCC 4.8.5) - SWIG: 3.0.12 - Python: 3.6 + SWIG: 4.0.2 + Python: 3.9 GridDB C client: V4.5 CE(Community Edition) GridDB server: V4.5 CE, CentOS 7.6(x64) (GCC 4.8.5) OS: Ubuntu 18.04(x64) (gcc 7.3.0) - SWIG: 3.0.12 - Python: 3.6 + SWIG: 4.0.2 + Python: 3.9 GridDB C client: V4.5 CE (Note: If you build from source code, please use GCC 4.8.5.) GridDB server: V4.5 CE, Ubuntu 18.04(x64) (Note: If you build from source code, please use GCC 4.8.5.) OS: Windows 10(x64) (VS2017) - SWIG: 3.0.12 - Python: 3.6 + SWIG: 4.0.2 + Python: 3.9 GridDB C client: V4.5 CE GridDB server: V4.5 CE, CentOS 7.6(x64) (GCC 4.8.5) OS: MacOS Catalina (x86_64) - SWIG: 3.0.12 - Python: 3.6.9 + SWIG: 4.0.2 + Python: 3.9 GridDB C client: V4.5 CE GridDB server: V4.5 CE, Centos 7.6(x64) (GCC 4.8.5) @@ -37,9 +37,10 @@ Building of the library and execution of the sample programs have been checked i Install SWIG as below. - $ wget https://prdownloads.sourceforge.net/swig/swig-3.0.12.tar.gz - $ tar xvfz swig-3.0.12.tar.gz - $ cd swig-3.0.12 + $ wget https://github.com/swig/swig/archive/refs/tags/v4.0.2.tar.gz + $ tar xvfz v4.0.2.tar.gz + $ cd swig-4.0.2 + $ ./autogen.sh $ ./configure $ make $ sudo make install @@ -114,9 +115,10 @@ Note: X.X.X is the software version. Install SWIG as below. - $ wget https://prdownloads.sourceforge.net/swig/swig-3.0.12.tar.gz - $ tar xvfz swig-3.0.12.tar.gz - $ cd swig-3.0.12 + $ wget https://github.com/swig/swig/archive/refs/tags/v4.0.2.tar.gz + $ tar xvfz v4.0.2.tar.gz + $ cd swig-4.0.2 + $ ./autogen.sh $ ./configure $ make $ sudo make install @@ -141,8 +143,8 @@ Modify **Makefile** to make python_client compatible with MacOS: - Change python include path and numpy include path on MacOS. For example: ```bash INCLUDES_PYTHON = $(INCLUDES) \ - -I$(HOME)/.pyenv/versions/3.6.9/include/python3.6m \ - -I$(HOME)/.pyenv/versions/3.6.9/lib/python3.6/site-packages/numpy/core/include + -I$(HOME)/.pyenv/versions/3.9.5/include/python3.9 \ + -I$(HOME)/.pyenv/versions/3.9.5/lib/python3.9/site-packages/numpy/core/include ``` - Remove "-Llibs -lrt" (not support on MacOS) from LDFLAGS on Makefile From daa0858e5ca91036ef3155f6107c66d98120149b Mon Sep 17 00:00:00 2001 From: dangtrungtin Date: Thu, 15 Jul 2021 09:59:54 +0700 Subject: [PATCH 4/4] Fix failed test cases in Windows - Fix error with LONG type when put row --- src/gstype_python.i | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/gstype_python.i b/src/gstype_python.i index 739d0af..cd63834 100755 --- a/src/gstype_python.i +++ b/src/gstype_python.i @@ -1466,13 +1466,13 @@ static bool getRowFields(GSRow* row, int columnCount, GSType* typeList, bool tim return false; } if (longValue) { - PyList_SetItem(outList, i, PyLong_FromLong(longValue)); + PyList_SetItem(outList, i, SWIG_From_dec(long long)(longValue)); } else if (checkNullField(row, i)) { // NULL value Py_INCREF(Py_None); PyList_SetItem(outList, i, Py_None); } else { - PyList_SetItem(outList, i, PyLong_FromLong(longValue)); + PyList_SetItem(outList, i, SWIG_From_dec(long long)(longValue)); } break; } @@ -1806,7 +1806,7 @@ static bool getRowFields(GSRow* row, int columnCount, GSType* typeList, bool tim return false; } for (int j = 0; j < size; j++) { - PyList_SetItem(list, j, PyLong_FromLong(longArr[j])); + PyList_SetItem(list, j, SWIG_From_dec(long long)(longArr[j])); } if (size) { PyList_SetItem(outList, i, list);