Salome HOME
Porting SWIG to Python3. V2_4_1
authorOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Mon, 1 Oct 2018 12:29:08 +0000 (14:29 +0200)
committerOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Mon, 1 Oct 2018 12:29:08 +0000 (14:29 +0200)
src/Python/libBatch_Swig_typemap.i

index cbd1a23e58359a482a26db8db0dfdfe31cfbfc58..5e23259cae4e53fb5237d8f3fdbd16cba12a1560 100644 (file)
 # %typemap(in) SWIGTYPE ;
 
 %{
+static std::string pystr2stdstr(PyObject * val)
+{
+  std::string str_val;
+  if (PyUnicode_Check(val))
+  {
+    PyObject * temp_bytes = PyUnicode_AsEncodedString(val, "UTF-8", "strict");
+    if (temp_bytes != NULL)
+    {
+      str_val = PyBytes_AS_STRING(temp_bytes);
+      Py_DECREF(temp_bytes);
+    }
+  }
+  return str_val;
+}
+
 // Helper function to initialize a Batch::Versatile from a PyObj
 static bool initVersatile(Batch::Versatile & newVersatile, PyObject * input)
 {
   if (PyList_Check(input)) { // c'est une liste
     for (Py_ssize_t i=0; i<PyList_Size(input); i++) {
       PyObject * val = PyList_GetItem(input, i);
-      if (PyString_Check(val)) {
-        newVersatile += PyString_AsString(val);
-
+      if (PyUnicode_Check(val)) {
+        newVersatile += pystr2stdstr(val);
       } else if (PyTuple_Check(val) &&
           (PyTuple_Size(val) == 2) &&
-          PyString_Check( PyTuple_GetItem(val,0) ) &&
-          PyString_Check( PyTuple_GetItem(val,1) )   ) {
-        newVersatile += Batch::Couple( PyString_AsString( PyTuple_GetItem(val,0) ),
-                                       PyString_AsString( PyTuple_GetItem(val,1) )
-                                     );
+          PyUnicode_Check( PyTuple_GetItem(val,0) ) &&
+          PyUnicode_Check( PyTuple_GetItem(val,1) )   ) {
+        newVersatile += Batch::Couple( pystr2stdstr(PyTuple_GetItem(val,0)),
+                                       pystr2stdstr(PyTuple_GetItem(val,1)));
 
       } else {
         PyErr_SetString(PyExc_RuntimeWarning, "initVersatile : invalid PyObject");
@@ -66,8 +79,8 @@ static bool initVersatile(Batch::Versatile & newVersatile, PyObject * input)
       }
     }
 
-  } else if (PyString_Check(input)) { // c'est une string
-    newVersatile = PyString_AsString(input);
+  } else if (PyUnicode_Check(input)) { // c'est une string
+    newVersatile = pystr2stdstr(input);
   } else if (PyBool_Check(input)) { // c'est un bool
     newVersatile = (input == Py_True);
   } else if (PyInt_Check(input)) { // c'est un int
@@ -156,7 +169,7 @@ static bool initParameter(Batch::Parametre & newParam, PyObject * input)
     PyObject *key, *value;
     Py_ssize_t pos = 0;
     while (PyDict_Next(input, &pos, &key, &value)) {
-      std::string mk = PyString_AsString(key);
+      std::string mk = pystr2stdstr(key);
       bool res = initVersatile(newParam[mk], value);
       if (!res)
         return false;
@@ -186,8 +199,8 @@ static bool initEnvironment(Batch::Environnement & newEnv, PyObject * input)
   PyObject *key, *value;
   Py_ssize_t pos = 0;
   while (PyDict_Next(input, &pos, &key, &value)) {
-    std::string mk  = PyString_AsString(key);
-    std::string val = PyString_AsString(value);
+    std::string mk  = pystr2stdstr(key);
+    std::string val = pystr2stdstr(value);
     newEnv[mk] = val;
   }
   return true;