Changeset: 865868e13317 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=865868e13317
Modified Files:
        sql/backends/monet5/UDF/pyapi/connection.c
        sql/backends/monet5/UDF/pyapi/conversion.c
        sql/backends/monet5/UDF/pyapi/convert_loops.h
        sql/backends/monet5/UDF/pyapi/emit.c
        sql/backends/monet5/UDF/pyapi/formatinput.c
        sql/backends/monet5/UDF/pyapi/pyheader.h
        sql/backends/monet5/UDF/pyapi/pyloader.c
        sql/backends/monet5/UDF/pyapi/pytypes.c
        sql/backends/monet5/UDF/pyapi/type_conversion.c
        sql/backends/monet5/UDF/pyapi/type_conversion.h
        sql/backends/monet5/UDF/pyapi/unicode.c
Branch: default
Log Message:

Fixes for Python3 UDFs.


diffs (278 lines):

diff --git a/sql/backends/monet5/UDF/pyapi/connection.c 
b/sql/backends/monet5/UDF/pyapi/connection.c
--- a/sql/backends/monet5/UDF/pyapi/connection.c
+++ b/sql/backends/monet5/UDF/pyapi/connection.c
@@ -5,12 +5,6 @@
 #include "type_conversion.h"
 #include "gdk_interprocess.h"
 
-#if PY_MAJOR_VERSION >= 3
-#define IS_PY3K
-#define PyString_CheckExact PyUnicode_CheckExact
-#define PyString_FromString PyUnicode_FromString
-#endif
-
 CREATE_SQL_FUNCTION_PTR(void,SQLdestroyResult);
 CREATE_SQL_FUNCTION_PTR(str,SQLstatementIntern);
 CREATE_SQL_FUNCTION_PTR(str,create_table_from_emit);
@@ -158,9 +152,21 @@ static PyMethodDef _connectionObject_met
 };
 
 PyTypeObject Py_ConnectionType = {
-    PyObject_HEAD_INIT(NULL)
-    0,
-    "monetdb._connection",
+    _PyObject_EXTRA_INIT
+// in python3 they use structs within structs to represent this information, 
and many compilers throw warnings if you don't use separate braces
+// to initialize these separate structs. However, in Python2, they use 
#defines to put this information in, so we have these nice #ifdefs
+#ifdef IS_PY3K
+    { { 
+#endif
+        1, NULL 
+#ifdef IS_PY3K
+    }
+#endif 
+    , 0
+#ifdef IS_PY3K
+    }
+#endif
+    ,"monetdb._connection",
     sizeof(Py_ConnectionObject),
     0,
     0,                                          /* tp_dealloc */
@@ -245,7 +251,7 @@ PyObject *Py_Connection_Create(Client cn
 }
 
 static void _connection_import_array(void) {
-    import_array();
+    _import_array();
 }
 
 str _connection_init(void)
diff --git a/sql/backends/monet5/UDF/pyapi/conversion.c 
b/sql/backends/monet5/UDF/pyapi/conversion.c
--- a/sql/backends/monet5/UDF/pyapi/conversion.c
+++ b/sql/backends/monet5/UDF/pyapi/conversion.c
@@ -915,7 +915,7 @@ bit IsStandardBATType(int type) {
 }
 
 static void conversion_import_array(void) {
-    import_array();
+    _import_array();
 }
 
 str _conversion_init(void) {
diff --git a/sql/backends/monet5/UDF/pyapi/convert_loops.h 
b/sql/backends/monet5/UDF/pyapi/convert_loops.h
--- a/sql/backends/monet5/UDF/pyapi/convert_loops.h
+++ b/sql/backends/monet5/UDF/pyapi/convert_loops.h
@@ -258,7 +258,7 @@
                        b->tnil = 1;                                            
                                                                                
            \
                        BUNappend(b, str_nil, FALSE);                           
                                                                                
              \
                    }  else {                                                   
                                                                                
              \
-                       if (!string_copy(&data[(index_offset * ret->count + iu) 
* ret->memory_size], utf8_string, ret->memory_size, true)) {                    
              \
+                       if (!string_copy(&data[(index_offset * ret->count + iu) 
* ret->memory_size], utf8_string, ret->memory_size, false)) {                   
               \
                            msg = createException(MAL, "pyapi.eval", "Invalid 
string encoding used. Please return a regular ASCII string, or a Numpy_Unicode 
object.\n");     \
                            goto wrapup;                                        
                                                                                
              \
                        }                                                       
                                                                                
              \
diff --git a/sql/backends/monet5/UDF/pyapi/emit.c 
b/sql/backends/monet5/UDF/pyapi/emit.c
--- a/sql/backends/monet5/UDF/pyapi/emit.c
+++ b/sql/backends/monet5/UDF/pyapi/emit.c
@@ -7,20 +7,6 @@
 
 #include "unicode.h"
 
-#if PY_MAJOR_VERSION >= 3
-#define IS_PY3K
-#define PyString_FromString PyUnicode_FromString
-#define PyString_Check PyUnicode_Check
-#define PyString_CheckExact PyUnicode_CheckExact
-#define PyString_AsString PyUnicode_AsUTF8
-#define PyString_AS_STRING PyUnicode_AsUTF8
-#define PyInt_FromLong PyLong_FromLong
-#define PyInt_Check PyLong_Check
-#define PythonUnicodeType char
-#else
-#define PythonUnicodeType Py_UNICODE
-#endif
-
 #define scalar_convert(tpe) {\
     tpe val = (tpe) tpe##_nil; msg = pyobject_to_##tpe(&dictEntry, 42, &val); \
     BUNappend(self->cols[i].b, &val, 0); \
@@ -313,9 +299,21 @@ static PyMethodDef _emitObject_methods[]
 };
 
 PyTypeObject PyEmitType = {
-    PyObject_HEAD_INIT(NULL)
-    0,
-    "monetdb._emit",
+    _PyObject_EXTRA_INIT
+// in python3 they use structs within structs to represent this information, 
and many compilers throw warnings if you don't use separate braces
+// to initialize these separate structs. However, in Python2, they use 
#defines to put this information in, so we have these nice #ifdefs
+#ifdef IS_PY3K
+    { { 
+#endif
+        1, NULL 
+#ifdef IS_PY3K
+    }
+#endif 
+    , 0
+#ifdef IS_PY3K
+    }
+#endif
+    ,"monetdb._emit",
     sizeof(PyEmitObject),
     0,
     0,                                          /* tp_dealloc */
diff --git a/sql/backends/monet5/UDF/pyapi/formatinput.c 
b/sql/backends/monet5/UDF/pyapi/formatinput.c
--- a/sql/backends/monet5/UDF/pyapi/formatinput.c
+++ b/sql/backends/monet5/UDF/pyapi/formatinput.c
@@ -2,15 +2,6 @@
 #include "formatinput.h"
 #include "type_conversion.h"
 
-#if PY_MAJOR_VERSION >= 3
-#define IS_PY3K
-#define PyString_FromStringAndSize PyUnicode_FromStringAndSize
-#endif
-
-#if PY_MAJOR_VERSION >= 3
-#define IS_PY3K
-#endif
-
 //! Parse a PyCodeObject from a string, the string is expected to be in the 
format {@<encoded_function>};, where <encoded_function> is the Marshalled code 
object
 PyObject *PyCodeObject_ParseString(char *string, char **msg);
 PyObject *PyCodeObject_ParseString(char *string, char **msg)
@@ -373,5 +364,5 @@ finally:
 }
 
 void _formatinput_init(void) {
-    import_array();
+    _import_array();
 }
diff --git a/sql/backends/monet5/UDF/pyapi/pyheader.h 
b/sql/backends/monet5/UDF/pyapi/pyheader.h
--- a/sql/backends/monet5/UDF/pyapi/pyheader.h
+++ b/sql/backends/monet5/UDF/pyapi/pyheader.h
@@ -72,9 +72,11 @@
 #define PyString_CheckExact PyUnicode_CheckExact
 #define PyString_AsString PyUnicode_AsUTF8
 #define PyString_AS_STRING PyUnicode_AsUTF8
+#define PyString_FromStringAndSize PyUnicode_FromStringAndSize
 #define PyInt_FromLong PyLong_FromLong
 #define PyInt_Check PyLong_Check
 #define PythonUnicodeType char
+
 #else
 #define PythonUnicodeType Py_UNICODE
 #endif
diff --git a/sql/backends/monet5/UDF/pyapi/pyloader.c 
b/sql/backends/monet5/UDF/pyapi/pyloader.c
--- a/sql/backends/monet5/UDF/pyapi/pyloader.c
+++ b/sql/backends/monet5/UDF/pyapi/pyloader.c
@@ -10,7 +10,7 @@
 #include "formatinput.h"
 
 static void _loader_import_array(void) {
-    import_array();
+    _import_array();
 }
 
 str _loader_init(void)
diff --git a/sql/backends/monet5/UDF/pyapi/pytypes.c 
b/sql/backends/monet5/UDF/pyapi/pytypes.c
--- a/sql/backends/monet5/UDF/pyapi/pytypes.c
+++ b/sql/backends/monet5/UDF/pyapi/pytypes.c
@@ -1,11 +1,6 @@
 
 #include "pytypes.h"
 
-#if PY_MAJOR_VERSION >= 3
-#define IS_PY3K
-#define PyString_AsString PyUnicode_AsUTF8
-#endif
-
 bool PyType_IsInteger(int type)
 {
     switch (type)
@@ -200,5 +195,5 @@ bool PyType_IsPyScalar(PyObject *object)
 
 
 void _pytypes_init(void) {
-    import_array();
+    _import_array();
 }
diff --git a/sql/backends/monet5/UDF/pyapi/type_conversion.c 
b/sql/backends/monet5/UDF/pyapi/type_conversion.c
--- a/sql/backends/monet5/UDF/pyapi/type_conversion.c
+++ b/sql/backends/monet5/UDF/pyapi/type_conversion.c
@@ -11,15 +11,10 @@
 
 #include <longintrepr.h>
 
-#if PY_MAJOR_VERSION >= 3
-#define IS_PY3K
-#endif
-
 bool string_copy(char * source, char* dest, size_t max_size, bool 
allow_unicode)
 {
     size_t i;
-    for(i = 0; i < max_size; i++)
-    {
+    for(i = 0; i < max_size; i++) {
         dest[i] = source[i];
         if (dest[i] == 0) return TRUE;
         if (!allow_unicode && (*(unsigned char*)&source[i]) >= 128) return 
FALSE;
@@ -343,13 +338,15 @@ 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
 #endif
 
 void _typeconversion_init(void) {
-    import_array();
+    _import_array();
 }
diff --git a/sql/backends/monet5/UDF/pyapi/type_conversion.h 
b/sql/backends/monet5/UDF/pyapi/type_conversion.h
--- a/sql/backends/monet5/UDF/pyapi/type_conversion.h
+++ b/sql/backends/monet5/UDF/pyapi/type_conversion.h
@@ -19,8 +19,6 @@
 
 #include "pyheader.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, bool 
allow_unicode);
 #ifdef HAVE_HGE
@@ -28,7 +26,7 @@ bool string_copy(char * source, char* de
 int hge_to_string(char *str, hge );
 //! Converts a base-10 string to a hge value
 str str_to_hge(char *ptr, size_t maxsize, hge *value);
-#if PY_MAJOR_VERSION >= 3
+#ifdef IS_PY3K
 //! Converts a base-10 utf32-encoded string to a hge value
 str unicode_to_hge(char *utf32, size_t maxsize, hge *value);
 #else
@@ -44,9 +42,7 @@ size_t pyobject_get_size(PyObject *obj);
 //! Converts a PyObject to a str; the output string will be a newly allocated 
string (if *value == NULL) or stored in *value (if *value != NULL)
 str pyobject_to_str(PyObject **ptr, size_t maxsize, str *value);
 
-
-
-#if PY_MAJOR_VERSION >= 3
+#ifdef IS_PY3K
 //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)          \
     str str_to_##tpe(char *ptr, size_t maxsize, tpe *value);          \
diff --git a/sql/backends/monet5/UDF/pyapi/unicode.c 
b/sql/backends/monet5/UDF/pyapi/unicode.c
--- a/sql/backends/monet5/UDF/pyapi/unicode.c
+++ b/sql/backends/monet5/UDF/pyapi/unicode.c
@@ -207,5 +207,5 @@ bool utf8_to_utf32(size_t offset, size_t
 }
 
 void _unicode_init(void) {
-    import_array();
+    _import_array();
 }
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to