/** * Function to call to calculate_ER * @return: effective radius value */ static PyObject * calculate_ER(CBCCrystalModel *self) { PyObject* pars; int npars; // Get parameters // Reader parameter dictionary self->model->phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "phi") ); self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); self->model->dnn = PyFloat_AsDouble( PyDict_GetItemString(self->params, "dnn") ); self->model->sldSph = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSph") ); self->model->d_factor = PyFloat_AsDouble( PyDict_GetItemString(self->params, "d_factor") ); self->model->psi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "psi") ); self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); self->model->theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "theta") ); self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") ); // Read in dispersion parameters PyObject* disp_dict; DispersionVisitor* visitor = new DispersionVisitor(); disp_dict = PyDict_GetItemString(self->dispersion, "radius"); self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict); disp_dict = PyDict_GetItemString(self->dispersion, "phi"); self->model->phi.dispersion->accept_as_destination(visitor, self->model->phi.dispersion, disp_dict); disp_dict = PyDict_GetItemString(self->dispersion, "psi"); self->model->psi.dispersion->accept_as_destination(visitor, self->model->psi.dispersion, disp_dict); disp_dict = PyDict_GetItemString(self->dispersion, "theta"); self->model->theta.dispersion->accept_as_destination(visitor, self->model->theta.dispersion, disp_dict); return Py_BuildValue("d",(*(self->model)).calculate_ER()); }
PyMODINIT_FUNC SIP_MODULE_ENTRY() #endif { static PyMethodDef sip_methods[] = { {0, 0, 0, 0} }; #if PY_MAJOR_VERSION >= 3 static PyModuleDef sip_module_def = { PyModuleDef_HEAD_INIT, "PyQt5.QtQuickWidgets", NULL, -1, sip_methods, NULL, NULL, NULL, NULL }; #endif PyObject *sipModule, *sipModuleDict; PyObject *sip_sipmod, *sip_capiobj; /* Initialise the module and get it's dictionary. */ #if PY_MAJOR_VERSION >= 3 sipModule = PyModule_Create(&sip_module_def); #elif PY_VERSION_HEX >= 0x02050000 sipModule = Py_InitModule(sipName_PyQt5_QtQuickWidgets, sip_methods); #else sipModule = Py_InitModule(const_cast<char *>(sipName_PyQt5_QtQuickWidgets), sip_methods); #endif if (sipModule == NULL) SIP_MODULE_RETURN(NULL); sipModuleDict = PyModule_GetDict(sipModule); /* Get the SIP module's API. */ #if PY_VERSION_HEX >= 0x02050000 sip_sipmod = PyImport_ImportModule(SIP_MODULE_NAME); #else sip_sipmod = PyImport_ImportModule(const_cast<char *>(SIP_MODULE_NAME)); #endif if (sip_sipmod == NULL) { SIP_MODULE_DISCARD(sipModule); SIP_MODULE_RETURN(NULL); } sip_capiobj = PyDict_GetItemString(PyModule_GetDict(sip_sipmod), "_C_API"); Py_DECREF(sip_sipmod); #if defined(SIP_USE_PYCAPSULE) if (sip_capiobj == NULL || !PyCapsule_CheckExact(sip_capiobj)) #else if (sip_capiobj == NULL || !PyCObject_Check(sip_capiobj)) #endif { SIP_MODULE_DISCARD(sipModule); SIP_MODULE_RETURN(NULL); } #if defined(SIP_USE_PYCAPSULE) sipAPI_QtQuickWidgets = reinterpret_cast<const sipAPIDef *>(PyCapsule_GetPointer(sip_capiobj, SIP_MODULE_NAME "._C_API")); #else sipAPI_QtQuickWidgets = reinterpret_cast<const sipAPIDef *>(PyCObject_AsVoidPtr(sip_capiobj)); #endif #if defined(SIP_USE_PYCAPSULE) if (sipAPI_QtQuickWidgets == NULL) { SIP_MODULE_DISCARD(sipModule); SIP_MODULE_RETURN(NULL); } #endif /* Export the module and publish it's API. */ if (sipExportModule(&sipModuleAPI_QtQuickWidgets,SIP_API_MAJOR_NR,SIP_API_MINOR_NR,0) < 0) { SIP_MODULE_DISCARD(sipModule); SIP_MODULE_RETURN(0); } sip_QtQuickWidgets_qt_metaobject = (sip_qt_metaobject_func)sipImportSymbol("qtcore_qt_metaobject"); sip_QtQuickWidgets_qt_metacall = (sip_qt_metacall_func)sipImportSymbol("qtcore_qt_metacall"); sip_QtQuickWidgets_qt_metacast = (sip_qt_metacast_func)sipImportSymbol("qtcore_qt_metacast"); if (!sip_QtQuickWidgets_qt_metacast) Py_FatalError("Unable to import qtcore_qt_metacast"); /* Initialise the module now all its dependencies have been set up. */ if (sipInitModule(&sipModuleAPI_QtQuickWidgets,sipModuleDict) < 0) { SIP_MODULE_DISCARD(sipModule); SIP_MODULE_RETURN(0); } /* Get the APIs of the modules that this one is dependent on. */ sipModuleAPI_QtQuickWidgets_QtCore = sipModuleAPI_QtQuickWidgets.em_imports[0].im_module; sipModuleAPI_QtQuickWidgets_QtGui = sipModuleAPI_QtQuickWidgets.em_imports[1].im_module; sipModuleAPI_QtQuickWidgets_QtWidgets = sipModuleAPI_QtQuickWidgets.em_imports[2].im_module; sipModuleAPI_QtQuickWidgets_QtNetwork = sipModuleAPI_QtQuickWidgets.em_imports[3].im_module; sipModuleAPI_QtQuickWidgets_QtQml = sipModuleAPI_QtQuickWidgets.em_imports[4].im_module; sipModuleAPI_QtQuickWidgets_QtQuick = sipModuleAPI_QtQuickWidgets.em_imports[5].im_module; SIP_MODULE_RETURN(sipModule); }
PyObject *_translate_pathtranslate ( PyObject *self, PyObject *args, PyObject *kwargs ) /* * Usage: translate ( path, map, sep = "/." ) */ { char *path = NULL, *sep = "/."; PyObject *ret, *dict, *val; /* Variable length buffer */ size_t i, j = 0, now = 0, end = BUFSIZ; char *out = malloc ( end ), *s, *ourpath; char delim; /* The argument list */ static char *kwlist[] = { "path", "map", "sep", NULL }; if ( !PyArg_ParseTupleAndKeywords ( args, kwargs, "sO!|s", kwlist, &path, &PyDict_Type, &dict, &sep ) ) return NULL; /* Path is mutated in process - make a copy */ ourpath = strdup( path ); for ( i = strcspn ( ourpath + j, sep ); i + j <= strlen(path); i = strcspn ( ourpath + j, sep )) { /* Get the separator character */ if ( (delim = ourpath[i+j]) != '\0' ) ourpath[i+j] = '\0'; /* The segment is from path + j to i */ if ( (val = PyDict_GetItemString ( dict, ourpath + j )) ) { /* Translate and copy */ if ( !(PyString_Check(val)) ) { PyErr_SetString ( PyExc_ValueError, "mapping should contain " "string values only" ); return NULL; } s = PyString_AS_STRING(val); } else { /* No translation, plain copy */ s = ourpath + j; } /* Reallocate if necessary */ while ( now + strlen(s) + 2 > end ) out = realloc ( out, (end = end + BUFSIZ) ); /* Copy the new value */ strcpy ( out + now, s ); now += strlen(s); out[now++] = delim; /* Add the separator or '\0'*/ /* Increment j */ j += i + 1; } /* Create the return value */ ret = PyString_FromString ( out ); free ( out ); free ( ourpath ); return ret; }
NPY_NO_EXPORT int PyArray_DTypeFromObjectHelper(PyObject *obj, int maxdims, PyArray_Descr **out_dtype, int string_type) { int i, size; PyArray_Descr *dtype = NULL; PyObject *ip; Py_buffer buffer_view; /* types for sequence handling */ PyObject ** objects; PyObject * seq; PyTypeObject * common_type; /* Check if it's an ndarray */ if (PyArray_Check(obj)) { dtype = PyArray_DESCR((PyArrayObject *)obj); Py_INCREF(dtype); goto promote_types; } /* See if it's a python None */ if (obj == Py_None) { dtype = PyArray_DescrFromType(NPY_OBJECT); if (dtype == NULL) { goto fail; } Py_INCREF(dtype); goto promote_types; } /* Check if it's a NumPy scalar */ else if (PyArray_IsScalar(obj, Generic)) { if (!string_type) { dtype = PyArray_DescrFromScalar(obj); if (dtype == NULL) { goto fail; } } else { int itemsize; PyObject *temp; if (string_type == NPY_STRING) { if ((temp = PyObject_Str(obj)) == NULL) { return -1; } #if defined(NPY_PY3K) #if PY_VERSION_HEX >= 0x03030000 itemsize = PyUnicode_GetLength(temp); #else itemsize = PyUnicode_GET_SIZE(temp); #endif #else itemsize = PyString_GET_SIZE(temp); #endif } else if (string_type == NPY_UNICODE) { #if defined(NPY_PY3K) if ((temp = PyObject_Str(obj)) == NULL) { #else if ((temp = PyObject_Unicode(obj)) == NULL) { #endif return -1; } itemsize = PyUnicode_GET_DATA_SIZE(temp); #ifndef Py_UNICODE_WIDE itemsize <<= 1; #endif } else { goto fail; } Py_DECREF(temp); if (*out_dtype != NULL && (*out_dtype)->type_num == string_type && (*out_dtype)->elsize >= itemsize) { return 0; } dtype = PyArray_DescrNewFromType(string_type); if (dtype == NULL) { goto fail; } dtype->elsize = itemsize; } goto promote_types; } /* Check if it's a Python scalar */ dtype = _array_find_python_scalar_type(obj); if (dtype != NULL) { if (string_type) { int itemsize; PyObject *temp; if (string_type == NPY_STRING) { if ((temp = PyObject_Str(obj)) == NULL) { return -1; } #if defined(NPY_PY3K) #if PY_VERSION_HEX >= 0x03030000 itemsize = PyUnicode_GetLength(temp); #else itemsize = PyUnicode_GET_SIZE(temp); #endif #else itemsize = PyString_GET_SIZE(temp); #endif } else if (string_type == NPY_UNICODE) { #if defined(NPY_PY3K) if ((temp = PyObject_Str(obj)) == NULL) { #else if ((temp = PyObject_Unicode(obj)) == NULL) { #endif return -1; } itemsize = PyUnicode_GET_DATA_SIZE(temp); #ifndef Py_UNICODE_WIDE itemsize <<= 1; #endif } else { goto fail; } Py_DECREF(temp); if (*out_dtype != NULL && (*out_dtype)->type_num == string_type && (*out_dtype)->elsize >= itemsize) { return 0; } dtype = PyArray_DescrNewFromType(string_type); if (dtype == NULL) { goto fail; } dtype->elsize = itemsize; } goto promote_types; } /* Check if it's an ASCII string */ if (PyBytes_Check(obj)) { int itemsize = PyString_GET_SIZE(obj); /* If it's already a big enough string, don't bother type promoting */ if (*out_dtype != NULL && (*out_dtype)->type_num == NPY_STRING && (*out_dtype)->elsize >= itemsize) { return 0; } dtype = PyArray_DescrNewFromType(NPY_STRING); if (dtype == NULL) { goto fail; } dtype->elsize = itemsize; goto promote_types; } /* Check if it's a Unicode string */ if (PyUnicode_Check(obj)) { int itemsize = PyUnicode_GET_DATA_SIZE(obj); #ifndef Py_UNICODE_WIDE itemsize <<= 1; #endif /* * If it's already a big enough unicode object, * don't bother type promoting */ if (*out_dtype != NULL && (*out_dtype)->type_num == NPY_UNICODE && (*out_dtype)->elsize >= itemsize) { return 0; } dtype = PyArray_DescrNewFromType(NPY_UNICODE); if (dtype == NULL) { goto fail; } dtype->elsize = itemsize; goto promote_types; } /* PEP 3118 buffer interface */ if (PyObject_CheckBuffer(obj) == 1) { memset(&buffer_view, 0, sizeof(Py_buffer)); if (PyObject_GetBuffer(obj, &buffer_view, PyBUF_FORMAT|PyBUF_STRIDES) == 0 || PyObject_GetBuffer(obj, &buffer_view, PyBUF_FORMAT) == 0) { PyErr_Clear(); dtype = _descriptor_from_pep3118_format(buffer_view.format); PyBuffer_Release(&buffer_view); if (dtype) { goto promote_types; } } else if (PyObject_GetBuffer(obj, &buffer_view, PyBUF_STRIDES) == 0 || PyObject_GetBuffer(obj, &buffer_view, PyBUF_SIMPLE) == 0) { PyErr_Clear(); dtype = PyArray_DescrNewFromType(NPY_VOID); dtype->elsize = buffer_view.itemsize; PyBuffer_Release(&buffer_view); goto promote_types; } else { PyErr_Clear(); } } /* The array interface */ ip = PyArray_GetAttrString_SuppressException(obj, "__array_interface__"); if (ip != NULL) { if (PyDict_Check(ip)) { PyObject *typestr; #if defined(NPY_PY3K) PyObject *tmp = NULL; #endif typestr = PyDict_GetItemString(ip, "typestr"); #if defined(NPY_PY3K) /* Allow unicode type strings */ if (PyUnicode_Check(typestr)) { tmp = PyUnicode_AsASCIIString(typestr); typestr = tmp; } #endif if (typestr && PyBytes_Check(typestr)) { dtype =_array_typedescr_fromstr(PyBytes_AS_STRING(typestr)); #if defined(NPY_PY3K) if (tmp == typestr) { Py_DECREF(tmp); } #endif Py_DECREF(ip); if (dtype == NULL) { goto fail; } goto promote_types; } } Py_DECREF(ip); } /* The array struct interface */ ip = PyArray_GetAttrString_SuppressException(obj, "__array_struct__"); if (ip != NULL) { PyArrayInterface *inter; char buf[40]; if (NpyCapsule_Check(ip)) { inter = (PyArrayInterface *)NpyCapsule_AsVoidPtr(ip); if (inter->two == 2) { PyOS_snprintf(buf, sizeof(buf), "|%c%d", inter->typekind, inter->itemsize); dtype = _array_typedescr_fromstr(buf); Py_DECREF(ip); if (dtype == NULL) { goto fail; } goto promote_types; } } Py_DECREF(ip); } /* The old buffer interface */ #if !defined(NPY_PY3K) if (PyBuffer_Check(obj)) { dtype = PyArray_DescrNewFromType(NPY_VOID); if (dtype == NULL) { goto fail; } dtype->elsize = Py_TYPE(obj)->tp_as_sequence->sq_length(obj); PyErr_Clear(); goto promote_types; } #endif /* The __array__ attribute */ ip = PyArray_GetAttrString_SuppressException(obj, "__array__"); if (ip != NULL) { Py_DECREF(ip); ip = PyObject_CallMethod(obj, "__array__", NULL); if(ip && PyArray_Check(ip)) { dtype = PyArray_DESCR((PyArrayObject *)ip); Py_INCREF(dtype); Py_DECREF(ip); goto promote_types; } Py_XDECREF(ip); if (PyErr_Occurred()) { goto fail; } } /* Not exactly sure what this is about... */ #if !defined(NPY_PY3K) if (PyInstance_Check(obj)) { dtype = _use_default_type(obj); if (dtype == NULL) { goto fail; } else { goto promote_types; } } #endif /* * If we reached the maximum recursion depth without hitting one * of the above cases, the output dtype should be OBJECT */ if (maxdims == 0 || !PySequence_Check(obj)) { if (*out_dtype == NULL || (*out_dtype)->type_num != NPY_OBJECT) { Py_XDECREF(*out_dtype); *out_dtype = PyArray_DescrFromType(NPY_OBJECT); if (*out_dtype == NULL) { return -1; } } return 0; } /* * fails if convertable to list but no len is defined which some libraries * require to get object arrays */ size = PySequence_Size(obj); if (size < 0) { goto fail; } /* Recursive case, first check the sequence contains only one type */ seq = PySequence_Fast(obj, "Could not convert object to sequence"); if (seq == NULL) { goto fail; } objects = PySequence_Fast_ITEMS(seq); common_type = size > 0 ? Py_TYPE(objects[0]) : NULL; for (i = 1; i < size; ++i) { if (Py_TYPE(objects[i]) != common_type) { common_type = NULL; break; } } /* all types are the same and scalar, one recursive call is enough */ if (common_type != NULL && !string_type && (common_type == &PyFloat_Type || /* TODO: we could add longs if we add a range check */ #if !defined(NPY_PY3K) common_type == &PyInt_Type || #endif common_type == &PyBool_Type || common_type == &PyComplex_Type)) { size = 1; } /* Recursive call for each sequence item */ for (i = 0; i < size; ++i) { int res = PyArray_DTypeFromObjectHelper(objects[i], maxdims - 1, out_dtype, string_type); if (res < 0) { Py_DECREF(seq); goto fail; } else if (res > 0) { Py_DECREF(seq); return res; } } Py_DECREF(seq); return 0; promote_types: /* Set 'out_dtype' if it's NULL */ if (*out_dtype == NULL) { if (!string_type && dtype->type_num == NPY_STRING) { Py_DECREF(dtype); return RETRY_WITH_STRING; } if (!string_type && dtype->type_num == NPY_UNICODE) { Py_DECREF(dtype); return RETRY_WITH_UNICODE; } *out_dtype = dtype; return 0; } /* Do type promotion with 'out_dtype' */ else { PyArray_Descr *res_dtype = PyArray_PromoteTypes(dtype, *out_dtype); Py_DECREF(dtype); if (res_dtype == NULL) { return -1; } if (!string_type && res_dtype->type_num == NPY_UNICODE && (*out_dtype)->type_num != NPY_UNICODE) { Py_DECREF(res_dtype); return RETRY_WITH_UNICODE; } if (!string_type && res_dtype->type_num == NPY_STRING && (*out_dtype)->type_num != NPY_STRING) { Py_DECREF(res_dtype); return RETRY_WITH_STRING; } Py_DECREF(*out_dtype); *out_dtype = res_dtype; return 0; } fail: Py_XDECREF(*out_dtype); *out_dtype = NULL; return -1; } #undef RETRY_WITH_STRING #undef RETRY_WITH_UNICODE /* new reference */ NPY_NO_EXPORT PyArray_Descr * _array_typedescr_fromstr(char *c_str) { PyArray_Descr *descr = NULL; PyObject *stringobj = PyString_FromString(c_str); if (stringobj == NULL) { return NULL; } if (PyArray_DescrConverter(stringobj, &descr) != NPY_SUCCEED) { Py_DECREF(stringobj); return NULL; } Py_DECREF(stringobj); return descr; } NPY_NO_EXPORT char * index2ptr(PyArrayObject *mp, npy_intp i) { npy_intp dim0; if (PyArray_NDIM(mp) == 0) { PyErr_SetString(PyExc_IndexError, "0-d arrays can't be indexed"); return NULL; } dim0 = PyArray_DIMS(mp)[0]; if (check_and_adjust_index(&i, dim0, 0, NULL) < 0) return NULL; if (i == 0) { return PyArray_DATA(mp); } return PyArray_BYTES(mp)+i*PyArray_STRIDES(mp)[0]; }
//------------------------------------------------------------------------------------- void Base::addPersistentsDataToStream(uint32 flags, MemoryStream* s) { std::vector<ENTITY_PROPERTY_UID> log; // 再将base中存储属性取出 PyObject* pydict = PyObject_GetAttrString(this, "__dict__"); // 先将celldata中的存储属性取出 ScriptDefModule::PROPERTYDESCRIPTION_MAP& propertyDescrs = pScriptModule_->getPersistentPropertyDescriptions(); ScriptDefModule::PROPERTYDESCRIPTION_MAP::const_iterator iter = propertyDescrs.begin(); if(pScriptModule_->hasCell()) { addPositionAndDirectionToStream(*s); } for(; iter != propertyDescrs.end(); ++iter) { PropertyDescription* propertyDescription = iter->second; std::vector<ENTITY_PROPERTY_UID>::const_iterator finditer = std::find(log.begin(), log.end(), propertyDescription->getUType()); if(finditer != log.end()) continue; const char* attrname = propertyDescription->getName(); if(propertyDescription->isPersistent() && (flags & propertyDescription->getFlags()) > 0) { PyObject *key = PyUnicode_FromString(attrname); if(cellDataDict_ != NULL && PyDict_Contains(cellDataDict_, key) > 0) { PyObject* pyVal = PyDict_GetItemString(cellDataDict_, attrname); if(!propertyDescription->getDataType()->isSameType(pyVal)) { CRITICAL_MSG(fmt::format("{}::addPersistentsDataToStream: {} persistent({}) type(curr_py: {} != {}) is error.\n", this->scriptName(), this->id(), attrname, (pyVal ? pyVal->ob_type->tp_name : "unknown"), propertyDescription->getDataType()->getName())); } else { (*s) << propertyDescription->getUType(); log.push_back(propertyDescription->getUType()); propertyDescription->addPersistentToStream(s, pyVal); DEBUG_PERSISTENT_PROPERTY("addCellPersistentsDataToStream", attrname); } } else if(PyDict_Contains(pydict, key) > 0) { PyObject* pyVal = PyDict_GetItem(pydict, key); if(!propertyDescription->getDataType()->isSameType(pyVal)) { CRITICAL_MSG(fmt::format("{}::addPersistentsDataToStream: {} persistent({}) type(curr_py: {} != {}) is error.\n", this->scriptName(), this->id(), attrname, (pyVal ? pyVal->ob_type->tp_name : "unknown"), propertyDescription->getDataType()->getName())); } else { (*s) << propertyDescription->getUType(); log.push_back(propertyDescription->getUType()); propertyDescription->addPersistentToStream(s, pyVal); DEBUG_PERSISTENT_PROPERTY("addBasePersistentsDataToStream", attrname); } } else { WARNING_MSG(fmt::format("{}::addPersistentsDataToStream: {} not found Persistent({}), use default values!\n", this->scriptName(), this->id(), attrname)); (*s) << propertyDescription->getUType(); log.push_back(propertyDescription->getUType()); propertyDescription->addPersistentToStream(s, NULL); } Py_DECREF(key); } SCRIPT_ERROR_CHECK(); } Py_XDECREF(pydict); SCRIPT_ERROR_CHECK(); }
static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *UNUSED(args)) { Main *bmain = CTX_data_main(BPy_GetContext()); Main *mainl = NULL; int err = 0; BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, true); /* here appending/linking starts */ mainl = BLO_library_link_begin(bmain, &(self->blo_handle), self->relpath); { int idcode_step = 0, idcode; while ((idcode = BKE_idcode_iter_step(&idcode_step))) { if (BKE_idcode_is_linkable(idcode)) { const char *name_plural = BKE_idcode_to_name_plural(idcode); PyObject *ls = PyDict_GetItemString(self->dict, name_plural); // printf("lib: %s\n", name_plural); if (ls && PyList_Check(ls)) { /* loop */ Py_ssize_t size = PyList_GET_SIZE(ls); Py_ssize_t i; for (i = 0; i < size; i++) { PyObject *item_src = PyList_GET_ITEM(ls, i); PyObject *item_dst; /* must be set below */ const char *item_idname = _PyUnicode_AsString(item_src); // printf(" %s\n", item_idname); if (item_idname) { ID *id = BLO_library_link_named_part(mainl, &(self->blo_handle), idcode, item_idname); if (id) { #ifdef USE_RNA_DATABLOCKS /* swap name for pointer to the id */ item_dst = PyCapsule_New((void *)id, NULL, NULL); #else /* leave as is */ continue; #endif } else { bpy_lib_exit_warn_idname(self, name_plural, item_idname); /* just warn for now */ /* err = -1; */ item_dst = Py_INCREF_RET(Py_None); } /* ID or None */ } else { /* XXX, could complain about this */ bpy_lib_exit_warn_type(self, item_src); PyErr_Clear(); item_dst = Py_INCREF_RET(Py_None); } /* item_dst must be new or already incref'd */ Py_DECREF(item_src); PyList_SET_ITEM(ls, i, item_dst); } } } } } if (err == -1) { /* exception raised above, XXX, this leaks some memory */ BLO_blendhandle_close(self->blo_handle); self->blo_handle = NULL; BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, false); return NULL; } else { Library *lib = mainl->curlib; /* newly added lib, assign before append end */ BLO_library_link_end(mainl, &(self->blo_handle), self->flag, NULL, NULL); BLO_blendhandle_close(self->blo_handle); self->blo_handle = NULL; GHash *old_to_new_ids = BLI_ghash_ptr_new(__func__); /* copied from wm_operator.c */ { /* mark all library linked objects to be updated */ BKE_main_lib_objects_recalc_all(bmain); /* append, rather than linking */ if ((self->flag & FILE_LINK) == 0) { BKE_library_make_local(bmain, lib, old_to_new_ids, true, false); } } BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, false); /* finally swap the capsules for real bpy objects * important since BLO_library_append_end initializes NodeTree types used by srna->refine */ #ifdef USE_RNA_DATABLOCKS { int idcode_step = 0, idcode; while ((idcode = BKE_idcode_iter_step(&idcode_step))) { if (BKE_idcode_is_linkable(idcode)) { const char *name_plural = BKE_idcode_to_name_plural(idcode); PyObject *ls = PyDict_GetItemString(self->dict, name_plural); if (ls && PyList_Check(ls)) { Py_ssize_t size = PyList_GET_SIZE(ls); Py_ssize_t i; PyObject *item; for (i = 0; i < size; i++) { item = PyList_GET_ITEM(ls, i); if (PyCapsule_CheckExact(item)) { PointerRNA id_ptr; ID *id; id = PyCapsule_GetPointer(item, NULL); id = BLI_ghash_lookup_default(old_to_new_ids, id, id); Py_DECREF(item); RNA_id_pointer_create(id, &id_ptr); item = pyrna_struct_CreatePyObject(&id_ptr); PyList_SET_ITEM(ls, i, item); } } } } } } #endif /* USE_RNA_DATABLOCKS */ BLI_ghash_free(old_to_new_ids, NULL, NULL); Py_RETURN_NONE; } }
int main() { // initial the interpreter char xv[100] = "abcdefg"; PyObject * tstr = PyString_FromString(xv); Py_Initialize(); if (!Py_IsInitialized()) { return -1; } // import the sys module PyRun_SimpleString("import sys"); // add the current path to sys.path PyRun_SimpleString("sys.path.append('./')"); PyObject * pModule = NULL; PyObject * pFunc = NULL; PyObject * pName = NULL; PyObject * pModule1 = NULL; PyObject * pFunc1 = NULL; PyObject * pDict = NULL; PyObject * pArgs = NULL; PyObject * pFunc2 = NULL; pName = PyString_FromString("pytest"); pModule1 = PyImport_Import(pName); if (!pModule1) { printf("can't find pytest.py"); getchar(); return -1; } pDict = PyModule_GetDict(pModule1); if (!pDict) { return -1; } pFunc1 = PyDict_GetItemString(pDict, "add"); if (!pFunc1 || !PyCallable_Check(pFunc1)) { printf("can't find function [add]"); getchar(); return -1; } pFunc2 = PyDict_GetItemString(pDict, "echo"); if (!pFunc2 || !PyCallable_Check(pFunc2)) { printf("can't find function [echo]"); getchar(); return -1; } pArgs = PyTuple_New(1); printf("array size = %d \n", PyTuple_Size(pArgs)); PyTuple_SetItem(pArgs, 0, tstr); PyObject_CallObject(pFunc2, pArgs); // create a Tuple(2) pArgs = PyTuple_New(2); // create long int and make Tumple points it PyTuple_SetItem(pArgs, 0, Py_BuildValue("l", 3)); PyTuple_SetItem(pArgs, 1, Py_BuildValue("l", 4)); // call a function with parameters PyObject_CallObject(pFunc1, pArgs); // reuse the pFunc1 parameter pFunc1 = PyDict_GetItemString(pDict, "foo"); if(!pFunc1 || !PyCallable_Check(pFunc1)) { printf("can't find function [foo]"); getchar(); return -1; } pArgs = PyTuple_New(1); PyTuple_SetItem(pArgs, 0, Py_BuildValue("l", 2)); PyObject_CallObject(pFunc1, pArgs); // a another way to import a module pModule = PyImport_ImportModule("helloworld"); // find a function in a module pFunc = PyObject_GetAttrString(pModule, "hello"); // call the function PyEval_CallObject(pFunc, NULL); // free the memory Py_DECREF(pName); Py_DECREF(pArgs); Py_DECREF(pModule1); Py_DECREF(pModule); // close python, release the resource Py_Finalize(); return 0; }
int NRELPythonApplicInterface::derived_map_ac(const Dakota::String& ac_name) { printf ("entered python:derived_map_ac\n"); // probably need to convert all of the following with SWIG or Boost!! // (there is minimal error checking for now) // need to cleanup ref counts on Python objects int fail_code = 0; // probably want to load the modules and functions at construction time, incl. // validation and store the objects for later, but need to resolve use of // analysisDriverIndex // for now we presume a single analysis component containing module:function const std::string& an_comp = analysisComponents[analysisDriverIndex][0]; size_t pos = an_comp.find(":"); std::string module_name = an_comp.substr(0,pos); std::string function_name = an_comp.substr(pos+1); printf ("importing the module %s\n", module_name.c_str()); // import the module and function and test for callable PyObject *pModule = PyImport_Import(PyString_FromString(module_name.c_str())); if (pModule == NULL) { Cerr << "Error (Direct:Python): Failure importing module " << module_name << ".\n Consider setting PYTHONPATH." << std::endl; abort_handler(-1); } printf ("imported the module\n"); // Microsoft compiler chokes on this: // char fn[function_name.size()+1]; char *fn = new char[function_name.size()+1]; strcpy(fn, function_name.c_str()); PyObject *pFunc = PyObject_GetAttrString(pModule, fn); if (!pFunc || !PyCallable_Check(pFunc)) { Cerr << "Error (Direct:Python): Function " << function_name << "not found " << "or not callable" << std::endl; abort_handler(-1); } delete fn; // must use empty tuple here to pass to function taking only kwargs PyObject *pArgs = PyTuple_New(0); PyObject *pDict = PyDict_New(); // convert DAKOTA data types to Python objects (lists and/or numpy arrays) PyObject *cv, *cv_labels, *div, *div_labels, *drv, *drv_labels, *av, *av_labels, *asv, *dvv; python_convert(xC, &cv); python_convert(xCLabels, &cv_labels); python_convert_int(xDI, xDI.length(), &div); python_convert(xDILabels, &div_labels); python_convert(xDR, &drv); python_convert(xDRLabels, &drv_labels); python_convert(xC, xDI, xDR, &av); python_convert(xCLabels, xDILabels, xDRLabels, &av_labels); python_convert_int(directFnASV, directFnASV.size(), &asv); python_convert_int(directFnDVV, directFnASV.size(), &dvv); // TO DO: analysis components // assemble everything into a dictionary to pass to user function // this should eat references to the objects declared above PyDict_SetItem(pDict, PyString_FromString("variables"), PyInt_FromLong((long) numVars)); PyDict_SetItem(pDict, PyString_FromString("functions"), PyInt_FromLong((long) numFns)); PyDict_SetItem(pDict, PyString_FromString("cv"), cv); PyDict_SetItem(pDict, PyString_FromString("cv_labels"), cv_labels); PyDict_SetItem(pDict, PyString_FromString("div"), div); PyDict_SetItem(pDict, PyString_FromString("div_labels"), div_labels); PyDict_SetItem(pDict, PyString_FromString("drv"), drv); PyDict_SetItem(pDict, PyString_FromString("drv_labels"), drv_labels); PyDict_SetItem(pDict, PyString_FromString("av"), av); PyDict_SetItem(pDict, PyString_FromString("av_labels"), av_labels); PyDict_SetItem(pDict, PyString_FromString("asv"), asv); PyDict_SetItem(pDict, PyString_FromString("dvv"), dvv); // Does not appear to exist in Windows version of Dakota, and I don't recall using it: // PyDict_SetItem(pDict, PyString_FromString("fnEvalId"), // PyInt_FromLong((long) fnEvalId)); /////// ///PyDict_SetItem(pDict, PyString_FromString("data"), gData); printf ("I have data at : %lx, NOW WHAT do I do!?\n", pUserData); /////// bp::object * tmp2 = NULL; if (pUserData != NULL) { printf ("Setting raw PyObject in dict\n"); tmp2 = (bp::object*)pUserData; PyDict_SetItem(pDict, PyString_FromString("user_data"), tmp2->ptr()); } else printf("no data\n"); // perform analysis if (outputLevel > NORMAL_OUTPUT) Cout << "Info (Direct:Python): Calling function " << function_name << " in module " << module_name << "." << std::endl; PyObject *retVal = PyObject_Call(pFunc, pArgs, pDict); Py_DECREF(pDict); Py_DECREF(pArgs); Py_DECREF(pFunc); Py_DECREF(pModule); if (!retVal) { // TODO: better error reporting from Python Cerr << "Error (Direct:Python): Unknown error evaluating python " << "function." << std::endl; abort_handler(-1); } bool fn_flag = false; for (int i=0; i<numFns; ++i) if (directFnASV[i] & 1) { fn_flag = true; break; } // process return type as dictionary, else assume list of functions only if (PyDict_Check(retVal)) { // or the user may return a dictionary containing entires fns, fnGrads, // fnHessians, fnLabels, failure (int) // fnGrads, e.g. is a list of lists of doubles // this is where Boost or SWIG could really help // making a lot of assumptions on types being returned PyObject *obj; if (fn_flag) { if ( !(obj = PyDict_GetItemString(retVal, "fns")) ) { Cerr << "Python dictionary must contain list 'fns'" << std::endl; Py_DECREF(retVal); abort_handler(-1); } if (!python_convert(obj, fnVals, numFns)) { Py_DECREF(retVal); abort_handler(-1); } } if (gradFlag) { if ( !(obj = PyDict_GetItemString(retVal, "fnGrads")) ) { Cerr << "Python dictionary must contain list 'fnGrads'" << std::endl; Py_DECREF(retVal); abort_handler(-1); } if (!python_convert(obj, fnGrads)) { Py_DECREF(retVal); abort_handler(-1); } } if (hessFlag) { if ( !(obj = PyDict_GetItemString(retVal, "fnHessians")) ) { Cerr << "Python dictionary must contain list 'fnHessians'" << std::endl; Py_DECREF(retVal); abort_handler(-1); } if (!python_convert(obj, fnHessians)){ Py_DECREF(retVal); abort_handler(-1); } } // optional returns if (obj = PyDict_GetItemString(retVal, "failure")) fail_code = PyInt_AsLong(obj); if (obj = PyDict_GetItemString(retVal, "fnLabels")) { if (!PyList_Check(obj) || PyList_Size(obj) != numFns) { Cerr << "'fnLabels' must be list of length numFns." << std::endl; Py_DECREF(retVal); abort_handler(-1); } for (int i=0; i<numFns; ++i) fnLabels[i] = PyString_AsString(PyList_GetItem(obj, i)); } } else { // asssume list/numpy array containing only functions if (fn_flag) python_convert(retVal, fnVals, numFns); } Py_DECREF(retVal); return(fail_code); }
/** * evalDistribution function evaluate a model function with input vector * @param args: input q as vector or [qx, qy] where qx, qy are vectors * */ static PyObject * evalDistribution(CPoly_GaussCoil *self, PyObject *args){ PyObject *qx, *qy; PyArrayObject * pars; int npars ,mpars; // Get parameters // Reader parameter dictionary self->model->poly_m = PyFloat_AsDouble( PyDict_GetItemString(self->params, "poly_m") ); self->model->rg = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rg") ); self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); // Read in dispersion parameters PyObject* disp_dict; DispersionVisitor* visitor = new DispersionVisitor(); disp_dict = PyDict_GetItemString(self->dispersion, "rg"); self->model->rg.dispersion->accept_as_destination(visitor, self->model->rg.dispersion, disp_dict); // Get input and determine whether we have to supply a 1D or 2D return value. if ( !PyArg_ParseTuple(args,"O",&pars) ) { PyErr_SetString(CPoly_GaussCoilError, "CPoly_GaussCoil.evalDistribution expects a q value."); return NULL; } // Check params if(PyArray_Check(pars)==1) { // Length of list should 1 or 2 npars = pars->nd; if(npars==1) { // input is a numpy array if (PyArray_Check(pars)) { return evaluateOneDim(self->model, (PyArrayObject*)pars); } }else{ PyErr_SetString(CPoly_GaussCoilError, "CPoly_GaussCoil.evalDistribution expect numpy array of one dimension."); return NULL; } }else if( PyList_Check(pars)==1) { // Length of list should be 2 for I(qx,qy) mpars = PyList_GET_SIZE(pars); if(mpars!=2) { PyErr_SetString(CPoly_GaussCoilError, "CPoly_GaussCoil.evalDistribution expects a list of dimension 2."); return NULL; } qx = PyList_GET_ITEM(pars,0); qy = PyList_GET_ITEM(pars,1); if (PyArray_Check(qx) && PyArray_Check(qy)) { return evaluateTwoDimXY(self->model, (PyArrayObject*)qx, (PyArrayObject*)qy); }else{ PyErr_SetString(CPoly_GaussCoilError, "CPoly_GaussCoil.evalDistribution expect 2 numpy arrays in list."); return NULL; } } PyErr_SetString(CPoly_GaussCoilError, "CPoly_GaussCoil.evalDistribution couln't be run."); return NULL; }
static PyObject * warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) { static char *kwd_list[] = {"message", "category", "filename", "lineno", "module", "registry", "module_globals", 0}; PyObject *message; PyObject *category; PyObject *filename; int lineno; PyObject *module = NULL; PyObject *registry = NULL; PyObject *module_globals = NULL; if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOO:warn_explicit", kwd_list, &message, &category, &filename, &lineno, &module, ®istry, &module_globals)) return NULL; if (module_globals) { _Py_IDENTIFIER(get_source); _Py_IDENTIFIER(splitlines); PyObject *tmp; PyObject *loader; PyObject *module_name; PyObject *source; PyObject *source_list; PyObject *source_line; PyObject *returned; if ((tmp = _PyUnicode_FromId(&PyId_get_source)) == NULL) return NULL; if ((tmp = _PyUnicode_FromId(&PyId_splitlines)) == NULL) return NULL; /* Check/get the requisite pieces needed for the loader. */ loader = PyDict_GetItemString(module_globals, "__loader__"); module_name = PyDict_GetItemString(module_globals, "__name__"); if (loader == NULL || module_name == NULL) goto standard_call; /* Make sure the loader implements the optional get_source() method. */ if (!_PyObject_HasAttrId(loader, &PyId_get_source)) goto standard_call; /* Call get_source() to get the source code. */ source = PyObject_CallMethodObjArgs(loader, PyId_get_source.object, module_name, NULL); if (!source) return NULL; else if (source == Py_None) { Py_DECREF(Py_None); goto standard_call; } /* Split the source into lines. */ source_list = PyObject_CallMethodObjArgs(source, PyId_splitlines.object, NULL); Py_DECREF(source); if (!source_list) return NULL; /* Get the source line. */ source_line = PyList_GetItem(source_list, lineno-1); if (!source_line) { Py_DECREF(source_list); return NULL; } /* Handle the warning. */ returned = warn_explicit(category, message, filename, lineno, module, registry, source_line); Py_DECREF(source_list); return returned; } standard_call: return warn_explicit(category, message, filename, lineno, module, registry, NULL); }
int local_scan(int fd, uschar **return_text) { PyObject *user_dict; PyObject *user_func; PyObject *result; PyObject *header_tuple; PyObject *original_recipients; PyObject *working_recipients; if (!expy_enabled) return LOCAL_SCAN_ACCEPT; if (!Py_IsInitialized()) /* local_scan() may have already been run */ { Py_Initialize(); ExPy_Header_Line.ob_type = &PyType_Type; } if (!expy_exim_dict) { PyObject *module = Py_InitModule(expy_exim_module, expy_exim_methods); /* Borrowed reference */ Py_INCREF(module); /* convert to New reference */ expy_exim_dict = PyModule_GetDict(module); /* Borrowed reference */ Py_INCREF(expy_exim_dict); /* convert to New reference */ } if (!expy_user_module) { if (expy_path_add) { PyObject *sys_module; PyObject *sys_dict; PyObject *sys_path; PyObject *add_value; sys_module = PyImport_ImportModule("sys"); /* New Reference */ if (!sys_module) { PyErr_Clear(); *return_text = "Internal error, can't import Python sys module"; log_write(0, LOG_REJECT, "Couldn't import Python 'sys' module"); /* FIXME: write out an exception traceback if possible to Exim log */ return PYTHON_FAILURE_RETURN; } sys_dict = PyModule_GetDict(sys_module); /* Borrowed Reference, never fails */ sys_path = PyMapping_GetItemString(sys_dict, "path"); /* New reference */ if (!sys_path || (!PyList_Check(sys_path))) { PyErr_Clear(); /* in case sys_path was NULL, harmless otherwise */ *return_text = "Internal error, sys.path doesn't exist or isn't a list"; log_write(0, LOG_REJECT, "expy: Python sys.path doesn't exist or isn't a list"); /* FIXME: write out an exception traceback if possible to Exim log */ return PYTHON_FAILURE_RETURN; } add_value = PyString_FromString(expy_path_add); /* New reference */ if (!add_value) { PyErr_Clear(); log_write(0, LOG_PANIC, "expy: Failed to create Python string from [%s]", expy_path_add); return PYTHON_FAILURE_RETURN; } if (PyList_Append(sys_path, add_value)) { PyErr_Clear(); log_write(0, LOG_PANIC, "expy: Failed to append [%s] to Python sys.path", expy_path_add); } Py_DECREF(add_value); Py_DECREF(sys_path); Py_DECREF(sys_module); } expy_user_module = PyImport_ImportModule(expy_scan_module); /* New Reference */ if (!expy_user_module) { PyErr_Clear(); *return_text = "Internal error, can't import Python local_scan module"; log_write(0, LOG_REJECT, "Couldn't import Python '%s' module", expy_scan_module); return PYTHON_FAILURE_RETURN; } } user_dict = PyModule_GetDict(expy_user_module); /* Borrowed Reference, never fails */ user_func = PyMapping_GetItemString(user_dict, expy_scan_function); /* New reference */ if (!user_func) { PyErr_Clear(); *return_text = "Internal error, module doesn't have local_scan function"; log_write(0, LOG_REJECT, "Python %s module doesn't have a %s function", expy_scan_module, expy_scan_function); return PYTHON_FAILURE_RETURN; } /* so far so good, prepare to run function */ /* Copy exim variables */ expy_dict_int("debug_selector", debug_selector); expy_dict_int("host_checking", host_checking); expy_dict_string("interface_address", interface_address); expy_dict_int("interface_port", interface_port); expy_dict_string("message_id", message_id); expy_dict_string("received_protocol", received_protocol); expy_dict_string("sender_address", sender_address); expy_dict_string("sender_host_address", sender_host_address); expy_dict_string("sender_host_authenticated", sender_host_authenticated); expy_dict_string("sender_host_name", sender_host_name); expy_dict_int("sender_host_port", sender_host_port); expy_dict_int("fd", fd); /* copy some constants */ expy_dict_int("LOG_MAIN", LOG_MAIN); expy_dict_int("LOG_PANIC", LOG_PANIC); expy_dict_int("LOG_REJECT", LOG_REJECT); expy_dict_int("LOCAL_SCAN_ACCEPT", LOCAL_SCAN_ACCEPT); expy_dict_int("LOCAL_SCAN_ACCEPT_FREEZE", LOCAL_SCAN_ACCEPT_FREEZE); expy_dict_int("LOCAL_SCAN_ACCEPT_QUEUE", LOCAL_SCAN_ACCEPT_QUEUE); expy_dict_int("LOCAL_SCAN_REJECT", LOCAL_SCAN_REJECT); expy_dict_int("LOCAL_SCAN_REJECT_NOLOGHDR", LOCAL_SCAN_REJECT_NOLOGHDR); expy_dict_int("LOCAL_SCAN_TEMPREJECT", LOCAL_SCAN_TEMPREJECT); expy_dict_int("LOCAL_SCAN_TEMPREJECT_NOLOGHDR", LOCAL_SCAN_TEMPREJECT_NOLOGHDR); expy_dict_int("MESSAGE_ID_LENGTH", MESSAGE_ID_LENGTH); expy_dict_int("SPOOL_DATA_START_OFFSET", SPOOL_DATA_START_OFFSET); expy_dict_int("D_v", D_v); expy_dict_int("D_local_scan", D_local_scan); /* set the headers */ header_tuple = get_headers(); PyDict_SetItemString(expy_exim_dict, "headers", header_tuple); /* * make list of recipients, give module a copy to work with in * List format, but keep original tuple to compare against later */ original_recipients = get_recipients(); /* New reference */ working_recipients = PySequence_List(original_recipients); /* New reference */ PyDict_SetItemString(expy_exim_dict, "recipients", working_recipients); Py_DECREF(working_recipients); /* Try calling our function */ result = PyObject_CallFunction(user_func, NULL); /* New reference */ Py_DECREF(user_func); /* Don't need ref to function anymore */ /* Check for Python exception */ if (!result) { PyErr_Clear(); *return_text = "Internal error, local_scan function failed"; Py_DECREF(original_recipients); clear_headers(header_tuple); Py_DECREF(header_tuple); return PYTHON_FAILURE_RETURN; // FIXME: should write exception to exim log somehow } /* User code may have replaced recipient list, so re-get ref */ working_recipients = PyDict_GetItemString(expy_exim_dict, "recipients"); /* Borrowed reference */ Py_XINCREF(working_recipients); /* convert to New reference */ /* * reconcile original recipient list with what's present after * Python code is done */ if ((!working_recipients) || (!PySequence_Check(working_recipients)) || (PySequence_Size(working_recipients) == 0)) /* Python code either deleted exim.recipients alltogether, or replaced it with a non-list, or emptied out the list */ recipients_count = 0; else { int i; /* remove original recipients not on the working list, reverse order important! */ for (i = recipients_count - 1; i >= 0; i--) { PyObject *addr = PyTuple_GET_ITEM(original_recipients, i); /* borrowed ref */ if (!PySequence_Contains(working_recipients, addr)) expy_remove_recipient(i); } /* add new recipients not in the original list */ for (i = PySequence_Size(working_recipients) - 1; i >= 0; i--) { PyObject *addr = PySequence_GetItem(working_recipients, i); if (!PySequence_Contains(original_recipients, addr)) receive_add_recipient(PyString_AsString(addr), -1); Py_DECREF(addr); } } Py_XDECREF(working_recipients); /* No longer needed */ Py_DECREF(original_recipients); /* No longer needed */ clear_headers(header_tuple); Py_DECREF(header_tuple); /* No longer needed */ /* Deal with the return value, first see if python returned a non-empty sequence */ if (PySequence_Check(result) && (PySequence_Size(result) > 0)) { /* save first item */ PyObject *rc = PySequence_GetItem(result, 0); /* if more than one item, convert 2nd item to string and use as return text */ if (PySequence_Size(result) > 1) { PyObject *str; PyObject *obj = PySequence_GetItem(result, 1); /* New reference */ str = PyObject_Str(obj); /* New reference */ *return_text = string_copy(PyString_AsString(str)); Py_DECREF(obj); Py_DECREF(str); } /* drop the sequence, and focus on the first item we saved */ Py_DECREF(result); result = rc; } /* If we have an integer, return that to Exim */ if (PyInt_Check(result)) { int rc = PyInt_AsLong(result); Py_DECREF(result); return rc; } /* didn't return anything usable */ Py_DECREF(result); *return_text = "Internal error, bad return code"; log_write(0, LOG_REJECT, "Python %s.%s function didn't return integer", expy_scan_module, expy_scan_function); return PYTHON_FAILURE_RETURN; }
/* Returns 0 on error (no new refs), 1 on success */ static int setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, PyObject **module, PyObject **registry) { PyObject *globals; /* Setup globals and lineno. */ PyFrameObject *f = PyThreadState_GET()->frame; while (--stack_level > 0 && f != NULL) f = f->f_back; if (f == NULL) { globals = PyThreadState_Get()->interp->sysdict; *lineno = 1; } else { globals = f->f_globals; *lineno = PyFrame_GetLineNumber(f); } *module = NULL; /* Setup registry. */ assert(globals != NULL); assert(PyDict_Check(globals)); *registry = PyDict_GetItemString(globals, "__warningregistry__"); if (*registry == NULL) { int rc; *registry = PyDict_New(); if (*registry == NULL) return 0; rc = PyDict_SetItemString(globals, "__warningregistry__", *registry); if (rc < 0) goto handle_error; } else Py_INCREF(*registry); /* Setup module. */ *module = PyDict_GetItemString(globals, "__name__"); if (*module == NULL) { *module = PyUnicode_FromString("<string>"); if (*module == NULL) goto handle_error; } else Py_INCREF(*module); /* Setup filename. */ *filename = PyDict_GetItemString(globals, "__file__"); if (*filename != NULL && PyUnicode_Check(*filename)) { Py_ssize_t len; int kind; void *data; if (PyUnicode_READY(*filename)) goto handle_error; len = PyUnicode_GetLength(*filename); kind = PyUnicode_KIND(*filename); data = PyUnicode_DATA(*filename); #define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0) /* if filename.lower().endswith((".pyc", ".pyo")): */ if (len >= 4 && PyUnicode_READ(kind, data, len-4) == '.' && ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' && ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' && (ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c' || ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'o')) { *filename = PyUnicode_Substring(*filename, 0, PyUnicode_GET_LENGTH(*filename)-1); if (*filename == NULL) goto handle_error; } else Py_INCREF(*filename); } else { *filename = NULL; if (*module != Py_None && PyUnicode_CompareWithASCIIString(*module, "__main__") == 0) { PyObject *argv = _PySys_GetObjectId(&PyId_argv); /* PyList_Check() is needed because sys.argv is set to None during Python finalization */ if (argv != NULL && PyList_Check(argv) && PyList_Size(argv) > 0) { int is_true; *filename = PyList_GetItem(argv, 0); Py_INCREF(*filename); /* If sys.argv[0] is false, then use '__main__'. */ is_true = PyObject_IsTrue(*filename); if (is_true < 0) { Py_DECREF(*filename); goto handle_error; } else if (!is_true) { Py_DECREF(*filename); *filename = PyUnicode_FromString("__main__"); if (*filename == NULL) goto handle_error; } } else { /* embedded interpreters don't have sys.argv, see bug #839151 */ *filename = PyUnicode_FromString("__main__"); if (*filename == NULL) goto handle_error; } } if (*filename == NULL) { *filename = *module; Py_INCREF(*filename); } } return 1; handle_error: /* filename not XDECREF'ed here as there is no way to jump here with a dangling reference. */ Py_XDECREF(*registry); Py_XDECREF(*module); return 0; }
/* * Recursively examines the object to determine an appropriate dtype * to use for converting to an ndarray. * * 'obj' is the object to be converted to an ndarray. * * 'maxdims' is the maximum recursion depth. * * 'out_contains_na' gets set to 1 if an np.NA object is encountered. * The NA does not affect the dtype produced, so if this is set to 1 * and the result is for an array without NA support, the dtype should * be switched to NPY_OBJECT. When adding multi-NA support, this should * also signal whether just regular NAs or NAs with payloads were seen. * * 'out_dtype' should be either NULL or a minimal starting dtype when * the function is called. It is updated with the results of type * promotion. This dtype does not get updated when processing NA objects. * This is reset to NULL on failure. * * Returns 0 on success, -1 on failure. */ NPY_NO_EXPORT int PyArray_DTypeFromObject(PyObject *obj, int maxdims, int *out_contains_na, PyArray_Descr **out_dtype) { int i, size; PyArray_Descr *dtype = NULL; PyObject *ip; #if PY_VERSION_HEX >= 0x02060000 Py_buffer buffer_view; #endif /* Check if it's an ndarray */ if (PyArray_Check(obj)) { /* Check for any NAs in the array */ int containsna = PyArray_ContainsNA((PyArrayObject *)obj, NULL, NULL); if (containsna == -1) { goto fail; } else if (containsna) { *out_contains_na = 1; } dtype = PyArray_DESCR((PyArrayObject *)obj); Py_INCREF(dtype); goto promote_types; } /* Check if it's a NumPy scalar */ if (PyArray_IsScalar(obj, Generic)) { dtype = PyArray_DescrFromScalar(obj); if (dtype == NULL) { goto fail; } goto promote_types; } /* Check if it's a Python scalar */ dtype = _array_find_python_scalar_type(obj); if (dtype != NULL) { goto promote_types; } /* Check if it's an NA */ if (NpyNA_Check(obj)) { *out_contains_na = 1; return 0; } /* Check if it's an ASCII string */ if (PyBytes_Check(obj)) { int itemsize = PyString_GET_SIZE(obj); /* If it's already a big enough string, don't bother type promoting */ if (*out_dtype != NULL && (*out_dtype)->type_num == NPY_STRING && (*out_dtype)->elsize >= itemsize) { return 0; } dtype = PyArray_DescrNewFromType(NPY_STRING); if (dtype == NULL) { goto fail; } dtype->elsize = itemsize; goto promote_types; } /* Check if it's a Unicode string */ if (PyUnicode_Check(obj)) { int itemsize = PyUnicode_GET_DATA_SIZE(obj); #ifndef Py_UNICODE_WIDE itemsize <<= 1; #endif /* * If it's already a big enough unicode object, * don't bother type promoting */ if (*out_dtype != NULL && (*out_dtype)->type_num == NPY_UNICODE && (*out_dtype)->elsize >= itemsize) { return 0; } dtype = PyArray_DescrNewFromType(NPY_UNICODE); if (dtype == NULL) { goto fail; } dtype->elsize = itemsize; goto promote_types; } #if PY_VERSION_HEX >= 0x02060000 /* PEP 3118 buffer interface */ memset(&buffer_view, 0, sizeof(Py_buffer)); if (PyObject_GetBuffer(obj, &buffer_view, PyBUF_FORMAT|PyBUF_STRIDES) == 0 || PyObject_GetBuffer(obj, &buffer_view, PyBUF_FORMAT) == 0) { PyErr_Clear(); dtype = _descriptor_from_pep3118_format(buffer_view.format); PyBuffer_Release(&buffer_view); if (dtype) { goto promote_types; } } else if (PyObject_GetBuffer(obj, &buffer_view, PyBUF_STRIDES) == 0 || PyObject_GetBuffer(obj, &buffer_view, PyBUF_SIMPLE) == 0) { PyErr_Clear(); dtype = PyArray_DescrNewFromType(NPY_VOID); dtype->elsize = buffer_view.itemsize; PyBuffer_Release(&buffer_view); goto promote_types; } else { PyErr_Clear(); } #endif /* The array interface */ ip = PyObject_GetAttrString(obj, "__array_interface__"); if (ip != NULL) { if (PyDict_Check(ip)) { PyObject *typestr; typestr = PyDict_GetItemString(ip, "typestr"); if (typestr && PyString_Check(typestr)) { dtype =_array_typedescr_fromstr(PyString_AS_STRING(typestr)); Py_DECREF(ip); if (dtype == NULL) { goto fail; } goto promote_types; } } Py_DECREF(ip); } else { PyErr_Clear(); } /* The array struct interface */ ip = PyObject_GetAttrString(obj, "__array_struct__"); if (ip != NULL) { PyArrayInterface *inter; char buf[40]; if (NpyCapsule_Check(ip)) { inter = (PyArrayInterface *)NpyCapsule_AsVoidPtr(ip); if (inter->two == 2) { PyOS_snprintf(buf, sizeof(buf), "|%c%d", inter->typekind, inter->itemsize); dtype = _array_typedescr_fromstr(buf); Py_DECREF(ip); if (dtype == NULL) { goto fail; } goto promote_types; } } Py_DECREF(ip); } else { PyErr_Clear(); } /* The old buffer interface */ #if !defined(NPY_PY3K) if (PyBuffer_Check(obj)) { dtype = PyArray_DescrNewFromType(NPY_VOID); if (dtype == NULL) { goto fail; } dtype->elsize = Py_TYPE(obj)->tp_as_sequence->sq_length(obj); PyErr_Clear(); goto promote_types; } #endif /* The __array__ attribute */ if (PyObject_HasAttrString(obj, "__array__")) { ip = PyObject_CallMethod(obj, "__array__", NULL); if(ip && PyArray_Check(ip)) { dtype = PyArray_DESCR((PyArrayObject *)ip); Py_INCREF(dtype); Py_DECREF(ip); goto promote_types; } Py_XDECREF(ip); if (PyErr_Occurred()) { goto fail; } } /* Not exactly sure what this is about... */ #if !defined(NPY_PY3K) if (PyInstance_Check(obj)) { dtype = _use_default_type(obj); if (dtype == NULL) { goto fail; } else { goto promote_types; } } #endif /* * If we reached the maximum recursion depth without hitting one * of the above cases, the output dtype should be OBJECT */ if (maxdims == 0 || !PySequence_Check(obj)) { if (*out_dtype == NULL || (*out_dtype)->type_num != NPY_OBJECT) { Py_XDECREF(*out_dtype); *out_dtype = PyArray_DescrFromType(NPY_OBJECT); if (*out_dtype == NULL) { return -1; } } return 0; } /* Recursive case */ size = PySequence_Size(obj); if (size < 0) { goto fail; } /* Recursive call for each sequence item */ for (i = 0; i < size; ++i) { ip = PySequence_GetItem(obj, i); if (ip==NULL) { goto fail; } if (PyArray_DTypeFromObject(ip, maxdims - 1, out_contains_na, out_dtype) < 0) { Py_DECREF(ip); goto fail; } Py_DECREF(ip); } return 0; promote_types: /* Set 'out_dtype' if it's NULL */ if (*out_dtype == NULL) { *out_dtype = dtype; return 0; } /* Do type promotion with 'out_dtype' */ else { PyArray_Descr *res_dtype = PyArray_PromoteTypes(dtype, *out_dtype); Py_DECREF(dtype); if (res_dtype == NULL) { return -1; } Py_DECREF(*out_dtype); *out_dtype = res_dtype; return 0; } fail: Py_XDECREF(*out_dtype); *out_dtype = NULL; return -1; }
/** * Function to call to evaluate model in cartesian coordinates * @param args: input q or [qx, qy]] * @return: function value */ static PyObject * runXY(CBCCrystalModel *self, PyObject *args) { double qx_value, qy_value; PyObject* pars; int npars; // Get parameters // Reader parameter dictionary self->model->phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "phi") ); self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); self->model->dnn = PyFloat_AsDouble( PyDict_GetItemString(self->params, "dnn") ); self->model->sldSph = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSph") ); self->model->d_factor = PyFloat_AsDouble( PyDict_GetItemString(self->params, "d_factor") ); self->model->psi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "psi") ); self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); self->model->theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "theta") ); self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") ); // Read in dispersion parameters PyObject* disp_dict; DispersionVisitor* visitor = new DispersionVisitor(); disp_dict = PyDict_GetItemString(self->dispersion, "radius"); self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict); disp_dict = PyDict_GetItemString(self->dispersion, "phi"); self->model->phi.dispersion->accept_as_destination(visitor, self->model->phi.dispersion, disp_dict); disp_dict = PyDict_GetItemString(self->dispersion, "psi"); self->model->psi.dispersion->accept_as_destination(visitor, self->model->psi.dispersion, disp_dict); disp_dict = PyDict_GetItemString(self->dispersion, "theta"); self->model->theta.dispersion->accept_as_destination(visitor, self->model->theta.dispersion, disp_dict); // Get input and determine whether we have to supply a 1D or 2D return value. if ( !PyArg_ParseTuple(args,"O",&pars) ) { PyErr_SetString(CBCCrystalModelError, "CBCCrystalModel.run expects a q value."); return NULL; } // Check params if( PyList_Check(pars)==1) { // Length of list should be 2 for I(qx, qy)) npars = PyList_GET_SIZE(pars); if(npars!=2) { PyErr_SetString(CBCCrystalModelError, "CBCCrystalModel.run expects a double or a list of dimension 2."); return NULL; } // We have a vector q, get the qx and qy values at which // to evaluate I(qx,qy) qx_value = CBCCrystalModel_readDouble(PyList_GET_ITEM(pars,0)); qy_value = CBCCrystalModel_readDouble(PyList_GET_ITEM(pars,1)); return Py_BuildValue("d",(*(self->model))(qx_value,qy_value)); } else { // We have a scalar q, we will evaluate I(q) qx_value = CBCCrystalModel_readDouble(pars); return Py_BuildValue("d",(*(self->model))(qx_value)); } }
/* Opens a connection to the LDAP server. Initializes LDAP structure. If TLS is true, starts TLS session. */ static int connecting(LDAPConnection *self) { int rc = -1; int tls_option = -1; char *binddn = NULL; char *pswstr = NULL; char *mech = NULL; char *authzid = ""; char *realm = NULL; char *authcid = NULL; PyObject *url = NULL; PyObject *tls = NULL; PyObject *tmp = NULL; PyObject *creds = NULL; url = PyObject_GetAttrString(self->client, "_LDAPClient__url"); if (url == NULL) return -1; tmp = PyObject_GetAttrString(self->client, "_LDAPClient__cert_policy"); tls_option = (int)PyLong_AsLong(tmp); Py_DECREF(tmp); rc = _LDAP_initialization(&(self->ld), url, tls_option); Py_DECREF(url); if (rc != LDAP_SUCCESS) { PyObject *ldaperror = get_error_by_code(rc); PyErr_SetString(ldaperror, ldap_err2string(rc)); Py_DECREF(ldaperror); return -1; } tls = PyObject_GetAttrString(self->client, "_LDAPClient__tls"); if (tls == NULL) return -1; /* Start TLS, if it necessary. */ if (PyObject_IsTrue(tls)) { #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) rc = ldap_start_tls_sA(self->ld, NULL, NULL, NULL, NULL); #else rc = ldap_start_tls_s(self->ld, NULL, NULL); #endif if (rc != LDAP_SUCCESS) { //TODO Proper errors PyObject *ldaperror = get_error_by_code(rc); PyErr_SetString(ldaperror, ldap_err2string(rc)); Py_DECREF(ldaperror); Py_DECREF(tls); return -1; } } Py_DECREF(tls); creds = PyObject_GetAttrString(self->client, "_LDAPClient__credentials"); if (creds == NULL) return -1; tmp = PyObject_GetAttrString(self->client, "_LDAPClient__mechanism"); if (tmp == NULL) return -1; mech = PyObject2char(tmp); Py_XDECREF(tmp); /* Get credential information, if it's given. */ if (PyTuple_Check(creds) && PyTuple_Size(creds) > 1) { if (strcmp(mech, "SIMPLE") == 0) { tmp = PyTuple_GetItem(creds, 0); binddn = PyObject2char(tmp); } else { tmp = PyTuple_GetItem(creds, 0); authcid = PyObject2char(tmp); tmp = PyDict_GetItemString(creds, "realm"); realm = PyObject2char(tmp); } tmp = PyTuple_GetItem(creds, 1); pswstr = PyObject2char(tmp); } if (authzid == NULL) authzid = ""; rc = _LDAP_bind_s(self->ld, mech, binddn, pswstr, authcid, realm, authzid); free(mech); free(binddn); free(pswstr); free(authcid); free(realm); if (strcmp(authzid, "") != 0) free(authzid); if (rc != LDAP_SUCCESS) { PyObject *ldaperror = get_error_by_code(rc); PyErr_SetString(ldaperror, ldap_err2string(rc)); Py_DECREF(ldaperror); Py_DECREF(creds); return -1; } Py_DECREF(creds); return 0; }
static void fixup_ulcase(void) { PyObject *mods, *strop, *string, *ulo; unsigned char ul[256]; int n, c; /* find the string and strop modules */ mods = PyImport_GetModuleDict(); if (!mods) return; string = PyDict_GetItemString(mods, "string"); if (string) string = PyModule_GetDict(string); strop=PyDict_GetItemString(mods, "strop"); if (strop) strop = PyModule_GetDict(strop); if (!string && !strop) return; /* create uppercase map string */ n = 0; for (c = 0; c < 256; c++) { if (isupper(c)) ul[n++] = c; } ulo = PyString_FromStringAndSize((const char *)ul, n); if (!ulo) return; if (string) PyDict_SetItemString(string, "uppercase", ulo); if (strop) PyDict_SetItemString(strop, "uppercase", ulo); Py_DECREF(ulo); /* create lowercase string */ n = 0; for (c = 0; c < 256; c++) { if (islower(c)) ul[n++] = c; } ulo = PyString_FromStringAndSize((const char *)ul, n); if (!ulo) return; if (string) PyDict_SetItemString(string, "lowercase", ulo); if (strop) PyDict_SetItemString(strop, "lowercase", ulo); Py_DECREF(ulo); /* create letters string */ n = 0; for (c = 0; c < 256; c++) { if (isalpha(c)) ul[n++] = c; } ulo = PyString_FromStringAndSize((const char *)ul, n); if (!ulo) return; if (string) PyDict_SetItemString(string, "letters", ulo); Py_DECREF(ulo); }
int CallPy() { Py_Initialize(); if (!Py_IsInitialized()) return -1; //c调用python: //直接执行python脚本 PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append('./')"); //导入模块 PyObject* pTestModule = PyImport_ImportModule("testpy"); if (!pTestModule) { printf("Cant open python file!\n"); return -1; } //模块的字典列表 PyObject* pDict = PyModule_GetDict(pTestModule); if (!pDict) { printf("Cant find dictionary.\n"); return -1; } //打印模块的字典 printDict(pDict); //演示函数调用 PyObject* pFunHi = PyDict_GetItemString(pDict, "HelloWorld"); PyObject_CallFunction(pFunHi, "s", "Hello World!"); //Py_DECREF(pFunHi); //不用减少引用计数 //Py_DECREF(pDict); Py_DECREF(pTestModule); /*********************************************************************/ //python 调用c: //手动导入cmodule到python环境 initcmodule(); //直接脚本调用,验证python已经导入cmodule模块 PyRun_SimpleString("import cmodule"); PyRun_SimpleString("print 'call in python: ', cmodule.print_info(123)"); //程序调用 PyObject* pCmodule = PyImport_ImportModule("cmodule"); PyObject* cprint_info = PyObject_GetAttrString(pCmodule, "print_info"); if (!PyCallable_Check(cprint_info)) { PyErr_SetString(PyExc_TypeError, "parameter must be callable"); return NULL; } int iarg = 9; PyObject* result = PyObject_CallFunction(cprint_info, "i", iarg) ; if (result == NULL) { return -1; } int i = 0, i2 = 0; int ok = PyArg_ParseTuple(result, "ii", &i,&i2); //返回多个参数 //i = PyInt_AsLong(result); //返回一个参数 printf("cmodule return: %d %d %d \n", ok, i, i2); Py_DECREF(result); Py_DECREF(cprint_info); Py_DECREF(pCmodule); Py_Finalize(); return 0; }
static int _rrdtool_fetch_cb_wrapper( const char *filename, enum cf_en cf_idx, time_t *start, time_t *end, unsigned long *step, unsigned long *ds_cnt, char ***ds_namv, rrd_value_t **data) { PyObject *args; PyObject *kwargs; PyObject *ret = NULL; PyObject *tmp; PyObject *tmp_min_ts; PyGILState_STATE gstate; Py_ssize_t rowcount = 0; int rc = -1; unsigned int i, ii; gstate = PyGILState_Ensure(); if (_rrdtool_fetch_callable == NULL) { rrd_set_error("use rrdtool.register_fetch_cb to register a fetch callback"); goto gil_release_err; } args = PyTuple_New(0); kwargs = PyDict_New(); /* minimum possible UNIX datetime */ tmp_min_ts = PyLong_FromLong(0); PyObject *po_filename = PyRRD_String_FromString(filename); PyDict_SetItemString(kwargs, "filename", po_filename); Py_DECREF(po_filename); PyObject *po_cfstr = PyRRD_String_FromString(PyRRD_String_FromCF(cf_idx)); PyDict_SetItemString(kwargs, "cf", po_cfstr); Py_DECREF(po_cfstr); PyObject *po_start = PyLong_FromLong(*start); PyDict_SetItemString(kwargs, "start", po_start); Py_DECREF(po_start); PyObject *po_end = PyLong_FromLong(*end); PyDict_SetItemString(kwargs, "end", po_end); Py_DECREF(po_end); PyObject *po_step = PyLong_FromUnsignedLong(*step); PyDict_SetItemString(kwargs, "step", po_step); Py_DECREF(po_step); /* execute Python callback method */ ret = PyObject_Call(_rrdtool_fetch_callable, args, kwargs); Py_DECREF(args); Py_DECREF(kwargs); if (ret == NULL) { rrd_set_error("calling python callback failed"); goto gil_release_err; } /* handle return value of callback */ if (!PyDict_Check(ret)) { rrd_set_error("expected callback method to be a dict"); goto gil_release_err; } tmp = PyDict_GetItemString(ret, "step"); if (tmp == NULL) { rrd_set_error("expected 'step' key in callback return value"); goto gil_release_err; } else if (!PyRRD_Long_Check(tmp)) { rrd_set_error("the 'step' key in callback return value must be int"); goto gil_release_err; } else *step = PyLong_AsLong(tmp); tmp = PyDict_GetItemString(ret, "start"); if (tmp == NULL) { rrd_set_error("expected 'start' key in callback return value"); goto gil_release_err; } else if (!PyRRD_Long_Check(tmp)) { rrd_set_error("expected 'start' key in callback return value to be " "of type int"); goto gil_release_err; } else if (PyObject_RichCompareBool(tmp, tmp_min_ts, Py_EQ) || PyObject_RichCompareBool(tmp, po_start, Py_LT)) { rrd_set_error("expected 'start' value in callback return dict to be " "equal or earlier than passed start timestamp"); goto gil_release_err; } else { *start = PyLong_AsLong(po_start); if (*start == -1) { rrd_set_error("expected 'start' value in callback return value to" " not exceed LONG_MAX"); goto gil_release_err; } } tmp = PyDict_GetItemString(ret, "data"); if (tmp == NULL) { rrd_set_error("expected 'data' key in callback return value"); goto gil_release_err; } else if (!PyDict_Check(tmp)) { rrd_set_error("expected 'data' key in callback return value of type " "dict"); goto gil_release_err; } else { *ds_cnt = (unsigned long)PyDict_Size(tmp); *ds_namv = (char **)calloc(*ds_cnt, sizeof(char *)); if (*ds_namv == NULL) { rrd_set_error("an error occured while allocating memory for " "ds_namv when allocating memory for python callback"); goto gil_release_err; } PyObject *key, *value; Py_ssize_t pos = 0; /* don't use pos for indexing */ unsigned int x = 0; while (PyDict_Next(tmp, &pos, &key, &value)) { char *key_str = PyRRD_String_AS_STRING(key); if (key_str == NULL) { rrd_set_error("key of 'data' element from callback return " "value is not a string"); goto gil_release_free_dsnamv_err; } else if (strlen(key_str) > DS_NAM_SIZE) { rrd_set_error("key '%s' longer than the allowed maximum of %d " "byte", key_str, DS_NAM_SIZE - 1); goto gil_release_free_dsnamv_err; } if ((((*ds_namv)[x]) = (char *)malloc(sizeof(char) * DS_NAM_SIZE)) == NULL) { rrd_set_error("malloc fetch ds_namv entry"); goto gil_release_free_dsnamv_err; } strncpy((*ds_namv)[x], key_str, DS_NAM_SIZE - 1); (*ds_namv)[x][DS_NAM_SIZE - 1] = '\0'; if (!PyList_Check(value)) { rrd_set_error("expected 'data' dict values in callback return " "value of type list"); goto gil_release_free_dsnamv_err; } else if (PyList_Size(value) > rowcount) rowcount = PyList_Size(value); ++x; } *end = *start + *step * rowcount; if (((*data) = (rrd_value_t *)malloc(*ds_cnt * rowcount * sizeof(rrd_value_t))) == NULL) { rrd_set_error("malloc fetch data area"); goto gil_release_free_dsnamv_err; } for (i = 0; i < *ds_cnt; i++) { for (ii = 0; ii < (unsigned int)rowcount; ii++) { char *ds_namv_i = (*ds_namv)[i]; double va; PyObject *lstv = PyList_GetItem(PyDict_GetItemString(tmp, ds_namv_i), ii); /* lstv may be NULL here in case an IndexError has been raised; in such case the rowcount is higher than the number of elements for the list of that ds. use DNAN as value for these then */ if (lstv == NULL || lstv == Py_None) { if (lstv == NULL) PyErr_Clear(); va = DNAN; } else { va = PyFloat_AsDouble(lstv); if (va == -1.0 && PyErr_Occurred()) { PyObject *exc_type, *exc_value, *exc_value_str = NULL, *exc_tb; PyErr_Fetch(&exc_type, &exc_value, &exc_tb); if (exc_value != NULL) { exc_value_str = PyObject_Str(exc_value); char *exc_str = PyRRD_String_AS_STRING(exc_value_str); rrd_set_error(exc_str); Py_DECREF(exc_value); } Py_DECREF(exc_type); Py_DECREF(exc_value_str); if (exc_tb != NULL) Py_DECREF(exc_tb); goto gil_release_free_dsnamv_err; } } (*data)[i + ii * (*ds_cnt)] = va; } } } /* success */ rc = 1; goto gil_release; gil_release_free_dsnamv_err: for (i = 0; i < *ds_cnt; i++) { if ((*ds_namv)[i]) { free((*ds_namv)[i]); } } free(*ds_namv); gil_release_err: rc = -1; gil_release: if (ret != NULL) Py_DECREF(ret); PyGILState_Release(gstate); return rc; }
int initumath(PyObject *m) { PyObject *d, *s, *s2; int UFUNC_FLOATING_POINT_SUPPORT = 1; #ifdef NO_UFUNC_FLOATING_POINT_SUPPORT UFUNC_FLOATING_POINT_SUPPORT = 0; #endif /* Add some symbolic constants to the module */ d = PyModule_GetDict(m); PyDict_SetItemString(d, "pi", s = PyFloat_FromDouble(NPY_PI)); Py_DECREF(s); PyDict_SetItemString(d, "e", s = PyFloat_FromDouble(NPY_E)); Py_DECREF(s); PyDict_SetItemString(d, "euler_gamma", s = PyFloat_FromDouble(NPY_EULER)); Py_DECREF(s); #define ADDCONST(str) PyModule_AddIntConstant(m, #str, UFUNC_##str) #define ADDSCONST(str) PyModule_AddStringConstant(m, "UFUNC_" #str, UFUNC_##str) ADDCONST(ERR_IGNORE); ADDCONST(ERR_WARN); ADDCONST(ERR_CALL); ADDCONST(ERR_RAISE); ADDCONST(ERR_PRINT); ADDCONST(ERR_LOG); ADDCONST(ERR_DEFAULT); ADDCONST(SHIFT_DIVIDEBYZERO); ADDCONST(SHIFT_OVERFLOW); ADDCONST(SHIFT_UNDERFLOW); ADDCONST(SHIFT_INVALID); ADDCONST(FPE_DIVIDEBYZERO); ADDCONST(FPE_OVERFLOW); ADDCONST(FPE_UNDERFLOW); ADDCONST(FPE_INVALID); ADDCONST(FLOATING_POINT_SUPPORT); ADDSCONST(PYVALS_NAME); #undef ADDCONST #undef ADDSCONST PyModule_AddIntConstant(m, "UFUNC_BUFSIZE_DEFAULT", (long)NPY_BUFSIZE); PyModule_AddObject(m, "PINF", PyFloat_FromDouble(NPY_INFINITY)); PyModule_AddObject(m, "NINF", PyFloat_FromDouble(-NPY_INFINITY)); PyModule_AddObject(m, "PZERO", PyFloat_FromDouble(NPY_PZERO)); PyModule_AddObject(m, "NZERO", PyFloat_FromDouble(NPY_NZERO)); PyModule_AddObject(m, "NAN", PyFloat_FromDouble(NPY_NAN)); #if defined(NPY_PY3K) s = PyDict_GetItemString(d, "true_divide"); PyDict_SetItemString(d, "divide", s); #endif s = PyDict_GetItemString(d, "conjugate"); s2 = PyDict_GetItemString(d, "remainder"); /* Setup the array object's numerical structures with appropriate ufuncs in d*/ _PyArray_SetNumericOps(d); PyDict_SetItemString(d, "conj", s); PyDict_SetItemString(d, "mod", s2); if (!intern_strings()) { PyErr_SetString(PyExc_RuntimeError, "cannot intern umath strings while initializing _multiarray_umath."); return -1; } return 0; }
void ThreadProc( void *data ) { PyObject *pName, *pModule, *pDict, *pFunc; PyThreadState *mainThreadState, *myThreadState, *tempState; PyInterpreterState *mainInterpreterState; CMD_LINE_STRUCT* arg = (CMD_LINE_STRUCT*)data; // Initialize python inerpreter Py_Initialize(); // Initialize thread support PyEval_InitThreads(); // Save a pointer to the main PyThreadState object mainThreadState = PyThreadState_Get(); // Get a reference to the PyInterpreterState mainInterpreterState = mainThreadState->interp; // Create a thread state object for this thread myThreadState = PyThreadState_New(mainInterpreterState); // Release global lock PyEval_ReleaseLock(); // Acquire global lock PyEval_AcquireLock(); // Swap in my thread state tempState = PyThreadState_Swap(myThreadState); // Now execute some python code (call python functions) pName = PyString_FromString(arg->argv[1]); pModule = PyImport_Import(pName); // pDict and pFunc are borrowed references pDict = PyModule_GetDict(pModule); pFunc = PyDict_GetItemString(pDict, arg->argv[2]); if (PyCallable_Check(pFunc)) { PyObject_CallObject(pFunc, NULL); } else { PyErr_Print(); } // Clean up Py_DECREF(pModule); Py_DECREF(pName); // Swap out the current thread PyThreadState_Swap(tempState); // Release global lock PyEval_ReleaseLock(); // Clean up thread state PyThreadState_Clear(myThreadState); PyThreadState_Delete(myThreadState); Py_Finalize(); printf("My thread is finishing...\n"); // Exiting the thread #ifdef WIN32 // Windows code _endthread(); #else // POSIX code pthread_exit(NULL); #endif }
std::string getPythonTraceback() { // get exception info PyObject *type, *value, *traceback; PyErr_Fetch(&type, &value, &traceback); PyErr_NormalizeException(&type, &value, &traceback); std::ostringstream mssg; if (traceback) { PyObject* tracebackModule; PyObject* tracebackDictionary; PyObject* tracebackFunction; tracebackModule = PyImport_ImportModule("traceback"); if (!tracebackModule) { throw python_error("unable to load traceback module while importing numpy inside PDAL"); } tracebackDictionary = PyModule_GetDict(tracebackModule); tracebackFunction = PyDict_GetItemString(tracebackDictionary, "format_exception"); if (!tracebackFunction) { throw python_error("unable to find traceback function while importing numpy inside PDAL"); } if (!PyCallable_Check(tracebackFunction)) { throw python_error("invalid traceback function while importing numpy inside PDAL"); } // create an argument for "format exception" PyObject* args = PyTuple_New(3); PyTuple_SetItem(args, 0, type); PyTuple_SetItem(args, 1, value); PyTuple_SetItem(args, 2, traceback); // get a list of string describing what went wrong PyObject* output = PyObject_CallObject(tracebackFunction, args); // print error message int i, n = PyList_Size(output); #if PY_MAJOR_VERSION >= 3 for (i=0; i<n; i++) { PyObject* u = PyUnicode_AsUTF8String(PyList_GetItem(output, i)); const char* p = PyBytes_AsString(u); mssg << p; } #else for (i=0; i<n; i++) mssg << PyString_AsString(PyList_GetItem(output, i)); #endif // clean up Py_XDECREF(args); Py_XDECREF(output); } else if (value != NULL) { PyObject *s = PyObject_Str(value); #if PY_MAJOR_VERSION >= 3 // const char* text = PyUnicode_AS_DATA(s); PyObject* u = PyUnicode_AsUTF8String(s); const char* text = PyBytes_AsString(u); #else const char* text = PyString_AS_STRING(s); #endif Py_DECREF(s); mssg << text; } else { mssg << "unknown error that we are unable to get a traceback for. Was it already printed/taken?"; } Py_XDECREF(value); Py_XDECREF(type); Py_XDECREF(traceback); return mssg.str(); }
/* zipimporter.__init__ Split the "subdirectory" from the Zip archive path, lookup a matching entry in sys.path_importer_cache, fetch the file directory from there if found, or else read it from the archive. */ static int zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds) { char *path, *p, *prefix, buf[MAXPATHLEN+2]; int len; if (!_PyArg_NoKeywords("zipimporter()", kwds)) return -1; if (!PyArg_ParseTuple(args, "s:zipimporter", &path)) return -1; len = strlen(path); if (len == 0) { PyErr_SetString(ZipImportError, "archive path is empty"); return -1; } if (len >= MAXPATHLEN) { PyErr_SetString(ZipImportError, "archive path too long"); return -1; } strcpy(buf, path); #ifdef ALTSEP for (p = buf; *p; p++) { if (*p == ALTSEP) *p = SEP; } #endif path = NULL; prefix = NULL; for (;;) { #ifndef RISCOS struct stat statbuf; int rv; rv = stat(buf, &statbuf); if (rv == 0) { /* it exists */ if (S_ISREG(statbuf.st_mode)) /* it's a file */ path = buf; break; } #else if (object_exists(buf)) { /* it exists */ if (isfile(buf)) /* it's a file */ path = buf; break; } #endif /* back up one path element */ p = strrchr(buf, SEP); if (prefix != NULL) *prefix = SEP; if (p == NULL) break; *p = '\0'; prefix = p; } if (path != NULL) { PyObject *files; files = PyDict_GetItemString(zip_directory_cache, path); if (files == NULL) { files = read_directory(buf); if (files == NULL) return -1; if (PyDict_SetItemString(zip_directory_cache, path, files) != 0) return -1; } else Py_INCREF(files); self->files = files; } else { PyErr_SetString(ZipImportError, "not a Zip file"); return -1; } if (prefix == NULL) prefix = ""; else { prefix++; len = strlen(prefix); if (prefix[len-1] != SEP) { /* add trailing SEP */ prefix[len] = SEP; prefix[len + 1] = '\0'; } } self->archive = PyString_FromString(buf); if (self->archive == NULL) return -1; self->prefix = PyString_FromString(prefix); if (self->prefix == NULL) return -1; return 0; }
PyMODINIT_FUNC init_mysql(void) { PyObject *dict, *module, *emod, *edict, *version_tuple; module = Py_InitModule3("_mysql", _mysql_methods, _mysql___doc__); if (!module) return; /* this really should never happen */ /* Populate final object settings */ _mysql_ConnectionObject_Type.ob_type = &PyType_Type; _mysql_ResultObject_Type.ob_type = &PyType_Type; _mysql_FieldObject_Type.ob_type = &PyType_Type; _mysql_ConnectionObject_Type.tp_alloc = PyType_GenericAlloc; _mysql_ConnectionObject_Type.tp_new = PyType_GenericNew; _mysql_ConnectionObject_Type.tp_free = _PyObject_GC_Del; _mysql_ResultObject_Type.tp_alloc = PyType_GenericAlloc; _mysql_ResultObject_Type.tp_new = PyType_GenericNew; _mysql_ResultObject_Type.tp_free = _PyObject_GC_Del; _mysql_FieldObject_Type.tp_alloc = PyType_GenericAlloc; _mysql_FieldObject_Type.tp_new = PyType_GenericNew; _mysql_FieldObject_Type.tp_free = _PyObject_GC_Del; if (!(dict = PyModule_GetDict(module))) goto error; /* Module constants */ version_tuple = PyRun_String(QUOTE(version_info), Py_eval_input, dict, dict); if (PyModule_AddObject(module, "version_info", version_tuple) < 0) goto error; if (PyModule_AddStringConstant(module, "__version__", QUOTE(__version__)) < 0) goto error; if (PyModule_AddStringConstant(module, "NULL", "NULL") < 0) goto error; /* Register types */ if (PyDict_SetItemString(dict, "connection", (PyObject *)&_mysql_ConnectionObject_Type)) goto error; Py_INCREF(&_mysql_ConnectionObject_Type); if (PyDict_SetItemString(dict, "result", (PyObject *)&_mysql_ResultObject_Type)) goto error; Py_INCREF(&_mysql_ResultObject_Type); if (PyDict_SetItemString(dict, "field", (PyObject *)&_mysql_FieldObject_Type)) goto error; Py_INCREF(&_mysql_FieldObject_Type); /* Reach into the exceptions module. */ if (!(emod = PyImport_ImportModule("MySQLdb.exceptions"))) goto error; if (!(edict = PyModule_GetDict(emod))) goto error; if (!(_mysql_MySQLError = _mysql_NewException(dict, edict, "MySQLError"))) goto error; if (!(_mysql_Warning = _mysql_NewException(dict, edict, "Warning"))) goto error; if (!(_mysql_Error = _mysql_NewException(dict, edict, "Error"))) goto error; if (!(_mysql_InterfaceError = _mysql_NewException(dict, edict, "InterfaceError"))) goto error; if (!(_mysql_DatabaseError = _mysql_NewException(dict, edict, "DatabaseError"))) goto error; if (!(_mysql_DataError = _mysql_NewException(dict, edict, "DataError"))) goto error; if (!(_mysql_OperationalError = _mysql_NewException(dict, edict, "OperationalError"))) goto error; if (!(_mysql_IntegrityError = _mysql_NewException(dict, edict, "IntegrityError"))) goto error; if (!(_mysql_InternalError = _mysql_NewException(dict, edict, "InternalError"))) goto error; if (!(_mysql_ProgrammingError = _mysql_NewException(dict, edict, "ProgrammingError"))) goto error; if (!(_mysql_NotSupportedError = _mysql_NewException(dict, edict, "NotSupportedError"))) goto error; if (!(_mysql_error_map = PyDict_GetItemString(edict, "error_map"))) goto error; Py_DECREF(emod); error: if (PyErr_Occurred()) PyErr_SetString(PyExc_ImportError, "_mysql: init failed"); return; }
static void tornado_loop() { if (!uwsgi.has_threads && uwsgi.mywid == 1) { uwsgi_log("!!! Running tornado without threads IS NOT recommended, enable them with --enable-threads !!!\n"); } if (uwsgi.socket_timeout < 30) { uwsgi_log("!!! Running tornado with a socket-timeout lower than 30 seconds is not recommended, tune it with --socket-timeout !!!\n"); } if (!uwsgi.async_waiting_fd_table) uwsgi.async_waiting_fd_table = uwsgi_calloc(sizeof(struct wsgi_request *) * uwsgi.max_fd); if (!uwsgi.async_proto_fd_table) uwsgi.async_proto_fd_table = uwsgi_calloc(sizeof(struct wsgi_request *) * uwsgi.max_fd); // get the GIL UWSGI_GET_GIL up.gil_get = gil_tornado_get; up.gil_release = gil_tornado_release; uwsgi.wait_write_hook = uwsgi_tornado_wait_write_hook; uwsgi.wait_read_hook = uwsgi_tornado_wait_read_hook; uwsgi.schedule_fix = uwsgi_tornado_schedule_fix; if (uwsgi.async < 2) { uwsgi_log("the tornado loop engine requires async mode (--async <n>)\n"); exit(1); } if (!uwsgi.schedule_to_main) { uwsgi_log("*** DANGER *** tornado mode without coroutine/greenthread engine loaded !!!\n"); } PyObject *tornado_dict = get_uwsgi_pydict("tornado.ioloop"); if (!tornado_dict) uwsgi_pyexit; PyObject *tornado_IOLoop = PyDict_GetItemString(tornado_dict, "IOLoop"); if (!tornado_IOLoop) uwsgi_pyexit; utornado.ioloop = PyObject_CallMethod(tornado_IOLoop, "instance", NULL); if (!utornado.ioloop) uwsgi_pyexit; // main greenlet waiting for connection (one greenlet per-socket) PyObject *uwsgi_tornado_accept = PyCFunction_New(uwsgi_tornado_accept_def, NULL); Py_INCREF(uwsgi_tornado_accept); utornado.request = PyCFunction_New(uwsgi_tornado_request_def, NULL); if (!utornado.request) uwsgi_pyexit; utornado.hook_fd = PyCFunction_New(uwsgi_tornado_hook_fd_def, NULL); if (!utornado.hook_fd) uwsgi_pyexit; utornado.hook_timeout = PyCFunction_New(uwsgi_tornado_hook_timeout_def, NULL); if (!utornado.hook_timeout) uwsgi_pyexit; utornado.hook_fix = PyCFunction_New(uwsgi_tornado_hook_fix_def, NULL); if (!utornado.hook_fix) uwsgi_pyexit; utornado.read = PyObject_GetAttrString(utornado.ioloop, "READ"); if (!utornado.read) uwsgi_pyexit; utornado.write = PyObject_GetAttrString(utornado.ioloop, "WRITE"); if (!utornado.write) uwsgi_pyexit; utornado.functools = PyImport_ImportModule("functools"); if (!utornado.functools) uwsgi_pyexit; Py_INCREF(utornado.request); Py_INCREF(utornado.hook_fd); Py_INCREF(utornado.hook_timeout); Py_INCREF(utornado.hook_fix); Py_INCREF(utornado.read); Py_INCREF(utornado.write); // call add_handler on each socket struct uwsgi_socket *uwsgi_sock = uwsgi.sockets; while(uwsgi_sock) { if (PyObject_CallMethod(utornado.ioloop, "add_handler", "iOO", uwsgi_sock->fd, uwsgi_tornado_accept, utornado.read) == NULL) { uwsgi_pyexit; } uwsgi_sock = uwsgi_sock->next; } if (PyObject_CallMethod(utornado.ioloop, "start", NULL) == NULL) { uwsgi_pyexit; } // never here ? }
static int _cffi_initialize_python(void) { /* This initializes Python, imports _cffi_backend, and then the present .dll/.so is set up as a CPython C extension module. */ int result; PyGILState_STATE state; PyObject *pycode=NULL, *global_dict=NULL, *x; #if PY_MAJOR_VERSION >= 3 /* see comments in _cffi_carefully_make_gil() about the Python2/Python3 difference */ #else /* Acquire the GIL. We have no threadstate here. If Python is already initialized, it is possible that there is already one existing for this thread, but it is not made current now. */ PyEval_AcquireLock(); _cffi_py_initialize(); /* The Py_InitializeEx() sometimes made a threadstate for us, but not always. Indeed Py_InitializeEx() could be called and do nothing. So do we have a threadstate, or not? We don't know, but we can replace it with NULL in all cases. */ (void)PyThreadState_Swap(NULL); /* Now we can release the GIL and re-acquire immediately using the logic of PyGILState(), which handles making or installing the correct threadstate. */ PyEval_ReleaseLock(); #endif state = PyGILState_Ensure(); /* Call the initxxx() function from the present module. It will create and initialize us as a CPython extension module, instead of letting the startup Python code do it---it might reimport the same .dll/.so and get maybe confused on some platforms. It might also have troubles locating the .dll/.so again for all I know. */ (void)_CFFI_PYTHON_STARTUP_FUNC(); if (PyErr_Occurred()) goto error; /* Now run the Python code provided to ffi.embedding_init_code(). */ pycode = Py_CompileString(_CFFI_PYTHON_STARTUP_CODE, "<init code for '" _CFFI_MODULE_NAME "'>", Py_file_input); if (pycode == NULL) goto error; global_dict = PyDict_New(); if (global_dict == NULL) goto error; if (PyDict_SetItemString(global_dict, "__builtins__", PyThreadState_GET()->interp->builtins) < 0) goto error; x = PyEval_EvalCode( #if PY_MAJOR_VERSION < 3 (PyCodeObject *) #endif pycode, global_dict, global_dict); if (x == NULL) goto error; Py_DECREF(x); /* Done! Now if we've been called from _cffi_start_and_call_python() in an ``extern "Python"``, we can only hope that the Python code did correctly set up the corresponding @ffi.def_extern() function. Otherwise, the general logic of ``extern "Python"`` functions (inside the _cffi_backend module) will find that the reference is still missing and print an error. */ result = 0; done: Py_XDECREF(pycode); Py_XDECREF(global_dict); PyGILState_Release(state); return result; error:; { /* Print as much information as potentially useful. Debugging load-time failures with embedding is not fun */ PyObject *exception, *v, *tb, *f, *modules, *mod; PyErr_Fetch(&exception, &v, &tb); if (exception != NULL) { PyErr_NormalizeException(&exception, &v, &tb); PyErr_Display(exception, v, tb); } Py_XDECREF(exception); Py_XDECREF(v); Py_XDECREF(tb); f = PySys_GetObject((char *)"stderr"); if (f != NULL && f != Py_None) { PyFile_WriteString("\nFrom: " _CFFI_MODULE_NAME "\ncompiled with cffi version: 1.5.1" "\n_cffi_backend module: ", f); modules = PyImport_GetModuleDict(); mod = PyDict_GetItemString(modules, "_cffi_backend"); if (mod == NULL) { PyFile_WriteString("not loaded", f); } else { v = PyObject_GetAttrString(mod, "__file__"); PyFile_WriteObject(v, f, 0); Py_XDECREF(v); } PyFile_WriteString("\nsys.path: ", f); PyFile_WriteObject(PySys_GetObject((char *)"path"), f, 0); PyFile_WriteString("\n\n", f); } } result = -1; goto done; }
PyObject* ItemDistribution::create(PyTypeObject* pytype, PyObject* args, PyObject* kwds) { try { // Pick up the item PyObject* it = PyDict_GetItemString(kwds,"item"); if (!it) throw DataException("missing item on ItemDistribution"); if (!PyObject_TypeCheck(it, Item::metadata->pythonClass)) throw DataException("ItemDistribution item must be of type item"); /* XXX // Pick up the priority PyObject* q1 = PyDict_GetItemString(kwds,"priority"); int q2 = q1 ? PythonData(q1).getInt() : 1; // Pick up the effective dates DateRange eff; PyObject* eff_start = PyDict_GetItemString(kwds,"effective_start"); if (eff_start) { PythonData d(eff_start); eff.setStart(d.getDate()); } PyObject* eff_end = PyDict_GetItemString(kwds,"effective_end"); if (eff_end) { PythonData d(eff_end); eff.setEnd(d.getDate()); } */ // Create the ItemDistribution ItemDistribution *l = new ItemDistribution(); l->setItem(static_cast<Item*>(it)); // Iterate over extra keywords, and set attributes. @todo move this responsibility to the readers... if (l) { PyObject *key, *value; Py_ssize_t pos = 0; while (PyDict_Next(kwds, &pos, &key, &value)) { PythonData field(value); PyObject* key_utf8 = PyUnicode_AsUTF8String(key); DataKeyword attr(PyBytes_AsString(key_utf8)); Py_DECREF(key_utf8); if (!attr.isA(Tags::item) && !attr.isA(Tags::type) && !attr.isA(Tags::action)) { const MetaFieldBase* fmeta = l->getType().findField(attr.getHash()); if (!fmeta && l->getType().category) fmeta = l->getType().category->findField(attr.getHash()); if (fmeta) // Update the attribute fmeta->setField(l, field); else l->setProperty(attr.getName(), value); } }; } // Return the object Py_INCREF(l); return static_cast<PyObject*>(l); } catch (...) { PythonType::evalException(); return nullptr; } }
PyObject *ObjectRow_PyObject__subscript(ObjectRow_PyObject *self, PyObject *key) { ObjectAttribute *attr = 0; PyObject *value, *pytmp; if (!self->query_info) { // If no query_info available, then we work strictly from the pickle // dict, which init() requires be available. value = PyDict_GetItem(self->pickle, key); if (!value) { PyErr_SetObject(PyExc_KeyError, key); return NULL; } Py_INCREF(value); return value; } // String is the more common case. if (PyString_Check(key)) { // Handle some special case attribute names. if (PyStr_Compare(key, "type") == 0) { // Returns the type name of this object. Py_INCREF(self->type_name); return self->type_name; } else if (PyStr_Compare(key, "parent") == 0) { /* Returns a tuple (type_name, id) for this object's parent. If * type_name can't be resolved from the parent_id, then the integer * value for the type is used instead. */ if (!self->parent) { // Generate the value if it's not available. ObjectAttribute *type_attr, *id_attr; PyObject *o_type, *o_id, *type_name = 0; // Lookup the parent_type and parent_id indexes within the // sql row. pytmp = PyDict_GetItemString(self->query_info->idxmap, "parent_type"); type_attr = pytmp ? (ObjectAttribute *)PyCObject_AsVoidPtr(pytmp) : NULL; pytmp = PyDict_GetItemString(self->query_info->idxmap, "parent_id"); id_attr = pytmp ? (ObjectAttribute *)PyCObject_AsVoidPtr(pytmp) : NULL; // If neither of these values are available in the row, raise an // exception. if (!type_attr || !id_attr || type_attr->index == -1 || id_attr->index == -1) { PyErr_Format(PyExc_IndexError, "Parent attribute not available."); return NULL; } // They're both available, so fetch them. o_type = PySequence_Fast_GET_ITEM(self->row, type_attr->index); o_id = PySequence_Fast_GET_ITEM(self->row, id_attr->index); // Resolve type id to type name. if (PyNumber_Check(o_type)) type_name = PyDict_GetItem(self->query_info->type_names, o_type); // Construct the (name, id) tuple. if (type_name) self->parent = Py_BuildValue("(OO)", type_name, o_id); else self->parent = Py_BuildValue("(OO)", o_type, o_id); } Py_INCREF(self->parent); return self->parent; } else if (PyStr_Compare(key, "_row") == 0) { Py_INCREF(self->row); return(self->row); } pytmp = PyDict_GetItem(self->query_info->idxmap, key); attr = pytmp ? (ObjectAttribute *)PyCObject_AsVoidPtr(pytmp) : NULL; } // But also support referencing the sql row by index. (Pickled attributes // cannot be accessed this way, though.) else if (PyNumber_Check(key)) { long index = -1; if (PyInt_Check(key)) index = PyInt_AsLong(key); else if (PyLong_Check(key)) index = PyLong_AsLong(key); if (index < 0 || index >= PySequence_Length(self->row)) { PyErr_Format(PyExc_IndexError, "index out of range"); return NULL; } return PySequence_GetItem(self->row, index); } //printf("REQUEST: %s attr=%p idx=%d has_pickle=%d pickle_idx=%d\n", skey, attr, attr->index, self->has_pickle, self->query_info->pickle_idx); if (attr && attr->index == -1 && !self->has_pickle && self->query_info->pickle_idx != -1) { /* Attribute is valid and pickle column exists in sql row, but pickle * is None, which means this attribute was never assigned a value, so * return suitable default ([] for ivtidx, and None for everything * else) */ return get_default_for_attr(attr); } /* Raise exception if attribute name isn't known, or if the requested * attribute, while valid for this object type, can't be obtained given the * query that was done. */ if (!attr || (attr->index == -1 && !self->has_pickle && attr->pickled)) { PyErr_SetObject(PyExc_KeyError, key); return NULL; } if (!attr->pickled || (IS_ATTR_INDEXED_IGNORE_CASE(attr->flags) && attr->index >= 0 && !self->has_pickle)) /* If the attribute isn't pickled, we return the value from the row * tuple. Also, if the attribute is ATTR_INDEXED_IGNORE_CASE but we * don't have a pickle available, and that attribute exists in the * row tuple, return what we have. */ return convert(self, attr, PySequence_Fast_GET_ITEM(self->row, attr->index)); // If we need to check the pickle but haven't unpickled, do so now. if (!self->unpickled && !do_unpickle(self)) return NULL; if (IS_ATTR_INDEXED_IGNORE_CASE(attr->flags)) { // ATTR_INDEXED_IGNORE_CASE, these attributes are prefixed with __ in // the pickled dict. PyObject *newkey = PyString_FromString("__"); PyString_Concat(&newkey, key); key = newkey; } else Py_INCREF(key); value = PyDict_GetItem(self->pickle, key); Py_DECREF(key); if (!value) // Attribute isn't stored in pickle, so return suitable default. return get_default_for_attr(attr); return convert(self, attr, value); }
void XBPyThread::Process() { CLog::Log(LOGDEBUG,"Python thread: start processing"); int m_Py_file_input = Py_file_input; // get the global lock PyEval_AcquireLock(); PyThreadState* state = Py_NewInterpreter(); if (!state) { PyEval_ReleaseLock(); CLog::Log(LOGERROR,"Python thread: FAILED to get thread state!"); return; } // swap in my thread state PyThreadState_Swap(state); m_pExecuter->InitializeInterpreter(addon); CLog::Log(LOGDEBUG, "%s - The source file to load is %s", __FUNCTION__, m_source); // get path from script file name and add python path's // this is used for python so it will search modules from script path first CStdString scriptDir; URIUtils::GetDirectory(CSpecialProtocol::TranslatePath(m_source), scriptDir); URIUtils::RemoveSlashAtEnd(scriptDir); CStdString path = scriptDir; // add on any addon modules the user has installed ADDON::VECADDONS addons; ADDON::CAddonMgr::Get().GetAddons(ADDON::ADDON_SCRIPT_MODULE, addons); for (unsigned int i = 0; i < addons.size(); ++i) #ifdef TARGET_WINDOWS { CStdString strTmp(CSpecialProtocol::TranslatePath(addons[i]->LibPath())); g_charsetConverter.utf8ToSystem(strTmp); path += PY_PATH_SEP + strTmp; } #else path += PY_PATH_SEP + CSpecialProtocol::TranslatePath(addons[i]->LibPath()); #endif // and add on whatever our default path is path += PY_PATH_SEP; // we want to use sys.path so it includes site-packages // if this fails, default to using Py_GetPath PyObject *sysMod(PyImport_ImportModule((char*)"sys")); // must call Py_DECREF when finished PyObject *sysModDict(PyModule_GetDict(sysMod)); // borrowed ref, no need to delete PyObject *pathObj(PyDict_GetItemString(sysModDict, "path")); // borrowed ref, no need to delete if( pathObj && PyList_Check(pathObj) ) { for( int i = 0; i < PyList_Size(pathObj); i++ ) { PyObject *e = PyList_GetItem(pathObj, i); // borrowed ref, no need to delete if( e && PyString_Check(e) ) { path += PyString_AsString(e); // returns internal data, don't delete or modify path += PY_PATH_SEP; } } } else { path += Py_GetPath(); } Py_DECREF(sysMod); // release ref to sysMod // set current directory and python's path. if (m_argv != NULL) PySys_SetArgv(m_argc, m_argv); CLog::Log(LOGDEBUG, "%s - Setting the Python path to %s", __FUNCTION__, path.c_str()); PySys_SetPath((char *)path.c_str()); CLog::Log(LOGDEBUG, "%s - Entering source directory %s", __FUNCTION__, scriptDir.c_str()); PyObject* module = PyImport_AddModule((char*)"__main__"); PyObject* moduleDict = PyModule_GetDict(module); // when we are done initing we store thread state so we can be aborted PyThreadState_Swap(NULL); PyEval_ReleaseLock(); // we need to check if we was asked to abort before we had inited bool stopping = false; { CSingleLock lock(m_pExecuter->m_critSection); m_threadState = state; stopping = m_stopping; } PyEval_AcquireLock(); PyThreadState_Swap(state); if (!stopping) { if (m_type == 'F') { // run script from file // We need to have python open the file because on Windows the DLL that python // is linked against may not be the DLL that xbmc is linked against so // passing a FILE* to python from an fopen has the potential to crash. PyObject* file = PyFile_FromString((char *) CSpecialProtocol::TranslatePath(m_source).c_str(), (char*)"r"); FILE *fp = PyFile_AsFile(file); if (fp) { PyObject *f = PyString_FromString(CSpecialProtocol::TranslatePath(m_source).c_str()); PyDict_SetItemString(moduleDict, "__file__", f); if (addon.get() != NULL) { PyObject *pyaddonid = PyString_FromString(addon->ID().c_str()); PyDict_SetItemString(moduleDict, "__xbmcaddonid__", pyaddonid); CStdString version = ADDON::GetXbmcApiVersionDependency(addon); PyObject *pyxbmcapiversion = PyString_FromString(version.c_str()); PyDict_SetItemString(moduleDict, "__xbmcapiversion__", pyxbmcapiversion); CLog::Log(LOGDEBUG,"Instantiating addon using automatically obtained id of \"%s\" dependent on version %s of the xbmc.python api",addon->ID().c_str(),version.c_str()); } Py_DECREF(f); PyRun_FileExFlags(fp, CSpecialProtocol::TranslatePath(m_source).c_str(), m_Py_file_input, moduleDict, moduleDict,1,NULL); } else CLog::Log(LOGERROR, "%s not found!", m_source); } else { //run script PyRun_String(m_source, m_Py_file_input, moduleDict, moduleDict); } } if (!PyErr_Occurred()) CLog::Log(LOGINFO, "Scriptresult: Success"); else if (PyErr_ExceptionMatches(PyExc_SystemExit)) CLog::Log(LOGINFO, "Scriptresult: Aborted"); else { PyObject* exc_type; PyObject* exc_value; PyObject* exc_traceback; PyObject* pystring; pystring = NULL; PyErr_Fetch(&exc_type, &exc_value, &exc_traceback); if (exc_type == 0 && exc_value == 0 && exc_traceback == 0) { CLog::Log(LOGINFO, "Strange: No Python exception occured"); } else { if (exc_type != NULL && (pystring = PyObject_Str(exc_type)) != NULL && (PyString_Check(pystring))) { PyObject *tracebackModule; CLog::Log(LOGINFO, "-->Python script returned the following error<--"); CLog::Log(LOGERROR, "Error Type: %s", PyString_AsString(PyObject_Str(exc_type))); if (PyObject_Str(exc_value)) CLog::Log(LOGERROR, "Error Contents: %s", PyString_AsString(PyObject_Str(exc_value))); tracebackModule = PyImport_ImportModule((char*)"traceback"); if (tracebackModule != NULL) { PyObject *tbList, *emptyString, *strRetval; tbList = PyObject_CallMethod(tracebackModule, (char*)"format_exception", (char*)"OOO", exc_type, exc_value == NULL ? Py_None : exc_value, exc_traceback == NULL ? Py_None : exc_traceback); emptyString = PyString_FromString(""); strRetval = PyObject_CallMethod(emptyString, (char*)"join", (char*)"O", tbList); CLog::Log(LOGERROR, "%s", PyString_AsString(strRetval)); Py_DECREF(tbList); Py_DECREF(emptyString); Py_DECREF(strRetval); Py_DECREF(tracebackModule); } CLog::Log(LOGINFO, "-->End of Python script error report<--"); } else { pystring = NULL; CLog::Log(LOGINFO, "<unknown exception type>"); } PYXBMC::PyXBMCGUILock(); CGUIDialogKaiToast *pDlgToast = (CGUIDialogKaiToast*)g_windowManager.GetWindow(WINDOW_DIALOG_KAI_TOAST); if (pDlgToast) { CStdString desc; CStdString path; CStdString script; URIUtils::Split(m_source, path, script); if (script.Equals("default.py")) { CStdString path2; URIUtils::RemoveSlashAtEnd(path); URIUtils::Split(path, path2, script); } desc.Format(g_localizeStrings.Get(2100), script); pDlgToast->QueueNotification(CGUIDialogKaiToast::Error, g_localizeStrings.Get(257), desc); } PYXBMC::PyXBMCGUIUnlock(); } Py_XDECREF(exc_type); Py_XDECREF(exc_value); // caller owns all 3 Py_XDECREF(exc_traceback); // already NULL'd out Py_XDECREF(pystring); } //tell xbmc.Monitor to call onAbortRequested() g_pythonParser.OnAbortRequested(); PyObject *m = PyImport_AddModule((char*)"xbmc"); if(!m || PyObject_SetAttrString(m, (char*)"abortRequested", PyBool_FromLong(1))) CLog::Log(LOGERROR, "Scriptresult: failed to set abortRequested"); // make sure all sub threads have finished for(PyThreadState* s = state->interp->tstate_head, *old = NULL; s;) { if(s == state) { s = s->next; continue; } if(old != s) { CLog::Log(LOGINFO, "Scriptresult: Waiting on thread %"PRIu64, (uint64_t)s->thread_id); old = s; } CPyThreadState pyState; Sleep(100); pyState.Restore(); s = state->interp->tstate_head; } // pending calls must be cleared out PyXBMC_ClearPendingCalls(state); PyThreadState_Swap(NULL); PyEval_ReleaseLock(); //set stopped event - this allows ::stop to run and kill remaining threads //this event has to be fired without holding m_pExecuter->m_critSection //before //Also the GIL (PyEval_AcquireLock) must not be held //if not obeyed there is still no deadlock because ::stop waits with timeout (smart one!) stoppedEvent.Set(); { CSingleLock lock(m_pExecuter->m_critSection); m_threadState = NULL; } PyEval_AcquireLock(); PyThreadState_Swap(state); m_pExecuter->DeInitializeInterpreter(); Py_EndInterpreter(state); PyThreadState_Swap(NULL); PyEval_ReleaseLock(); }
/* Convert 9-item tuple to tm structure. Return 1 on success, set * an exception and return 0 on error. */ static int gettmarg(PyObject *args, struct tm *p) { int y; memset((void *) p, '\0', sizeof(struct tm)); if (!PyTuple_Check(args)) { PyErr_SetString(PyExc_TypeError, "Tuple or struct_time argument required"); return 0; } if (!PyArg_ParseTuple(args, "iiiiiiiii", &y, &p->tm_mon, &p->tm_mday, &p->tm_hour, &p->tm_min, &p->tm_sec, &p->tm_wday, &p->tm_yday, &p->tm_isdst)) return 0; /* If year is specified with less than 4 digits, its interpretation * depends on the accept2dyear value. * * If accept2dyear is true (default), a backward compatibility behavior is * invoked as follows: * * - for 2-digit year, century is guessed according to POSIX rules for * %y strptime format: 21st century for y < 69, 20th century * otherwise. A deprecation warning is issued when century * information is guessed in this way. * * - for 3-digit or negative year, a ValueError exception is raised. * * If accept2dyear is false (set by the program or as a result of a * non-empty value assigned to PYTHONY2K environment variable) all year * values are interpreted as given. */ if (y < 1000) { PyObject *accept = PyDict_GetItemString(moddict, "accept2dyear"); if (accept != NULL) { int acceptval = PyObject_IsTrue(accept); if (acceptval == -1) return 0; if (acceptval) { if (0 <= y && y < 69) y += 2000; else if (69 <= y && y < 100) y += 1900; else { PyErr_SetString(PyExc_ValueError, "year out of range"); return 0; } if (PyErr_WarnEx(PyExc_DeprecationWarning, "Century info guessed for a 2-digit year.", 1) != 0) return 0; } } else return 0; } p->tm_year = y - 1900; p->tm_mon--; p->tm_wday = (p->tm_wday + 1) % 7; p->tm_yday--; return 1; }
static PyObject * structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyObject *arg = NULL; PyObject *dict = NULL; PyObject *ob; PyStructSequence *res = NULL; Py_ssize_t len, min_len, max_len, i, n_unnamed_fields; static char *kwlist[] = {"sequence", "dict", 0}; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:structseq", kwlist, &arg, &dict)) return NULL; arg = PySequence_Fast(arg, "constructor requires a sequence"); if (!arg) { return NULL; } if (dict && !PyDict_Check(dict)) { PyErr_Format(PyExc_TypeError, "%.500s() takes a dict as second arg, if any", type->tp_name); Py_DECREF(arg); return NULL; } len = PySequence_Fast_GET_SIZE(arg); min_len = VISIBLE_SIZE_TP(type); max_len = REAL_SIZE_TP(type); n_unnamed_fields = UNNAMED_FIELDS_TP(type); if (min_len != max_len) { if (len < min_len) { PyErr_Format(PyExc_TypeError, "%.500s() takes an at least %zd-sequence (%zd-sequence given)", type->tp_name, min_len, len); Py_DECREF(arg); return NULL; } if (len > max_len) { PyErr_Format(PyExc_TypeError, "%.500s() takes an at most %zd-sequence (%zd-sequence given)", type->tp_name, max_len, len); Py_DECREF(arg); return NULL; } } else { if (len != min_len) { PyErr_Format(PyExc_TypeError, "%.500s() takes a %zd-sequence (%zd-sequence given)", type->tp_name, min_len, len); Py_DECREF(arg); return NULL; } } res = (PyStructSequence*) PyStructSequence_New(type); if (res == NULL) { Py_DECREF(arg); return NULL; } for (i = 0; i < len; ++i) { PyObject *v = PySequence_Fast_GET_ITEM(arg, i); Py_INCREF(v); res->ob_item[i] = v; } for (; i < max_len; ++i) { if (dict && (ob = PyDict_GetItemString( dict, type->tp_members[i-n_unnamed_fields].name))) { } else { ob = Py_None; } Py_INCREF(ob); res->ob_item[i] = ob; } Py_DECREF(arg); return (PyObject*) res; }