Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 26th, 2012  |  syntax: None  |  size: 1.69 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Calling C   extensions in Python
  2. #include <Python.h>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. static PyObject *SpamError;
  7.  
  8. static PyObject *spam_system(PyObject *self, PyObject *args)
  9. {
  10.     const char *command;
  11.     int sts;
  12.  
  13.     if (!PyArg_ParseTuple(args, "s", &command))
  14.         return NULL;
  15.     sts = system(command);
  16.     return PyLong_FromLong(sts);
  17. }
  18.  
  19. static PyMethodDef SpamMethods[] = {
  20.     {"system",  spam_system, METH_VARARGS,
  21.      "Execute a shell command."},
  22.     {NULL, NULL, 0, NULL}        /* Sentinel */
  23. };
  24.  
  25. static struct PyModuleDef spammodule = {
  26.    PyModuleDef_HEAD_INIT,
  27.    "spam",   /* name of module */
  28.    NULL,     /* module documentation, may be NULL */
  29.    -1,       /* size of per-interpreter state of the module,
  30.                 or -1 if the module keeps state in global variables. */
  31.    SpamMethods
  32. };
  33.  
  34.  
  35.  
  36. PyMODINIT_FUNC
  37. PyInit_spam(void)
  38. {
  39.     PyObject *m;
  40.  
  41.     m = PyModule_Create(&spammodule);
  42.     if (m == NULL)
  43.         return NULL;
  44.  
  45.     SpamError = PyErr_NewException("spam.error", NULL, NULL);
  46.     Py_INCREF(SpamError);
  47.     PyModule_AddObject(m, "error", SpamError);
  48.     return m;
  49. }
  50.  
  51. int main(int argc, wchar_t *argv[])
  52. {
  53.     // Add a builtin module, before Py_Initialize
  54.     PyImport_AppendInittab("spam", PyInit_spam);
  55.  
  56.     // Pass argv[0] to the Python Interpreter
  57.     Py_SetProgramName(argv[0]);
  58.  
  59.     // Initialise the Python interpreter
  60.     Py_Initialize();
  61.  
  62.     // Import module
  63.     PyImport_ImportModule("spam");
  64.  
  65.     cout << "test" << endl;
  66.     system("PAUSE");
  67. }
  68.        
  69. #include <boost/python.hpp>
  70.  
  71. BOOST_PYTHON_MODULE(my_module)
  72. {
  73.     boost::python::def("function_name", function, boost::python::args("start", "length", "offset", "boundry", "byte", "data", "variable" ), "docstring");
  74. }