本文整理汇总了C++中PyObjectPtr::get方法的典型用法代码示例。如果您正苦于以下问题:C++ PyObjectPtr::get方法的具体用法?C++ PyObjectPtr::get怎么用?C++ PyObjectPtr::get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyObjectPtr
的用法示例。
在下文中一共展示了PyObjectPtr::get方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: throwPythonException
void throwPythonException()
{
PyObject* error = PyErr_Occurred(); // Borrowed reference
if(error != NULL)
{
PyObject* ptype, * pvalue, * ptraceback;
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
// Exception type
PyObjectPtr pyexception = makePyObjectPtr(PyObject_GetAttrString(
ptype, (char*)"__name__"));
std::string type = PyString_AsString(pyexception.get());
// Message
PyObjectPtr pymessage = makePyObjectPtr(PyObject_Str(pvalue));
std::string message = PyString_AsString(pymessage.get());
// Traceback
PyObjectPtr tracebackModule = makePyObjectPtr(PyImport_ImportModule("traceback"));
std::string traceback;
if (tracebackModule != NULL) {
PyObjectPtr tbList = makePyObjectPtr(
PyObject_CallMethod(
tracebackModule.get(), (char*)"format_exception",
(char*)"OOO", ptype, pvalue == NULL ? Py_None : pvalue,
ptraceback == NULL ? Py_None : ptraceback));
PyObjectPtr emptyString = makePyObjectPtr(PyString_FromString(""));
PyObjectPtr strRetval = makePyObjectPtr(
PyObject_CallMethod(emptyString.get(), (char*)"join", (char*)"O",
tbList.get()));
traceback = PyString_AsString(strRetval.get());
}
else
{
traceback = "Empty traceback";
}
Py_XDECREF(ptype);
Py_XDECREF(pvalue);
Py_XDECREF(ptraceback);
std::string error_message = "Python exception (" + type + "): " + message;
if(traceback != (type + ": " + message + "\n"))
error_message += "\n" + traceback;
throw std::runtime_error(error_message);
}
}
示例2: ownerptr
static PyObject*
_BoundSignal_New( PyObject* owner, PyObject* objref )
{
PyObjectPtr ownerptr( owner, true );
PyObjectPtr objrefptr( objref, true );
PyObjectPtr bsigptr;
if( numfree > 0 )
{
PyObject* o = reinterpret_cast<PyObject*>( freelist[ --numfree ] );
_Py_NewReference( o );
bsigptr = o;
}
else
{
bsigptr = PyType_GenericAlloc( &BoundSignal_Type, 0 );
if( !bsigptr )
return 0;
}
BoundSignal* bsig = reinterpret_cast<BoundSignal*>( bsigptr.get() );
bsig->owner = ownerptr.release();
bsig->objref = objrefptr.release();
return bsigptr.release();
}
示例3: iterator
static PyObject*
_CAtom_unobserve_2( CAtom* self, PyObject* topic, PyObject* callback )
{
if( PyString_Check( topic ) )
{
if( !self->unobserve( topic, callback ) )
return 0;
}
else
{
PyObjectPtr iterator( PyObject_GetIter( topic ) );
if( !iterator )
return 0;
PyObjectPtr topicptr;
while( ( topicptr = PyIter_Next( iterator.get() ) ) )
{
if( !self->unobserve( topicptr.get(), callback ) )
return 0;
}
if( PyErr_Occurred() )
return 0;
}
Py_RETURN_NONE;
}
示例4: size
const unsigned size() { return PyTuple_Size(obj.get()); }
示例5: ndim
const int ndim() { return PyArray_NDIM(obj.get()); }
示例6: oldptr
static int
slot_handler( Member* member, CAtom* atom, PyObject* value )
{
if( member->index >= atom->get_slot_count() )
{
py_no_attr_fail( pyobject_cast( atom ), (char *)Py23Str_AS_STRING( member->name ) );
return -1;
}
if( atom->is_frozen() )
{
PyErr_SetString( PyExc_AttributeError, "can't set attribute of frozen Atom" );
return -1;
}
PyObjectPtr oldptr( atom->get_slot( member->index ) );
PyObjectPtr newptr( newref( value ) );
if( oldptr == newptr )
return 0;
bool valid_old = oldptr.get() != 0;
if( !valid_old )
oldptr.set( newref( Py_None ) );
newptr = member->full_validate( atom, oldptr.get(), newptr.get() );
if( !newptr )
return -1;
atom->set_slot( member->index, newptr.get() );
if( member->get_post_setattr_mode() )
{
if( member->post_setattr( atom, oldptr.get(), newptr.get() ) < 0 )
return -1;
}
if( ( !valid_old || oldptr != newptr ) && atom->get_notifications_enabled() )
{
PyObjectPtr argsptr;
if( member->has_observers() )
{
if( valid_old && oldptr.richcompare( newptr, Py_EQ ) )
return 0;
if( valid_old )
argsptr = updated_args( atom, member, oldptr.get(), newptr.get() );
else
argsptr = created_args( atom, member, newptr.get() );
if( !argsptr )
return -1;
if( !member->notify( atom, argsptr.get(), 0 ) )
return -1;
}
if( atom->has_observers( member->name ) )
{
if( !argsptr )
{
if( valid_old && oldptr.richcompare( newptr, Py_EQ ) )
return 0;
if( valid_old )
argsptr = updated_args( atom, member, oldptr.get(), newptr.get() );
else
argsptr = created_args( atom, member, newptr.get() );
if( !argsptr )
return -1;
}
if( !atom->notify( member->name, argsptr.get(), 0 ) )
return -1;
}
}
return 0;
}
示例7: get
const bool get() { return (bool)PyObject_IsTrue(obj.get()); }
示例8: objptr
static PyObject*
load_dynamic_attr( PyObject* obj, PyObject* name, PyObject* tracer=0 )
{
PyTypeObject* tp;
PyObject** dictptr;
PyObjectPtr descr;
descrgetfunc descr_f;
PyObjectPtr objptr( newref( obj ) );
// The body of this loop is PyObject_GenericGetAttr, modified to
// use smart pointers and _PyObject_GetDictPtr, and run a tracer.
while( objptr.get() != Py_None )
{
tp = Py_TYPE( objptr.get() );
// Data descriptor
descr_f = 0;
descr = xnewref( _PyType_Lookup( tp, name ) );
if( descr && PyType_HasFeature( descr.get()->ob_type, Py_TPFLAGS_HAVE_CLASS ) )
{
descr_f = descr.get()->ob_type->tp_descr_get;
if( descr_f && PyDescr_IsData( descr.get() ) )
{
PyObjectPtr res(
descr_f( descr.get(), objptr.get(), pyobject_cast( tp ) )
);
if( !res )
maybe_translate_key_error();
else if( tracer && !run_tracer( tracer, objptr.get(), name, res.get() ) )
return 0;
return res.release();
}
}
// Instance dictionary
dictptr = _PyObject_GetDictPtr( objptr.get() );
if( dictptr && *dictptr )
{
PyObject* item = PyDict_GetItem( *dictptr, name );
if( item )
{
if( tracer && !run_tracer( tracer, objptr.get(), name, item ) )
return 0;
return newref( item );
}
}
// Non-data descriptor
if( descr_f )
{
PyObjectPtr res(
descr_f( descr.get(), objptr.get(), pyobject_cast( tp ) )
);
if( !res )
maybe_translate_key_error();
else if( tracer && !run_tracer( tracer, objptr.get(), name, res.get() ) )
return 0;
return res.release();
}
// Non-readable descriptor
if( descr )
{
if( tracer && !run_tracer( tracer, objptr.get(), name, descr.get() ) )
return 0;
return descr.release();
}
// Step up to the parent object
objptr = PyObject_GetAttr( objptr.get(), parent_str );
if( !objptr )
return 0;
}
return 0;
}
示例9: check
static const bool check(PyObjectPtr obj) { return PyTuple_Check(obj.get()); }
示例10: append
bool append(PyObjectPtr item) { return PyList_Append(obj.get(), item.get()) == 0; }
示例11: isDouble
const bool isDouble() { return PyArray_TYPE(obj.get()) == NPY_DOUBLE; }