本文整理汇总了C++中PythonException函数的典型用法代码示例。如果您正苦于以下问题:C++ PythonException函数的具体用法?C++ PythonException怎么用?C++ PythonException使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PythonException函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getDerivativeSupportItemObjectAttr
EpetraExt::ModelEvaluator::DerivativeSupport
getDerivativeSupportItemObjectAttr(PyObject * object, CONST char * name, int i)
{
// The DerivativeSupport python class object
static
PyObject * classDerivativeSupport = NULL;
if (!classDerivativeSupport)
{
classDerivativeSupport = getClassFromModule(PyTrilinosEpetraExt, "DerivativeSupport");
if (!classDerivativeSupport) throw PythonException();
}
// Get the item from the object attribute
PyObject * tuple = getTupleObjectAttr(object, name);
PyObject * item = PyTuple_GetItem(tuple, i);
Py_DECREF(tuple);
if (!item) throw PythonException();
if (!PyObject_IsInstance(item, classDerivativeSupport))
{
PyErr_Format(PyExc_TypeError, "Attribute '%s' is not tuple of DerivativeSupport", name);
Py_DECREF(item);
throw PythonException();
}
EpetraExt::ModelEvaluator::EDerivativeLinearOp linearOp;
EpetraExt::ModelEvaluator::EDerivativeMultiVectorOrientation orientation;
EpetraExt::ModelEvaluator::DerivativeSupport result;
if (getBoolObjectAttr(item, "linearOp"))
result.plus(EpetraExt::ModelEvaluator::DERIV_LINEAR_OP);
if (getBoolObjectAttr(item, "mVByCol"))
result.plus(EpetraExt::ModelEvaluator::DERIV_MV_BY_COL);
if (getBoolObjectAttr(item, "transMVByRow"))
result.plus(EpetraExt::ModelEvaluator::DERIV_TRANS_MV_BY_ROW);
Py_DECREF(item);
return result;
}
示例2: matches
inline bool matches( PyObject *exception ) const
{
#if PYTHON_VERSION >= 300
if ( PyTuple_Check( exception ))
{
Py_ssize_t length = PyTuple_Size( exception );
for ( Py_ssize_t i = 0; i < length; i += 1 )
{
PyObject *element = PyTuple_GET_ITEM( exception, i );
if (unlikely( !PyExceptionClass_Check( element ) ))
{
PyErr_Format( PyExc_TypeError, "catching classes that do not inherit from BaseException is not allowed" );
throw PythonException();
}
}
}
else if (unlikely( !PyExceptionClass_Check( exception ) ))
{
PyErr_Format( PyExc_TypeError, "catching classes that do not inherit from BaseException is not allowed" );
throw PythonException();
}
#endif
return
PyErr_GivenExceptionMatches( this->exception_type, exception ) ||
PyErr_GivenExceptionMatches( this->exception_value, exception );
}
示例3: getEvaluationObjectAttr
EpetraExt::ModelEvaluator::Evaluation<Epetra_Vector>
getEvaluationObjectAttr(PyObject * object, CONST char * name)
{
// The Evaluation python object
static
PyObject * classEvaluation = NULL;
if (!classEvaluation)
{
classEvaluation = getClassFromModule(PyTrilinosEpetraExt, "Evaluation");
if (!classEvaluation) throw PythonException();
}
PyObject * value = PyObject_GetAttrString(object, name);
if (!value) throw PythonException();
if (!PyObject_IsInstance(value, classEvaluation))
{
PyErr_Format(PyExc_TypeError, "Attribute '%s' is not of type Evaluation", name);
Py_DECREF(value);
throw PythonException();
}
// vector attribute
Teuchos::RCP<Epetra_Vector> vector = getEpetraVectorObjectAttr(value, "vector");
// type attribute
EpetraExt::ModelEvaluator::EEvalType type;
CONST char * typeStr = getStringObjectAttr(value, "type");
if (strncmp(typeStr, "exact", 5) == 0)
type = EpetraExt::ModelEvaluator::EVAL_TYPE_EXACT;
if (strncmp(typeStr, "approx_deriv", 12) == 0)
type = EpetraExt::ModelEvaluator::EVAL_TYPE_APPROX_DERIV;
if (strncmp(typeStr, "very_approx_deriv", 17) == 0)
type = EpetraExt::ModelEvaluator::EVAL_TYPE_VERY_APPROX_DERIV;
Py_DECREF(value);
return EpetraExt::ModelEvaluator::Evaluation<Epetra_Vector>(vector, type);
}
示例4: getDerivativeSupportObjectAttr
EpetraExt::ModelEvaluator::DerivativeSupport
getDerivativeSupportObjectAttr(PyObject * object, CONST char * name)
{
static
PyObject * classDerivativeSupport = NULL;
if (!classDerivativeSupport)
{
classDerivativeSupport = getClassFromModule(PyTrilinosEpetraExt, "DerivativeSupport");
if (!classDerivativeSupport) throw PythonException();
}
PyObject * value = PyObject_GetAttrString(object, name);
if (!value) throw PythonException();
if (!PyObject_IsInstance(value, classDerivativeSupport))
{
PyErr_Format(PyExc_TypeError, "Attribute '%s' is not of type DerivativeSupport", name);
Py_DECREF(value);
throw PythonException();
}
EpetraExt::ModelEvaluator::EDerivativeLinearOp linearOp;
EpetraExt::ModelEvaluator::EDerivativeMultiVectorOrientation orientation;
EpetraExt::ModelEvaluator::DerivativeSupport result;
if (getBoolObjectAttr(value, "linearOp"))
result.plus(EpetraExt::ModelEvaluator::DERIV_LINEAR_OP);
if (getBoolObjectAttr(value, "mVByCol"))
result.plus(EpetraExt::ModelEvaluator::DERIV_MV_BY_COL);
if (getBoolObjectAttr(value, "transMVByRow"))
result.plus(EpetraExt::ModelEvaluator::DERIV_TRANS_MV_BY_ROW);
Py_DECREF(value);
return result;
}
示例5: PyArray_SimpleNew
// =============================================================================
int * Epetra_NumPyMultiVector::getRange(PyObject * range,
const Epetra_MultiVector & source)
{
// Handle the default case (range == NULL), which is to return a
// range of all the Epetra_MultiVector vectors
if (range == NULL)
{
npy_intp dims[ ] = { (npy_intp) source.NumVectors() };
tmp_range = (PyArrayObject *) PyArray_SimpleNew(1,dims,NPY_INT);
if (!tmp_range)
{
cleanup();
throw PythonException();
}
int * data = (int *) PyArray_DATA(tmp_range);
for (int i=0; i<dims[0]; i++) data[i] = i;
}
// Try to create a contiguous array of integers from the PyObject
if (!tmp_range)
tmp_range = (PyArrayObject *)
PyArray_ContiguousFromObject(range,NPY_INT,1,1);
// If this fails, clean up and throw a PythonException
if (!tmp_range)
{
cleanup();
throw PythonException();
}
// Obtain the length and return the array of integers
return (int *) (PyArray_DATA(tmp_range));
}
示例6: getConstEpetraVectorItemObjectAttr
Teuchos::RCP< const Epetra_Vector >
getConstEpetraVectorItemObjectAttr(PyObject * object,
CONST char * name,
int i)
{
static swig_type_info * swig_EV_ptr =
SWIG_TypeQuery("Teuchos::RCP< Epetra_Vector > *");
void * argp;
PyObject * tuple = getTupleObjectAttr(object, name);
PyObject * item = PyTuple_GetItem(tuple, i);
Py_DECREF(tuple);
if (!item) throw PythonException();
int newmem = 0;
if (!SWIG_CheckState(SWIG_Python_ConvertPtrAndOwn(item, &argp, swig_EV_ptr, 0, &newmem)))
{
PyErr_Format(PyExc_TypeError,
"Attribute '%s' is not tuple of type Epetra.Vector",
name);
Py_DECREF(item);
throw PythonException();
}
Teuchos::RCP< const Epetra_Vector > result =
*reinterpret_cast< Teuchos::RCP< const Epetra_Vector > * >(argp);
if (newmem)
delete reinterpret_cast< Teuchos::RCP< const Epetra_Vector > * >(argp);
Py_DECREF(item);
return result;
}
示例7: assert
PyObject *asObject0() const
{
assert( this->storage );
if ( this->storage->object == NULL )
{
PyErr_Format(
PyExc_UnboundLocalError,
"free variable '%s' referenced before assignment in enclosing scope",
Nuitka_String_AsString( this->storage->getVarName() )
);
throw PythonException();
}
if ( Py_REFCNT( this->storage->object ) == 0 )
{
PyErr_Format(
PyExc_UnboundLocalError,
"free variable '%s' referenced after its finalization in enclosing scope",
Nuitka_String_AsString( this->storage->getVarName() )
);
throw PythonException();
}
return this->storage->object;
}
示例8: PyArray_SimpleNew
// =============================================================================
int * Epetra_NumPyIntVector::getArray(const Epetra_BlockMap & blockMap,
PyObject * pyObject)
{
// Only build the tmp_array if it does not already exist
if (!tmp_array)
{
// Default dimensions
npy_intp defaultDims[ ] = { blockMap.NumMyPoints() };
// PyObject argument is a bool
if (PyBool_Check(pyObject))
{
tmp_array = (PyArrayObject *)
PyArray_SimpleNew(1,defaultDims,NPY_INT);
}
// PyObject argument is not a bool ... try to build a contiguous
// PyArrayObject from it
else
{
tmp_array = (PyArrayObject *)
PyArray_ContiguousFromObject(pyObject,NPY_INT,0,0);
}
// If any PyArray factory functions fail, clean up and throw a
// PythonException
if (!tmp_array)
{
cleanup();
throw PythonException();
}
int nd = PyArray_NDIM(tmp_array);
npy_intp arraySize = PyArray_MultiplyList(PyArray_DIMS(tmp_array),nd);
if (arraySize != defaultDims[0])
{
PyArrayObject * myArray = (PyArrayObject *)
PyArray_SimpleNew(1,defaultDims,NPY_INT);
if (!myArray)
{
cleanup();
throw PythonException();
}
int * myData = (int *) PyArray_DATA(myArray);
int * tmpData = (int *) PyArray_DATA(tmp_array);
for (int i=0; i<defaultDims[0]; i++)
{
myData[i] = tmpData[i];
}
Py_XDECREF(tmp_array);
tmp_array = myArray;
}
}
return (int*)(PyArray_DATA(tmp_array));
}
示例9: CONVERT_TO_INDEX
NUITKA_MAY_BE_UNUSED static Py_ssize_t CONVERT_TO_INDEX( PyObject *value )
{
assertObject( value );
#if PYTHON_VERSION < 300
if ( PyInt_Check( value ) )
{
return PyInt_AS_LONG( value );
}
else
#endif
if ( PyIndex_Check( value ) )
{
Py_ssize_t result = PyNumber_AsSsize_t( value, NULL );
if (unlikely( result == -1 ))
{
THROW_IF_ERROR_OCCURED();
}
return result;
}
else
{
PyErr_Format( PyExc_TypeError, "slice indices must be integers or None or have an __index__ method" );
throw PythonException();
}
}
示例10: PyArray_ContiguousFromObject
// Static helper functions
// =============================================================================
double * Epetra_NumPyMultiVector::getArray(PyObject * pyObject)
{
// Try to build a contiguous PyArrayObject from the pyObject
if (!tmp_array)
tmp_array = (PyArrayObject *)
PyArray_ContiguousFromObject(pyObject,NPY_DOUBLE,0,0);
// If this fails, clean up and throw a PythonException
if (!tmp_array)
{
cleanup();
throw PythonException();
}
// If the contiguous PyArrayObject built successfully, make sure it has the correct
// number of dimensions
else
{
if (PyArray_NDIM(tmp_array) < 2)
{
PyObject * tuple = Py_BuildValue("(ii)",1,-1);
tmp_array = (PyArrayObject *) PyArray_Reshape(tmp_array,tuple);
Py_DECREF(tuple);
}
}
return (double *) (PyArray_DATA(tmp_array));
}
示例11: getEpetraOperatorObjectAttr
Teuchos::RCP< Epetra_Operator >
getEpetraOperatorObjectAttr(PyObject * object,
CONST char * name)
{
static swig_type_info * swig_EO_ptr =
SWIG_TypeQuery("Teuchos::RCP< Epetra_Operator > *");
void * argp;
PyObject * value = PyObject_GetAttrString(object, name);
int newmem = 0;
if (!SWIG_CheckState(SWIG_Python_ConvertPtrAndOwn(value,
&argp,
swig_EO_ptr,
0,
&newmem)))
{
PyErr_Format(PyExc_TypeError,
"Attribute '%s' is not of type Epetra.Operator",
name);
Py_DECREF(value);
throw PythonException();
}
Teuchos::RCP<Epetra_Operator > result =
*reinterpret_cast< Teuchos::RCP< Epetra_Operator > * >(argp);
if (newmem)
delete reinterpret_cast< Teuchos::RCP< Epetra_Operator > * >(argp);
Py_DECREF(value);
return result;
}
示例12: M
// =============================================================================
void Epetra_NumPySerialSymDenseMatrix::setArray(bool copy)
{
if (tmp_array)
{
array = tmp_array;
tmp_array = NULL;
}
else
{
npy_intp dimensions[ ] = { M(), N() };
double * data = NULL;
if (!copy) data = Epetra_SerialSymDenseMatrix::A();
// This NumPy function returns a borrowed pointer: no DECREF
PyArray_Descr * dtype = PyArray_DescrFromType(NPY_DOUBLE);
array = (PyArrayObject*)
PyArray_NewFromDescr(&PyArray_Type,dtype,2,dimensions,NULL,(void*)data,
NPY_ARRAY_FARRAY,NULL);
if (!array)
{
cleanup();
throw PythonException();
}
if (copy)
{
double * oldData = Epetra_SerialSymDenseMatrix::A();
double * newData = (double*) PyArray_DATA(array);
int size = dimensions[0] * dimensions[1];
for (int i=0; i<size; ++i) newData[i] = oldData[i];
}
}
}
示例13: convertToMDVector
Teuchos::RCP< Domi::MDVector< Scalar > >
convertToMDVector(const Teuchos::RCP< const Teuchos::Comm< int > > teuchosComm,
const DistArrayProtocol & distarray)
{
// Get the equivalent MDMap
Teuchos::RCP< const Domi::MDMap<> > mdMap =
convertToMDMap(teuchosComm, distarray);
// Get the equivalent MDArrayRCP
Domi::MDArrayRCP< Scalar > mdArrayRcp =
convertToMDArrayRCP< Scalar >((PyArrayObject*) distarray.buffer());
#ifdef PYTRILINOS_DOMI_UTIL_VERBOSE
std::cout << "mdArrayRcp = " << mdArrayRcp << std::endl;
#endif
// Return the result
try
{
return Teuchos::rcp(new Domi::MDVector< Scalar >(mdMap, mdArrayRcp));
}
catch (Domi::InvalidArgument & e)
{
PyErr_SetString(PyExc_ValueError, e.what());
throw PythonException();
}
}
示例14: THROW_IF_ERROR_OCCURED
NUITKA_MAY_BE_UNUSED static void THROW_IF_ERROR_OCCURED( void )
{
if ( ERROR_OCCURED() )
{
throw PythonException();
}
}
示例15: PyInt_AsLong
// Static helper functions
// =============================================================================
double * Epetra_NumPySerialDenseVector::getArray(PyObject * pyObject)
{
// If tmp_array is NULL, build a PyArrayObject from the pyObject
if (tmp_array == NULL)
{
// If pyObject is an int, build an array of that length
if (PyInt_Check(pyObject))
{
npy_intp dimensions[ ] = {(npy_intp) PyInt_AsLong(pyObject)};
tmp_array = (PyArrayObject*)
PyArray_SimpleNew(1,dimensions,PyArray_DOUBLE);
}
// Else try to build a contiguous PyArrayObject from the pyObject
else
{
tmp_array = (PyArrayObject *)
PyArray_ContiguousFromObject(pyObject,PyArray_DOUBLE,0,0);
}
}
// If these fail, clean up and throw a PythonException
if (!tmp_array)
{
cleanup();
throw PythonException();
}
return (double*)(tmp_array->data);
}