From 7a2c9f9348e1bf6bd95033d0b4da09a243fd9e6d Mon Sep 17 00:00:00 2001
From: Even Rouault <even.rouault@spatialys.com>
Date: Mon, 8 Jul 2019 14:34:55 +0200
Subject: [PATCH] Python bindings: add compatibility with SWIG 4.0 (based on
 patch by @perestoronin, fixes #1702)

---
 gdal/swig/include/gdal_array.i                  |  6 ++++++
 gdal/swig/include/python/typemaps_python.i      |  9 ++++++++-
 gdal/swig/python/extensions/gdal_array_wrap.cpp | 15 ++++++++++++++-
 gdal/swig/python/extensions/gdal_wrap.cpp       |  9 ++++++++-
 4 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/swig/include/gdal_array.i b/swig/include/gdal_array.i
index c4f6f19b610..bf053eb2476 100644
--- a/swig/include/gdal_array.i
+++ b/swig/include/gdal_array.i
@@ -899,12 +899,18 @@ retStringAndCPLFree* GetArrayFilename(PyArrayObject *psArray)
     }
 
     /* Keep a reference to the VirtualMem object */
+%#if SWIGVERSION >= 0x040000
+%#define obj0 swig_obj[0]
+%#endif
 %#if NPY_API_VERSION >= 0x00000007
     PyArray_SetBaseObject(ar, obj0);
 %#else
     PyArray_BASE(ar) = obj0;
 %#endif
     Py_INCREF(obj0);
+%#if SWIGVERSION >= 0x040000
+%#undef obj0
+%#endif
     Py_DECREF($result);
     $result = (PyObject*) ar;
 }
diff --git a/swig/include/python/typemaps_python.i b/swig/include/python/typemaps_python.i
index 5c9f36ab278..9266422e9c4 100644
--- a/swig/include/python/typemaps_python.i
+++ b/swig/include/python/typemaps_python.i
@@ -2104,7 +2104,14 @@ DecomposeSequenceOf4DCoordinates( PyObject *seq, int nCount, double *x, double *
 %#if PY_VERSION_HEX >= 0x02070000
   /* %typemap(argout) (void** pptr, size_t* pnsize, GDALDataType* pdatatype, int* preadonly)*/
   Py_buffer *buf=(Py_buffer*)malloc(sizeof(Py_buffer));
-  if (PyBuffer_FillInfo(buf,  obj0,  *($1), *($2), *($4), PyBUF_ND)) {
+
+  if (PyBuffer_FillInfo(buf,
+%#if SWIGVERSION >= 0x040000
+                        swig_obj[0],
+%#else
+                        obj0,
+%#endif
+                        *($1), *($2), *($4), PyBUF_ND)) {
     // error, handle
   }
   if( *($3) == GDT_Byte )
diff --git a/swig/python/extensions/gdal_array_wrap.cpp b/swig/python/extensions/gdal_array_wrap.cpp
index 807e12dd4a0..4435803e6d9 100644
--- a/swig/python/extensions/gdal_array_wrap.cpp
+++ b/swig/python/extensions/gdal_array_wrap.cpp
@@ -4598,7 +4598,14 @@ SWIGINTERN PyObject *_wrap_VirtualMem_GetAddr(PyObject *SWIGUNUSEDPARM(self), Py
 #if PY_VERSION_HEX >= 0x02070000
     /* %typemap(argout) (void** pptr, size_t* pnsize, GDALDataType* pdatatype, int* preadonly)*/
     Py_buffer *buf=(Py_buffer*)malloc(sizeof(Py_buffer));
-    if (PyBuffer_FillInfo(buf,  obj0,  *(arg2), *(arg3), *(arg5), PyBUF_ND)) {
+    
+    if (PyBuffer_FillInfo(buf,
+    #if SWIGVERSION >= 0x040000
+        swig_obj[0],
+    #else
+        obj0,
+    #endif
+        *(arg2), *(arg3), *(arg5), PyBUF_ND)) {
       // error, handle
     }
     if( *(arg4) == GDT_Byte )
@@ -5597,12 +5604,18 @@ SWIGINTERN PyObject *_wrap_VirtualMemGetArray(PyObject *SWIGUNUSEDPARM(self), Py
     }
     
     /* Keep a reference to the VirtualMem object */
+#if SWIGVERSION >= 0x040000
+#define obj0 swig_obj[0]
+#endif
 #if NPY_API_VERSION >= 0x00000007
     PyArray_SetBaseObject(ar, obj0);
 #else
     PyArray_BASE(ar) = obj0;
 #endif
     Py_INCREF(obj0);
+#if SWIGVERSION >= 0x040000
+#undef obj0
+#endif
     Py_DECREF(resultobj);
     resultobj = (PyObject*) ar;
   }
diff --git a/swig/python/extensions/gdal_wrap.cpp b/swig/python/extensions/gdal_wrap.cpp
index 37cb635339e..f2f014fd0b5 100644
--- a/swig/python/extensions/gdal_wrap.cpp
+++ b/swig/python/extensions/gdal_wrap.cpp
@@ -15238,7 +15238,14 @@ SWIGINTERN PyObject *_wrap_VirtualMem_GetAddr(PyObject *SWIGUNUSEDPARM(self), Py
 #if PY_VERSION_HEX >= 0x02070000
     /* %typemap(argout) (void** pptr, size_t* pnsize, GDALDataType* pdatatype, int* preadonly)*/
     Py_buffer *buf=(Py_buffer*)malloc(sizeof(Py_buffer));
-    if (PyBuffer_FillInfo(buf,  obj0,  *(arg2), *(arg3), *(arg5), PyBUF_ND)) {
+    
+    if (PyBuffer_FillInfo(buf,
+    #if SWIGVERSION >= 0x040000
+        swig_obj[0],
+    #else
+        obj0,
+    #endif
+        *(arg2), *(arg3), *(arg5), PyBUF_ND)) {
       // error, handle
     }
     if( *(arg4) == GDT_Byte )
