2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-21 17:12:22 +00:00

Fix lots of bugs in the numeric interface and tests.

Tests:
* Coerce a result to bool to deal with Python's new Bool type
* Better reporting of mismatches in expected and received results
* Remove bogus nullary y.astype() call
* Fix all uses of trace and diagonal so they don't cause errors
* Use appropriate typecodes
* Use doctest detailed API to run just the relevant tests
* Factor out error handling from macro

API:
* Added get_module_name() function to get current numeric module
* new_(x) now returns an array instead of object
* Fixed the signatures of the factory() family of functions
* Updated docs accordingly.


[SVN r35528]
This commit is contained in:
Dave Abrahams
2006-10-09 04:05:25 +00:00
parent 545be29ad3
commit 49d4aac8ec
5 changed files with 267 additions and 210 deletions

View File

@@ -7,6 +7,7 @@
#include <boost/python/tuple.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/str.hpp>
using namespace boost::python;
@@ -39,19 +40,28 @@ void info(numeric::array const& z)
z.info();
}
namespace
{
object handle_error()
{
PyObject* type, *value, *traceback;
PyErr_Fetch(&type, &value, &traceback);
handle<> ty(type), v(value), tr(traceback);
return object("exception");
str format("exception type: %sn");
format += "exception value: %sn";
format += "traceback:n%s" ;
object ret = format % boost::python::make_tuple(ty, v, tr);
return ret;
}
}
#define CHECK(expr) \
{ \
object result; \
try { result = object(expr); } \
catch(error_already_set) \
{ \
PyObject* type, *value, *traceback; \
PyErr_Fetch(&type, &value, &traceback); \
handle<> ty(type), v(value), tr(traceback); \
str format("exception type: %s\n"); \
format += "exception value: %s\n"; \
format += "traceback:\n%s" ; \
result = format % boost::python::make_tuple(ty, v, tr); \
result = handle_error(); \
} \
check(result); \
}
@@ -73,7 +83,7 @@ void exercise(numeric::array& y, object check)
// the results of corresponding python operations.
void exercise_numarray(numeric::array& y, object check)
{
CHECK(y.astype());
CHECK(str(y));
CHECK(y.argmax());
CHECK(y.argmax(0));
@@ -89,7 +99,7 @@ void exercise_numarray(numeric::array& y, object check)
CHECK(y.diagonal());
CHECK(y.diagonal(1));
CHECK(y.diagonal(0, 1));
CHECK(y.diagonal(0, 0));
CHECK(y.diagonal(0, 1, 0));
CHECK(y.is_c_array());
@@ -97,19 +107,22 @@ void exercise_numarray(numeric::array& y, object check)
CHECK(y.trace());
CHECK(y.trace(1));
CHECK(y.trace(0, 1));
CHECK(y.trace(0, 0));
CHECK(y.trace(0, 1, 0));
CHECK(y.new_('D'));
CHECK(y.new_("D").getshape());
CHECK(y.new_("D").type());
y.sort();
CHECK(y);
CHECK(y.type());
CHECK(y.factory(make_tuple(1.2, 3.4)));
CHECK(y.factory(make_tuple(1.2, 3.4), "Double"));
CHECK(y.factory(make_tuple(1.2, 3.4), "Double", make_tuple(1,2,1)));
CHECK(y.factory(make_tuple(1.2, 3.4), "Double", make_tuple(2,1,1), false));
CHECK(y.factory(make_tuple(1.2, 3.4), "Double", make_tuple(2), true, true));
CHECK(y.factory(make_tuple(1.2, 3.4), "f8"));
CHECK(y.factory(make_tuple(1.2, 3.4), "f8", true));
CHECK(y.factory(make_tuple(1.2, 3.4), "f8", true, false));
CHECK(y.factory(make_tuple(1.2, 3.4), "f8", true, false, object()));
CHECK (y.factory(make_tuple(1.2, 3.4), "f8", true, false, object(), make_tuple(1,2,1)));
}
BOOST_PYTHON_MODULE(numpy_ext)
@@ -119,6 +132,7 @@ BOOST_PYTHON_MODULE(numpy_ext)
def("exercise", exercise);
def("exercise_numarray", exercise_numarray);
def("set_module_and_type", &numeric::array::set_module_and_type);
def("get_module_name", &numeric::array::get_module_name);
def("info", info);
}