Article Index

 

Page 72

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 *stringRaw(PyObject *self, PyObject *args)

{

     char *name;

     if (!PyArg_ParseTuple(args, "s", &name))

          return NULL;

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

     for (int i = 0; name[i]; i++)

     {

          printf("%02X ", (unsigned char)name[i]);

     }

     printf("\n");

     Py_RETURN_NONE;

}

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

{

     char *name;

     Py_ssize_t len;

     if (!PyArg_ParseTuple(args, "s#", &name, &len))

          return NULL;

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

     printf("%d\n", (int)len);

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

     {

          printf("%02X ", (unsigned char)name[i]);

     }

     printf("\n");

     Py_RETURN_NONE;

}

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

{

     Py_buffer myBuf;

     if (!PyArg_ParseTuple(args, "s*", &myBuf))

          return NULL;

     char *myString = (char *)myBuf.buf;

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

     for (int i = 0; i < myBuf.len; i++)

     {

          printf("%02X ", (unsigned char)myString[i]);

     }

     printf("\n");

     PyBuffer_Release(&myBuf);

     Py_RETURN_NONE;

}

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

{

     char *name = NULL;

     if (!PyArg_ParseTuple(args, "es", "cp1252", &name))

          return NULL;

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

     for (int i = 0; name[i]; i++)

     {

          printf("%02X ", (unsigned char)name[i]);

     }

     printf("\n");

     PyMem_Free(name);

     Py_RETURN_NONE;

}

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

{

     char *name = NULL;

     Py_ssize_t len;

     if (!PyArg_ParseTuple(args, "es#", "utf-16", &name, &len))

          return NULL;

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

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

     {

          printf("%02X ", (unsigned char)name[i]);

     }

     printf("\n");

     PyMem_Free(name);

     Py_RETURN_NONE;

}

static PyObject *stringEncodeAllocate(PyObject *self,

                                      PyObject *args)

{

     Py_ssize_t len = 25;

     char *name = malloc(sizeof(char) * (len + 1));

     if (!PyArg_ParseTuple(args, "es#", "cp1252", &name, &len))

          return NULL;

     printf("%d\n", (int)len);

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

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

     {

          printf("%02X ", (unsigned char)name[i]);

     }

     printf("\n");

     free(name);

     Py_RETURN_NONE;

}

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

{

     int number;

     double x, y;

     char *name = NULL;

     if (!PyArg_ParseTuple(args, "(idds)", &number, &x, &y, &name))

          return NULL;

     printf("%d %f %f %s \n", number, x, y, name);

     Py_RETURN_NONE;

}

static PyMethodDef AddMethods[] = {

    {"stringRaw", stringRaw, METH_VARARGS, "an example"},

    {"stringRawLen", stringRawLen, METH_VARARGS, "an example"},

    {"stringPBuffer", stringPBuffer, METH_VARARGS, "an example"},

    {"stringEncode", stringEncode, METH_VARARGS, "an example"},

    {"stringEncode2", stringEncode2, METH_VARARGS, "an example"},

    {"stringEncodeAllocate", stringEncodeAllocate, METH_VARARGS, "an example"},

    {"sequenceElements", sequenceElements, 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

example.stringRaw("Hello World")

example.stringRawLen("Hello \0 World")

example.stringPBuffer("Hello \0 World")

example.stringEncode("Hello World")

example.stringEncode("\u2020")

example.stringEncode2("Hello World")

example.stringEncodeAllocate("Hello World")

example.sequenceElements((2,1.2,1.3,"test"))