Changeset: 512925811120 for MonetDB URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=512925811120 Added Files: monetdb5/extras/pyapi/unicode.c Modified Files: monetdb5/extras/pyapi/Makefile.ag monetdb5/extras/pyapi/pyapi.c monetdb5/extras/pyapi/pytypes.h monetdb5/extras/pyapi/type_conversion.h monetdb5/extras/pyapi/unicode.h Branch: pyapi Log Message:
Minor fixes for other compilers. diffs (truncated from 800 to 300 lines): diff --git a/monetdb5/extras/pyapi/Makefile.ag b/monetdb5/extras/pyapi/Makefile.ag --- a/monetdb5/extras/pyapi/Makefile.ag +++ b/monetdb5/extras/pyapi/Makefile.ag @@ -17,7 +17,7 @@ MTSAFE lib__pyapi = { MODULE DIR = libdir/monetdb5 - SOURCES = pyapi.c pyapi.h + SOURCES = pyapi.c pyapi.h unicode.c unicode.h XDEPS = $(libpy_LIBDEP) LIBS = ../../tools/libmonetdb5 \ ../../../gdk/libbat \ 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 @@ -15,21 +15,21 @@ #include "sql_catalog.h" #include "pyapi.h" -#include "unicode.h" -#include "pytypes.h" -#include "type_conversion.h" - #undef _GNU_SOURCE #undef _XOPEN_SOURCE #undef _POSIX_C_SOURCE #include <Python.h> +#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#include <numpy/arrayobject.h> + +#include "unicode.h" +#include "pytypes.h" +#include "type_conversion.h" + //#define _PYAPI_VERBOSE_ #define _PYAPI_DEBUG_ -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION -#include <numpy/arrayobject.h> - #include <stdint.h> #include <string.h> @@ -40,9 +40,12 @@ const char* pyapi_enableflag = "embedded #ifdef _PYAPI_VERBOSE_ -#define VERBOSE_MESSAGE printf +#define VERBOSE_MESSAGE(...) { \ + printf(__VA_ARGS__); \ + fflush(stdout); \ +} #else -#define VERBOSE_MESSAGE ((void) 0) +#define VERBOSE_MESSAGE(...) ((void) 0) #endif struct _PyReturn{ @@ -68,7 +71,6 @@ static MT_Lock pyapiLock; static MT_Lock pyapiSluice; static int pyapiInitialized = FALSE; - #define BAT_TO_NP(bat, mtpe, nptpe) \ PyArray_New(&PyArray_Type, 1, (npy_intp[1]) {BATcount(bat)}, \ nptpe, NULL, (mtpe*) Tloc(bat, BUNfirst(bat)), 0, \ @@ -1340,5 +1342,3 @@ void ReleaseLock(PyGILState_STATE gstate sem_post(&execute_semaphore); } } - - 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 @@ -14,12 +14,9 @@ #ifndef _PYTYPE_LIB_ #define _PYTYPE_LIB_ -#include "unicode.h" - -#include <Python.h> - -#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION -#include <numpy/arrayobject.h> +#include <stdint.h> +#include <stdbool.h> +#include <stddef.h> //! Returns true if a NPY_#type is an integral type, and false otherwise bool PyType_IsInteger(int); @@ -27,6 +24,8 @@ bool PyType_IsInteger(int); bool PyType_IsFloat(int); //! Returns true if a NPY_#type is a double type, and false otherwise bool PyType_IsDouble(int); +//! Formats NPY_#type as a String (so NPY_INT => "INT"), for usage in error reporting and warnings +char *PyType_Format(int); //! Returns true if a PyObject is a scalar type ('scalars' in this context means numeric or string types) bool PyType_IsPyScalar(PyObject *object); //! Returns true if the PyObject is of type numpy.ndarray, and false otherwise @@ -35,14 +34,12 @@ bool PyType_IsNumpyArray(PyObject *objec bool PyType_IsNumpyMaskedArray(PyObject *object); //! Returns true if the PyObject is of type pandas.core.frame.DataFrame, and false otherwise bool PyType_IsPandasDataFrame(PyObject *object); -//! Formats NPY_#type as a String (so NPY_INT => "INT"), for usage in error reporting and warnings -char *PyType_Format(int); + char *BatType_Format(int); int PyType_ToBat(int); int BatType_ToPyType(int); - bool PyType_IsInteger(int type) { switch (type) @@ -57,8 +54,8 @@ bool PyType_IsInteger(int type) case NPY_USHORT: case NPY_UINT: case NPY_ULONG: - case NPY_ULONGLONG: return TRUE; - default: return FALSE; + case NPY_ULONGLONG: return true; + default: return false; } } @@ -67,8 +64,8 @@ bool PyType_IsFloat(int type) switch (type) { case NPY_FLOAT16: - case NPY_FLOAT: return TRUE; - default: return FALSE; + case NPY_FLOAT: return true; + default: return false; } } @@ -77,52 +74,11 @@ bool PyType_IsDouble(int type) switch (type) { case NPY_DOUBLE: - case NPY_LONGDOUBLE: return TRUE; - default: return FALSE; + case NPY_LONGDOUBLE: return true; + default: return false; } } -//Returns TRUE if the type of [object] is a scalar (i.e. numeric scalar or string, basically "not an array but a single value") -bool PyType_IsPyScalar(PyObject *object) -{ - PyArray_Descr *descr; - - if (object == NULL) return FALSE; - if (PyList_Check(object)) return FALSE; - if (PyObject_HasAttrString(object, "mask")) return FALSE; - - descr = PyArray_DescrFromScalar(object); - if (descr == NULL) return FALSE; - if (descr->type_num != NPY_OBJECT) return TRUE; //check if the object is a numpy scalar - if (PyInt_Check(object) || PyFloat_Check(object) || PyLong_Check(object) || PyString_Check(object) || PyBool_Check(object) || PyUnicode_Check(object)) return TRUE; - - return FALSE; -} - -bool PyType_IsPandasDataFrame(PyObject *object) -{ - PyObject *str = PyObject_Str(PyObject_Type(object)); - bool ret = strcmp(PyString_AsString(str), "<class 'pandas.core.frame.DataFrame'>") == 0; - Py_DECREF(str); - return ret; -} - -bool PyType_IsNumpyArray(PyObject *object) -{ - PyObject *str = PyObject_Str(PyObject_Type(object)); - bool ret = strcmp(PyString_AsString(str), "<type 'numpy.ndarray'>") == 0; - Py_DECREF(str); - return ret; -} - -bool PyType_IsNumpyMaskedArray(PyObject *object) -{ - PyObject *str = PyObject_Str(PyObject_Type(object)); - bool ret = strcmp(PyString_AsString(str), "<class 'numpy.ma.core.MaskedArray'>") == 0; - Py_DECREF(str); - return ret; -} - char *PyType_Format(int type) { switch (type) @@ -215,4 +171,46 @@ int BatType_ToPyType(int type) default: return NPY_STRING; } } + +//Returns true if the type of [object] is a scalar (i.e. numeric scalar or string, basically "not an array but a single value") +bool PyType_IsPyScalar(PyObject *object) +{ + PyArray_Descr *descr; + + if (object == NULL) return false; + if (PyList_Check(object)) return false; + if (PyObject_HasAttrString(object, "mask")) return false; + + descr = PyArray_DescrFromScalar(object); + if (descr == NULL) return false; + if (descr->type_num != NPY_OBJECT) return true; //check if the object is a numpy scalar + if (PyInt_Check(object) || PyFloat_Check(object) || PyLong_Check(object) || PyString_Check(object) || PyBool_Check(object) || PyUnicode_Check(object)) return true; + + return false; +} + +bool PyType_IsPandasDataFrame(PyObject *object) +{ + PyObject *str = PyObject_Str(PyObject_Type(object)); + bool ret = strcmp(PyString_AsString(str), "<class 'pandas.core.frame.DataFrame'>") == 0; + Py_DECREF(str); + return ret; +} + +bool PyType_IsNumpyArray(PyObject *object) +{ + PyObject *str = PyObject_Str(PyObject_Type(object)); + bool ret = strcmp(PyString_AsString(str), "<type 'numpy.ndarray'>") == 0; + Py_DECREF(str); + return ret; +} + +bool PyType_IsNumpyMaskedArray(PyObject *object) +{ + PyObject *str = PyObject_Str(PyObject_Type(object)); + bool ret = strcmp(PyString_AsString(str), "<class 'numpy.ma.core.MaskedArray'>") == 0; + Py_DECREF(str); + return ret; +} + #endif /* _PYTYPE_LIB_ */ 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 @@ -14,6 +14,9 @@ #ifndef _TYPE_CONVERSION_ #define _TYPE_CONVERSION_ +#include <stdint.h> +#include <stdbool.h> +#include <stddef.h> //! Copies the string of size up to max_size from the source to the destination, returns FALSE if "source" is not a legal ASCII string (i.e. a character is >= 128) bool string_copy(char * source, char* dest, size_t max_size); @@ -36,18 +39,50 @@ bool utf32_to_dbl(uint32_t *utf32, dbl * //! Converts a base-10 utf32-encoded string to a hge value bool utf32_to_hge(uint32_t *utf32, hge *value); +//using macros, create a number of str_to_<type> and unicode_to_<type> functions +#define CONVERSION_FUNCTION_FACTORY(tpe, strconv, utfconv, strval) \ + bool str_to_##tpe(void *ptr, size_t size, tpe *value); \ + bool str_to_##tpe(void *ptr, size_t size, tpe *value) \ + { \ + strval val; \ + if (!strconv((char*)ptr, size, &val)) return false; \ + *value = (tpe)val; \ + return true; \ + } \ + bool unicode_to_##tpe(void *ptr, size_t size, tpe *value); \ + bool unicode_to_##tpe(void *ptr, size_t size, tpe *value) \ + { \ + strval val; \ + (void) size; \ + if (!utfconv((uint32_t*)ptr, &val)) return false; \ + *value = (tpe)val; \ + return true; \ + } + +CONVERSION_FUNCTION_FACTORY(bit, s_to_lng, utf32_to_lng, lng) +CONVERSION_FUNCTION_FACTORY(sht, s_to_lng, utf32_to_lng, lng) +CONVERSION_FUNCTION_FACTORY(int, s_to_lng, utf32_to_lng, lng) +CONVERSION_FUNCTION_FACTORY(lng, s_to_lng, utf32_to_lng, lng) +CONVERSION_FUNCTION_FACTORY(hge, s_to_hge, utf32_to_hge, hge) +CONVERSION_FUNCTION_FACTORY(flt, s_to_dbl, utf32_to_dbl, dbl) +CONVERSION_FUNCTION_FACTORY(dbl, s_to_dbl, utf32_to_dbl, dbl) + + +#include "type_conversion.h" + + bool string_copy(char * source, char* dest, size_t max_size) { - size_t i; - for(i = 0; i < max_size; i++) - { - dest[i] = source[i]; - if (dest[i] == 0) return TRUE; - if ((*(unsigned char*)&source[i]) >= 128) return FALSE; - } _______________________________________________ checkin-list mailing list checkin-list@monetdb.org https://www.monetdb.org/mailman/listinfo/checkin-list