python::detail::new_reference dtype::convert(python::object const & arg, bool align) { PyArray_Descr* obj=NULL; if (align) { if (PyArray_DescrAlignConverter(arg.ptr(), &obj) < 0) python::throw_error_already_set(); } else { if (PyArray_DescrConverter(arg.ptr(), &obj) < 0) python::throw_error_already_set(); } return python::detail::new_reference(reinterpret_cast<PyObject*>(obj)); }
void set_type_mask(PairQuantity& obj, python::object smbli, python::object smblj, python::object msk, python::object others) { using namespace std; if (Py_None != others.ptr()) mask_all_pairs(obj, others); python::extract<string> getsmbli(smbli); python::extract<string> getsmblj(smblj); bool mask = msk; // short circuit for normal call if (getsmbli.check() && getsmblj.check()) { obj.setTypeMask(getsmbli(), getsmblj(), mask); return; } vector<string> isymbols = parsepairtypes(getsmbli, smbli); vector<string> jsymbols = parsepairtypes(getsmblj, smblj); vector<string>::const_iterator tii, tjj; for (tii = isymbols.begin(); tii != isymbols.end(); ++tii) { for (tjj = jsymbols.begin(); tjj != jsymbols.end(); ++tjj) { obj.setTypeMask(*tii, *tjj, mask); } } }
void set_pair_mask(PairQuantity& obj, python::object i, python::object j, python::object msk, python::object others) { if (Py_None != others.ptr()) mask_all_pairs(obj, others); python::extract<int> geti(i); python::extract<int> getj(j); bool mask = msk; // short circuit for normal call if (geti.check() && getj.check()) { obj.setPairMask(geti(), getj(), mask); return; } std::vector<int> iindices = parsepairindex(i); std::vector<int> jindices = parsepairindex(j); std::vector<int>::const_iterator ii, jj; for (ii = iindices.begin(); ii != iindices.end(); ++ii) { for (jj = jindices.begin(); jj != jindices.end(); ++jj) { obj.setPairMask(*ii, *jj, mask); } } }
static python::object apply(python::object const & out_obj, OutArrService const & out_arr_service) { if(out_obj.ptr() == Py_None) return static_cast<python::object>(out_arr_service.get_arr()); else return python::object(); }
TailGF::TailGF(int OrderMinMIN_, int OrderMaxMAX_, python::object Arr, int OrderMax_, python::list IndicesL_, python::list IndicesR_ ) : IndicesL(IndicesL_),IndicesR(IndicesR_), N1(int(PySequence_Size(IndicesL_.ptr()))),// int for 64bits N2(int(PySequence_Size(IndicesR_.ptr()))),// int for 64bits OrderMinMIN(OrderMinMIN_),OrderMaxMAX(OrderMaxMAX_), M(Arr.ptr()), OrderMaxArray(N1,N2,COrder) { if (M.shape() != TinyVector<int,3>(OrderMaxMAX - OrderMinMIN +1,N1,N2)) TRIQS_RUNTIME_ERROR<<"Reconstruction : size mismatch"; M.reindexSelf(TinyVector<int,3>(OrderMinMIN,1,1)); OrderMaxArray = OrderMax_; }
void initPython(int argc, char **argv) { assert(!pythonInitialized); try { Py_Initialize(); python_main_module = python::import("__main__"); python_main_namespace = python_main_module.attr("__dict__"); } catch(python::error_already_set const &) { printf("Error in Python initialization\r\n"); PyErr_Print(); assert(0 && "Halting on Python error"); } pythonInitialized = true; // Set up general stuff to allow Python to be used EXEC_PYTHON("import sys, os"); // TODO: remove sys.path in binary builds, leave only a tiny subset of all of CPy #ifdef INTENSITY_INSTALL_ROOT printf("Changing directory to install root: %s\r\n", INTENSITY_INSTALL_ROOT); EXEC_PYTHON("os.chdir('" + std::string(INTENSITY_INSTALL_ROOT) + "')"); #endif EXEC_PYTHON("sys.path = ['', 'src', os.path.join('src', 'python') ] + sys.path"); EXEC_PYTHON("print 'Python path:', sys.path"); EXEC_PYTHON("from intensity.c_module import *"); #if 0 //def WINDOWS - TODO: Probably need to place in BAT file // For Windows, use pre-prepared DLL files in windows/dll EXEC_PYTHON("os.environ['PATH'] = os.path.join(os.getcwd(), 'windows', 'dll') + ';' + os.environ['PATH']"); #endif // Pass C commandline arguments untouched to Python try { boost::python::list args; for (int i = 0; i < argc; i++) args.append(std::string(argv[i])); REFLECT_PYTHON( set_python_args ) set_python_args(args); } catch(boost::python::error_already_set const &) { printf("Error in Python execution of setting args\r\n"); PyErr_Print(); assert(0 && "Halting on Python error"); } }
std::vector<string> PythonConsoleViewImpl::getMemberNames(python::object& moduleObject) { PyObject* pPyObject = moduleObject.ptr(); if(pPyObject == NULL){ return std::vector<string>(); } python::handle<> h( PyObject_Dir(pPyObject) ); python::list memberNames = python::extract<python::list>(python::object(h)); std::vector<string> retNames; for(int i=0; i < python::len(memberNames); ++i){ if(!strstr(string(python::extract<string>(memberNames[i])).c_str(), "__" )) retNames.push_back(string(python::extract<string>(memberNames[i]))); } return retNames; }
python::object PythonConsoleViewImpl::getMemberObject(std::vector<string>& moduleNames, python::object& parentObject) { if(moduleNames.size() == 0){ return parentObject; }else{ string moduleName = moduleNames.front(); moduleNames.erase(moduleNames.begin()); std::vector<string> memberNames = getMemberNames(parentObject); if(std::find(memberNames.begin(),memberNames.end(),moduleName) == memberNames.end()){ return python::object(); }else{ python::object childObject = parentObject.attr(moduleName.c_str()); return getMemberObject(moduleNames,childObject); } } }
Poco::SharedPtr<Poco::DynamicAny> RecordHelper::pythonObjectToAny(python::object obj) { if (PyBool_Check(obj.ptr())) { return new Poco::DynamicAny(python::extract<bool>(obj)()); } else if (PyInt_Check(obj.ptr())) { return new Poco::DynamicAny(python::extract<long>(obj)()); } else if (PyLong_Check(obj.ptr())) { return new Poco::DynamicAny(python::extract<long>(obj)()); } else if (PyFloat_Check(obj.ptr())) { return new Poco::DynamicAny(python::extract<double>(obj)()); } else if (PyString_Check(obj.ptr())) { return new Poco::DynamicAny(python::extract<std::string>(obj)()); } else if (PyUnicode_Check(obj.ptr())) { return new Poco::DynamicAny(python::extract<std::string>(python::str(obj))()); } else if (PyList_Check(obj.ptr()) || PyTuple_Check(obj.ptr())) { ssize_t max = python::len(obj); ssize_t i = 0; std::vector<Poco::DynamicAny> result; for (; i < max; ++i) { python::object item = obj[i]; result.push_back(*pythonObjectToAny(item)); } return new Poco::DynamicAny(result); } else { throw SmartCubeError("Unsupported Python object type."); } return NULL; }
void PyRegisterService( //const Service::ClassType& name, const python::object& py_service_class) { Service::ClassType serv_type; try { serv_type = python::extract<Service::ClassType> ( py_service_class.attr("__name__")); } catch ( ... ) { PyErr_Print(); PyErr_Clear(); Logger::failure("PyRegister: Unable to register python service"); return; } theServiceManager().registerPyService( serv_type, py_service_class ); }
void PyRegisterEntity( //const Entity::ClassType& name, const python::object& py_entity_class) { Entity::ClassType ent_type; try { ent_type = python::extract<Entity::ClassType> ( py_entity_class.attr("__name__")); } catch ( ... ) { PyErr_Print(); PyErr_Clear(); Logger::failure("PyRegister: Unable to register python entity"); return; } theEntityManager().registerPyEntity( ent_type, py_entity_class ); }
System::System(python::object _pyobj) { //This is defined in mpi4py.h //struct __pyx_obj_6mpi4py_3MPI_Comm { // PyObject_HEAD // MPI_Comm ob_mpi; // int flags; //}; // Following is some extreme typecasting which we need to convert the // pmi python object pmi._MPIcomm into a shared_ptr< boost::mpi::communicator > // I have not yet figured out how to do it in a more elegant way PyObject *pyobj = _pyobj.ptr(); // in mpi4py.1.2.1 this has to be: // __pyx_obj_6mpi4py_3MPI_Comm * pyMPIComm = (__pyx_obj_6mpi4py_3MPI_Comm *) pyobj; // in version >= mpi4py.1.2.2 this has to be: PyMPICommObject* pyMPIComm = (PyMPICommObject*) pyobj; MPI_Comm * comm_p = &pyMPIComm->ob_mpi; shared_ptr< mpi::communicator > newcomm = make_shared< mpi::communicator >(*comm_p, mpi::comm_attach); comm = newcomm; maxCutoff = 0.0; }