Changeset: 0aa6ecda698b for MonetDB URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=0aa6ecda698b Modified Files: monetdb5/extras/pyapi/connection.c monetdb5/extras/pyapi/pyapi.c monetdb5/extras/pyapi/pytypes.h tools/embeddedpy/Makefile.ag tools/embeddedpy/embeddedpy.c Branch: pyapi Log Message:
Always copy input/output arrays in MonetDBLite (because, unlike in MonetDB/Python, people can reuse Numpy arrays and stealing their data can cause segfaults). diffs (89 lines): diff --git a/monetdb5/extras/pyapi/connection.c b/monetdb5/extras/pyapi/connection.c --- a/monetdb5/extras/pyapi/connection.c +++ b/monetdb5/extras/pyapi/connection.c @@ -140,9 +140,7 @@ static PyObject * numpy_array = PyMaskedArray_FromBAT(self->cntxt, &input, 0, input.count, &msg, true); if (!numpy_array) { PyErr_Format(PyExc_Exception, "SQL Query Failed: %s", (msg ? msg : "<no error>")); - msg = release_shared_memory_shmid(self->query_ptr->shmid, ptr); - if (msg != MAL_SUCCEED) - printf("%s\n", msg); + release_shared_memory_shmid(release_shmid, ptr); return NULL; } PyDict_SetItem(result, PyString_FromString(colname), numpy_array); diff --git a/monetdb5/extras/pyapi/pyapi.c b/monetdb5/extras/pyapi/pyapi.c --- a/monetdb5/extras/pyapi/pyapi.c +++ b/monetdb5/extras/pyapi/pyapi.c @@ -273,7 +273,7 @@ static bool python_call_active = false; goto wrapup; \ } \ data = (char*) ret->array_data; \ - if (ZEROCOPY_OUTPUT ret->count > 0 && TYPE_##mtpe == PyType_ToBat(ret->result_type) && (ret->count * ret->memory_size < BUN_MAX) && \ + if (!copy && ZEROCOPY_OUTPUT ret->count > 0 && TYPE_##mtpe == PyType_ToBat(ret->result_type) && (ret->count * ret->memory_size < BUN_MAX) && \ (ret->numpy_array == NULL || PyArray_FLAGS((PyArrayObject*)ret->numpy_array) & NPY_ARRAY_OWNDATA)) \ { \ /*We can only create a direct map if the numpy array type and target BAT type*/ \ @@ -1152,7 +1152,7 @@ returnvalues: bat_type = PyType_ToBat(ret->result_type); } - b = PyObject_ConvertToBAT(ret, sql_subtype, bat_type, i, seqbase, &msg); + b = PyObject_ConvertToBAT(ret, sql_subtype, bat_type, i, seqbase, &msg, false); if (b == NULL) { goto wrapup; } @@ -2171,7 +2171,7 @@ wrapup: return FALSE; } -BAT *PyObject_ConvertToBAT(PyReturn *ret, sql_subtype *type, int bat_type, int i, int seqbase, char **return_message) +BAT *PyObject_ConvertToBAT(PyReturn *ret, sql_subtype *type, int bat_type, int i, int seqbase, char **return_message, bool copy) { BAT *b = NULL; size_t index_offset = 0; diff --git a/monetdb5/extras/pyapi/pytypes.h b/monetdb5/extras/pyapi/pytypes.h --- a/monetdb5/extras/pyapi/pytypes.h +++ b/monetdb5/extras/pyapi/pytypes.h @@ -107,7 +107,7 @@ pyapi_export PyObject *PyObject_CheckFor //! Preprocess a PyObject (that is the result of PyObject_CheckForConversion), pyreturn_values must be an array of PyReturn structs of size column_count pyapi_export bool PyObject_PreprocessObject(PyObject *pResult, PyReturn *pyreturn_values, int column_count, char **return_message); //! Create a BAT from the i'th PyReturn struct (filled by PyObject_PreprocessObject), with bat_type set to the expected BAT Type (set this to PyType_ToBat(ret->result_type) if there is no expected type), seqbase should be set to 0 unless you know what you're doing -pyapi_export BAT *PyObject_ConvertToBAT(PyReturn *ret, sql_subtype *type, int bat_type, int index, int seqbase, char **return_message); +pyapi_export BAT *PyObject_ConvertToBAT(PyReturn *ret, sql_subtype *type, int bat_type, int index, int seqbase, char **return_message, bool copy); pyapi_export char *BatType_Format(int); diff --git a/tools/embeddedpy/Makefile.ag b/tools/embeddedpy/Makefile.ag --- a/tools/embeddedpy/Makefile.ag +++ b/tools/embeddedpy/Makefile.ag @@ -39,6 +39,8 @@ lib_pyembedded = { VERSION = 1 LIBS = ../../monetdb5/tools/libmonetdb5 \ ../../gdk/libbat \ + ../../monetdb5/extras/pyapi/lib_pyapi \ + ../../sql/backends/monet5/lib_sql \ $(pcre_LIBS) $(PSAPILIB) \ $(SOCKET_LIBS) $(MALLOC_LIBS) $(DL_LIBS) $(PTHREAD_LIBS) } diff --git a/tools/embeddedpy/embeddedpy.c b/tools/embeddedpy/embeddedpy.c --- a/tools/embeddedpy/embeddedpy.c +++ b/tools/embeddedpy/embeddedpy.c @@ -192,7 +192,7 @@ PyObject *monetdb_create(PyObject *self, append_bats[i].batid = int_nil; } for(i = 0; i < columns; i++) { - BAT *b = PyObject_ConvertToBAT(&pyreturn_values[i], NULL, PyType_ToBat(pyreturn_values[i].result_type), i, 0, &msg); + BAT *b = PyObject_ConvertToBAT(&pyreturn_values[i], NULL, PyType_ToBat(pyreturn_values[i].result_type), i, 0, &msg, true); if (b == NULL) goto cleanup; append_bats[i].batid = b->batCacheid; append_bats[i].colname = PyString_AS_STRING(PyList_GetItem(dict, i)); @@ -252,7 +252,7 @@ PyObject *monetdb_insert(PyObject *self, append_bats[i].colname = column_names[i]; } for(i = 0; i < columns; i++) { - BAT *b = PyObject_ConvertToBAT(&pyreturn_values[i], NULL, column_types[i], i, 0, &msg); + BAT *b = PyObject_ConvertToBAT(&pyreturn_values[i], NULL, column_types[i], i, 0, &msg, true); if (b == NULL) goto cleanup; append_bats[i].batid = b->batCacheid; _______________________________________________ checkin-list mailing list checkin-list@monetdb.org https://www.monetdb.org/mailman/listinfo/checkin-list