本文整理汇总了C++中PyObject_Str函数的典型用法代码示例。如果您正苦于以下问题:C++ PyObject_Str函数的具体用法?C++ PyObject_Str怎么用?C++ PyObject_Str使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PyObject_Str函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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;
}
开发者ID:david12345678901,项目名称:inviwo,代码行数:37,代码来源:pythonscript.cpp
示例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);
}
开发者ID:089git,项目名称:calibre,代码行数:37,代码来源:util.c
示例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;
}
开发者ID:jadore,项目名称:cpython,代码行数:37,代码来源:fileobject.c
示例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
开发者ID:juannavascalvente,项目名称:scipion,代码行数:38,代码来源:python_image.cpp
示例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);
}
开发者ID:0x0d,项目名称:lorcon,代码行数:37,代码来源:PyLorcon2.c
示例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;
}
开发者ID:behdad84,项目名称:jep,代码行数:41,代码来源:pyjlist.c
示例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;
}
开发者ID:DavidGuben,项目名称:rcbplayspokemon,代码行数:24,代码来源:win32assoc.cpp
示例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;
}
开发者ID:coocoky,项目名称:scipion,代码行数:25,代码来源:xmippmodule.cpp
示例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;
}
开发者ID:Zereges,项目名称:geometric-figures,代码行数:24,代码来源:scriptFigure.c
示例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;
}
开发者ID:jeffmahoney,项目名称:libkdumpfile,代码行数:36,代码来源:kdumpfile.c
示例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;
}
}
开发者ID:gitter-badger,项目名称:quinoa,代码行数:24,代码来源:wrap_LOCA_TimeDependent.C
示例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;
}
开发者ID:koplyarov,项目名称:joint,代码行数:24,代码来源:PythonUtils.hpp
示例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." );
}
开发者ID:linkerlin,项目名称:Nuitka,代码行数:24,代码来源:exceptions.hpp
示例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);
}
开发者ID:Atomistica,项目名称:atomistica,代码行数:36,代码来源:coulomb_callback.c
示例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);
}
开发者ID:skillian,项目名称:pyodbc,代码行数:101,代码来源:pyodbcmodule.cpp
示例16: downcast
static
PyObject* downcast(PyObject* self, PyObject* args) {
PyObject *obj, *cls;
if (!PyArg_ParseTuple(args, "OO", &obj, &cls)) {
return NULL;
}
if ((PyObject *) Py_TYPE(obj) == cls) {
Py_INCREF(obj);
return obj;
}
PyObject* apiModule = GetAPIModule();
auto_pyobject fromTy = PyObject_GetAttrString(obj, "_llvm_type_");
auto_pyobject toTy = PyObject_GetAttrString(cls, "_llvm_type_");
std::ostringstream oss;
auto_pyobject fromTyStr = PyObject_Str(*fromTy);
auto_pyobject toTyStr = PyObject_Str(*toTy);
const char * fromCS = PyString_AsString(*fromTyStr);
const char * toCS = PyString_AsString(*toTyStr);
oss << "downcast_";
NormalizeString(oss, fromCS);
oss << "_to_";
NormalizeString(oss, toCS);
std::string fname = oss.str();
auto_pyobject caster = PyObject_GetAttrString(GetDowncastModule(),
fname.c_str());
if (!caster) {
std::ostringstream oss;
oss << "Downcast from " << fromCS << " to " << toCS;
std::string errmsg = oss.str();
PyErr_SetString(PyExc_TypeError, errmsg.c_str());
return NULL;
}
auto_pyobject oldObj = Unwrap(obj);
auto_pyobject newObj = PyObject_CallFunctionObjArgs(*caster, *oldObj,
NULL);
bool used_to_own = HasOwnership(*oldObj);
PyObject *result = Wrap(*newObj, !used_to_own);
int status = PyObject_Not(result);
switch(status) {
case 0:
return result;
case 1:
default:
PyErr_SetString(PyExc_ValueError, "Downcast failed");
Py_XDECREF(result);
return NULL;
}
}
开发者ID:KennethNielsen,项目名称:llvmpy,代码行数:61,代码来源:capsule.cpp
示例17: PyObject_Str
static void *PyBigIntToSTR(JSOBJ _obj, JSONTypeContext *tc, void *outValue, size_t *_outLen)
{
PyObject *obj = PyObject_Str((PyObject *) _obj);
*_outLen = PyString_GET_SIZE(obj);
return PyString_AS_STRING(obj);
}
开发者ID:MikeAthene,项目名称:ultrajson,代码行数:6,代码来源:objToJSON.c
示例18: iosource_init
static int iosource_init(iosource *self, PyObject *args, PyObject *kwds) {
char *keywords[] = { "opts", NULL};
char *drivername;
PyObject *opts = NULL;
IOOptions options = NULL;
PyObject *tmp;
if(!PyArg_ParseTupleAndKeywords(args, kwds, "|O", keywords,
&opts)) {
return -1;
};
if(!PyList_Check(opts)) {
PyErr_Format(PyExc_TypeError, "Options must be a list of tuples");
return -1;
};
if(!opts) {
PyErr_Format(PyExc_Exception, "No options provided to driver");
return -1;
};
options = CONSTRUCT(IOOptions, IOOptions, add, NULL, NULL, NULL, NULL);
{
int i=0;
for(i=0;i<PyList_Size(opts);i++) {
PyObject *temp,*key,*value;
char *keyc, *valuec;
temp = PyList_GetItem(opts,i);
if(!PyList_Check(temp)) {
tmp = PyObject_Str(temp);
PyErr_Format(PyExc_TypeError, "Element must be a list, not %s", PyString_AsString(tmp));
Py_DECREF(tmp);
return -1;
};
key = PyList_GetItem(temp,0);
if(!key) return -1;
value = PyList_GetItem(temp,1);
if(!value) return -1;
key = PyObject_Str(key);
keyc = PyString_AsString(key);
if(!keyc) {
talloc_free(options);
PyErr_Format(PyExc_Exception, "Not a string - driver options must be encoded as strings.");
return -1;
};
value = PyObject_Str(value);
valuec= PyString_AsString(value);
if(!valuec) {
talloc_free(options);
PyErr_Format(PyExc_Exception, "Not a string - driver options must be encoded as strings.");
return -1;
};
CONSTRUCT(IOOptions, IOOptions, add,options, options, keyc, valuec);
};
};
drivername = CALL(options, get_value, "subsys");
if(!drivername) {
PyErr_Format(PyExc_TypeError, "No iodriver specified");
return -1;
};
TRY {
self->driver = iosubsys_Open(drivername, options);
} EXCEPT(E_ANY) {
talloc_free(options);
PyErr_Format(map_exceptions_for_python(__EXCEPT__), "Unable to open iosource");
return -1;
};
// We failed to instantiate this driver
if(!self->driver) {
talloc_free(options);
PyErr_Format(map_errors_for_python(), "%s", _error_buff);
return -1;
};
//Check that all the options have been consumed:
if(!list_empty(&options->list)) {
IOOptions first;
list_next(first, &options->list, list);
PyErr_Format(PyExc_RuntimeError, "Subsystem %s does not accept parameter %s", drivername,
first->name);
talloc_free(options);
return -1;
};
//Now ensure that the options are stolen to the iosource:
talloc_steal(self->driver, options);
self->size = self->driver->size;
//.........这里部分代码省略.........
开发者ID:anarchivist,项目名称:pyflag,代码行数:101,代码来源:iosubsys.c
示例19: PyUnicode_AsASCIIString
tmp = PyUnicode_AsASCIIString(obj);
}
else {
PyObject *tmp2;
tmp2 = PyObject_Str(obj);
if (tmp2) {
tmp = PyUnicode_AsASCIIString(tmp2);
Py_DECREF(tmp2);
}
else {
tmp = NULL;
}
}
#else
else {
tmp = PyObject_Str(obj);
}
#endif
if (tmp == NULL) goto capi_fail;
if (*len == -1)
*len = PyString_GET_SIZE(tmp);
STRINGMALLOC(*str,*len);
STRINGCOPYN(*str,PyString_AS_STRING(tmp),*len+1);
Py_DECREF(tmp);
return 1;
capi_fail:
Py_XDECREF(tmp);
{
PyObject* err = PyErr_Occurred();
if (err==NULL) err = _lbfgsb_error;
PyErr_SetString(err,errmess);
开发者ID:wskinner,项目名称:LeapHMM,代码行数:31,代码来源:_lbfgsbmodule.c
示例20: pbs_python_run_code_in_namespace
//.........这里部分代码省略.........
(nbuf.st_size == obuf.st_size) &&
(nbuf.st_mtime == obuf.st_mtime)
) {
recompile = 0;
} else {
recompile = 1;
(void) memcpy(&(py_script->cur_sbuf), &nbuf,
sizeof(py_script->cur_sbuf));
Py_CLEAR(py_script->py_code_obj); /* we are rebuilding */
}
}
} while (0);
if (recompile) {
snprintf(log_buffer, LOG_BUF_SIZE-1,
"Compiling script file: <%s>", py_script->path);
log_buffer[LOG_BUF_SIZE-1] = '\0';
if (IS_PBS_PYTHON_CMD(pbs_python_daemon_name))
log_event(PBSEVENT_DEBUG3, PBS_EVENTCLASS_SERVER,
LOG_INFO, interp_data->daemon_name, log_buffer);
else
log_event(PBSEVENT_SYSTEM|PBSEVENT_ADMIN |
PBSEVENT_DEBUG, PBS_EVENTCLASS_SERVER,
LOG_INFO, interp_data->daemon_name, log_buffer);
if (!(py_script->py_code_obj =
_pbs_python_compile_file(py_script->path,
"<embedded code object>"))) {
pbs_python_write_error_to_log("Failed to compile script");
return -2;
}
}
/* make new namespace dictionary, NOTE new reference */
if (!(pdict = (PyObject *)pbs_python_ext_namespace_init(interp_data))) {
log_err(-1, func_id, "while calling pbs_python_ext_namespace_init");
return -1;
}
if ((pbs_python_setup_namespace_dict(pdict) == -1)) {
Py_CLEAR(pdict);
return -1;
}
/* clear previous global/local dictionary */
if (py_script->global_dict) {
PyDict_Clear((PyObject *)py_script->global_dict); /* clear k,v */
Py_CLEAR(py_script->global_dict);
}
py_script->global_dict = pdict;
PyErr_Clear(); /* clear any exceptions before starting code */
/* precompile strings of code to bytecode objects */
(void) PyEval_EvalCode((PyCodeObject *)py_script->py_code_obj,
pdict, pdict);
/* check for exception */
if (PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) {
pbs_python_write_error_to_log("Python script received a KeyboardInterrupt");
return -3;
}
if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
PyErr_Clear(); /* just in case, not clear from API doc */
if (pvalue) {
pobjStr = PyObject_Str(pvalue); /* new ref */
pStr = PyString_AsString(pobjStr);
rc = (int) atol(pStr);
Py_XDECREF(pobjStr);
}
Py_XDECREF(ptype);
Py_XDECREF(pvalue);
#if !defined(WIN32)
Py_XDECREF(ptraceback);
#elif !defined(_DEBUG)
/* for some reason this crashes on Windows Debug version */
Py_XDECREF(ptraceback);
#endif
} else {
pbs_python_write_error_to_log("Error evaluating Python script");
return -2;
}
}
PyErr_Clear();
if (exit_code)
*exit_code=rc; /* set exit code if var is not null */
return 0;
#else /* !PYTHON */
return -1;
#endif /* PYTHON */
}
开发者ID:A9-William,项目名称:pbspro,代码行数:101,代码来源:pbs_python_external.c
注:本文中的PyObject_Str函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论