Skip to content

Add support for Python 3.9 and SWIG 4.0.2 #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 18 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
83 changes: 41 additions & 42 deletions src/gstype_python.i
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -2265,12 +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
%extend griddb::ContainerInfo{
%pythoncode %{
__swig_getmethods__["column_info_list"] = get_column_info_list
__swig_setmethods__["column_info_list"] = set_column_info_list
%}
};
%attributeval(griddb::ContainerInfo, ColumnInfoList, column_info_list, get_column_info_list, set_column_info_list);

/**
* Typemap for Container::multi_put
Expand Down Expand Up @@ -2335,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");
Expand All @@ -2369,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) {
Expand Down