Fixed issue #8. Segmentation fault when using the __repr__ method

parent 59e3341c
...@@ -444,11 +444,11 @@ PyObject* PyBobLearnLibsvmMachine_Repr(PyBobLearnLibsvmMachineObject* self) { ...@@ -444,11 +444,11 @@ PyObject* PyBobLearnLibsvmMachine_Repr(PyBobLearnLibsvmMachineObject* self) {
* <bob.learn.libsvm.Machine float64@(3, 2)> * <bob.learn.libsvm.Machine float64@(3, 2)>
*/ */
auto shape = make_safe(PyObject_GetAttrString((PyObject*)self, "shape")); PyObject* shape = Py_BuildValue("(nn)", self->cxx->inputSize(),
auto shape_str = make_safe(PyObject_Str(shape.get())); self->cxx->outputSize());
PyObject* retval = PyUnicode_FromFormat("<%s float64@%U>", PyObject* retval = PyUnicode_FromFormat("<%s float64@%U>",
Py_TYPE(self)->tp_name, shape_str.get()); Py_TYPE(self)->tp_name, PyObject_Unicode(shape));
#if PYTHON_VERSION_HEX < 0x03000000 #if PYTHON_VERSION_HEX < 0x03000000
if (!retval) return 0; if (!retval) return 0;
......
...@@ -79,6 +79,7 @@ def test_can_load(): ...@@ -79,6 +79,7 @@ def test_can_load():
assert -1 in machine.labels assert -1 in machine.labels
assert +1 in machine.labels assert +1 in machine.labels
assert abs(machine.gamma - 0.0769231) < 1e-6 assert abs(machine.gamma - 0.0769231) < 1e-6
assert type(machine.__repr__()) is str
def test_can_save(): def test_can_save():
...@@ -251,6 +252,7 @@ def test_correctness_iris(): ...@@ -251,6 +252,7 @@ def test_correctness_iris():
pred_labels, pred_probs = machine.predict_class_and_probabilities(data) pred_labels, pred_probs = machine.predict_class_and_probabilities(data)
assert numpy.array_equal(pred_labels, real_labels) assert numpy.array_equal(pred_labels, real_labels)
assert numpy.all(abs(numpy.vstack(pred_probs) - numpy.vstack(real_probs)) < 1e-6) assert numpy.all(abs(numpy.vstack(pred_probs) - numpy.vstack(real_probs)) < 1e-6)
@nose.tools.raises(RuntimeError) @nose.tools.raises(RuntimeError)
def test_correctness_inputsize_exceeds(): def test_correctness_inputsize_exceeds():
......
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