Changeset: 416c59283e80 for MonetDB URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=416c59283e80 Modified Files: monetdb5/extras/pyapi/Tests/pyapi_types_string.malC monetdb5/extras/pyapi/pyapi.c monetdb5/extras/pyapi/type_conversion.c monetdb5/extras/pyapi/type_conversion.h Branch: pyapi Log Message:
Remove unnecessary type conversion functions. diffs (truncated from 481 to 300 lines): diff --git a/monetdb5/extras/pyapi/Tests/pyapi_types_string.malC b/monetdb5/extras/pyapi/Tests/pyapi_types_string.malC --- a/monetdb5/extras/pyapi/Tests/pyapi_types_string.malC +++ b/monetdb5/extras/pyapi/Tests/pyapi_types_string.malC @@ -54,3 +54,48 @@ io.print(rstr); # convert boolean to string rstr:bat[:oid,:str] := pyapi.eval(nil:ptr,"return([[True, True, False]])"); io.print(rstr); + +# convert bit to string +bbit:= bat.new(:oid,:bit); +bat.append(bbit,1:bit); +bat.append(bbit,0:bit); +rbit:bat[:oid,:str] := pyapi.eval(nil:ptr,"return(arg1)",bbit); +io.print(rbit); + +# convert bte to string +bbte:= bat.new(:oid,:bte); +bat.append(bbte,42:bte); +bat.append(bbte,84:bte); +rbte:bat[:oid,:str] := pyapi.eval(nil:ptr,"return(arg1)",bbte); +io.print(rbte); + +# convert short to string +bsht:= bat.new(:oid,:sht); +bat.append(bsht,42:sht); +bat.append(bsht,82:sht); +rsht:bat[:oid,:str] := pyapi.eval(nil:ptr,"return(arg1)",bsht); +io.print(rsht); + +# convert word to string +bwrd:= bat.new(:oid,:wrd); +bat.append(bwrd,1804289383:wrd); +bat.append(bwrd,846930886:wrd); +rwrd:bat[:oid,:str] := pyapi.eval(nil:ptr,"return(arg1)",bwrd); +io.print(rwrd); + +# convert long to string +blng:= bat.new(:oid,:lng); +bat.append(blng,1804289383L); +bat.append(blng,846930886L); +rlng:bat[:oid,:str] := pyapi.eval(nil:ptr,"return(arg1)",blng); +io.print(rlng); + +# convert dbl to string +bdbl:= bat.new(:oid,:dbl); +bat.append(bdbl,18042.89383:dbl); +bat.append(bdbl,846.930886:dbl); +bat.append(bdbl,16.81692777:dbl); +bat.append(bdbl,1714636.915:dbl); +bat.append(bdbl,nil:dbl); +rstr:bat[:oid,:str] := pyapi.eval(nil:ptr,"return(arg1)",bdbl); +io.print(rstr); 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 @@ -212,13 +212,13 @@ static PyObject *GetDictionary(Client c) // This #define converts a Numpy Array to a BAT by copying the internal data to the BAT. It converts the data from the Numpy Array to the BAT using a function // This function has to have the prototype 'bool function(void *data, size_t memory_size, mtpe_to *resulting_value)', and either return False (if conversion fails) // or write the value into the 'resulting_value' pointer. This is used convertring strings/unicodes/python objects to numeric values. -#define NP_COL_BAT_LOOP_FUNC(bat, mtpe_to, func) { \ +#define NP_COL_BAT_LOOP_FUNC(bat, mtpe_to, func, ptrtpe) { \ mtpe_to value; \ if (mask == NULL) \ { \ for (iu = 0; iu < ret->count; iu++) \ { \ - if (!func(&data[(index_offset * ret->count + iu) * ret->memory_size], ret->memory_size, &value)) \ + 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); \ goto wrapup; \ @@ -237,7 +237,7 @@ static PyObject *GetDictionary(Client c) } \ else \ { \ - if (!func(&data[(index_offset * ret->count + iu) * ret->memory_size], ret->memory_size, &value)) \ + 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); \ goto wrapup; \ @@ -340,9 +340,9 @@ Array of type %s no copying will be need case NPY_FLOAT: NP_COL_BAT_LOOP(bat, mtpe, flt); break; \ case NPY_DOUBLE: \ case NPY_LONGDOUBLE: NP_COL_BAT_LOOP(bat, mtpe, dbl); break; \ - case NPY_STRING: NP_COL_BAT_LOOP_FUNC(bat, mtpe, str_to_##mtpe); break; \ - case NPY_UNICODE: NP_COL_BAT_LOOP_FUNC(bat, mtpe, unicode_to_##mtpe); break; \ - case NPY_OBJECT: NP_COL_BAT_LOOP_FUNC(bat, mtpe, pyobject_to_##mtpe); break; \ + case NPY_STRING: NP_COL_BAT_LOOP_FUNC(bat, mtpe, str_to_##mtpe, char); break; \ + case NPY_UNICODE: NP_COL_BAT_LOOP_FUNC(bat, mtpe, unicode_to_##mtpe, Py_UNICODE); break; \ + case NPY_OBJECT: NP_COL_BAT_LOOP_FUNC(bat, mtpe, pyobject_to_##mtpe, PyObject*); break; \ default: \ msg = createException(MAL, "pyapi.eval", "Unrecognized type. Could not convert to %s.\n", BatType_Format(TYPE_##mtpe)); \ goto wrapup; \ @@ -2096,11 +2096,11 @@ BAT *PyObject_ConvertToBAT(PyReturn *ret } else if (PyBool_Check(obj) || PyLong_Check(obj) || PyInt_Check(obj) || PyFloat_Check(obj)) { #ifdef HAVE_HGE hge h; - py_to_hge(obj, &h); + pyobject_to_hge(&obj, 0, &h); hge_to_string(utf8_string, h); #else lng h; - py_to_lng(obj, &h); + pyobject_to_lng(&obj, 0, &h); snprintf(utf8_string, utf8string_minlength, LLFMT, h); #endif } else { 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 @@ -63,13 +63,13 @@ int hge_to_string(char * str, hge x) return TRUE; } -#endif -bool s_to_lng(char *ptr, size_t size, lng *value) +bool str_to_hge(char *ptr, size_t maxsize, hge *value) { - size_t length = size - 1; - int i = length; - lng factor = 1; + int i = maxsize - 1; + hge factor = 1; + if (i < 0) i = strlen(ptr) - 1; + *value = 0; for( ; i >= 0; i--) { @@ -99,160 +99,23 @@ bool s_to_lng(char *ptr, size_t size, ln return true; } -#ifdef HAVE_HGE -bool s_to_hge(char *ptr, size_t size, hge *value) +bool unicode_to_hge(Py_UNICODE *utf32, size_t maxsize, hge *value) { - size_t length = size - 1; - int i = length; - hge factor = 1; - *value = 0; - for( ; i >= 0; i--) - { - switch(ptr[i]) - { - case '0': break; - case '1': *value += factor; break; - case '2': *value += 2 * factor; break; - case '3': *value += 3 * factor; break; - case '4': *value += 4 * factor; break; - case '5': *value += 5 * factor; break; - case '6': *value += 6 * factor; break; - case '7': *value += 7 * factor; break; - case '8': *value += 8 * factor; break; - case '9': *value += 9 * factor; break; - case '-': *value *= -1; break; - case '.': - case ',': *value = 0; factor = 1; continue; - case '\0': continue; - default: - { - return false; - } - } - factor *= 10; - } - return true; + char utf8[255]; + utf32_to_utf8(0, 255, utf8, utf32); + return str_to_hge(utf8, maxsize, value); } #endif -bool s_to_dbl(char *ptr, size_t size, dbl *value) -{ - size_t length = size - 1; - int i = length; - dbl factor = 1; - *value = 0; - for( ; i >= 0; i--) - { - switch(ptr[i]) - { - case '0': break; - case '1': *value += factor; break; - case '2': *value += 2 * factor; break; - case '3': *value += 3 * factor; break; - case '4': *value += 4 * factor; break; - case '5': *value += 5 * factor; break; - case '6': *value += 6 * factor; break; - case '7': *value += 7 * factor; break; - case '8': *value += 8* factor; break; - case '9': *value += 9 * factor; break; - case '-': *value *= -1; break; - case '.': - case ',': *value /= factor; factor = 1; continue; - case '\0': continue; - default: return false; - } - factor *= 10; - } - return true; -} - - -bool utf32_to_lng(Py_UNICODE *utf32, size_t maxsize, lng *value) -{ - size_t length = utf32_strlen(utf32); - int i; - size_t factor = 1; - if (length > maxsize) length = maxsize; - i = length; - *value = 0; - for( ; i >= 0; i--) - { - switch(utf32[i]) - { - case '0': break; - case '1': *value += factor; break; - case '2': *value += 2 * factor; break; - case '3': *value += 3 * factor; break; - case '4': *value += 4 * factor; break; - case '5': *value += 5 * factor; break; - case '6': *value += 6 * factor; break; - case '7': *value += 7 * factor; break; - case '8': *value += 8 * factor; break; - case '9': *value += 9 * factor; break; - case '-': *value *= -1; break; - case '.': - case ',': *value = 0; factor = 1; continue; - case '\0': continue; - default: return false; - } - factor *= 10; - } - return true; -} - -bool utf32_to_dbl(Py_UNICODE *utf32, size_t maxsize, dbl *value) -{ - size_t length = utf32_strlen(utf32); - int i; - size_t factor = 1; - if (length > maxsize) length = maxsize; - i = length; - *value = 0; - for( ; i >= 0; i--) - { - switch(utf32[i]) - { - case '0': break; - case '1': *value += factor; break; - case '2': *value += 2 * factor; break; - case '3': *value += 3 * factor; break; - case '4': *value += 4 * factor; break; - case '5': *value += 5 * factor; break; - case '6': *value += 6 * factor; break; - case '7': *value += 7 * factor; break; - case '8': *value += 8 * factor; break; - case '9': *value += 9 * factor; break; - case '-': *value *= -1; break; - case '.': - case ',': *value /= factor; factor = 1; continue; - case '\0': continue; - default: return false; - } - factor *= 10; - } - return true; -} - -#ifdef HAVE_HGE -bool utf32_to_hge(Py_UNICODE *utf32, size_t maxsize, hge *value) -{ - size_t length = utf32_strlen(utf32) + 1; - char utf8[200]; - if (length > maxsize) length = maxsize; - utf32_to_utf8(0, maxsize, utf8, utf32); - return s_to_hge(utf8, length, value); -} -#endif - - -//py_to_hge and py_to_lng are almost identical, so use a generic #define to make them -#define PY_TO_(type) \ -bool py_to_##type(PyObject *ptr, type *value) \ +#define PY_TO_(type, inttpe) \ +bool pyobject_to_##type(PyObject **pyobj, size_t maxsize, type *value) \ { \ + PyObject *ptr = *pyobj; \ + (void) maxsize; \ if (PyLong_CheckExact(ptr)) { \ PyLongObject *p = (PyLongObject*) ptr; \ - type h = 0; \ - type prev = 0; \ + inttpe h = 0; \ + inttpe prev = 0; \ int i = Py_SIZE(p); \ int sign = i < 0 ? -1 : 1; \ i *= sign; \ _______________________________________________ checkin-list mailing list checkin-list@monetdb.org https://www.monetdb.org/mailman/listinfo/checkin-list