本文整理汇总了C++中py::Object::as_string方法的典型用法代码示例。如果您正苦于以下问题:C++ Object::as_string方法的具体用法?C++ Object::as_string怎么用?C++ Object::as_string使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类py::Object
的用法示例。
在下文中一共展示了Object::as_string方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: asElement
Element CyPy_Element::asElement(const Py::Object& o)
{
if (o.isLong()) {
return Py::Long(o).as_long();
}
if (o.isFloat()) {
return Py::Float(o).as_double();
}
if (o.isString()) {
return o.as_string();
}
if (CyPy_ElementList::check(o)) {
return CyPy_ElementList::value(o);
}
if (CyPy_ElementMap::check(o)) {
return CyPy_ElementMap::value(o);
}
if (CyPy_Operation::check(o)) {
return CyPy_Operation::value(o)->asMessage();
}
if (CyPy_RootEntity::check(o)) {
return CyPy_RootEntity::value(o)->asMessage();
}
if (CyPy_Root::check(o)) {
return CyPy_Root::value(o)->asMessage();
}
if (CyPy_Oplist::check(o)) {
auto& oplist = CyPy_Oplist::value(o);
ListType list;
for (auto& entry : oplist) {
list.push_back(entry->asMessage());
}
return list;
}
if (CyPy_Location::check(o)) {
MapType map;
CyPy_Location::value(o).addToMessage(map);
return map;
}
if (o.isList()) {
return listAsElement(Py::List(o));
}
if (o.isDict()) {
return dictAsElement(Py::Dict(o));
}
if (o.isSequence()) {
return sequenceAsElement(Py::Sequence(o));
}
if (o.isNone()) {
return Element();
}
throw Py::TypeError(String::compose("Contained object (of type %1) could not be converted to an Element.", o.type().as_string()));
}
示例2: CreateUuid
//**************************************************************************
//**************************************************************************
// Get the UUID
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
std::string Uuid::CreateUuid(void)
{
#ifdef FC_OS_WIN32
RPC_STATUS rstat;
UUID uuid;
unsigned char *uuidStr;
rstat = UuidCreate(&uuid);
if (rstat != RPC_S_OK) throw Base::Exception("Cannot convert a unique Windows UUID to a string");
rstat = UuidToString(&uuid, &uuidStr);
if (rstat != RPC_S_OK) throw Base::Exception("Cannot convert a unique Windows UUID to a string");
std::string Uuid((char *)uuidStr);
/* convert it from rcp memory to our own */
//container = nssUTF8_Duplicate(uuidStr, NULL);
RpcStringFree(&uuidStr);
#elif 1
std::string Uuid;
QString uuid = QUuid::createUuid().toString();
uuid = uuid.mid(1);
uuid.chop(1);
Uuid = (const char*)uuid.toAscii();
#else
// use Python's implemententation
std::string Uuid;
PyGILStateLocker lock;
try {
Py::Module module(PyImport_ImportModule("uuid"),true);
Py::Callable method(module.getAttr("uuid4"));
Py::Tuple arg;
Py::Object guid = method.apply(arg);
Uuid = guid.as_string();
}
catch (Py::Exception& e) {
e.clear();
throw Base::Exception("Creation of UUID failed");
}
#endif
return Uuid;
}
示例3: callMethod
QVariant PythonObject::callMethod(const QString& name, const QVariantList& args)
{
#ifdef QROSS_PYTHON_FUNCTION_DEBUG
qrossdebug( QString("PythonObject(%1)::call(%2) isInstance=%3").arg(d->m_pyobject.as_string().c_str()).arg(name).arg(d->m_pyobject.isInstance()) );
#endif
//if(d->m_pyobject.isInstance()) { // if it inherits a PyQt4 QObject/QWidget then it's not counted as instance
try {
Py::Callable method = d->m_pyobject.getAttr(name.toLatin1().data());
if (!method.isCallable()) {
qrossdebug( QString("%1 is not callable (%2).").arg(name).arg(method.str().as_string().c_str()) );
return QVariant();
}
Py::Object pyresult = method.apply( PythonType<QVariantList,Py::Tuple>::toPyObject(args) );
QVariant result = PythonType<QVariant>::toVariant(pyresult);
#ifdef QROSS_PYTHON_FUNCTION_DEBUG
qrossdebug( QString("PythonScript::callFunction() result=%1 variant.toString=%2 variant.typeName=%3").arg(pyresult.as_string().c_str()).arg(result.toString()).arg(result.typeName()) );
#endif
return result;
}
catch(Py::Exception& e) {
//#ifdef QROSS_PYTHON_SCRIPT_CALLFUNC_DEBUG
qrosswarning( QString("PythonScript::callFunction() Exception: %1").arg(Py::value(e).as_string().c_str()) );
//#endif
Py::Object err = Py::value(e);
if(err.ptr() == Py_None) err = Py::type(e); // e.g. string-exceptions have there errormessage in the type-object
QStringList trace;
int lineno;
PythonInterpreter::extractException(trace, lineno);
setError(err.as_string().c_str(), trace.join("\n"), lineno);
PyErr_Print();
}
//}
return QVariant();
}
示例4: sysmod
//.........这里部分代码省略.........
// by e.g. LiquidWeather and those attr is missing in StringIO
// and cause it's buildin we can't just add it but would need to
// implement our own class. Grrrr, what a stupid design :-/
//"try:\n"
//" import cStringIO\n"
//" sys.stdin = cStringIO.StringIO()\n"
//"except:\n"
//" pass\n"
// Class to redirect something. We use this class e.g. to redirect
// <stdout> and <stderr> to a c++ event.
//"class Redirect:\n"
//" def __init__(self, target):\n"
//" self.target = target\n"
//" def write(self, s):\n"
//" self.target.call(s)\n"
// Wrap builtin __import__ method. All import requests are
// first redirected to our PythonModule.import method and
// if the call returns None, then we call the original
// python import mechanism.
"import __builtin__\n"
"import __main__\n"
"import traceback\n"
"sys.modules['_oldmain'] = sys.modules['__main__']\n"
"_main_builtin_import_ = __main__.__builtin__.__import__\n"
"class _Importer:\n"
" def __init__(self, script):\n"
" self.script = script\n"
" self.realImporter = __main__.__builtin__.__import__\n"
" __main__.__builtin__.__import__ = self._import\n"
" def _import(self, name, globals=None, locals=None, fromlist=[], level = -1):\n"
//" try:\n"
//" print \"1===========> _Importer name=%s fromlist=%s\" % (name,fromlist)\n"
#if PY_MAJOR_VERSION >= 3 || (PY_MAJOR_VERSION >= 2 && PY_MINOR_VERSION >= 5)
" mod = __main__._import(self.script, name, globals, locals, fromlist, level)\n"
#else
" mod = __main__._import(self.script, name, globals, locals, fromlist)\n"
#endif
" if mod == None:\n"
" if name == 'qt':\n"
" raise ImportError('Import of the PyQt3 module is not allowed. Please use PyQt4 instead.')\n"
" if name == 'dcop':\n"
" raise ImportError('Import of the KDE3 DCOP module is not allowed. Please use PyQt4 DBUS instead.')\n"
#if PY_MAJOR_VERSION >= 3 || (PY_MAJOR_VERSION >= 2 && PY_MINOR_VERSION >= 5)
" mod = self.realImporter(name, globals, locals, fromlist, level)\n"
#else
" mod = self.realImporter(name, globals, locals, fromlist)\n"
#endif
" if mod != None:\n"
//" print \"3===========> _Importer name=%s fromlist=%s\" % (name,fromlist)\n"
" if globals != None and (not fromlist or len(fromlist)==0 or '*' in fromlist):\n"
" globals[name] = mod\n"
" return mod\n"
//" except ImportError:\n"
//" except:\n"
//" print \"9===========> _Importer Trying ImportError with name=%s fromlist=%s insysmodules=%s\" % (name,fromlist,name in sys.modules)\n"
//" print \" \".join( traceback.format_exception(sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]) )\n"
//" return None\n"
/*
" print \"_Importer name=%s fromlist=%s\" % (name,fromlist)\n"
" if fromlist == None:\n"
" mod = __main__._import(self.script, name, globals, locals, fromlist)\n"
" if mod != None:\n"
" print \"2===========> _Importer name=%s fromlist=%s\" % (name,fromlist)\n"
" globals[name] = mod\n"
" return mod\n"
//" if name in sys.modules:\n" // hack to preserve module paths, needed e.g. for "import os.path"
//" print \"3===========> _Importer name=%s fromlist=%s\" % (name,fromlist)\n"
//" return sys.modules[name]\n"
" print \"3===========> _Importer Trying realImporter with name=%s fromlist=%s insysmodules=%s\" % (name,fromlist,name in sys.modules)\n"
" try:\n"
" mod = self.realImporter(name, globals, locals, fromlist)\n"
" print \"4===========> _Importer Trying realImporter with name=%s fromlist=%s insysmodules=%s module=%s\" % (name,fromlist,name in sys.modules,mod)\n"
//" mod.__init__(name)\n"
//" globals[name] = mod\n"
//" sys.modules[name] = mod\n"
" print \"5===========> _Importer Trying realImporter with name=%s fromlist=%s insysmodules=%s module=%s\" % (name,fromlist,name in sys.modules,mod)\n"
" return mod\n"
" except ImportError:\n"
" print \"6===========> _Importer Trying ImportError with name=%s fromlist=%s insysmodules=%s\" % (name,fromlist,name in sys.modules)\n"
" n = name.split('.')\n"
" if len(n) >= 2:\n"
" print \"7===========> _Importer Trying ImportError with name=%s fromlist=%s insysmodules=%s\" % (name,fromlist,name in sys.modules)\n"
" m = self._import(\".\".join(n[:-1]),globals,locals,[n[-1],])\n"
" print \"8===========> _Importer Trying ImportError with name=%s fromlist=%s insysmodules=%s\" % (name,fromlist,name in sys.modules)\n"
" return self.realImporter(name, globals, locals, fromlist)\n"
" print \"9===========> _Importer Trying ImportError with name=%s fromlist=%s insysmodules=%s\" % (name,fromlist,name in sys.modules)\n"
" raise\n"
*/
;
PyObject* pyrun = PyRun_String(s.toLatin1().data(), Py_file_input, moduledict.ptr(), moduledict.ptr());
if(! pyrun) {
Py::Object errobj = Py::value(Py::Exception()); // get last error
setError( QString("Failed to prepare the __main__ module: %1").arg(errobj.as_string().c_str()) );
}
Py_XDECREF(pyrun); // free the reference.
}