Article Index

 

Page 157

example.c

#define PY_SSIZE_T_CLEAN

#include <Python.h>

static PyObject *exampleFunction(PyObject *self, PyObject *args)

{

     // example function code

}

static PyMethodDef AddMethods[] = {

    {"exampleFunction", exampleFunction, METH_VARARGS, "an example"},

    {NULL, NULL, 0, NULL} // sentinel

};

static PyObject *myFunc(PyObject *self, PyObject *args)

{

     printf("hello world\n");

     Py_RETURN_NONE;

}

static PyObject *myFunc2(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)

{

     for (int i = 0; i < nargs; i++)

     {

          const char *myString = PyUnicode_AsUTF8(args[i]);

          printf("%s\n", myString);

     }

     Py_ssize_t nKwds = PyTuple_Size(kwnames);

     for (int i = 0; i < nKwds; i++)

     {

          const char *myValue = PyUnicode_AsUTF8(args[i + nargs]);

          const char *myKeyword = PyUnicode_AsUTF8(PyTuple_GetItem(kwnames, i));

          printf("Keyword = %s, Value = %s \n", myKeyword, myValue);

     }

     Py_RETURN_NONE;

}

static PyMethodDef myFunc_def = {

    "myFunc",

    myFunc,

    METH_VARARGS,

    "the doc string"};

static PyMethodDef myFunc2_def = {

    "myFunc2",

    (PyCFunction)myFunc2,

    METH_FASTCALL | METH_KEYWORDS,

    "the doc string"};

static struct PyModuleDef myModule = {

    PyModuleDef_HEAD_INIT,

    "example",

    "C library to test API",

    -1,

    NULL};

PyMODINIT_FUNC PyInit_example(void)

{

     PyObject *m = PyModule_Create(&myModule);

     if (m == NULL)

          return NULL;

     // Add a int

     PyObject *myValue = PyLong_FromLong(42);

     PyModule_AddObject(m, "myValue", myValue);

     // Add a list

     PyObject *myList = PyList_New(0);

     PyModule_AddObject(m, "myList", myList);

     // Add a function

     PyObject *myPyFun = PyCFunction_New(&myFunc_def, m);

     PyModule_AddObject(m, "myFunc1", myPyFun);

     // Add a fastcall function

     PyObject *myPyFun2 = PyCFunction_New(&myFunc2_def, m);

     PyModule_AddObject(m, "myFunc2", myPyFun2);

     //Add a function in another module

     PyObject *math=PyImport_ImportModule("math");

     PyObject *mathdict = PyModule_GetDict(math);

     PyObject *myFunction = PyDict_GetItemString(mathdict, "sqrt");

     PyModule_AddObject(m, "myFunc3", myFunction);

     return m;

}

test.py

import example
print("module constant",example.myValue)
example.myValue=43
print("module constant",example.myValue)
print(example.myList)
example.myList.append("spam")
print(example.myList)
print(example.myFunc1())
example.myFunc2("Hello","World",MyKeyWord="myValue")
print(example.myFunc3(2))
print(example.myFunc3.__name__)

 

 

Page 160

example.c

#define PY_SSIZE_T_CLEAN

#include <Python.h>

int exec(PyObject *m)

{

  PyObject *myValue = PyLong_FromLong(42);

  PyModule_AddObject(m, "myValue", myValue);

  return 0;

}

PyObject *create(PyObject *spec, PyModuleDef *def)

{

  PyObject *res=PyObject_GetAttrString(spec,"origin");

  printf("%s\n",PyUnicode_AsUTF8(res));

  PyObject *m= PyModule_New("example");

  return m;

}

PyModuleDef_Slot twoPhase[] = {

    {Py_mod_create, create},

    {Py_mod_exec, exec},

    {0, 0}};

static struct PyModuleDef myModuledef = {

    PyModuleDef_HEAD_INIT,

    "example",

    "C library to test API",

    0,

    NULL,

    twoPhase};

PyMODINIT_FUNC PyInit_example(void)

{

  return PyModuleDef_Init(&myModuledef);

}

test.py

import example
print(example.myValue)

Page 164

 example.c

#define PY_SSIZE_T_CLEAN

#include <Python.h>

struct myData

{

  int count;

  char name[20];

};

static struct myData myModuleData = {42, "spam"};

static PyObject *myFunc(PyObject *self, PyObject *args)

{

  printf("module data %d , %s\n", myModuleData.count, myModuleData.name);

  myModuleData.count++;

  struct myData *myModData = (struct myData *)PyModule_GetState(self);

  printf("module data %d , %s\n", myModData->count, myModData->name);

  myModData->count++;

  Py_RETURN_NONE;

}

static PyMethodDef myFunc_def = {

    "myFunc",

    myFunc,

    METH_VARARGS,

    "the doc string"};

int exec(PyObject *m)

{

  struct myData *myModData = (struct myData *)PyModule_GetState(m);

  myModData->count = 42;

  strcpy(myModData->name, "spam");

  PyObject *myValue = PyLong_FromLong(42);

  PyModule_AddObject(m, "myValue", myValue);

  PyObject *myPyFun = PyCFunction_New(&myFunc_def, m);

  PyModule_AddObject(m, "myFunc", myPyFun);

  return 0;

}

PyObject *create(PyObject *spec, PyModuleDef *def)

{

  PyObject *res = PyObject_GetAttrString(spec, "origin");

  printf("%s\n", PyUnicode_AsUTF8(res));

  PyObject *m = PyModule_New("example");  

  return m;

}



void freeModule(void *m){

    struct myData *myModData = (struct myData *)PyModule_GetState(m);

    PyMem_Free(myModData);

}

PyModuleDef_Slot twoPhase[] = {

    {Py_mod_create, create},

    {Py_mod_exec, exec},

    {0, 0}};

static struct PyModuleDef myModuledef = {

    PyModuleDef_HEAD_INIT,

    "example",

    "C library to test API",

    sizeof(struct myData),

    NULL,

    twoPhase,

    NULL,

    NULL,

    freeModule};

PyMODINIT_FUNC PyInit_example(void)

{

  return PyModuleDef_Init(&myModuledef);

}

test.py

import example
example.myFunc()
example.myFunc()