static PyObject *pygrpc_finished_event_args(grpc_event *c_event) { PyObject *code; PyObject *details; PyObject *status; PyObject *event_args; code = pygrpc_status_code(c_event->data.finished.status); if (code == NULL) { PyErr_SetString(PyExc_RuntimeError, "Unrecognized status code!"); return NULL; } if (c_event->data.finished.details == NULL) { details = PyBytes_FromString(""); } else { details = PyBytes_FromString(c_event->data.finished.details); } if (details == NULL) { return NULL; } status = PyObject_CallFunctionObjArgs(status_class, code, details, NULL); Py_DECREF(details); if (status == NULL) { return NULL; } event_args = PyTuple_Pack(7, finish_event_kind, (PyObject *)c_event->tag, Py_None, Py_None, Py_None, Py_None, status); Py_DECREF(status); return event_args; }
PyObject* init_local_module(PyObject *m) { PyObject *d, *sd, *v; PyObject *sys_modules, *module; PyMethodDef *ml; #ifdef PY3 PyObject *mod_name = PyUnicode_FromString(MODULE_NAME "." LOCAL_MOD_NAME); #else PyObject *mod_name = PyBytes_FromString(MODULE_NAME "." LOCAL_MOD_NAME); #endif if(mod_name == NULL){ return NULL; } LocalObjectType.tp_new = PyType_GenericNew; if(PyType_Ready(&LocalObjectType) < 0){ return NULL; } sys_modules = PySys_GetObject("modules"); d = PyModule_GetDict(m); module = PyDict_GetItem(d, mod_name); if(module == NULL) { module = PyModule_New(MODULE_NAME "." LOCAL_MOD_NAME); if(module != NULL) { PyDict_SetItem(sys_modules, mod_name, module); PyModule_AddObject(m, LOCAL_MOD_NAME, module); } } sd = PyModule_GetDict(module); for(ml = LocalMod_methods; ml->ml_name != NULL; ml++){ v = PyCFunction_NewEx(ml, (PyObject *)NULL, mod_name); if(v == NULL) { goto fin; } if(PyDict_SetItemString(sd, ml->ml_name, v) != 0){ Py_DECREF(v); return NULL; } Py_DECREF(v); } fin: Py_DECREF(mod_name); Py_INCREF(&LocalObjectType); PyModule_AddObject(module, "local", (PyObject *)&LocalObjectType); #ifdef PY3 dict_key = PyUnicode_FromString("_jega_local_dict__"); #else dict_key = PyBytes_FromString("_jega_local_dict__"); #endif return module; }
static PyObject *pygrpc_service_event_args(grpc_event *c_event) { if (c_event->data.server_rpc_new.method == NULL) { return PyTuple_Pack(7, service_event_kind, c_event->tag, Py_None, Py_None, Py_None, Py_None, Py_None); } else { PyObject *method = NULL; PyObject *host = NULL; PyObject *service_deadline = NULL; Call *call = NULL; PyObject *service_acceptance = NULL; PyObject *event_args = NULL; method = PyBytes_FromString(c_event->data.server_rpc_new.method); if (method == NULL) { goto error; } host = PyBytes_FromString(c_event->data.server_rpc_new.host); if (host == NULL) { goto error; } service_deadline = pygrpc_as_py_time(&c_event->data.server_rpc_new.deadline); if (service_deadline == NULL) { goto error; } call = PyObject_New(Call, &pygrpc_CallType); if (call == NULL) { goto error; } call->c_call = c_event->call; service_acceptance = PyObject_CallFunctionObjArgs(service_acceptance_class, call, method, host, service_deadline, NULL); if (service_acceptance == NULL) { goto error; } event_args = PyTuple_Pack(7, service_event_kind, (PyObject *)c_event->tag, Py_None, Py_None, service_acceptance, Py_None, Py_None); Py_DECREF(service_acceptance); error: Py_XDECREF(call); Py_XDECREF(method); Py_XDECREF(host); Py_XDECREF(service_deadline); return event_args; } }
/** * @brief Checks if a given language pair is available * * pythonInit() must have been called before or an error will occur (the module is not loaded) * @param source String containing the source language * @param source String containing the target language * @return 1 if the call was successful and the language pair exists, or 0 otherwise */ int pairExists(char* source, char* target){ PyObject *pFunc, *pArgs, *pArg, *result; if (iface_module != NULL) { pFunc = PyObject_GetAttrString(iface_module, "pairExists"); if (pFunc) { pArgs = PyTuple_New(2); pArg = PyBytes_FromString(source); PyTuple_SetItem(pArgs, 0, pArg); pArg = PyBytes_FromString(target); PyTuple_SetItem(pArgs, 1, pArg); result = PyObject_CallObject(pFunc, pArgs); if (result != NULL) { if(PyDict_GetItemString(result,"ok") == Py_True){ Py_XDECREF(pFunc); if(PyDict_GetItemString(result,"result") == Py_True){ return 1; } else{ notify_error("Pair does not exist"); return 0; } } else{ Py_XDECREF(pFunc); notify_error(PyBytes_AsString(PyDict_GetItemString(result,"errorMsg"))); return 0; } } else { Py_XDECREF(pFunc); return 0; } } else { return 0; } } else { notify_error("Module: \'apertiumInterfaceAPY\' is not loaded"); return 0; } }
/** * @brief Creates a new entry in the language pairs dictionary * * pythonInit() must have been called before or an error will occur (the module is not loaded) * @param user Name of the user to create a new entry for * @param direction Direction to create a new entry in ("incoming" or "outgoing") * @param source Source language of the language pair * @param target Target language of the language pair * @return 1 on success, or 0 otherwise */ int dictionarySetUserEntry(const char* user, const char* direction, const char* source, const char* target){ PyObject *pFunc, *pArgs, *result; if (files_module != NULL) { pFunc = PyObject_GetAttrString(files_module, "setLangPair"); if (pFunc) { pArgs = PyTuple_New(4); PyTuple_SetItem(pArgs, 0, PyUnicode_FromString(direction)); PyTuple_SetItem(pArgs, 1, PyUnicode_FromString(user)); PyTuple_SetItem(pArgs, 2, PyBytes_FromString(source)); PyTuple_SetItem(pArgs, 3, PyBytes_FromString(target)); result = PyObject_CallObject(pFunc, pArgs); if(result != NULL){ if(result == Py_True){ Py_XDECREF(result); Py_XDECREF(pFunc); Py_XDECREF(pArgs); return 1; } else{ Py_XDECREF(result); Py_XDECREF(pFunc); Py_XDECREF(pArgs); return 0; } } else{ Py_XDECREF(pFunc); Py_XDECREF(pArgs); return 0; } } else{ return 0; } } else { notify_error("Module: \'apertiumFiles\' is not loaded"); return 0; } }
PyObject* newItem(PyTypeObject* enumType, long itemValue, const char* itemName) { bool newValue = true; SbkEnumObject* enumObj; if (!itemName) { enumObj = reinterpret_cast<SbkEnumObject*>(getEnumItemFromValue(enumType, itemValue)); if (enumObj) return reinterpret_cast<PyObject*>(enumObj); newValue = false; } enumObj = PyObject_New(SbkEnumObject, enumType); if (!enumObj) return 0; enumObj->ob_name = itemName ? PyBytes_FromString(itemName) : 0; enumObj->ob_value = itemValue; if (newValue) { PyObject* values = PyDict_GetItemString(enumType->tp_dict, const_cast<char*>("values")); if (!values) { values = PyDict_New(); PyDict_SetItemString(enumType->tp_dict, const_cast<char*>("values"), values); Py_DECREF(values); // ^ values still alive, because setitemstring incref it } PyDict_SetItemString(values, itemName, reinterpret_cast<PyObject*>(enumObj)); } return reinterpret_cast<PyObject*>(enumObj); }
static PyObject * HunSpell_generate2(HunSpell * self, PyObject *args) { char *word1, *desc, **slist; int i, num_slist, ret; PyObject *slist_list, *pystr; if (!PyArg_ParseTuple(args, "etet", self->encoding, &word1, self->encoding, &desc)) return NULL; slist_list = PyList_New(0); if (!slist_list) { return NULL; } num_slist = self->handle->generate(&slist, word1, &desc, 1); PyMem_Free(word1); PyMem_Free(desc); for (i = 0, ret = 0; !ret && i < num_slist; i++) { pystr = PyBytes_FromString(slist[i]); if (!pystr) break; ret = PyList_Append(slist_list, pystr); Py_DECREF(pystr); } self->handle->free_list(&slist, num_slist); return slist_list; }
/** Convert a Python decimal.Decimal to MySQL DECIMAL. Convert a Python decimal.Decimal to MySQL DECIMAL. This function also removes the 'L' suffix from the resulting string when using Python v2. @param obj PyObject to be converted @return Converted decimal as string @retval PyBytes Python v3 @retval PyString Python v2 */ PyObject* pytomy_decimal(PyObject *obj) { #ifdef PY3 return PyBytes_FromString((const char *)PyUnicode_1BYTE_DATA( PyObject_Str(obj))); #else PyObject *numeric, *new_num; int tmp_size; char *tmp; numeric= PyObject_Str(obj); tmp= PyString_AsString(numeric); tmp_size= (int)PyString_Size(numeric); if (tmp[tmp_size - 1] == 'L') { new_num= PyString_FromStringAndSize(tmp, tmp_size); _PyString_Resize(&new_num, tmp_size - 1); return new_num; } else { return numeric; } #endif }
// ------------------------------------------ PyObject *LinkServiceBinder::flavorToName(PyObject *self, PyObject *args) { __PYTHON_EXCEPTION_GUARD_BEGIN_; PyObject *result = NULL; LinkServicePtr o; if (!python_cast<LinkServicePtr>(self, &msType, &o)) __PY_CONVERR_RET; int id; if (PyArg_ParseTuple(args, "i", &id)) { const std::string &res = o->flavorToName(id); #ifdef IS_PY3K result = PyBytes_FromString(res.c_str()); #else result = PyString_FromString(res.c_str()); #endif return result; } else { // Invalid parameters PyErr_SetString(PyExc_TypeError, "Expected an integer argument!"); return NULL; } __PYTHON_EXCEPTION_GUARD_END_; }
PyObject *Vcf_writeFile( PyObject *self, PyObject *args ){ char *filename; PyObject *cards; FILE * fptr; Py_ssize_t i, j, max, nprops; PyObject * ccard/*, * cprop, *name, *parval, *partype, *value*/; VcFile * wfile; Vcard * crrtC; // VcStatus status; // int m, n; if(!PyArg_ParseTuple( args, "sO", &filename, &cards)){ return NULL; } fptr = fopen(filename, "w"); if(fptr == NULL){ char * errmsg = strerror(errno); char msg[25+strlen(errmsg)]; sprintf(msg, "Cannot open file due to %s", errmsg); return PyBytes_FromString(msg); } wfile = calloc(1, sizeof(VcFile)); max = PyList_Size(cards); wfile->ncards = max; wfile->cardp = calloc(max, sizeof(Vcard*)); for(i = 0; i < max; i++){ ccard = PyList_GetItem(cards, i); nprops = PyList_Size(ccard); crrtC = calloc(nprops, sizeof(Vcard)+sizeof(VcProp)); crrtC->nprops = nprops; // printf("This is what crrtC = %p", crrtC); for(j = 0; j < nprops; j++){ //grab python object PyArg_ParseTuple(PyList_GetItem(ccard, j), "isss", &(crrtC->prop[j].name), &(crrtC->prop[j].partype), &(crrtC->prop[j].parval), &(crrtC->prop[j].value)); }//end of j loop wfile->cardp[i] = crrtC; }//end of i loop // printf("This is the number of cards: %d\n", wfile->ncards); // for(m = 0; m < wfile->ncards; m++){ // printf("Is this even f*****g working?\n"); // crrtC = wfile->cardp[m]; // printf("This is card #%d\n", m+1); // for(n = 0; n < crrtC->nprops; n++){ // VcProp crrtP = crrtC->prop[n]; // printf("Prop #%d: VcPName= %d Partype = %s, Parval = %s, Value = %s\n", n, crrtP.name, crrtP.partype, crrtP.parval, crrtP.value); // } // } //status = writeVcFile(fptr, wfile); //freeVcFile(wfile); return PyLong_FromLong(1); //generateError(status); }//end of wrapper function for writeVcFile
PyObject *pylzma_decompressobj_compat(PyObject *self, PyObject *args) { CCompatDecompressionObject *result=NULL; if (!PyArg_ParseTuple(args, "")) goto exit; result = PyObject_New(CCompatDecompressionObject, &CompatDecompressionObject_Type); CHECK_NULL(result); result->unconsumed_tail = NULL; result->unconsumed_length = 0; result->unused_data = PyBytes_FromString(""); if (result->unused_data == NULL) { PyErr_NoMemory(); PyObject_Del(result); result = NULL; goto exit; } memset(&result->stream, 0, sizeof(result->stream)); lzmaCompatInit(&result->stream); exit: return (PyObject *)result; }
static PyObject* parse_h_aliases(struct hostent *h) { char **pch; PyObject *result = NULL; PyObject *tmp; result = PyList_New(0); if (result && h->h_aliases) { for (pch = h->h_aliases; *pch != NULL; pch++) { if (*pch != h->h_name && strcmp(*pch, h->h_name)) { int status; tmp = PyBytes_FromString(*pch); if (tmp == NULL) { break; } status = PyList_Append(result, tmp); Py_DECREF(tmp); if (status) { break; } } } } return result; }
static PyObject * Pipe_func_getsockname(Pipe *self) { #ifdef _WIN32 /* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */ char buf[MAX_PATH * 4]; #else char buf[PATH_MAX]; #endif size_t buf_len; int err; RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL); RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL); buf_len = sizeof(buf); err = uv_pipe_getsockname(&self->pipe_h, buf, &buf_len); if (err < 0) { RAISE_UV_EXCEPTION(err, PyExc_PipeError); return NULL; } if (buf_len == 0) { return PyBytes_FromString(""); } else if (buf[0] == '\0') { /* Linux abstract namespace */ return PyBytes_FromStringAndSize(buf, buf_len); } else { return PyBytes_FromStringAndSize(buf, buf_len-1); } }
static PyObject * generateError(VcStatus result){ char msg[100]; switch(result.code){ case SYNTAX: sprintf(msg, "Syntax error on lines %d-%d", result.linefrom, result.lineto); break; case PAROVER: sprintf(msg, "Parameter overflow on lines %d-%d", result.linefrom, result.lineto); break; case BEGEND: sprintf(msg, "Invalid BEGIN or END on lines %d-%d", result.linefrom, result.lineto); break; case BADVER: sprintf(msg, "Bad version on lines %d-%d", result.linefrom, result.lineto); break; case NOPNFN: sprintf(msg, "Missing formatted or name on lines %d-%d", result.linefrom, result.lineto); break; case NOPVER: sprintf(msg, "Missing version on lines %d-%d", result.linefrom, result.lineto); break; case OK: sprintf(msg, "OK"); break; case IOERR: sprintf(msg, "IO error on lines %d-%d", result.linefrom, result.lineto); break; }//end of switch return PyBytes_FromString(msg); }//en d of generateError
//---------------------------------------------------------------------------- // Return set(dict.keys()).issubset(set([vkeys, ...]) // keys of dict must be string or unicode in Py2 and string in Py3! // Parameters: // dict - the Python dictionary object to be checked // vkeys - a null-terminated list of keys (char *) //---------------------------------------------------------------------------- int checkDictKeys(PyObject *dict, const char *vkeys, ...) { int i, j, rc; PyObject *dkeys = PyDict_Keys(dict); // = dict.keys() if (!dkeys) return 0; // no valid dictionary j = PySequence_Size(dkeys); // len(dict.keys()) PyObject *validkeys = PyList_New(0); // make list of valid keys va_list ap; // def var arg list va_start(ap, vkeys); // start of args while (vkeys != 0) // reached end yet? { // build list of valid keys to check against #if PY_MAJOR_VERSION < 3 PyList_Append(validkeys, PyBytes_FromString(vkeys)); // Python 2 #else PyList_Append(validkeys, PyUnicode_FromString(vkeys)); // python 3 #endif vkeys = va_arg(ap, const char *); // get next char string } va_end(ap); // end the var args loop rc = 1; // prepare for success for (i = 0; i < j; i++) { // loop through dictionary keys if (!PySequence_Contains(validkeys, PySequence_GetItem(dkeys, i))) { rc = 0; break; } } Py_DECREF(validkeys); Py_DECREF(dkeys); return rc; }
/** * @brief Translates a given text * * pythonInit() must have been called before or an error will occur (the module is not loaded) * @param text String containing the text to be translated * @param source String containing the source language to translate the text from * @param target String containing the target language to translate the text to * @return A string containing the translated text if the call was successful, or NULL otherwise */ char* translate(char* text, char* source, char* target){ char* translation; PyObject *pFunc, *pArgs, *pArg, *result; if (iface_module != NULL) { pFunc = PyObject_GetAttrString(iface_module, "translate"); if (pFunc) { pArgs = PyTuple_New(3); pArg = PyBytes_FromString(text); PyTuple_SetItem(pArgs, 0, pArg); PyTuple_SetItem(pArgs, 1, PyBytes_FromString(source)); PyTuple_SetItem(pArgs, 2, PyBytes_FromString(target)); result = PyObject_CallObject(pFunc, pArgs); if (result != NULL) { Py_XDECREF(pFunc); if(PyDict_GetItemString(result,"ok") == Py_True){ translation = PyBytes_AsString(PyDict_GetItemString(result,"result")); Py_XDECREF(result); return translation; } else{ notify_error(PyBytes_AsString(PyDict_GetItemString(result,"errorMsg"))); return NULL; } } else { Py_XDECREF(pFunc); notify_error("There was an error in the translate call"); return NULL; } } else { return NULL; } } else { notify_error("Module: \'apertiumInterfaceAPY\' is not loaded"); return NULL; } }
void py_leak() { PyObject *pObj = NULL; /* Object creation, ref count = 1. */ pObj = PyBytes_FromString("Hello world\n"); PyObject_Print(pObj, stdout, 0); /* Object still has ref count = 1. */ }
// returns py string object DLL_EXPORT PyObject * serializeModel( void *modelPtr ) { std::string str; ((BoosterModel *)modelPtr)->serializeToString( &str ); return PyBytes_FromString( str.c_str() ); }
// Collator.sort_key {{{ static PyObject * icu_Collator_sort_key(icu_Collator *self, PyObject *args, PyObject *kwargs) { char *input; int32_t sz; UChar *buf; uint8_t *buf2; PyObject *ans; int32_t key_size; UErrorCode status = U_ZERO_ERROR; if (!PyArg_ParseTuple(args, "es", "UTF-8", &input)) return NULL; sz = (int32_t)strlen(input); buf = (UChar*)calloc(sz*4 + 1, sizeof(UChar)); if (buf == NULL) return PyErr_NoMemory(); u_strFromUTF8(buf, sz*4 + 1, &key_size, input, sz, &status); PyMem_Free(input); if (U_SUCCESS(status)) { buf2 = (uint8_t*)calloc(7*sz+1, sizeof(uint8_t)); if (buf2 == NULL) return PyErr_NoMemory(); key_size = ucol_getSortKey(self->collator, buf, -1, buf2, 7*sz+1); if (key_size == 0) { ans = PyBytes_FromString(""); } else { if (key_size >= 7*sz+1) { free(buf2); buf2 = (uint8_t*)calloc(key_size+1, sizeof(uint8_t)); if (buf2 == NULL) return PyErr_NoMemory(); ucol_getSortKey(self->collator, buf, -1, buf2, key_size+1); } ans = PyBytes_FromString((char *)buf2); } free(buf2); } else ans = PyBytes_FromString(""); free(buf); if (ans == NULL) return PyErr_NoMemory(); return ans; } // }}}
PyObject* fromCString(const char* value) { #ifdef IS_PY3K return PyUnicode_FromString(value); #else return PyBytes_FromString(value); #endif }
static PyObject * TagData_getepc(TagData *self, void *closure) { char epcStr[128]; TMR_bytesToHex(self->tag.epc, self->tag.epcByteCount, epcStr); return PyBytes_FromString(epcStr); }
static PyObject * pylog_repr(PyLogObject *s) { char buf[512]; PyOS_snprintf(buf, sizeof(buf), "<Log object ... TODO>"); return PyBytes_FromString(buf); }
PyObject * Diff_patch(Diff *self) { PyObject *patch = PyBytes_FromString(""); git_diff_print_patch(self->diff, &patch, &diff_print_cb); return patch; }
/* after code that pyrex generates */ void _ctypes_add_traceback(char *funcname, char *filename, int lineno) { PyObject *py_srcfile = 0; PyObject *py_funcname = 0; PyObject *py_globals = 0; PyObject *empty_tuple = 0; PyObject *empty_string = 0; PyCodeObject *py_code = 0; PyFrameObject *py_frame = 0; py_srcfile = PyUnicode_DecodeFSDefault(filename); if (!py_srcfile) goto bad; py_funcname = PyUnicode_FromString(funcname); if (!py_funcname) goto bad; py_globals = PyDict_New(); if (!py_globals) goto bad; empty_tuple = PyTuple_New(0); if (!empty_tuple) goto bad; empty_string = PyBytes_FromString(""); if (!empty_string) goto bad; py_code = PyCode_New( 0, /*int argcount,*/ 0, /*int kwonlyargcount,*/ 0, /*int nlocals,*/ 0, /*int stacksize,*/ 0, /*int flags,*/ empty_string, /*PyObject *code,*/ empty_tuple, /*PyObject *consts,*/ empty_tuple, /*PyObject *names,*/ empty_tuple, /*PyObject *varnames,*/ empty_tuple, /*PyObject *freevars,*/ empty_tuple, /*PyObject *cellvars,*/ py_srcfile, /*PyObject *filename,*/ py_funcname, /*PyObject *name,*/ lineno, /*int firstlineno,*/ empty_string /*PyObject *lnotab*/ ); if (!py_code) goto bad; py_frame = PyFrame_New( PyThreadState_Get(), /*PyThreadState *tstate,*/ py_code, /*PyCodeObject *code,*/ py_globals, /*PyObject *globals,*/ 0 /*PyObject *locals*/ ); if (!py_frame) goto bad; py_frame->f_lineno = lineno; PyTraceBack_Here(py_frame); bad: Py_XDECREF(py_globals); Py_XDECREF(py_srcfile); Py_XDECREF(py_funcname); Py_XDECREF(empty_tuple); Py_XDECREF(empty_string); Py_XDECREF(py_code); Py_XDECREF(py_frame); }
PyObject *qlz_d_str(PyObject *self) { char buf[100]; sprintf(buf, "QLZStateDecompress(%ld)", (long)self); #if PY_MAJOR_VERSION >= 3 return PyBytes_FromString(buf); #else return PyString_FromString(buf); #endif }
static void _stream_handler(ConstFSEventStreamRef stream, void *info, size_t numEvents, void *eventPaths, const FSEventStreamEventFlags eventFlags[], const FSEventStreamEventId eventIds[]) { const char **paths = eventPaths; streamobject *object = info; PyObject *result = NULL, *str = NULL, *num = NULL; assert(numEvents <= PY_SSIZE_T_MAX); PyGILState_STATE gil_state = PyGILState_Ensure(); PyThreadState *thread_state = PyThreadState_Swap(object->state); /* Convert event data to Python objects */ PyObject *event_paths = PyList_New(numEvents); if (!event_paths) { goto final; } PyObject *event_flags = PyList_New(numEvents); if (!event_flags) { goto final; } for (size_t i = 0; i < numEvents; i++) { str = PyBytes_FromString(paths[i]); #if PY_MAJOR_VERSION >= 3 num = PyLong_FromLong(eventFlags[i]); #else num = PyInt_FromLong(eventFlags[i]); #endif if ((!num) || (!str)) { goto final; } PyList_SET_ITEM(event_paths, i, str); PyList_SET_ITEM(event_flags, i, num); } str = NULL; num = NULL; if ( (result = PyObject_CallFunctionObjArgs( object->callback, event_paths, event_flags, NULL)) == NULL) { /* May can return NULL if an exception is raised */ if (!PyErr_Occurred()) PyErr_SetString(PyExc_ValueError, callback_error_msg); /* Stop listening */ CFRunLoopStop(object->loop); } final:
static PyObject *tc_BDB_errmsg(PyTypeObject *type, PyObject *args, PyObject *keywds) { log_trace("ENTER"); int ecode; static char *kwlist[] = {"ecode", NULL}; if (!PyArg_ParseTupleAndKeywords(args, keywds, "i:errmsg", kwlist, &ecode)) { return NULL; } return PyBytes_FromString(tcbdberrmsg(ecode)); }
static PyObject *pkgGetSection(rpmSpecPkg pkg, int section) { char *sect = rpmSpecPkgGetSection(pkg, section); if (sect != NULL) { PyObject *ps = PyBytes_FromString(sect); free(sect); if (ps != NULL) return ps; } Py_RETURN_NONE; }
void py_access_after_free() { PyObject *pObj = NULL; /* Object creation, ref count = 1. */ pObj = PyBytes_FromString("Hello world\n"); PyObject_Print(pObj, stdout, 0); /* ref count = 0 so object deallocated. */ Py_DECREF(pObj); /* Now use pObj... */ PyObject_Print(pObj, stdout, 0); }
/* string format should never fail but do regular repr just in case it does */ static PyObject *rpmtd_str(rpmtdObject *s) { PyObject *res = NULL; char *str = rpmtdFormat(&(s->td), RPMTD_FORMAT_STRING, NULL); if (str) { res = PyBytes_FromString(str); free(str); } else { res = PyObject_Repr((PyObject *)s); } return res; }