Article Index

 Page 96

example.c

This is the combined code from the chapter - the json files are as for example given ealier

#define PY_SSIZE_T_CLEAN

#include <Python.h>

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

{

     PyObject *myObject;

     if (!PyArg_ParseTuple(args, "O", &myObject))

          return NULL;

     PyObject *attribName = Py_BuildValue("s", "myAttribute1");

     PyObject *myAtt1 = PyObject_GetAttr(myObject, attribName);

     Py_XDECREF(attribName);

     return myAtt1;

}

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

{

     PyObject *myObject;

     PyObject *myString;

     if (!PyArg_ParseTuple(args, "OO", &myObject, &myString))

          return NULL;

     PyObject *myAtt1 = PyObject_GetAttr(myObject, myString);

     if (PyUnicode_Check(myAtt1))

     {

          const char *mytext = PyUnicode_AsUTF8(myAtt1);

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

     }

     if (PyLong_Check(myAtt1))

     {

          int myValue = PyLong_AsLong(myAtt1);

          printf("%d\n", myValue);

     }

     return myAtt1;

}

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

{

     PyObject *myObject;

     if (!PyArg_ParseTuple(args, "O", &myObject))

          return NULL;

     PyObject *newValue = PyLong_FromLong(42);

     if (PyObject_SetAttrString(myObject, "myAttribute1", newValue))

          return NULL;

     Py_RETURN_NONE;

}

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

{

     PyObject *myObject;

     if (!PyArg_ParseTuple(args, "O", &myObject))

          return NULL;

     if (PyObject_HasAttrString(myObject, "myAttribute1"))

     {

          PyObject *myAtt1 = PyObject_GetAttrString(myObject, "myAttribute1");

          if (PyLong_Check(myAtt1))

          {

               int myValue = PyLong_AsLong(myAtt1);

               printf("%d\n", myValue);

          }

     }

     Py_RETURN_NONE;

}

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

{

  PyObject *myObject;

  if (!PyArg_ParseTuple(args, "O", &myObject))

    return NULL;

  PyObject *main = PyImport_AddModule("__main__");

  PyObject *maindict = PyModule_GetDict(main);

  PyObject *myclass = PyDict_GetItemString(maindict, "myClass");

  if (PyObject_IsInstance(myObject, myclass))

  {

    PyObject *myAtt1 = PyObject_GetAttrString(myObject, "myAttribute1");

    if (PyLong_Check(myAtt1))

    {

      int myValue = PyLong_AsLong(myAtt1);

      printf("%d\n", myValue);

    }

    Py_XDECREF(myAtt1);

  }

  Py_RETURN_NONE;

}

static PyMethodDef AddMethods[] = {

    {"Attributes1", Attributes1, METH_VARARGS, "an example"},

    {"displayAttributes", displayAttributes, METH_VARARGS, "an example"},

    {"Attributes2", Attributes2, METH_VARARGS, "an example"},

    {"Attributes3", Attributes3, METH_VARARGS, "an example"},

    {"Attributes4", Attributes4, METH_VARARGS, "an example"},

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

};

static struct PyModuleDef addmodule = {

    PyModuleDef_HEAD_INIT,

    "example",

    "C library to test API",

    -1,

    AddMethods};

PyMODINIT_FUNC PyInit_example(void)

{

     return PyModule_Create(&addmodule);

}

test.py

import example

class myClass:

    myAttribute1=10

    myAttribute2="Brian"

myInstance=myClass()

print(example.Attributes1(myInstance))

value=example.displayAttributes(myInstance,"myAttribute1")

print(value)

value=example.displayAttributes(myInstance,"myAttribute2")

print(value)

example.Attributes2(myInstance)

print(myInstance.myAttribute1)

example.Attributes3(myInstance)

example.Attributes4(myInstance)