1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
Author: Alastair McKinstry <mckinstry@debian.org>
Description: SWIG changes needed for python3 build
Last-Updated: 2016-08-01
Forwarded: no
Index: grib-api-1.18.0/python/CMakeLists.txt
===================================================================
--- grib-api-1.18.0.orig/python/CMakeLists.txt
+++ grib-api-1.18.0/python/CMakeLists.txt
@@ -1,9 +1,9 @@
if( HAVE_PYTHON )
- #find_package( SWIG )
- #if( SWIG_FOUND )
- # include( ${SWIG_USE_FILE} )
- #endif()
+ find_package( SWIG )
+ if( SWIG_FOUND )
+ include( ${SWIG_USE_FILE} )
+ endif()
# check for Numpy
find_package( NumPy )
@@ -53,27 +53,30 @@ if( HAVE_PYTHON )
# compile swig interface
- set(CMAKE_SWIG_FLAGS "")
+ set(CMAKE_SWIG_FLAGS "")
+ if (${PYTHON_VERSION_MAJOR} EQUAL 3)
+ set_source_files_properties( gribapi_swig.i PROPERTIES SWIG_FLAGS "-py3" )
+ endif()
include_directories( ${PYTHON_INCLUDE_PATH} )
set_source_files_properties( gribapi_swig.i PROPERTIES C ON )
if( NUMPY_FOUND )
- set( CMAKE_SWIG_FLAGS "-DNUMPY" )
+ set(CMAKE_SWIG_FLAGS "-DNUMPY" )
include_directories( ${NUMPY_INCLUDE_DIRS} )
endif()
# Invoke swig to generate the C wrapper
- # swig_add_module( gribapi_swig python grib_interface.h grib_interface.c gribapi_swig.i )
- # swig_link_libraries( gribapi_swig grib_api ${PYTHON_LIBRARIES} )
+ SWIG_ADD_MODULE( gribapi_swig python grib_interface.h grib_interface.c gribapi_swig.i )
+ SWIG_LINK_LIBRARIES( gribapi_swig grib_api ${PYTHON_LIBRARIES} )
####### Do not invoke swig. Use our own generated C wrapper file ######
- ecbuild_add_library(TARGET _gribapi_swig
- TYPE SHARED
- NOINSTALL
- SOURCES grib_interface.h grib_interface.c ${_swig_c_wrapper}
- LIBS grib_api ${PYTHON_LIBRARIES} )
+ #ecbuild_add_library(TARGET _gribapi_swig
+ # TYPE SHARED
+ # NOINSTALL
+ # SOURCES grib_interface.h grib_interface.c ${_swig_c_wrapper}
+ # LIBS grib_api ${PYTHON_LIBRARIES} )
# Don't use the lib prefix. This is needed for the python case where a _modulename.so is generated
set_target_properties(_gribapi_swig PROPERTIES PREFIX "")
# SET_TARGET_PROPERTIES(_gribapi_swig PROPERTIES SUFFIX ".so")
Index: grib-api-1.18.0/python/gribapi_swig.i
===================================================================
--- grib-api-1.18.0.orig/python/gribapi_swig.i
+++ grib-api-1.18.0/python/gribapi_swig.i
@@ -10,6 +10,7 @@
%{
#define SWIG_FILE_WITH_INIT
#include "grib_interface.h"
+#include <fcntl.h>
%}
#if defined(NUMPY)
@@ -21,14 +22,27 @@ import_array();
#endif
-/* Converts a PyFile instance to a stdio FILE* */
+
%typemap(in) FILE* {
- if ( PyFile_Check($input) ){
- $1 = PyFile_AsFile($input);
- } else {
- PyErr_SetString(PyExc_TypeError, "$1_name must be a file type.");
- return NULL;
+ int fd, val, accmode;
+ char mode[3] = "r\0\0";
+ fd = PyObject_AsFileDescriptor($input);
+ if ( fd >= 0 ) {
+ val = fcntl(fd, F_GETFL, 0);
+ accmode = val & O_ACCMODE;
+ if (accmode == O_RDONLY) mode[0] = 'r';
+ else if (accmode == O_WRONLY) mode[0] = 'w';
+ else if (accmode == O_RDWR) mode[1] = '+';
+ if (val & O_APPEND) mode[0] = 'a';
+ $1 = fdopen(fd, mode);
+ if ($1 == NULL) {
+ PyErr_SetString(PyExc_TypeError, "Failed to open file.");
+ return NULL;
}
+ } else {
+ PyErr_SetString(PyExc_TypeError, "$1_name must be a file type.");
+ return NULL;
+ }
}
%pointer_class(int, intp);
@@ -65,6 +79,7 @@ int grib_c_count_in_file(FILE* f,int* OU
// grib handle operations
int grib_c_release(int* gid);
int grib_c_write(int* gid, FILE* f);
+
int grib_c_get_size_long(int* gid, char* key, long* OUTPUT);
int grib_c_get_string_length(int* gid, char* key, size_t* OUTPUT);
int grib_c_clone(int* gid,int* INOUT);
@@ -75,9 +90,9 @@ int grib_c_get_native_type(int* gid, cha
// ---
// multi support
+int grib_c_multi_write(int* gid, FILE* f);
int grib_c_multi_new(int* OUTPUT);
int grib_c_multi_support_on(void);
-int grib_c_multi_write(int* gid, FILE* f);
int grib_c_multi_support_off(void);
int grib_c_multi_release(int* gid);
int grib_c_multi_append(int* INPUT, int* INPUT,int* INPUT);
Index: grib-api-1.18.0/python/gribapi.py
===================================================================
--- grib-api-1.18.0.orig/python/gribapi.py
+++ grib-api-1.18.0/python/gribapi.py
@@ -30,8 +30,9 @@ NumPy support can be disabled by using t
"""
from __future__ import print_function
-import gribapi_swig as _internal
-import types
+from . import gribapi_swig as _internal
+from six import get_function_code
+from io import _io
import sys
import os
from array import array
@@ -48,6 +49,9 @@ KEYTYPES = {
# environment variable is defined
no_type_checks = os.environ.get('GRIB_API_PYTHON_NO_TYPE_CHECKS') is not None
+if sys.version_info.major == 3:
+ file = _io.TextIOWrapper
+ long = int
# Function-arguments type-checking decorator
# inspired from http://code.activestate.com/recipes/454322-type-checking-decorator/
@@ -62,12 +66,13 @@ def require(**_params_):
@wraps(_func_)
# The wrapper function. Replaces the target function and receives its args
def modified(*args, **kw):
- arg_names = _func_.__code__.func_code.co_varnames
+ code = get_function_code(_func_)
+ arg_names = code.co_varnames
# argnames, varargs, kwargs, defaults = inspect.getargspec(_func_)
kw.update(list(zip(arg_names, args)))
for name, allowed_types in _params_.items():
param = kw[name]
- if isinstance(allowed_types, types):
+ if isinstance(allowed_types, type):
allowed_types = (allowed_types,)
assert isinstance(param, allowed_types), \
"Parameter '%s' is of type %s and should be of type %s" % (name, type(param).__name__, " or ".join([t.__name__ for t in allowed_types]))
@@ -409,7 +414,7 @@ def grib_get_size(gribid, key):
"""
err, result = _internal.grib_c_get_size_long(gribid, key)
GRIB_CHECK(err)
- return result
+ return int(result)
@require(gribid=int, key=str)
Index: grib-api-1.18.0/examples/python/include.ctest.sh.in
===================================================================
--- grib-api-1.18.0.orig/examples/python/include.ctest.sh.in
+++ grib-api-1.18.0/examples/python/include.ctest.sh.in
@@ -24,5 +24,5 @@ samp_dir="@CMAKE_BINARY_DIR@/share/@PROJ
GRIB_SAMPLES_PATH=${samp_dir}
export GRIB_SAMPLES_PATH
-PYTHONPATH=$proj_dir/python:@PROJECT_BINARY_DIR@/python:$PYTHONPATH
+PYTHONPATH=$PYTHONPATH:$proj_dir/python:@PROJECT_BINARY_DIR@/python
export PYTHONPATH
|