From 75e62bb9fca77d0a9bc2be8a5344b32d0e8e4831 Mon Sep 17 00:00:00 2001
From: Manuel Gunther <siebenkopf@googlemail.com>
Date: Fri, 18 Aug 2017 14:05:18 -0600
Subject: [PATCH] Implemented better PyBobIo_FilenameConverter that directly
 returns const char*

---
 bob/io/base/file.cpp                     | 45 ++++++++++--------------
 bob/io/base/hdf5.cpp                     | 12 ++-----
 bob/io/base/include/bob.io.base/api.h    |  2 +-
 bob/io/base/include/bob.io.base/config.h |  2 +-
 version.txt                              |  2 +-
 5 files changed, 24 insertions(+), 39 deletions(-)

diff --git a/bob/io/base/file.cpp b/bob/io/base/file.cpp
index 859ad36..8856bac 100644
--- a/bob/io/base/file.cpp
+++ b/bob/io/base/file.cpp
@@ -67,34 +67,35 @@ static void PyBobIoFile_Delete (PyBobIoFileObject* o) {
 
 }
 
-int PyBobIo_FilenameConverter (PyObject* o, PyObject** b) {
-#if PY_VERSION_HEX >= 0x03020000
-  if (!PyUnicode_FSConverter(o, b)) return 0;
-#else
+int PyBobIo_FilenameConverter (PyObject* o, const char** b) {
+#if PY_VERSION_HEX >= 0x03000000
   if (PyUnicode_Check(o)) {
-    *b = PyUnicode_AsEncodedString(o, Py_FileSystemDefaultEncoding, "strict");
+    *b = PyUnicode_AsUTF8(o);
+  } else {
+    PyObject* temp = PyObject_Bytes(o);
+    auto temp_ = make_safe(temp);
+    *b = PyBytes_AsString(temp);
   }
-  else {
-#if PY_VERSION_HEX >= 0x03000000
-    *b = PyObject_Bytes(o);
 #else
-    *b = PyObject_Str(o);
-#endif
+  if (PyUnicode_Check(o)) {
+    PyObject* temp = PyUnicode_AsEncodedString(o, Py_FileSystemDefaultEncoding, "strict");
+    auto temp_ = make_safe(temp);
+    *b = PyString_AsString(temp);
+  } else {
+    *b = PyString_AsString(o);
   }
-  if (!b) return 0;
 #endif
-  return 1;
+  return b != 0;
 }
 
 /* The __init__(self) method */
 static int PyBobIoFile_init(PyBobIoFileObject* self, PyObject *args, PyObject* kwds) {
-  const char* c_filename = 0;
 BOB_TRY
   /* Parses input arguments in a single shot */
   static char** kwlist = s_file.kwlist();
 
-  PyObject* filename = 0;
-  char* pretend_extension = 0;
+  const char* filename;
+  const char* pretend_extension = 0;
 
 #if PY_VERSION_HEX >= 0x03000000
 #  define MODE_CHAR "C"
@@ -109,28 +110,20 @@ BOB_TRY
 
 #undef MODE_CHAR
 
-  auto filename_ = make_safe(filename);
-
   if (mode != 'r' && mode != 'w' && mode != 'a') {
     PyErr_Format(PyExc_ValueError, "file open mode string should have 1 element and be either 'r' (read), 'w' (write) or 'a' (append)");
     return -1;
   }
 
-#if PY_VERSION_HEX >= 0x03000000
-  c_filename = PyBytes_AS_STRING(filename);
-#else
-  c_filename = PyString_AS_STRING(filename);
-#endif
-
   if (pretend_extension) {
-    self->f = bob::io::base::open(c_filename, mode, pretend_extension);
+    self->f = bob::io::base::open(filename, mode, pretend_extension);
   }
   else {
-    self->f = bob::io::base::open(c_filename, mode);
+    self->f = bob::io::base::open(filename, mode);
   }
 
   return 0; ///< SUCCESS
-BOB_CATCH_MEMBER(c_filename, -1);
+BOB_CATCH_MEMBER("constructor", -1);
 }
 
 static PyObject* PyBobIoFile_repr(PyBobIoFileObject* self) {
diff --git a/bob/io/base/hdf5.cpp b/bob/io/base/hdf5.cpp
index e9f7957..42347bd 100644
--- a/bob/io/base/hdf5.cpp
+++ b/bob/io/base/hdf5.cpp
@@ -137,15 +137,13 @@ BOB_TRY
   char mode = 'r';
 #endif
 
-  PyObject* filename = 0;
+  const char* filename;
   if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|" MODE_CHAR, kwlist1,
         &PyBobIo_FilenameConverter, &filename, &mode))
     return -1;
 
 #undef MODE_CHAR
 
-  auto filename_ = make_safe(filename);
-
   if (mode != 'r' && mode != 'w' && mode != 'a' && mode != 'x') {
     PyErr_Format(PyExc_ValueError, "file open mode string should have 1 element and be either 'r' (read), 'w' (write), 'a' (append), 'x' (exclusive)");
     return -1;
@@ -153,13 +151,7 @@ BOB_TRY
   bob::io::base::HDF5File::mode_t mode_mode = mode_from_char(mode);
   if (PyErr_Occurred()) return -1;
 
-#if PY_VERSION_HEX >= 0x03000000
-  const char* c_filename = PyBytes_AS_STRING(filename);
-#else
-  const char* c_filename = PyString_AS_STRING(filename);
-#endif
-
-  self->f.reset(new bob::io::base::HDF5File(c_filename, mode_mode));
+  self->f.reset(new bob::io::base::HDF5File(filename, mode_mode));
   return 0; ///< SUCCESS
 BOB_CATCH_MEMBER("hdf5 constructor", -1)
 }
diff --git a/bob/io/base/include/bob.io.base/api.h b/bob/io/base/include/bob.io.base/api.h
index fc7fb6f..fe714e4 100644
--- a/bob/io/base/include/bob.io.base/api.h
+++ b/bob/io/base/include/bob.io.base/api.h
@@ -92,7 +92,7 @@ typedef struct {
 #define PyBobIo_TypeInfoAsTuple_PROTO (const bob::io::base::array::typeinfo& ti)
 
 #define PyBobIo_FilenameConverter_RET int
-#define PyBobIo_FilenameConverter_PROTO (PyObject* o, PyObject** b)
+#define PyBobIo_FilenameConverter_PROTO (PyObject* o, const char** b)
 
 /*****************
  * HDF5 bindings *
diff --git a/bob/io/base/include/bob.io.base/config.h b/bob/io/base/include/bob.io.base/config.h
index a3f9627..cc07a9a 100644
--- a/bob/io/base/include/bob.io.base/config.h
+++ b/bob/io/base/include/bob.io.base/config.h
@@ -9,7 +9,7 @@
 #define BOB_IO_BASE_CONFIG_H
 
 /* Macros that define versions and important names */
-#define BOB_IO_BASE_API_VERSION 0x0200
+#define BOB_IO_BASE_API_VERSION 0x0201
 
 
 #ifdef BOB_IMPORT_VERSION
diff --git a/version.txt b/version.txt
index 2091b3b..875abfc 100644
--- a/version.txt
+++ b/version.txt
@@ -1 +1 @@
-2.1.1b0
\ No newline at end of file
+2.2.0b0
-- 
GitLab