Skip to content

Instantly share code, notes, and snippets.

@w32zhong
Created April 22, 2016 02:24
Show Gist options
  • Save w32zhong/3eda9468d13036ecd9ac51fbf17adfa3 to your computer and use it in GitHub Desktop.
Save w32zhong/3eda9468d13036ecd9ac51fbf17adfa3 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <Python.h>
#include <string.h>
/////////////////////////
//pFunc = PyObject_GetAttrString(pModule, "myjieba_p");
//pArgs = PyTuple_New(1);
//pValue = PyUnicode_FromString(txt);
//PyTuple_SetItem(pArgs, 0, pValue);
//PyObject_CallObject(pFunc, pArgs);
////////////////////////
void foreach_pyobj(PyObject*);
int main(int argc, char *argv[])
{
const char txt[] = "其实,工信处女干事microsoft每月经过下属办公室";
PyObject *pName, *pModule, *pFunc;
PyObject *pValue, *pArgs, *pRes;
Py_Initialize();
if (!Py_IsInitialized()) {
printf("bad init\n");
return 1;
}
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
pName = PyUnicode_DecodeFSDefault("myjieba");
pModule = PyImport_Import(pName);
/* decrement pName reference counts by one, basically free() it */
Py_DECREF(pName);
if (pModule == NULL) {
printf("no module\n");
goto py_finalize;
}
pFunc = PyObject_GetAttrString(pModule, "myjieba_init");
if (pFunc == NULL)
printf("no function\n");
if (PyCallable_Check(pFunc))
printf("callable.\n");
PyObject_CallObject(pFunc, NULL);
Py_DECREF(pFunc);
pFunc = PyObject_GetAttrString(pModule, "myjieba_tokenize");
if (!pFunc)
printf("no function\n");
if (PyCallable_Check(pFunc))
printf("callable too.\n");
pArgs = PyTuple_New(1);
pValue = PyUnicode_FromStringAndSize(txt, strlen(txt));
if (pValue==NULL)
printf("value bad\n");
PyTuple_SetItem(pArgs, 0, pValue);
pRes = PyObject_CallObject(pFunc, pArgs);
//Py_XDECREF(pArgs); // do not uncomment this
// Py_XDECREF(pValue);
// Py_XDECREF(pFunc);
//
// pFunc = PyObject_GetAttrString(pModule, "myjieba_print");
// if (!pFunc)
// printf("nooooooooooooo\n");
//
// pArgs2 = PyTuple_New(1);
// PyTuple_SetItem(pArgs2, 0, pRes);
// PyObject_CallObject(pFunc, pArgs2);
//
PyObject *iterator = PyObject_GetIter(pRes);
PyObject *item;
if (iterator == NULL)
printf("bad it\n");
while (NULL != (item = PyIter_Next(iterator))) {
printf("hello\n");
if (!PyTuple_Check(item))
printf("is not tuple!\n");
// Py_ssize_t sz = PySequence_Length(item);
// printf("size=%ld\n", sz);
PyObject *pword = PySequence_GetItem(item, 0);
PyObject *pbegin = PySequence_GetItem(item, 1);
PyObject *pend = PySequence_GetItem(item, 2);
PyObject *ptype = PySequence_GetItem(item, 3);
char *word = PyUnicode_AsUTF8(pword);
long int begin = PyLong_AsLong(pbegin);
long int end = PyLong_AsLong(pend);
char *type = PyUnicode_AsUTF8(ptype);
printf("%s: <%ld, %ld> type: `%s'\n", word, begin, end, type);
//foreach_pyobj(item);
Py_DECREF(item);
}
Py_DECREF(iterator);
//
Py_XDECREF(pArgs);
Py_XDECREF(pRes);
Py_XDECREF(pFunc);
Py_DECREF(pModule);
py_finalize:
Py_Finalize();
return 0;
}
void foreach_pyobj(PyObject *seq)
{
PyObject *iterator = PyObject_GetIter(seq);
PyObject *item;
if (iterator == NULL)
printf("sub: bad it\n");
while (NULL != (item = PyIter_Next(iterator))) {
printf("world\n");
Py_DECREF(item);
}
Py_DECREF(iterator);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment