Skip to content

Commit 4f9c394

Browse files
authored
Merge pull request #21 from dangtrungtin/master
Add support for Python 3.9 and SWIG 4.0.2
2 parents d696d84 + daa0858 commit 4f9c394

File tree

4 files changed

+69
-65
lines changed

4 files changed

+69
-65
lines changed

CMakeLists.txt

+8-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ project(griddb_python)
44

55
set(C_LIB E:/c_client-master/c_client-master/bin/x64/Release)
66
set(C_HEADER E:/c_client-master/c_client-master/client/c/include)
7-
set(SOURCE_HEADER E:/cmake-fun/SourceCode/src)
8-
set(PYTHON_RESOURCE C:/Users/griddb/AppData/Local/Programs/Python/Python36/)
9-
set(SWIG_RESOURCE E:/swigwin-3.0.12)
7+
set(SOURCE_HEADER E:/griddb/python_client/SourceCode/src)
8+
set(PYTHON_RESOURCE C:/Users/griddb/AppData/Local/Programs/Python/Python39/)
9+
set(NUMPY_RESOURCE C:/Users/griddb/AppData/Local/Programs/Python/Python39/Lib/site-packages/numpy/core/include)
10+
set(SWIG_RESOURCE E:/swigwin-4.0.2)
1011

1112
set(PYTHON_LIBRARIES ${PYTHON_RESOURCE}/libs)
1213
set(PYTHON_INCLUDE_DIRS ${PYTHON_RESOURCE}/include)
@@ -23,8 +24,8 @@ include(${SWIG_USE_FILE})
2324
set(CMAKE_SWIG_FLAGS -c++)
2425
set(CMAKE_CXX_FLAGS "/DLL /LD /D_USRDLL /D_WINDLL /MT /MD")
2526

26-
include_directories(${C_HEADER} ${PYTHON_RESOURCE}/include ${SOURCE_HEADER})
27-
link_directories(${PYTHON_RESOURCE}/libs ${C_LIB})
27+
include_directories(${C_HEADER} ${PYTHON_RESOURCE}/include ${SOURCE_HEADER} ${NUMPY_RESOURCE})
28+
link_directories(${PYTHON_RESOURCE}/libs ${C_LIB} ${NUMPY_RESOURCE})
2829

2930
set_property(SOURCE ${SOURCE_HEADER}/griddb.i PROPERTY CPLUSPLUS ON)
3031
swig_add_library(griddb_python TYPE SHARED LANGUAGE python SOURCES ${SOURCE_HEADER}/griddb.i
@@ -41,6 +42,8 @@ ${SOURCE_HEADER}/RowKeyPredicate.cpp
4142
${SOURCE_HEADER}/RowSet.cpp
4243
${SOURCE_HEADER}/TimestampUtils.cpp
4344
${SOURCE_HEADER}/Field.cpp
45+
${SOURCE_HEADER}/RowList.cpp
46+
${SOURCE_HEADER}/RowSet.cpp
4447
${SOURCE_HEADER}/Util.cpp)
4548

4649
target_link_libraries(griddb_python gridstore_c)

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ CPPFLAGS = -fPIC -std=c++0x -g -O2
99
INCLUDES = -Iinclude -Isrc
1010

1111
INCLUDES_PYTHON = $(INCLUDES) \
12-
-I/usr/include/python3.6 \
13-
-I$(HOME)/.pyenv/versions/3.6.4/lib/python3.6/site-packages/numpy/core/include
12+
-I/usr/include/python3.9/ \
13+
-I$(HOME)/.pyenv/versions/3.9.5/lib/python3.9/site-packages/numpy/core/include
1414

1515
PROGRAM = _griddb_python.so
1616
EXTRA = griddb_python.py griddb_python.pyc

README.md

+18-16
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,26 @@ GridDB Python Client is developed using GridDB C Client and [SWIG](http://www.sw
99
Building of the library and execution of the sample programs have been checked in the following environment.
1010

1111
OS: CentOS 7.6(x64) (GCC 4.8.5)
12-
SWIG: 3.0.12
13-
Python: 3.6
12+
SWIG: 4.0.2
13+
Python: 3.9
1414
GridDB C client: V4.5 CE(Community Edition)
1515
GridDB server: V4.5 CE, CentOS 7.6(x64) (GCC 4.8.5)
1616

1717
OS: Ubuntu 18.04(x64) (gcc 7.3.0)
18-
SWIG: 3.0.12
19-
Python: 3.6
18+
SWIG: 4.0.2
19+
Python: 3.9
2020
GridDB C client: V4.5 CE (Note: If you build from source code, please use GCC 4.8.5.)
2121
GridDB server: V4.5 CE, Ubuntu 18.04(x64) (Note: If you build from source code, please use GCC 4.8.5.)
2222

2323
OS: Windows 10(x64) (VS2017)
24-
SWIG: 3.0.12
25-
Python: 3.6
24+
SWIG: 4.0.2
25+
Python: 3.9
2626
GridDB C client: V4.5 CE
2727
GridDB server: V4.5 CE, CentOS 7.6(x64) (GCC 4.8.5)
2828

2929
OS: MacOS Catalina (x86_64)
30-
SWIG: 3.0.12
31-
Python: 3.6.9
30+
SWIG: 4.0.2
31+
Python: 3.9
3232
GridDB C client: V4.5 CE
3333
GridDB server: V4.5 CE, Centos 7.6(x64) (GCC 4.8.5)
3434

@@ -37,9 +37,10 @@ Building of the library and execution of the sample programs have been checked i
3737

3838
Install SWIG as below.
3939

40-
$ wget https://prdownloads.sourceforge.net/swig/swig-3.0.12.tar.gz
41-
$ tar xvfz swig-3.0.12.tar.gz
42-
$ cd swig-3.0.12
40+
$ wget https://github.com/swig/swig/archive/refs/tags/v4.0.2.tar.gz
41+
$ tar xvfz v4.0.2.tar.gz
42+
$ cd swig-4.0.2
43+
$ ./autogen.sh
4344
$ ./configure
4445
$ make
4546
$ sudo make install
@@ -114,9 +115,10 @@ Note: X.X.X is the software version.
114115
115116
Install SWIG as below.
116117
117-
$ wget https://prdownloads.sourceforge.net/swig/swig-3.0.12.tar.gz
118-
$ tar xvfz swig-3.0.12.tar.gz
119-
$ cd swig-3.0.12
118+
$ wget https://github.com/swig/swig/archive/refs/tags/v4.0.2.tar.gz
119+
$ tar xvfz v4.0.2.tar.gz
120+
$ cd swig-4.0.2
121+
$ ./autogen.sh
120122
$ ./configure
121123
$ make
122124
$ sudo make install
@@ -141,8 +143,8 @@ Modify **Makefile** to make python_client compatible with MacOS:
141143
- Change python include path and numpy include path on MacOS. For example:
142144
```bash
143145
INCLUDES_PYTHON = $(INCLUDES) \
144-
-I$(HOME)/.pyenv/versions/3.6.9/include/python3.6m \
145-
-I$(HOME)/.pyenv/versions/3.6.9/lib/python3.6/site-packages/numpy/core/include
146+
-I$(HOME)/.pyenv/versions/3.9.5/include/python3.9 \
147+
-I$(HOME)/.pyenv/versions/3.9.5/lib/python3.9/site-packages/numpy/core/include
146148
```
147149
148150
- Remove "-Llibs -lrt" (not support on MacOS) from LDFLAGS on Makefile

src/gstype_python.i

+41-42
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ char* convertObjToStr(PyObject *string) {
136136
%#if PY_MAJOR_VERSION < 3
137137
return PyString_AsString(string);
138138
%#else
139-
return PyUnicode_AsUTF8(string);
139+
return (char*) PyUnicode_AsUTF8(string);
140140
%#endif
141141
}
142142
}
@@ -1466,13 +1466,13 @@ static bool getRowFields(GSRow* row, int columnCount, GSType* typeList, bool tim
14661466
return false;
14671467
}
14681468
if (longValue) {
1469-
PyList_SetItem(outList, i, PyLong_FromLong(longValue));
1469+
PyList_SetItem(outList, i, SWIG_From_dec(long long)(longValue));
14701470
} else if (checkNullField(row, i)) {
14711471
// NULL value
14721472
Py_INCREF(Py_None);
14731473
PyList_SetItem(outList, i, Py_None);
14741474
} else {
1475-
PyList_SetItem(outList, i, PyLong_FromLong(longValue));
1475+
PyList_SetItem(outList, i, SWIG_From_dec(long long)(longValue));
14761476
}
14771477
break;
14781478
}
@@ -1806,7 +1806,7 @@ static bool getRowFields(GSRow* row, int columnCount, GSType* typeList, bool tim
18061806
return false;
18071807
}
18081808
for (int j = 0; j < size; j++) {
1809-
PyList_SetItem(list, j, PyLong_FromLong(longArr[j]));
1809+
PyList_SetItem(list, j, SWIG_From_dec(long long)(longArr[j]));
18101810
}
18111811
if (size) {
18121812
PyList_SetItem(outList, i, list);
@@ -2265,12 +2265,7 @@ static bool getRowFields(GSRow* row, int columnCount, GSType* typeList, bool tim
22652265
%attribute(griddb::ExpirationInfo, int, division_count, get_division_count, set_division_count);
22662266

22672267
//Attribute ContainerInfo::columnInfoList
2268-
%extend griddb::ContainerInfo{
2269-
%pythoncode %{
2270-
__swig_getmethods__["column_info_list"] = get_column_info_list
2271-
__swig_setmethods__["column_info_list"] = set_column_info_list
2272-
%}
2273-
};
2268+
%attributeval(griddb::ContainerInfo, ColumnInfoList, column_info_list, get_column_info_list, set_column_info_list);
22742269

22752270
/**
22762271
* Typemap for Container::multi_put
@@ -2335,32 +2330,29 @@ static bool getRowFields(GSRow* row, int columnCount, GSType* typeList, bool tim
23352330
%typemap(doc, name = "row_list") (GSRow** listRowdata, int rowCount) "list[list[object]]";
23362331

23372332
//attribute ContainerInfo::column_info_list
2338-
%typemap(in, fragment = "SWIG_AsCharPtrAndSize", fragment = "cleanString") (ColumnInfoList columnInfoList) (int* alloc = NULL){
2333+
%typemap(in, fragment = "SWIG_AsCharPtrAndSize", fragment = "cleanString") (ColumnInfoList*) (int* alloc = NULL){
23392334

23402335
if (!PyList_Check($input)) {
23412336
PyErr_SetString(PyExc_ValueError, "Expected a List");
23422337
SWIG_fail;
23432338
}
2344-
int res;
2345-
char* v = 0;
2346-
bool vbool;
2347-
size_t size = 0;
2348-
size = (size_t)PyInt_AsLong(PyLong_FromSsize_t(PyList_Size($input)));
2349-
$1.columnInfo = NULL;
2350-
$1.size = size;
2351-
size_t stringSize = 0;
2339+
2340+
$1 = NULL;
2341+
size_t size = (size_t)PyInt_AsLong(PyLong_FromSsize_t(PyList_Size($input)));
23522342
if (size) {
23532343
try {
2354-
alloc = new int[size]();
2355-
$1.columnInfo = new GSColumnInfo[size]();
2344+
$1 = new ColumnInfoList();
2345+
alloc = new int[size]();
2346+
$1->columnInfo = new GSColumnInfo[size]();
23562347
} catch (bad_alloc& ba) {
23572348
PyErr_SetString(PyExc_ValueError, "Memory allocation for set column_info_list is error");
23582349
SWIG_fail;
23592350
}
2351+
$1->size = size;
23602352
PyObject* columInfoList;
23612353
int option;
23622354
for (int i = 0; i < size; i++) {
2363-
$1.columnInfo[i] = GS_COLUMN_INFO_INITIALIZER;
2355+
$1->columnInfo[i] = GS_COLUMN_INFO_INITIALIZER;
23642356
columInfoList = PyList_GetItem($input, i);
23652357
if (!PyList_Check(columInfoList)) {
23662358
PyErr_SetString(PyExc_ValueError, "Expected a List");
@@ -2369,55 +2361,62 @@ static bool getRowFields(GSRow* row, int columnCount, GSType* typeList, bool tim
23692361
size_t sizeColumn = (size_t)PyInt_AsLong(PyLong_FromSsize_t(PyList_Size(columInfoList)));
23702362
if (sizeColumn == 3) {
23712363
option = PyInt_AsLong(PyList_GetItem(columInfoList, 2));
2372-
$1.columnInfo[i].options = option;
2364+
$1->columnInfo[i].options = option;
23732365
if (option != GS_TYPE_OPTION_NULLABLE && option != GS_TYPE_OPTION_NOT_NULL) {
23742366
PyErr_SetString(PyExc_ValueError, "Invalid value for column option");
23752367
SWIG_fail;
23762368
}
23772369
} else if (sizeColumn == 2) {
2378-
if (i == 0) {
2379-
$1.columnInfo[i].options = GS_TYPE_OPTION_NOT_NULL;
2380-
} else {
2381-
$1.columnInfo[i].options = GS_TYPE_OPTION_NULLABLE;
2382-
}
2370+
if (i == 0) {
2371+
$1->columnInfo[i].options = GS_TYPE_OPTION_NOT_NULL;
2372+
} else {
2373+
$1->columnInfo[i].options = GS_TYPE_OPTION_NULLABLE;
2374+
}
23832375
} else {
23842376
PyErr_SetString(PyExc_ValueError, "Invalid element number for List");
23852377
SWIG_fail;
23862378
}
23872379

2380+
char* v = 0;
2381+
size_t stringSize = 0;
2382+
int res;
23882383
res = SWIG_AsCharPtrAndSize(PyList_GetItem(columInfoList, 0), &v, &stringSize, &alloc[i]);
23892384
if (!SWIG_IsOK(res)) {
23902385
PyErr_SetString(PyExc_ValueError, "Can't convert field to string");
23912386
SWIG_fail;
23922387
}
2393-
$1.columnInfo[i].name = v;
2394-
$1.columnInfo[i].type = PyInt_AsLong(PyList_GetItem(columInfoList, 1));
2388+
$1->columnInfo[i].name = v;
2389+
$1->columnInfo[i].type = PyInt_AsLong(PyList_GetItem(columInfoList, 1));
23952390
}
23962391
}
23972392
}
23982393

2399-
%typemap(freearg, fragment = "cleanString") (ColumnInfoList columnInfoList) {
2394+
%typemap(freearg, fragment = "cleanString") (ColumnInfoList*) {
24002395
size_t size = 0;
2401-
if ($1.size) {
2402-
size = $1.size;
2403-
}
2404-
if ($1.columnInfo != NULL) {
2405-
if (alloc$argnum) {
2406-
for (int i = 0; i < size; i++) {
2407-
if ($1.columnInfo[i].name) {
2408-
cleanString($1.columnInfo[i].name, alloc$argnum[i]);
2396+
if ($1) {
2397+
if ($1->size) {
2398+
size = $1->size;
2399+
}
2400+
if ($1->columnInfo != NULL) {
2401+
if (alloc$argnum) {
2402+
for (int i = 0; i < size; i++) {
2403+
if ($1->columnInfo[i].name) {
2404+
cleanString($1->columnInfo[i].name, alloc$argnum[i]);
2405+
}
24092406
}
24102407
}
2408+
delete [] $1->columnInfo;
24112409
}
2412-
delete [] $1.columnInfo;
2410+
2411+
delete $1;
24132412
}
24142413
if (alloc$argnum) {
24152414
delete [] alloc$argnum;
24162415
}
24172416
}
24182417

2419-
%typemap(out, fragment = "convertStrToObj") (ColumnInfoList) {
2420-
ColumnInfoList data = $1;
2418+
%typemap(out, fragment = "convertStrToObj") (ColumnInfoList*) {
2419+
ColumnInfoList data = *$1;
24212420
size_t size = data.size;
24222421
$result = PyList_New(size);
24232422
if ($result == NULL) {

0 commit comments

Comments
 (0)