From 8ab8764737a35352c010970946ec6634737d94d2 Mon Sep 17 00:00:00 2001 From: Ovidiu Mircescu Date: Mon, 1 Oct 2018 14:29:08 +0200 Subject: [PATCH 1/1] Porting SWIG to Python3. --- src/Python/libBatch_Swig_typemap.i | 39 ++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/src/Python/libBatch_Swig_typemap.i b/src/Python/libBatch_Swig_typemap.i index cbd1a23..5e23259 100644 --- a/src/Python/libBatch_Swig_typemap.i +++ b/src/Python/libBatch_Swig_typemap.i @@ -43,22 +43,35 @@ # %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