本文整理汇总了C++中PyBytes_FromString函数的典型用法代码示例。如果您正苦于以下问题:C++ PyBytes_FromString函数的具体用法?C++ PyBytes_FromString怎么用?C++ PyBytes_FromString使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PyBytes_FromString函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: pygrpc_status_code
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;
}
示例2: init_local_module
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;
}
示例3: PyTuple_Pack
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;
}
}
示例4: pairExists
/**
* @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;
}
}
示例5: dictionarySetUserEntry
/**
* @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;
}
}
示例6: newItem
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);
}
示例7: HunSpell_generate2
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;
}
示例8: pytomy_decimal
/**
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
}
示例9: PyBytes_FromString
// ------------------------------------------
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_;
}
示例10: fopen
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 fucking 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
示例11: PyObject_New
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;
}
示例12: parse_h_aliases
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;
}
示例13: Pipe_func_getsockname
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);
}
}
示例14: generateError
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
示例15: checkDictKeys
//----------------------------------------------------------------------------
// 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;
}