From: Axel R. <ro...@us...> - 2014-05-23 10:33:17
|
Update of /cvsroot/sdif/Easdif/swig/python In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv18608 Modified Files: easdif-python.i Log Message: Changed so that ReadNext..Frame methods properly throw EOF exception. Index: easdif-python.i =================================================================== RCS file: /cvsroot/sdif/Easdif/swig/python/easdif-python.i,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** easdif-python.i 21 May 2014 23:55:21 -0000 1.16 --- easdif-python.i 23 May 2014 10:33:15 -0000 1.17 *************** *** 6,9 **** --- 6,12 ---- // // $Log$ + // Revision 1.17 2014/05/23 10:33:15 roebel + // Changed so that ReadNext..Frame methods properly throw EOF exception. + // // Revision 1.16 2014/05/21 23:55:21 roebel // Fixed FrameType constructor to accept python strings with exactly 4 chars. *************** *** 76,79 **** --- 79,116 ---- %include ../easdif-defines.i + %exception ReadNextFrame { + try { + $action + } catch(const SDIFEof& e) { + PyErr_SetString(PyExc_EOFError,e.getmessage().c_str()); + return 0; + } catch(const SDIFException& e) { + PyErr_SetString(PyExc_IOError,e.getmessage().c_str()); + return 0; + } catch(const std::exception& e) { + PyErr_SetString(PyExc_IOError, e.what()); + return 0; + } catch(...) { + SWIG_exception(SWIG_UnknownError,"Unknown exception"); + } + } + + %exception ReadNextSelectedFrame { + try { + $action + } catch(const SDIFEof& e) { + PyErr_SetString(PyExc_EOFError,e.getmessage().c_str()); + return 0; + } catch(const SDIFException& e) { + PyErr_SetString(PyExc_IOError,e.getmessage().c_str()); + return 0; + } catch(const std::exception& e) { + PyErr_SetString(PyExc_IOError, e.what()); + return 0; + } catch(...) { + SWIG_exception(SWIG_UnknownError,"Unknown exception"); + } + } + %{ #include "easdif/easdif.h" *************** *** 232,239 **** --- 269,311 ---- } + + #ifdef USE_NUMPY + %exception GetColA { + $action + if( result == 0) { + PyErr_SetString(PyExc_IndexError,"GetColA::output array does not match column size"); + return 0; + } + } + %exception SetColA { + $action + if( result == 0) { + PyErr_SetString(PyExc_IndexError,"SetColA::input array does not match column size"); + return 0; + } + } + %exception GetRowA { + $action + if( result == 0) { + PyErr_SetString(PyExc_IndexError,"GetRowA::output array does not match row size"); + return 0; + } + } + %exception SetRowA { + $action + if( result == 0) { + PyErr_SetString(PyExc_IndexError,"SetRowA::input array does not match row size"); + return 0; + } + } + + + + %extend Easdif::SDIFMatrix { + %apply (double* INPLACE_ARRAY1, int DIM1) {(double *outarr, int outarrsize)} %apply (double* IN_ARRAY1, int DIM1) {(const double *inarr, int inarrsize)} *************** *** 255,272 **** %apply (unsigned int* IN_ARRAY1, int DIM1) {(const unsigned int *inarr, int inarrsize)} ! #define MakeGetColA(type) void \ GetColA(type * outarr, int outarrsize, int col) { \ if ($self->GetNbRows() != outarrsize ) {\ ! throw std::runtime_error("GetColA::output array does not match column size"); \ }\ $self->GetCol(outarr, col); \ ! return; \ } ! void GetColA(signed char * outarr, int outarrsize, int col) { if ($self->GetNbRows() != outarrsize ) { ! throw std::runtime_error("GetColA::output array does not match column size"); } $self->GetCol(reinterpret_cast<char*> (outarr), col); ! return; } --- 327,344 ---- %apply (unsigned int* IN_ARRAY1, int DIM1) {(const unsigned int *inarr, int inarrsize)} ! #define MakeGetColA(type) int \ GetColA(type * outarr, int outarrsize, int col) { \ if ($self->GetNbRows() != outarrsize ) {\ ! return 0; \ }\ $self->GetCol(outarr, col); \ ! return 1; \ } ! int GetColA(signed char * outarr, int outarrsize, int col) { if ($self->GetNbRows() != outarrsize ) { ! return 0; } $self->GetCol(reinterpret_cast<char*> (outarr), col); ! return 1; } *************** *** 280,298 **** MakeGetColA(char); ! #define MakeGetRowA(type) void \ GetRowA(type * outarr, int outarrsize, int col) { \ if ($self->GetNbCols() != outarrsize ) {\ ! throw std::runtime_error("GetRowA::output array does not match row size"); \ }\ $self->GetRow(outarr, col); \ ! return; \ } ! void GetRowA(signed char * outarr, int outarrsize, int col) { if ($self->GetNbRows() != outarrsize ) { ! throw std::runtime_error("GetRowA::output array does not match column size"); } $self->GetRow(reinterpret_cast<char*> (outarr), col); ! return; } --- 352,370 ---- MakeGetColA(char); ! #define MakeGetRowA(type) int \ GetRowA(type * outarr, int outarrsize, int col) { \ if ($self->GetNbCols() != outarrsize ) {\ ! return 0; \ }\ $self->GetRow(outarr, col); \ ! return 1; \ } ! int GetRowA(signed char * outarr, int outarrsize, int col) { if ($self->GetNbRows() != outarrsize ) { ! return 0; } $self->GetRow(reinterpret_cast<char*> (outarr), col); ! return 1; } *************** *** 306,320 **** MakeGetRowA(char); ! #define MakeSetColA(type) void \ SetColA(const type * inarr, int inarrsize, int col) { \ if ($self->GetNbRows() != inarrsize ) {\ ! throw std::runtime_error("SetColA::input array does not match column size"); \ }\ $self->SetCol(inarr, col); \ ! return; \ } - - MakeSetColA(double); MakeSetColA(float); --- 378,390 ---- MakeGetRowA(char); ! #define MakeSetColA(type) int \ SetColA(const type * inarr, int inarrsize, int col) { \ if ($self->GetNbRows() != inarrsize ) {\ ! return 0; \ }\ $self->SetCol(inarr, col); \ ! return 1; \ } MakeSetColA(double); MakeSetColA(float); *************** *** 326,344 **** MakeSetColA(char); ! void SetColA(const signed char * outarr, int outarrsize, int col) { if ($self->GetNbRows() != outarrsize ) { ! throw std::runtime_error("SetColA::output array does not match column size"); } $self->SetCol(reinterpret_cast<const char*> (outarr), col); ! return; } ! #define MakeSetRowA(type) void \ SetRowA(const type * inarr, int inarrsize, int col) { \ if ($self->GetNbCols() != inarrsize ) {\ ! throw std::runtime_error("SetRowA::input array does not match row size"); \ }\ $self->SetRow(inarr, col); \ ! return; \ } --- 396,414 ---- MakeSetColA(char); ! int SetColA(const signed char * outarr, int outarrsize, int col) { if ($self->GetNbRows() != outarrsize ) { ! return 0; } $self->SetCol(reinterpret_cast<const char*> (outarr), col); ! return 1; } ! #define MakeSetRowA(type) int \ SetRowA(const type * inarr, int inarrsize, int col) { \ if ($self->GetNbCols() != inarrsize ) {\ ! return 0; \ }\ $self->SetRow(inarr, col); \ ! return 1; \ } *************** *** 353,364 **** MakeSetRowA(char); ! void SetRowA(const signed char * outarr, int outarrsize, int col) { if ($self->GetNbRows() != outarrsize ) { ! throw std::runtime_error("SetRowA::output array does not match column size"); } $self->SetRow(reinterpret_cast<const char*> (outarr), col); ! return; } } #endif --- 423,493 ---- MakeSetRowA(char); ! int SetRowA(const signed char * outarr, int outarrsize, int col) { if ($self->GetNbRows() != outarrsize ) { ! return 0; } $self->SetRow(reinterpret_cast<const char*> (outarr), col); ! return 1; ! } ! ! ! PyObject* ! GetMatrixData() { ! int ssrows = $self->GetNbRows(); ! int sscols = $self->GetNbCols(); ! int type = 0; ! int internal_type = $self->GetType(); ! if (internal_type == eFloat8) ! type =NPY_DOUBLE; ! else if (internal_type == eFloat4) ! type =NPY_FLOAT32; ! else if (internal_type == eInt8) ! type =NPY_INT64; ! else if (internal_type == eUInt8) ! type =NPY_UINT64; ! else if (internal_type == eInt4) ! type =NPY_INT32; ! else if (internal_type == eUInt4) ! type =NPY_UINT32; ! else if (internal_type == eInt2) ! type =NPY_INT16; ! else if (internal_type == eUInt2) ! type =NPY_UINT16; ! ! npy_intp ss[2]; ! ss[0] = ssrows; ! ss[1] = sscols; ! PyArrayObject *pa = reinterpret_cast<PyArrayObject*>(PyArray_SimpleNew(2, ss, type)); ! if (!pa) { ! PyErr_SetString(PyExc_RuntimeError, "cannot create umpy array for matrix content\n"); ! return 0; ! } ! // or fortran style column continuous which is the format used in SDIF ! PyArray_ENABLEFLAGS(pa, NPY_ARRAY_F_CONTIGUOUS); ! PyArray_CLEARFLAGS(pa, NPY_ARRAY_C_CONTIGUOUS); ! if (ssrows * sscols) { ! void *pp = PyArray_DATA(pa); ! memcpy(pp, $self->GetData(), ssrows*sscols*$self->GetElementSize()); ! } ! return reinterpret_cast<PyObject*>(pa); ! } ! ! int GetRowA(signed char * outarr, int outarrsize, int col) { ! if ($self->GetNbRows() != outarrsize ) { ! return 0; ! } ! $self->GetRow(reinterpret_cast<char*> (outarr), col); ! return 1; } + MakeGetRowA(double); + MakeGetRowA(float); + MakeGetRowA(unsigned int); + MakeGetRowA(unsigned short); + MakeGetRowA(unsigned char); + MakeGetRowA(int); + MakeGetRowA(short); + MakeGetRowA(char); + } #endif |