Commit 6062ded3 authored by André Anjos's avatar André Anjos 💬

Merge branch 'PyBobIo_FilenameConverter' into 'master'

Switched to new API of FilenameConverter

See merge request !6
parents 521fe146 f28817da
Pipeline #11715 passed with stages
in 7 minutes and 40 seconds
...@@ -56,22 +56,14 @@ static int PyBobLearnLibsvmFile_init ...@@ -56,22 +56,14 @@ static int PyBobLearnLibsvmFile_init
static const char* const_kwlist[] = {"path", 0}; static const char* const_kwlist[] = {"path", 0};
static char** kwlist = const_cast<char**>(const_kwlist); static char** kwlist = const_cast<char**>(const_kwlist);
PyObject* filename = 0; const char* filename = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&", kwlist, if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&", kwlist,
&PyBobIo_FilenameConverter, &filename)) &PyBobIo_FilenameConverter, &filename))
return -1; return -1;
auto filename_ = make_safe(filename);
#if PY_VERSION_HEX >= 0x03000000
const char* c_filename = PyBytes_AS_STRING(filename);
#else
const char* c_filename = PyString_AS_STRING(filename);
#endif
try { try {
self->cxx = new bob::learn::libsvm::File(c_filename); self->cxx = new bob::learn::libsvm::File(filename);
} }
catch (std::exception& ex) { catch (std::exception& ex) {
PyErr_SetString(PyExc_RuntimeError, ex.what()); PyErr_SetString(PyExc_RuntimeError, ex.what());
...@@ -488,10 +480,10 @@ static PyObject* PyBobLearnLibsvmFile_read_all ...@@ -488,10 +480,10 @@ static PyObject* PyBobLearnLibsvmFile_read_all
auto bzval = PyBlitzArrayCxx_AsBlitz<double,2>(values); auto bzval = PyBlitzArrayCxx_AsBlitz<double,2>(values);
blitz::Range all = blitz::Range::all(); blitz::Range all = blitz::Range::all();
int k = 0; int k = 0;
while ((self->cxx->good()) && ((size_t)k < self->cxx->samples())) { while ((self->cxx->good()) && ((size_t)k < self->cxx->samples())) {
blitz::Array<double,1> v_ = (*bzval)(k, all); blitz::Array<double,1> v_ = (*bzval)(k, all);
int label = 0; int label = 0;
bool ok = self->cxx->read_(label, v_); bool ok = self->cxx->read_(label, v_);
if (ok) (*bzlab)(k) = label; if (ok) (*bzlab)(k) = label;
......
...@@ -69,22 +69,14 @@ static int PyBobLearnLibsvmMachine_init_svmfile ...@@ -69,22 +69,14 @@ static int PyBobLearnLibsvmMachine_init_svmfile
static const char* const_kwlist[] = {"filename", 0}; static const char* const_kwlist[] = {"filename", 0};
static char** kwlist = const_cast<char**>(const_kwlist); static char** kwlist = const_cast<char**>(const_kwlist);
PyObject* filename = 0; const char* filename = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&", kwlist, if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&", kwlist,
&PyBobIo_FilenameConverter, &filename)) &PyBobIo_FilenameConverter, &filename))
return -1; return -1;
auto filename_ = make_safe(filename);
#if PY_VERSION_HEX >= 0x03000000
const char* c_filename = PyBytes_AS_STRING(filename);
#else
const char* c_filename = PyString_AS_STRING(filename);
#endif
try { try {
self->cxx = new bob::learn::libsvm::Machine(c_filename); self->cxx = new bob::learn::libsvm::Machine(filename);
} }
catch (std::exception& ex) { catch (std::exception& ex) {
PyErr_SetString(PyExc_RuntimeError, ex.what()); PyErr_SetString(PyExc_RuntimeError, ex.what());
...@@ -985,31 +977,22 @@ static PyObject* PyBobLearnLibsvmMachine_Save ...@@ -985,31 +977,22 @@ static PyObject* PyBobLearnLibsvmMachine_Save
} }
// try a filename conversion and use libsvm's original file format // try a filename conversion and use libsvm's original file format
PyObject* filename = 0; const char* filename = 0;
int ok = PyBobIo_FilenameConverter(f, &filename); int ok = PyBobIo_FilenameConverter(f, &filename);
if (!ok) { if (!ok) {
PyErr_Format(PyExc_TypeError, "cannot convert `%s' into a valid string for a file path - objects of type `%s' can only save to HDF5 files or text files using LIBSVM's original file format (pass a string referring to a valid filesystem path in this case)", Py_TYPE(f)->tp_name, Py_TYPE(self)->tp_name); PyErr_Format(PyExc_TypeError, "cannot convert `%s' into a valid string for a file path - objects of type `%s' can only save to HDF5 files or text files using LIBSVM's original file format (pass a string referring to a valid filesystem path in this case)", Py_TYPE(f)->tp_name, Py_TYPE(self)->tp_name);
return 0; return 0;
} }
// at this point we know we have a valid file system string
auto filename_ = make_safe(filename);
#if PY_VERSION_HEX >= 0x03000000
const char* c_filename = PyBytes_AS_STRING(filename);
#else
const char* c_filename = PyString_AS_STRING(filename);
#endif
try { try {
self->cxx->save(c_filename); self->cxx->save(filename);
} }
catch (std::exception& ex) { catch (std::exception& ex) {
PyErr_SetString(PyExc_RuntimeError, ex.what()); PyErr_SetString(PyExc_RuntimeError, ex.what());
return 0; return 0;
} }
catch (...) { catch (...) {
PyErr_Format(PyExc_RuntimeError, "`%s' cannot write data to file `%s' (using LIBSVM's original text format): unknown exception caught", Py_TYPE(self)->tp_name, c_filename); PyErr_Format(PyExc_RuntimeError, "`%s' cannot write data to file `%s' (using LIBSVM's original text format): unknown exception caught", Py_TYPE(self)->tp_name, filename);
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment