本文整理汇总了C++中PyObject_Str函数的典型用法代码示例。如果您正苦于以下问题:C++ PyObject_Str函数的具体用法?C++ PyObject_Str怎么用?C++ PyObject_Str使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PyObject_Str函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: PyErr_Fetch
bool PythonScript::checkCompileError() {
if (!PyErr_Occurred())
return true;
PyObject* errtype, *errvalue, *traceback;
PyErr_Fetch(&errtype, &errvalue, &traceback);
std::string log = "";
char* msg = nullptr;
PyObject* obj = nullptr;
if (PyArg_ParseTuple(errvalue, "sO", &msg, &obj)) {
int line, col;
char* code = nullptr;
char* mod = nullptr;
if (PyArg_ParseTuple(obj, "siis", &mod, &line, &col, &code)) {
log = "[" + toString(line) + ":" + toString(col) + "] " + toString(msg) + ": " + toString(code);
}
}
// convert error to string, if it could not be parsed
if (log.empty()) {
LogWarn("Failed to parse exception, printing as string:");
PyObject* s = PyObject_Str(errvalue);
if (s && PyValueParser::is<std::string>(s)) {
log = std::string(PyValueParser::parse<std::string>(s));
Py_XDECREF(s);
}
}
Py_XDECREF(errtype);
Py_XDECREF(errvalue);
Py_XDECREF(traceback);
LogError(log);
return false;
}
示例2: report_python_error
int report_python_error(const char *preamble, int code) {
PyObject *exc, *val, *tb, *str;
int ret, issysexit = 0; char *i, *buf;
if (!PyErr_Occurred()) return code;
issysexit = PyErr_ExceptionMatches(PyExc_SystemExit);
PyErr_Fetch(&exc, &val, &tb);
if (exc != NULL) {
PyErr_NormalizeException(&exc, &val, &tb);
if (issysexit) {
return (val) ? handle_sysexit(val) : 0;
}
if (val != NULL) {
str = PyObject_Unicode(val);
if (str == NULL) {
PyErr_Clear();
str = PyObject_Str(val);
}
i = PyString_AsString(str);
if (i == NULL) OOM;
buf = (char*)calloc(strlen(i)+strlen(preamble)+5, sizeof(char));
if (buf == NULL) OOM;
sprintf(buf, "%s::%s", preamble, i);
ret = report_error(buf, code);
if (buf) free(buf);
if (tb != NULL) {
PyErr_Restore(exc, val, tb);
PyErr_Print();
}
return ret;
}
}
return report_error(preamble, code);
}
示例3: PyFile_WriteObject
int
PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
{
PyObject *writer, *value, *args, *result;
_Py_IDENTIFIER(write);
if (f == NULL) {
PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
return -1;
}
writer = _PyObject_GetAttrId(f, &PyId_write);
if (writer == NULL)
return -1;
if (flags & Py_PRINT_RAW) {
value = PyObject_Str(v);
}
else
value = PyObject_Repr(v);
if (value == NULL) {
Py_DECREF(writer);
return -1;
}
args = PyTuple_Pack(1, value);
if (args == NULL) {
Py_DECREF(value);
Py_DECREF(writer);
return -1;
}
result = PyEval_CallObject(writer, args);
Py_DECREF(args);
Py_DECREF(value);
Py_DECREF(writer);
if (result == NULL)
return -1;
Py_DECREF(result);
return 0;
}
示例4: Image_readPreview
/* read preview*/
PyObject *
Image_readPreview(PyObject *obj, PyObject *args, PyObject *kwargs)
{
ImageObject *self = (ImageObject*) obj;
if (self != NULL)
{
PyObject *input = NULL;
int x = 0;
int slice = CENTRAL_SLICE;
if (PyArg_ParseTuple(args, "O|ii", &input, &x, &slice))
{
try
{
PyObject *pyStr;
if ((pyStr = PyObject_Str(input)) != NULL)
{
readImagePreview(self->image, PyString_AsString(pyStr), x, slice);
Py_RETURN_NONE;
}
else
{
PyErr_SetString(PyExc_TypeError,
"Image_readPreview: Expected string or FileName as first argument");
}
}
catch (XmippError &xe)
{
PyErr_SetString(PyXmippError, xe.msg.c_str());
}
}
else
PyErr_SetString(PyXmippError,"Error with input arguments, expected filename and optional size");
}
return NULL;
}//function Image_readPreview
示例5: PyLorcon2_Context_send_bytes
static PyObject*
PyLorcon2_Context_send_bytes(PyLorcon2_Context *self, PyObject *args, PyObject *kwds)
{
char *pckt_buffer;
ssize_t pckt_size, sent;
PyObject *pckt, *pckt_string;
if (!PyArg_ParseTuple(args, "O", &pckt))
return NULL;
if (!self->monitored) {
PyErr_SetString(PyExc_RuntimeError, "Context must be in monitor/injection-mode");
return NULL;
}
pckt_string = PyObject_Str(pckt);
if (!pckt_string) {
PyErr_SetString(PyExc_ValueError, "Failed to get string-representation from object.");
return NULL;
}
if (PyString_AsStringAndSize(pckt_string, &pckt_buffer, &pckt_size)) {
Py_DECREF(pckt_string);
return NULL;
}
sent = lorcon_send_bytes(self->context, pckt_size, (u_char*)pckt_buffer);
if (sent < 0) {
PyErr_SetString(Lorcon2Exception, lorcon_get_error(self->context));
Py_DECREF(pckt_string);
return NULL;
}
Py_DECREF(pckt_string);
return PyInt_FromLong(sent);
}
示例6: pyjlist_setitem
/*
* Method for the setting items with the [int] operator on pyjlist. For example,
* o[i] = v
*/
static int pyjlist_setitem(PyObject *o, Py_ssize_t i, PyObject *v) {
jmethodID set = NULL;
PyJobject_Object *obj = (PyJobject_Object*) o;
JNIEnv *env = pyembed_get_env();
jobject value = NULL;
if(v == Py_None) {
value = NULL;
} else {
value = pyembed_box_py(env, v);
if(process_java_exception(env)) {
return -1;
} else if(!value) {
/*
* with the way pyembed_box_py is currently implemented, shouldn't
* be able to get here
*/
PyErr_Format(PyExc_TypeError,
"__setitem__ received an incompatible type: %s",
PyString_AsString(PyObject_Str((PyObject*) Py_TYPE(v))));
return -1;
}
}
set = (*env)->GetMethodID(env, obj->clazz, "set", "(ILjava/lang/Object;)Ljava/lang/Object;");
if(process_java_exception(env) || !set) {
return -1;
}
(*env)->CallObjectMethod(env, obj->object, set, (jint) i, value);
if(process_java_exception(env)) {
return -1;
}
// have to return 0 on success even though it's not documented
return 0;
}
示例7: repr
CString ui_assoc_object::repr()
{
CString csRet;
static TCHAR *no_repr=_T("<None>");
TCHAR *py_repr=NULL;
BOOL bfree_repr=FALSE;
if (virtualInst == NULL)
py_repr=no_repr;
else{
PyObject *vi_repr=PyObject_Str(virtualInst);
if (vi_repr==NULL || !PyWinObject_AsTCHAR(vi_repr, &py_repr, FALSE)){
PyErr_Clear();
py_repr=no_repr;
}
else
bfree_repr=TRUE;
Py_XDECREF(vi_repr);
}
csRet.Format(_T(" - assoc is %p, vi=%s"), assoc, py_repr);
if (bfree_repr)
PyWinObject_FreeTCHAR(py_repr);
return ui_base_class::repr() + csRet;
}
示例8: xmipp_getImageSize
/* getImageSize */
PyObject *
xmipp_getImageSize(PyObject *obj, PyObject *args, PyObject *kwargs)
{
PyObject *pyValue; //Only used to skip label and value
if (PyArg_ParseTuple(args, "O", &pyValue))
{
try
{
PyObject * pyStr = PyObject_Str(pyValue);
char * str = PyString_AsString(pyStr);
size_t xdim, ydim, zdim, ndim;
getImageSize(str, xdim, ydim, zdim, ndim);
Py_DECREF(pyStr);
return Py_BuildValue("kkkk", xdim, ydim, zdim, ndim);
}
catch (XmippError &xe)
{
PyErr_SetString(PyXmippError, xe.msg.c_str());
}
}
return NULL;
}
示例9: scriptAcquireGIL
char *scriptFigureToPythonExpr(struct figureData *figure) {
bool gil=!scriptIsGILAcquired();
if (gil)
scriptAcquireGIL();
static PyObject *str=NULL;
Py_XDECREF(str); str=NULL;
PyObject *pyFigure=figureToPython(figure);
if (pyFigure==Py_None) {
if (gil)
scriptReleaseGIL();
return NULL;
}
str=PyObject_Str(pyFigure);
Py_DECREF(pyFigure);
char *ret=PyString_AsString(str);
if (gil)
scriptReleaseGIL();
return ret;
}
示例10: lookup_attribute
static int
lookup_attribute(attr_dir_object *self, PyObject *key, kdump_attr_ref_t *ref)
{
PyObject *stringkey;
char *keystr;
int ret;
if (!PyString_Check(key)) {
stringkey = PyObject_Str(key);
if (!stringkey)
return -1;
} else
stringkey = key;
ret = -1;
keystr = PyString_AsString(stringkey);
if (keystr) {
kdump_ctx *ctx = self->kdumpfile->ctx;
kdump_status status;
status = kdump_sub_attr_ref(ctx, &self->baseref, keystr, ref);
if (status == kdump_ok)
ret = 1;
else if (status == kdump_nokey)
ret = 0;
else
PyErr_SetString(exception_map(status),
kdump_err_str(ctx));
}
if (stringkey != key)
Py_DECREF(stringkey);
return ret;
}
示例11: SWIG_Python_AddErrMesg
static int
SWIG_Python_AddErrMesg(const char* mesg, int infront)
{
if (PyErr_Occurred()) {
PyObject *type = 0;
PyObject *value = 0;
PyObject *traceback = 0;
PyErr_Fetch(&type, &value, &traceback);
if (value) {
PyObject *old_str = PyObject_Str(value);
Py_XINCREF(type);
PyErr_Clear();
if (infront) {
PyErr_Format(type, "%s %s", mesg, PyString_AsString(old_str));
} else {
PyErr_Format(type, "%s %s", PyString_AsString(old_str), mesg);
}
Py_DECREF(old_str);
}
return 1;
} else {
return 0;
}
}
示例12: PyObjectToStringNoExcept
inline bool PyObjectToStringNoExcept(PyObject* obj, std::string& result)
{
if (!obj)
return false;
#if PY_VERSION_HEX >= 0x03000000
PyObjectHolder obj_str(PyObject_Str(obj));
#else
PyObjectHolder obj_str(PyObject_Unicode(obj));
#endif
if (!obj_str)
return false;
PyObjectHolder py_bytes(PyUnicode_AsUTF8String(obj_str));
if (!py_bytes)
return false;
const char* content = PyBytes_AsString(py_bytes);
if (!content)
return false;
result = content;
return true;
}
示例13: dumpTraceback
NUITKA_MAY_BE_UNUSED static void dumpTraceback( PyTracebackObject *traceback )
{
puts( "Dumping traceback:" );
if ( traceback == NULL ) puts( "<NULL traceback?!>" );
while( traceback )
{
puts( " Frame object chain:" );
PyFrameObject *frame = traceback->tb_frame;
while ( frame )
{
printf( " Frame at %s\n", PyString_AsString( PyObject_Str( (PyObject *)frame->f_code )));
frame = frame->f_back;
}
traceback = traceback->tb_next;
}
puts( "End of Dump." );
}
示例14: py_coulomb_potential
void py_coulomb_potential(PyObject *self, void *p, void *nl, double *q,
double *phi, int *error)
{
particles_t *py_p;
neighbors_t *py_nl;
PyObject *py_q, *py_phi, *r;
int nat;
npy_intp dims[2];
#ifdef DEBUG
printf("[py_coulomb_potential] self = %s\n",
PyString_AsString(PyObject_Str(self)));
#endif
f_particles_get_tag(p, (void**) &py_p);
assert(py_p->f90obj == p);
nat = data_get_len(py_p->f90data);
f_neighbors_get_tag(nl, (void**) &py_nl);
assert(py_nl->f90obj == nl);
dims[0] = nat;
dims[1] = 3;
py_q = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, q);
py_phi = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, phi);
r = PyObject_CallMethod(self, "potential", "(OOOO)", py_p, py_nl, py_q,
py_phi);
Py_DECREF(py_q);
Py_DECREF(py_phi);
PASS_PYTHON_ERROR(error, r);
Py_DECREF(r);
}
示例15: mod_connect
//.........这里部分代码省略.........
while (PyDict_Next(kwargs, &pos, &key, &value))
{
if (!Text_Check(key))
return PyErr_Format(PyExc_TypeError, "Dictionary keys passed to connect must be strings");
// // Note: key and value are *borrowed*.
//
// // Check for the two non-connection string keywords we accept. (If we get many more of these, create something
// // table driven. Are we sure there isn't a Python function to parse keywords but leave those it doesn't know?)
// const char* szKey = PyString_AsString(key);
if (Text_EqualsI(key, "autocommit"))
{
fAutoCommit = PyObject_IsTrue(value);
continue;
}
if (Text_EqualsI(key, "ansi"))
{
fAnsi = PyObject_IsTrue(value);
continue;
}
if (Text_EqualsI(key, "timeout"))
{
timeout = PyInt_AsLong(value);
if (PyErr_Occurred())
return 0;
continue;
}
if (Text_EqualsI(key, "readonly"))
{
fReadOnly = PyObject_IsTrue(value);
continue;
}
if (Text_EqualsI(key, "attrs_before"))
{
attrs_before = _CheckAttrsDict(value);
if (PyErr_Occurred())
return 0;
continue;
}
if (Text_EqualsI(key, "encoding"))
{
#if PY_MAJOR_VERSION < 3
if (!PyString_Check(value) || !PyUnicode_Check(value))
return PyErr_Format(PyExc_TypeError, "encoding must be a string or unicode object");
#else
if (!PyUnicode_Check(value))
return PyErr_Format(PyExc_TypeError, "encoding must be a string");
#endif
encoding = value;
continue;
}
// Map DB API recommended names to ODBC names (e.g. user --> uid).
for (size_t i = 0; i < _countof(keywordmaps); i++)
{
if (Text_EqualsI(key, keywordmaps[i].oldname))
{
if (keywordmaps[i].newnameObject == 0)
{
keywordmaps[i].newnameObject = PyString_FromString(keywordmaps[i].newname);
if (keywordmaps[i].newnameObject == 0)
return 0;
}
key = keywordmaps[i].newnameObject;
break;
}
}
PyObject* str = PyObject_Str(value); // convert if necessary
if (!str)
return 0;
if (PyDict_SetItem(partsdict.Get(), key, str) == -1)
{
Py_XDECREF(str);
return 0;
}
Py_XDECREF(str);
}
if (PyDict_Size(partsdict.Get()))
pConnectString.Attach(MakeConnectionString(pConnectString.Get(), partsdict));
}
if (!pConnectString.IsValid())
return PyErr_Format(PyExc_TypeError, "no connection information was passed");
if (henv == SQL_NULL_HANDLE)
{
if (!AllocateEnv())
return 0;
}
return (PyObject*)Connection_New(pConnectString.Get(), fAutoCommit != 0, fAnsi != 0, timeout,
fReadOnly != 0, attrs_before, encoding);
}