void IcePy::handleSystemExit(PyObject* ex) { // // This code is similar to handle_system_exit in pythonrun.c. // PyObjectHandle code; if(PyExceptionInstance_Check(ex)) { code = getAttr(ex, "code", true); } else { code = ex; Py_INCREF(ex); } int status; if(PyLong_Check(code.get())) { status = static_cast<int>(PyLong_AsLong(code.get())); } else { PyObject_Print(code.get(), stderr, Py_PRINT_RAW); PySys_WriteStderr(STRCAST("\n")); status = 1; } code = 0; Py_Exit(status); }
static PyObject* communicatorFlushBatchRequests(CommunicatorObject* self, PyObject* args) { PyObject* compressBatchType = lookupType("Ice.CompressBatch"); PyObject* compressBatch; if(!PyArg_ParseTuple(args, STRCAST("O!"), compressBatchType, &compressBatch)) { return 0; } PyObjectHandle v = getAttr(compressBatch, "_value", false); assert(v.get()); Ice::CompressBatch cb = static_cast<Ice::CompressBatch>(PyLong_AsLong(v.get())); assert(self->communicator); try { AllowThreads allowThreads; // Release Python's global interpreter lock to avoid a potential deadlock. (*self->communicator)->flushBatchRequests(cb); } catch(const Ice::Exception& ex) { setPythonException(ex); return 0; } Py_INCREF(Py_None); return Py_None; }
Ice::ValuePtr IcePy::FactoryWrapper::create(const string& id) { AdoptThread adoptThread; // Ensure the current thread is able to call into Python. // // Get the type information. // ValueInfoPtr info = getValueInfo(id); if(!info) { return 0; } PyObjectHandle obj = PyObject_CallFunction(_valueFactory, STRCAST("s"), id.c_str()); if(!obj.get()) { assert(PyErr_Occurred()); throw AbortMarshaling(); } if(obj.get() == Py_None) { return 0; } return new ObjectReader(obj.get(), info); }
static PyObject* adapterGetPublishedEndpoints(ObjectAdapterObject* self) { assert(self->adapter); Ice::EndpointSeq endpoints; try { endpoints = (*self->adapter)->getPublishedEndpoints(); } catch(const Ice::Exception& ex) { setPythonException(ex); return 0; } int count = static_cast<int>(endpoints.size()); PyObjectHandle result = PyTuple_New(count); int i = 0; for(Ice::EndpointSeq::const_iterator p = endpoints.begin(); p != endpoints.end(); ++p, ++i) { PyObjectHandle endp = createEndpoint(*p); if(!endp.get()) { return 0; } PyTuple_SET_ITEM(result.get(), i, endp.release()); // PyTuple_SET_ITEM steals a reference. } return result.release(); }
void IcePy::setPythonException(const Ice::Exception& ex) { PyObjectHandle p = convertException(ex); if(p.get()) { setPythonException(p.get()); } }
PyObject* IcePy::callMethod(PyObject* obj, const string& name, PyObject* arg1, PyObject* arg2) { PyObjectHandle method = PyObject_GetAttrString(obj, const_cast<char*>(name.c_str())); if(!method.get()) { return 0; } return callMethod(method.get(), arg1, arg2); }
string IcePy::LoggerWrapper::getPrefix() { AdoptThread adoptThread; PyObjectHandle tmp = PyObject_CallMethod(_logger.get(), STRCAST("getPrefix"), 0); if(!tmp.get()) { throwPythonException(); } return getString(tmp.get()); }
Ice::ObjectAdapterPtr IcePy::unwrapObjectAdapter(PyObject* obj) { #ifndef NDEBUG PyObject* wrapperType = lookupType("Ice.ObjectAdapterI"); #endif assert(wrapperType); assert(PyObject_IsInstance(obj, wrapperType)); PyObjectHandle impl = PyObject_GetAttrString(obj, STRCAST("_impl")); assert(impl.get()); return getObjectAdapter(impl.get()); }
static PyObject* communicatorFindAllAdminFacets(CommunicatorObject* self) { assert(self->communicator); Ice::FacetMap facetMap; try { facetMap = (*self->communicator)->findAllAdminFacets(); } catch(const Ice::Exception& ex) { setPythonException(ex); return 0; } PyObjectHandle result = PyDict_New(); if(!result.get()) { return 0; } PyTypeObject* objectType = reinterpret_cast<PyTypeObject*>(lookupType("Ice.Object")); PyObjectHandle plainObject = objectType->tp_alloc(objectType, 0); for(Ice::FacetMap::const_iterator p = facetMap.begin(); p != facetMap.end(); ++p) { PyObjectHandle obj = plainObject; ServantWrapperPtr wrapper = ServantWrapperPtr::dynamicCast(p->second); if(wrapper) { obj = wrapper->getObject(); } else { Ice::NativePropertiesAdminPtr props = Ice::NativePropertiesAdminPtr::dynamicCast(p->second); if(props) { obj = createNativePropertiesAdmin(props); } } if(PyDict_SetItemString(result.get(), const_cast<char*>(p->first.c_str()), obj.get()) < 0) { return 0; } } return result.release(); }
string IcePy::PyException::getTypeName() { PyObject* cls = reinterpret_cast<PyObject*>(ex.get()->ob_type); PyObjectHandle name = getAttr(cls, "__name__", false); assert(name.get()); PyObjectHandle mod = getAttr(cls, "__module__", false); assert(mod.get()); string result = getString(mod.get()); result += "."; result += getString(name.get()); return result; }
string IcePy::getFunction() { // // Get name of current function. // PyFrameObject *f = PyThreadState_GET()->frame; PyObjectHandle code = getAttr(reinterpret_cast<PyObject*>(f), "f_code", false); assert(code.get()); PyObjectHandle func = getAttr(code.get(), "co_name", false); assert(func.get()); return getString(func.get()); }
Ice::LoggerPtr IcePy::LoggerWrapper::cloneWithPrefix(const string& prefix) { AdoptThread adoptThread; // Ensure the current thread is able to call into Python. PyObjectHandle tmp = PyObject_CallMethod(_logger.get(), STRCAST("cloneWithPrefix"), STRCAST("s"), prefix.c_str()); if(!tmp.get()) { throwPythonException(); } return new LoggerWrapper(tmp.get()); }
string IcePy::PyException::getTraceback() { if(!_tb.get()) { return string(); } // // We need the equivalent of the following Python code: // // import traceback // list = traceback.format_exception(type, ex, tb) // PyObjectHandle str = createString("traceback"); PyObjectHandle mod = PyImport_Import(str.get()); assert(mod.get()); // Unable to import traceback module - Python installation error? PyObject* func = PyDict_GetItemString(PyModule_GetDict(mod.get()), "format_exception"); assert(func); // traceback.format_exception must be present. PyObjectHandle args = Py_BuildValue("(OOO)", _type.get(), ex.get(), _tb.get()); assert(args.get()); PyObjectHandle list = PyObject_CallObject(func, args.get()); assert(list.get()); string result; for(Py_ssize_t i = 0; i < PyList_GET_SIZE(list.get()); ++i) { string s = getString(PyList_GetItem(list.get(), i)); result += s; } return result; }
static PyObject* communicatorProxyToProperty(CommunicatorObject* self, PyObject* args) { // // We don't want to accept None here, so we can specify ProxyType and force // the caller to supply a proxy object. // PyObject* proxyObj; PyObject* strObj; if(!PyArg_ParseTuple(args, STRCAST("O!O"), &ProxyType, &proxyObj, &strObj)) { return 0; } Ice::ObjectPrx proxy = getProxy(proxyObj); string str; if(!getStringArg(strObj, "property", str)) { return 0; } assert(self->communicator); Ice::PropertyDict dict; try { dict = (*self->communicator)->proxyToProperty(proxy, str); } catch(const Ice::Exception& ex) { setPythonException(ex); return 0; } PyObjectHandle result = PyDict_New(); if(result.get()) { for(Ice::PropertyDict::iterator p = dict.begin(); p != dict.end(); ++p) { PyObjectHandle key = createString(p->first); PyObjectHandle val = createString(p->second); if(!val.get() || PyDict_SetItem(result.get(), key.get(), val.get()) < 0) { return 0; } } } return result.release(); }
void IcePy::ServantLocatorWrapper::finished(const Ice::Current& current, const Ice::ObjectPtr&, const Ice::LocalObjectPtr& cookie) { AdoptThread adoptThread; // Ensure the current thread is able to call into Python. CookiePtr c = CookiePtr::dynamicCast(cookie); assert(c); ServantWrapperPtr wrapper = ServantWrapperPtr::dynamicCast(c->servant); PyObjectHandle servantObj = wrapper->getObject(); PyObjectHandle res = PyObject_CallMethod(_locator, STRCAST("finished"), STRCAST("OOO"), c->current, servantObj.get(), c->cookie); if(PyErr_Occurred()) { PyException ex; // Retrieve the exception before another Python API call clears it. // // A locator that calls sys.exit() will raise the SystemExit exception. // This is normally caught by the interpreter, causing it to exit. // However, we have no way to pass this exception to the interpreter, // so we act on it directly. // ex.checkSystemExit(); PyObject* userExceptionType = lookupType("Ice.UserException"); if(PyObject_IsInstance(ex.ex.get(), userExceptionType)) { throw ExceptionWriter(ex.ex); } ex.raise(); } }
void invoke(const Ice::ConnectionPtr& con) { AdoptThread adoptThread; // Ensure the current thread is able to call into Python. #ifndef NDEBUG ConnectionObject* c = reinterpret_cast<ConnectionObject*>(_con); assert(con == *(c->connection)); #endif PyObjectHandle args = Py_BuildValue(STRCAST("(O)"), _con); assert(_cb); PyObjectHandle tmp = PyObject_Call(_cb, args.get(), 0); if(PyErr_Occurred()) { PyException ex; // Retrieve it before another Python API call clears it. // // A callback that calls sys.exit() will raise the SystemExit exception. // This is normally caught by the interpreter, causing it to exit. // However, we have no way to pass this exception to the interpreter, // so we act on it directly. // ex.checkSystemExit(); ex.raise(); } }
static PyObject* adapterRemoveAllFacets(ObjectAdapterObject* self, PyObject* args) { PyObject* identityType = lookupType("Ice.Identity"); PyObject* id; if(!PyArg_ParseTuple(args, STRCAST("O!"), identityType, &id)) { return 0; } Ice::Identity ident; if(!getIdentity(id, ident)) { return 0; } assert(self->adapter); Ice::FacetMap facetMap; try { facetMap = (*self->adapter)->removeAllFacets(ident); } catch(const Ice::Exception& ex) { setPythonException(ex); return 0; } PyObjectHandle result = PyDict_New(); if(!result.get()) { return 0; } for(Ice::FacetMap::iterator p = facetMap.begin(); p != facetMap.end(); ++p) { ServantWrapperPtr wrapper = ServantWrapperPtr::dynamicCast(p->second); assert(wrapper); PyObjectHandle obj = wrapper->getObject(); if(PyDict_SetItemString(result.get(), const_cast<char*>(p->first.c_str()), obj.get()) < 0) { return 0; } } return result.release(); }
bool IcePy::setIdentity(PyObject* p, const Ice::Identity& ident) { assert(checkIdentity(p)); PyObjectHandle name = createString(ident.name); PyObjectHandle category = createString(ident.category); if(!name.get() || !category.get()) { return false; } if(PyObject_SetAttrString(p, STRCAST("name"), name.get()) < 0 || PyObject_SetAttrString(p, STRCAST("category"), category.get()) < 0) { return false; } return true; }
PyObject* IcePy::createIdentity(const Ice::Identity& ident) { PyObject* identityType = lookupType("Ice.Identity"); PyObjectHandle obj = PyObject_CallObject(identityType, 0); if(!obj.get()) { return 0; } if(!setIdentity(obj.get(), ident)) { return 0; } return obj.release(); }
template<typename T> PyObject* createVersion(const T& version, const char* type) { PyObject* versionType = lookupType(type); PyObjectHandle obj = PyObject_CallObject(versionType, 0); if(!obj.get()) { return 0; } if(!setVersion<T>(obj.get(), version, type)) { return 0; } return obj.release(); }
template<typename T> bool setVersion(PyObject* p, const T& version, const char* type) { assert(checkIsInstance(p, type)); PyObjectHandle major = PyLong_FromLong(version.major); PyObjectHandle minor = PyLong_FromLong(version.minor); if(!major.get() || !minor.get()) { return false; } if(PyObject_SetAttrString(p, STRCAST("major"), major.get()) < 0 || PyObject_SetAttrString(p, STRCAST("minor"), minor.get()) < 0) { return false; } return true; }
static PyObject* implicitContextGetContext(ImplicitContextObject* self) { Ice::Context ctx = (*self->implicitContext)->getContext(); PyObjectHandle dict = PyDict_New(); if(!dict.get()) { return 0; } if(!contextToDictionary(ctx, dict.get())) { return 0; } return dict.release(); }
static PyObject* propertiesGetPropertiesForPrefix(PropertiesObject* self, PyObject* args) { PyObject* prefixObj; if(!PyArg_ParseTuple(args, STRCAST("O"), &prefixObj)) { return 0; } string prefix; if(!getStringArg(prefixObj, "prefix", prefix)) { return 0; } assert(self->properties); Ice::PropertyDict dict; try { dict = (*self->properties)->getPropertiesForPrefix(prefix); } catch(const Ice::Exception& ex) { setPythonException(ex); return 0; } PyObjectHandle result = PyDict_New(); if(result.get()) { for(Ice::PropertyDict::iterator p = dict.begin(); p != dict.end(); ++p) { PyObjectHandle key = createString(p->first); PyObjectHandle val = createString(p->second); if(!val.get() || PyDict_SetItem(result.get(), key.get(), val.get()) < 0) { return 0; } } } return result.release(); }
Ice::ValuePtr IcePy::DefaultValueFactory::create(const string& id) { AdoptThread adoptThread; // Ensure the current thread is able to call into Python. Ice::ValuePtr v; // // Give the application-provided default factory a chance to create the object first. // if(_delegate) { v = _delegate->create(id); if(v) { return v; } } // // Get the type information. // ValueInfoPtr info = getValueInfo(id); if(!info) { return 0; } // // Instantiate the object. // PyTypeObject* type = reinterpret_cast<PyTypeObject*>(info->pythonType.get()); PyObjectHandle args = PyTuple_New(0); PyObjectHandle obj = type->tp_new(type, args.get(), 0); if(!obj.get()) { assert(PyErr_Occurred()); throw AbortMarshaling(); } return new ObjectReader(obj.get(), info); }
void IcePy::LoggerWrapper::error(const string& message) { AdoptThread adoptThread; // Ensure the current thread is able to call into Python. PyObjectHandle tmp = PyObject_CallMethod(_logger.get(), STRCAST("error"), STRCAST("s"), message.c_str()); if(!tmp.get()) { throwPythonException(); } }
bool IcePy::contextToDictionary(const Ice::Context& ctx, PyObject* dict) { assert(PyDict_Check(dict)); for(Ice::Context::const_iterator p = ctx.begin(); p != ctx.end(); ++p) { PyObjectHandle key = createString(p->first); PyObjectHandle value = createString(p->second); if(!key.get() || !value.get()) { return false; } if(PyDict_SetItem(dict, key.get(), value.get()) < 0) { return false; } } return true; }
PyObject* IcePy::wrapObjectAdapter(const Ice::ObjectAdapterPtr& adapter) { // // Create an Ice.ObjectAdapter wrapper for IcePy.ObjectAdapter. // PyObjectHandle adapterI = createObjectAdapter(adapter); if(!adapterI.get()) { return 0; } PyObject* wrapperType = lookupType("Ice.ObjectAdapterI"); assert(wrapperType); PyObjectHandle args = PyTuple_New(1); if(!args.get()) { return 0; } PyTuple_SET_ITEM(args.get(), 0, adapterI.release()); return PyObject_Call(wrapperType, args.get(), 0); }
void IcePy::UpdateCallbackWrapper::updated(const Ice::PropertyDict& dict) { AdoptThread adoptThread; // Ensure the current thread is able to call into Python. PyObjectHandle result = PyDict_New(); if(result.get()) { for(Ice::PropertyDict::const_iterator p = dict.begin(); p != dict.end(); ++p) { PyObjectHandle key = createString(p->first); PyObjectHandle val = createString(p->second); if(!val.get() || PyDict_SetItem(result.get(), key.get(), val.get()) < 0) { return; } } } PyObjectHandle obj = PyObject_CallMethod(_callback, STRCAST("updated"), STRCAST("O"), result.get()); if(!obj.get()) { assert(PyErr_Occurred()); throw AbortMarshaling(); } }
bool IcePy::getIdentity(PyObject* p, Ice::Identity& ident) { assert(checkIdentity(p)); PyObjectHandle name = getAttr(p, "name", true); PyObjectHandle category = getAttr(p, "category", true); if(name.get()) { if(!checkString(name.get())) { PyErr_Format(PyExc_ValueError, STRCAST("identity name must be a string")); return false; } ident.name = getString(name.get()); } if(category.get()) { if(!checkString(category.get())) { PyErr_Format(PyExc_ValueError, STRCAST("identity category must be a string")); return false; } ident.category = getString(category.get()); } return true; }
static PyObject* wssConnectionInfoGetHeaders(ConnectionInfoObject* self) { IceSSL::WSSConnectionInfoPtr info = IceSSL::WSSConnectionInfoPtr::dynamicCast(*self->connectionInfo); assert(info); PyObjectHandle result = PyDict_New(); if(result.get()) { for(Ice::HeaderDict::iterator p = info->headers.begin(); p != info->headers.end(); ++p) { PyObjectHandle key = createString(p->first); PyObjectHandle val = createString(p->second); if(!val.get() || PyDict_SetItem(result.get(), key.get(), val.get()) < 0) { return 0; } } } return result.release(); }