Changeset: cabe05a3fa39 for MonetDB URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=cabe05a3fa39 Modified Files: monetdb5/extras/pyapi/pyapi.c monetdb5/extras/pyapi/type_conversion.c monetdb5/extras/pyapi/type_conversion.h Branch: pythonudf Log Message:
Give a reason why conversion from PyObject -> numeric type fails instead of just saying that it fails. diffs (208 lines): 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 @@ -254,9 +254,8 @@ static bool enable_zerocopy_output = tru { \ for (iu = 0; iu < ret->count; iu++) \ { \ - if (!func((ptrtpe*)&data[(index_offset * ret->count + iu) * ret->memory_size], ret->memory_size, &value)) \ - { \ - msg = createException(MAL, "pyapi.eval", "Could not convert from type %s to type %s", PyType_Format(ret->result_type), #mtpe_to); \ + msg = func((ptrtpe*)&data[(index_offset * ret->count + iu) * ret->memory_size], ret->memory_size, &value); \ + if (msg != MAL_SUCCEED) { \ goto wrapup; \ } \ ((mtpe_to*) Tloc(bat, BUNfirst(bat)))[iu] = value; \ @@ -273,9 +272,8 @@ static bool enable_zerocopy_output = tru } \ else \ { \ - if (!func((ptrtpe*)&data[(index_offset * ret->count + iu) * ret->memory_size], ret->memory_size, &value)) \ - { \ - msg = createException(MAL, "pyapi.eval", "Could not convert from type %s to type %s", PyType_Format(ret->result_type), #mtpe_to); \ + msg = func((ptrtpe*)&data[(index_offset * ret->count + iu) * ret->memory_size], ret->memory_size, &value); \ + if (msg != MAL_SUCCEED) { \ goto wrapup; \ } \ ((mtpe_to*) Tloc(bat, BUNfirst(bat)))[iu] = value; \ diff --git a/monetdb5/extras/pyapi/type_conversion.c b/monetdb5/extras/pyapi/type_conversion.c --- a/monetdb5/extras/pyapi/type_conversion.c +++ b/monetdb5/extras/pyapi/type_conversion.c @@ -91,9 +91,10 @@ PyObject *PyLong_FromHge(hge h) #ifndef IS_PY3K #define PY_TO_(type, inttpe) \ -bool pyobject_to_##type(PyObject **pyobj, size_t maxsize, type *value) \ +str pyobject_to_##type(PyObject **pyobj, size_t maxsize, type *value) \ { \ PyObject *ptr = *pyobj; \ + str retval = MAL_SUCCEED; \ (void) maxsize; \ if (PyLong_CheckExact(ptr)) { \ PyLongObject *p = (PyLongObject*) ptr; \ @@ -106,18 +107,14 @@ bool pyobject_to_##type(PyObject **pyobj prev = h; (void)prev; \ h = (h << PyLong_SHIFT) + p->ob_digit[i]; \ if ((h >> PyLong_SHIFT) != prev) { \ - printf("Overflow!\n"); \ - return false; \ + return GDKstrdup("Overflow when converting value."); \ } \ } \ *value = (type)(h * sign); \ - return true; \ } else if (PyInt_CheckExact(ptr) || PyBool_Check(ptr)) { \ *value = (type)((PyIntObject*)ptr)->ob_ival; \ - return true; \ } else if (PyFloat_CheckExact(ptr)) { \ *value = (type) ((PyFloatObject*)ptr)->ob_fval; \ - return true; \ } else if (PyString_CheckExact(ptr)) { \ return str_to_##type(((PyStringObject*)ptr)->ob_sval, -1, value); \ } else if (PyByteArray_CheckExact(ptr)) { \ @@ -125,10 +122,10 @@ bool pyobject_to_##type(PyObject **pyobj } else if (PyUnicode_CheckExact(ptr)) { \ return unicode_to_##type(((PyUnicodeObject*)ptr)->str, -1, value); \ } \ - return false; \ + return retval; \ } #define CONVERSION_FUNCTION_FACTORY(tpe, inttpe) \ - bool str_to_##tpe(char *ptr, size_t maxsize, tpe *value) \ + str str_to_##tpe(char *ptr, size_t maxsize, tpe *value) \ { \ ssize_t i = maxsize - 1; \ tpe factor = 1; \ @@ -154,14 +151,14 @@ bool pyobject_to_##type(PyObject **pyobj case '\0': continue; \ default: \ { \ - return false; \ + return "Error converting string."; \ } \ } \ factor *= 10; \ } \ - return true; \ + return MAL_SUCCEED; \ } \ - bool unicode_to_##tpe(Py_UNICODE *ptr, size_t maxsize, tpe *value) \ + str unicode_to_##tpe(Py_UNICODE *ptr, size_t maxsize, tpe *value) \ { \ char utf8[255]; \ utf32_to_utf8(0, 255, utf8, ptr); \ @@ -177,16 +174,19 @@ CONVERSION_FUNCTION_FACTORY(sht, sht) CONVERSION_FUNCTION_FACTORY(int, int) CONVERSION_FUNCTION_FACTORY(lng, lng) CONVERSION_FUNCTION_FACTORY(flt, lng) -CONVERSION_FUNCTION_FACTORY(dbl, lng) #ifdef HAVE_HGE CONVERSION_FUNCTION_FACTORY(hge, hge) +CONVERSION_FUNCTION_FACTORY(dbl, hge) +#else +CONVERSION_FUNCTION_FACTORY(dbl, lng) #endif #else #define PY_TO_(type, inttpe) \ -bool pyobject_to_##type(PyObject **pyobj, size_t maxsize, type *value) \ +str pyobject_to_##type(PyObject **pyobj, size_t maxsize, type *value) \ { \ PyObject *ptr = *pyobj; \ + str retval = MAL_SUCCEED; \ (void) maxsize; \ if (PyLong_CheckExact(ptr)) { \ PyLongObject *p = (PyLongObject*) ptr; \ @@ -199,27 +199,23 @@ bool pyobject_to_##type(PyObject **pyobj prev = h; (void)prev; \ h = (h << PyLong_SHIFT) + p->ob_digit[i]; \ if ((h >> PyLong_SHIFT) != prev) { \ - printf("Overflow!\n"); \ - return false; \ + return GDKstrdup("Overflow when converting value."); \ } \ } \ *value = (type)(h * sign); \ - return true; \ } else if (PyBool_Check(ptr)) { \ *value = ptr == Py_True ? 1 : 0; \ - return true; \ } else if (PyFloat_CheckExact(ptr)) { \ *value = (type) ((PyFloatObject*)ptr)->ob_fval; \ - return true; \ } else if (PyUnicode_CheckExact(ptr)) { \ return str_to_##type(PyUnicode_AsUTF8(ptr), -1, value); \ } else if (PyByteArray_CheckExact(ptr)) { \ return str_to_##type(((PyByteArrayObject*)ptr)->ob_bytes, -1, value);\ } \ - return false; \ + return retval; \ } #define CONVERSION_FUNCTION_FACTORY(tpe, inttpe) \ - bool str_to_##tpe(char *ptr, size_t maxsize, tpe *value) \ + str str_to_##tpe(char *ptr, size_t maxsize, tpe *value) \ { \ int i = maxsize - 1; \ tpe factor = 1; \ @@ -245,14 +241,14 @@ bool pyobject_to_##type(PyObject **pyobj case '\0': continue; \ default: \ { \ - return false; \ + return "Error converting string."; \ } \ } \ factor *= 10; \ } \ - return true; \ + return MAL_SUCCEED; \ } \ - bool unicode_to_##tpe(char *ptr, size_t maxsize, tpe *value) { return str_to_##tpe(ptr, maxsize, value); }\ + str unicode_to_##tpe(char *ptr, size_t maxsize, tpe *value) { return str_to_##tpe(ptr, maxsize, value); }\ PY_TO_(tpe, inttpe); CONVERSION_FUNCTION_FACTORY(bte, bte) diff --git a/monetdb5/extras/pyapi/type_conversion.h b/monetdb5/extras/pyapi/type_conversion.h --- a/monetdb5/extras/pyapi/type_conversion.h +++ b/monetdb5/extras/pyapi/type_conversion.h @@ -27,15 +27,15 @@ bool string_copy(char * source, char* de //! Converts a hge to a string and writes it into the string "str" int hge_to_string(char *str, hge ); //! Converts a base-10 string to a hge value -bool str_to_hge(char *ptr, size_t maxsize, hge *value); +str str_to_hge(char *ptr, size_t maxsize, hge *value); #if PY_MAJOR_VERSION >= 3 //! Converts a base-10 utf32-encoded string to a hge value -bool unicode_to_hge(char *utf32, size_t maxsize, hge *value); +str unicode_to_hge(char *utf32, size_t maxsize, hge *value); #else -bool unicode_to_hge(Py_UNICODE *utf32, size_t maxsize, hge *value); +str unicode_to_hge(Py_UNICODE *utf32, size_t maxsize, hge *value); #endif //! Converts a PyObject to a hge value -bool pyobject_to_hge(PyObject **ptr, size_t maxsize, hge *value); +str pyobject_to_hge(PyObject **ptr, size_t maxsize, hge *value); //! Create a PyLongObject from a hge integer PyObject *PyLong_FromHge(hge h); #endif @@ -45,14 +45,14 @@ PyObject *PyLong_FromHge(hge h); #if PY_MAJOR_VERSION >= 3 //using macros, create a number of str_to_<type>, unicode_to_<type> and pyobject_to_<type> functions (we are Java now) #define CONVERSION_FUNCTION_HEADER_FACTORY(tpe) \ - bool str_to_##tpe(char *ptr, size_t maxsize, tpe *value); \ - bool unicode_to_##tpe(char *ptr, size_t maxsize, tpe *value); \ - bool pyobject_to_##tpe(PyObject **ptr, size_t maxsize, tpe *value); + str str_to_##tpe(char *ptr, size_t maxsize, tpe *value); \ + str unicode_to_##tpe(char *ptr, size_t maxsize, tpe *value); \ + str pyobject_to_##tpe(PyObject **ptr, size_t maxsize, tpe *value); #else #define CONVERSION_FUNCTION_HEADER_FACTORY(tpe) \ - bool str_to_##tpe(char *ptr, size_t maxsize, tpe *value); \ - bool unicode_to_##tpe(Py_UNICODE *ptr, size_t maxsize, tpe *value); \ - bool pyobject_to_##tpe(PyObject **ptr, size_t maxsize, tpe *value); + str str_to_##tpe(char *ptr, size_t maxsize, tpe *value); \ + str unicode_to_##tpe(Py_UNICODE *ptr, size_t maxsize, tpe *value); \ + str pyobject_to_##tpe(PyObject **ptr, size_t maxsize, tpe *value); #endif CONVERSION_FUNCTION_HEADER_FACTORY(bte) _______________________________________________ checkin-list mailing list checkin-list@monetdb.org https://www.monetdb.org/mailman/listinfo/checkin-list