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; }
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 ); }
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); }
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; }
// ============================================================================= 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)); }
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; }
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; }
// ============================================================================= 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)); }
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(); } }
// 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)); }
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; }
// ============================================================================= 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]; } } }
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(); } }
NUITKA_MAY_BE_UNUSED static void THROW_IF_ERROR_OCCURED( void ) { if ( ERROR_OCCURED() ) { throw PythonException(); } }
// 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); }
// Static helper functions // ============================================================================= double * Epetra_NumPySerialSymDenseMatrix::getArray(PyObject * pyObject) { // If tmp_array is NULL, build a PyArrayObject from the pyObject if (!tmp_array) { // If pyObject is an int, then emulate an int-int constructor if (PyInt_Check(pyObject)) { int numRows = (int) PyInt_AsLong(pyObject); npy_intp dimensions[ ] = {numRows, numRows}; tmp_array = (PyArrayObject *) PyArray_SimpleNew(2,dimensions,NPY_DOUBLE); } // If pyObject is not a bool nor an int, try to build a // contiguous 2D PyArrayObject from the pyObject else { // This function returns a borrowed ptr: no DECREF PyArray_Descr * dtype = PyArray_DescrFromType(NPY_DOUBLE); tmp_array = (PyArrayObject *) PyArray_FromAny(pyObject, dtype, 2, 2, NPY_ARRAY_FARRAY, NULL); } } // If no array has been correctly constructed, clean up and throw a // PythonException if (!tmp_array) { cleanup(); throw PythonException(); } return (double*)(PyArray_DATA(tmp_array)); }
RunPythonFileAction::RunPythonFileAction(const ParameterValueMap ¶meters) : RunPythonAction(parameters) { // Converting to boost::filesystem::path first buys us some useful path expansion and validation const std::string filename(boost::filesystem::path(parameters[PATH]).string()); std::FILE *fp = std::fopen(filename.c_str(), "r"); if (!fp) { throw SimpleException(M_FILE_MESSAGE_DOMAIN, "Unable to open Python file", strerror(errno)); } BOOST_SCOPE_EXIT(fp) { std::fclose(fp); } BOOST_SCOPE_EXIT_END ScopedGILAcquire sga; struct _node *node = PyParser_SimpleParseFile(fp, filename.c_str(), Py_file_input); BOOST_SCOPE_EXIT(node) { PyNode_Free(node); } BOOST_SCOPE_EXIT_END if (!node || !(codeObject = PyNode_Compile(node, filename.c_str()))) { throw PythonException("Python compilation failed"); } }
NUITKA_MAY_BE_UNUSED static PyObject *CALL_FUNCTION( PyObject *function_object, PyObject *positional_args, PyObject *named_args ) { assertObject( function_object ); assertObject( positional_args ); assert( named_args == NULL || Py_REFCNT( named_args ) > 0 ); ternaryfunc call_slot = Py_TYPE( function_object )->tp_call; if (unlikely( call_slot == NULL )) { PyErr_Format( PyExc_TypeError, "'%s' object is not callable", function_object->ob_type->tp_name ); throw PythonException(); } if (unlikely( Py_EnterRecursiveCall( (char *)" while calling a Python object") )) { throw PythonException(); } PyObject *result = (*call_slot)( function_object, positional_args, named_args ); Py_LeaveRecursiveCall(); if ( result == NULL ) { if (unlikely( !ERROR_OCCURED() )) { PyErr_Format( PyExc_SystemError, "NULL result without error in PyObject_Call" ); } throw PythonException(); } else { return result; } }
EpetraExt::ModelEvaluator::DerivativeProperties getDerivativePropertiesItemObjectAttr(PyObject * object, CONST char * name, int i) { // The DerivativeProperties python class object static PyObject * classDerivativeProperties = NULL; if (!classDerivativeProperties) { classDerivativeProperties = getClassFromModule(PyTrilinosEpetraExt, "DerivativeProperties"); if (!classDerivativeProperties) 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, classDerivativeProperties)) { PyErr_Format(PyExc_TypeError, "Attribute '%s' is not tuple of DerivativeProperties", name); Py_DECREF(item); throw PythonException(); } EpetraExt::ModelEvaluator::DerivativeProperties result; // linearity attribute CONST char * linearity = getStringObjectAttr(item, "linearity"); if (strncmp(linearity, "unknown", 7) == 0) result.linearity = EpetraExt::ModelEvaluator::DERIV_LINEARITY_UNKNOWN; if (strncmp(linearity, "const", 5) == 0) result.linearity = EpetraExt::ModelEvaluator::DERIV_LINEARITY_CONST; if (strncmp(linearity, "nonconst", 8) == 0) result.linearity = EpetraExt::ModelEvaluator::DERIV_LINEARITY_NONCONST; // rank attribute CONST char * rank = getStringObjectAttr(item, "rank"); if (strncmp(rank, "unknown", 7) == 0) result.rank = EpetraExt::ModelEvaluator::DERIV_RANK_UNKNOWN; if (strncmp(rank, "full", 4) == 0) result.rank = EpetraExt::ModelEvaluator::DERIV_RANK_FULL; if (strncmp(rank, "deficient", 9) == 0) result.rank = EpetraExt::ModelEvaluator::DERIV_RANK_DEFICIENT; // supportsAdjoint attribute result.supportsAdjoint = getBoolObjectAttr(item, "supportsAdjoint"); Py_DECREF(item); return result; }
void DistArrayProtocol::checkAxis(int axis) const { if ((axis < 0) || (axis >= dim_data.size())) { PyErr_Format(PyExc_IndexError, "axis = %d out of range [0, %ld)", axis, dim_data.size()); throw PythonException(); } }
void del( bool tolerant ) const { int status = PyDict_DelItem( (PyObject *)_moduledict_django__core__management, (PyObject *)*this->var_name ); if (unlikely( status == -1 && tolerant == false )) { PyErr_Format( PyExc_NameError, "global name '%s' is not defined", Nuitka_String_AsString( (PyObject *)*this->var_name ) ); throw PythonException(); } }
EpetraExt::ModelEvaluator::Derivative getDerivativeItemObjectAttr(PyObject * object, CONST char * name, int i) { // The Derivative python class object static PyObject * classDerivative = NULL; if (!classDerivative) { classDerivative = getClassFromModule(PyTrilinosEpetraExt, "Derivative"); if (!classDerivative) 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, classDerivative)) { PyErr_Format(PyExc_TypeError, "Attribute '%s' is not tuple of Derivative", name); Py_DECREF(item); throw PythonException(); } if (!objectAttrIsNone(item, "operator")) { // operator attribute Teuchos::RCP<Epetra_Operator> linearOp = getEpetraOperatorObjectAttr(item, "operator"); Py_DECREF(item); return EpetraExt::ModelEvaluator::Derivative(linearOp); } if (!objectAttrIsNone(item, "derivativeMultiVector")) { // derivativeMultiVector attribute EpetraExt::ModelEvaluator::DerivativeMultiVector derivativeMultiVector = getDerivativeMultiVectorObjectAttr(item, "derivativeMultiVector"); Py_DECREF(item); return EpetraExt::ModelEvaluator::Derivative(derivativeMultiVector); } Py_DECREF(item); return EpetraExt::ModelEvaluator::Derivative(); }
Teuchos::RCP< const Epetra_Map > getEpetraMapPtrFromEpetraBlockMap(const Epetra_BlockMap & ebm) { const Epetra_Map * em_ptr = dynamic_cast< const Epetra_Map* >(&ebm); if (!em_ptr) { PyErr_SetString(PyExc_TypeError, "Cannot upcast BlockMap to Map"); throw PythonException(); } return Teuchos::rcp(em_ptr, false); }
EpetraExt::ModelEvaluator::DerivativeProperties getDerivativePropertiesObjectAttr(PyObject * object, CONST char * name) { static PyObject * classDerivativeProperties = NULL; if (!classDerivativeProperties) { classDerivativeProperties = getClassFromModule(PyTrilinosEpetraExt, "DerivativeProperties"); if (!classDerivativeProperties) throw PythonException(); } PyObject * value = PyObject_GetAttrString(object, name); if (!value) throw PythonException(); if (!PyObject_IsInstance(value, classDerivativeProperties)) { PyErr_Format(PyExc_TypeError, "Attribute '%s' is not of type DerivativeProperties", name); Py_DECREF(value); throw PythonException(); } EpetraExt::ModelEvaluator::DerivativeProperties result; // linearity attribute CONST char * linearity = getStringObjectAttr(value, "linearity"); if (strncmp(linearity, "unknown", 7) == 0) result.linearity = EpetraExt::ModelEvaluator::DERIV_LINEARITY_UNKNOWN; if (strncmp(linearity, "const", 5) == 0) result.linearity = EpetraExt::ModelEvaluator::DERIV_LINEARITY_CONST; if (strncmp(linearity, "nonconst", 8) == 0) result.linearity = EpetraExt::ModelEvaluator::DERIV_LINEARITY_NONCONST; // rank attribute CONST char * rank = getStringObjectAttr(value, "rank"); if (strncmp(rank, "unknown", 7) == 0) result.rank = EpetraExt::ModelEvaluator::DERIV_RANK_UNKNOWN; if (strncmp(rank, "full", 4) == 0) result.rank = EpetraExt::ModelEvaluator::DERIV_RANK_FULL; if (strncmp(rank, "deficient", 9) == 0) result.rank = EpetraExt::ModelEvaluator::DERIV_RANK_DEFICIENT; // supportsAdjoint attribute result.supportsAdjoint = getBoolObjectAttr(value, "supportsAdjoint"); Py_DECREF(value); return result; }
PyObject *asObject0() const { if ( this->object == NULL && this->var_name != NULL ) { PyErr_Format( PyExc_UnboundLocalError, "local variable '%s' referenced before assignment", Nuitka_String_AsString( this->var_name ) ); throw PythonException(); } assertObject( this->object ); return this->object; }
NUITKA_NO_RETURN NUITKA_MAY_BE_UNUSED static void RAISE_EXCEPTION_WITH_VALUE_NO_NORMALIZE( PyObject *exception_type, PyObject *value, PyObject *tb ) { PyTracebackObject *traceback = (PyTracebackObject *)tb; assertObject( exception_type ); assert( !PyTuple_Check( exception_type ) ); if ( PyExceptionClass_Check( exception_type ) ) { throw PythonException( INCREASE_REFCOUNT( exception_type ), INCREASE_REFCOUNT( value ), INCREASE_REFCOUNT_X( traceback ) ); } else if ( PyExceptionInstance_Check( exception_type ) ) { assert ( value == NULL || value == Py_None ); // The type is rather a value, so we are overriding it here. value = exception_type; exception_type = PyExceptionInstance_Class( exception_type ); throw PythonException( INCREASE_REFCOUNT( exception_type ), INCREASE_REFCOUNT( value ), INCREASE_REFCOUNT_X( traceback ) ); } else { assert( false ); PyErr_Format( PyExc_TypeError, WRONG_EXCEPTION_TYPE_ERROR_MESSAGE, Py_TYPE( exception_type )->tp_name ); throw PythonException(); } }
void del( bool tolerant ) { if ( this->object ) { Py_DECREF( this->object ); } else if ( !tolerant ) { PyErr_Format( PyExc_NameError, "free variable '%s' referenced before assignment in enclosing scope", Nuitka_String_AsString( this->var_name ) ); throw PythonException(); } this->object = NULL; }
NUITKA_MAY_BE_UNUSED static void THROW_IF_ERROR_OCCURED_NOT( PyObject *ignored ) { if ( ERROR_OCCURED() ) { if ( PyErr_ExceptionMatches( ignored )) { PyErr_Clear(); } else { throw PythonException(); } } }
EpetraExt::ModelEvaluator::Derivative getDerivativeObjectAttr(PyObject * object, CONST char * name) { static PyObject * classDerivative = NULL; if (!classDerivative) { classDerivative = getClassFromModule(PyTrilinosEpetraExt, "Derivative"); if (!classDerivative) throw PythonException(); } PyObject * value = PyObject_GetAttrString(object, name); if (!value) throw PythonException(); if (!PyObject_IsInstance(value, classDerivative)) { PyErr_Format(PyExc_TypeError, "Attribute '%s' is not of type Derivative", name); Py_DECREF(value); throw PythonException(); } if (!objectAttrIsNone(value, "operator")) { // operator attribute Teuchos::RCP<Epetra_Operator> linearOp = getEpetraOperatorObjectAttr(value, "operator"); Py_DECREF(value); return EpetraExt::ModelEvaluator::Derivative(linearOp); } if (!objectAttrIsNone(value, "derivativeMultiVector")) { // derivativeMultiVector attribute EpetraExt::ModelEvaluator::DerivativeMultiVector derivativeMultiVector= getDerivativeMultiVectorObjectAttr(value, "derivativeMultiVector"); Py_DECREF(value); return EpetraExt::ModelEvaluator::Derivative(derivativeMultiVector); } Py_DECREF(value); return EpetraExt::ModelEvaluator::Derivative(); }
EpetraExt::ModelEvaluator::Evaluation<Epetra_Vector> getEvaluationItemObjectAttr(PyObject * object, CONST char * name, int i) { // The Evaluation python class object static PyObject * classEvaluation = NULL; if (!classEvaluation) { classEvaluation = getClassFromModule(PyTrilinosEpetraExt, "Evaluation"); if (!classEvaluation) 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, classEvaluation)) { PyErr_Format(PyExc_TypeError, "Attribute '%s' is not tuple of Evaluation", name); Py_DECREF(item); throw PythonException(); } // vector attribute Teuchos::RCP<Epetra_Vector> vector = getEpetraVectorObjectAttr(item, "vector"); // type attribute EpetraExt::ModelEvaluator::EEvalType type; CONST char * typeStr = getStringObjectAttr(item, "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(item); return EpetraExt::ModelEvaluator::Evaluation<Epetra_Vector>(vector, type); }