Page 148
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 *bytes1(PyObject *self, PyObject *args)
{
char *myString = "Hello World";
PyObject *myBytes = PyBytes_FromString(myString);
return myBytes;
}
static PyObject *bytes2(PyObject *self, PyObject *args)
{
PyObject *myBytes;
if (!PyArg_ParseTuple(args, "O", &myBytes))
return NULL;
Py_ssize_t len = PyBytes_Size(myBytes);
for (int i = 1; i < len; i++)
{
PyObject *item = PySequence_GetItem(myBytes, i);
char c = (char)PyLong_AsLong(item);
printf("%X ", c);
}
printf("\n");
PyObject *myByteArray = PyByteArray_FromStringAndSize(PyBytes_AsString(myBytes), PyBytes_Size(myBytes));
char *HelloMessage = "Hello World";
PyObject *insert = PyByteArray_FromStringAndSize(HelloMessage, strlen(HelloMessage));
PySequence_SetSlice(myByteArray, 3, 7, insert);
return myByteArray;
}
static PyObject *string1(PyObject *self, PyObject *args)
{
PyObject *uni1 = PyUnicode_New(10, 255);
PyUnicode_Fill(uni1, 0, 10, 120);
PyUnicode_WriteChar(uni1, 0, 72);
return uni1;
}
static PyObject *string2(PyObject *self, PyObject *args)
{
PyObject *myString = PyUnicode_FromString("Spam, Spam, Spam, Spam… Lovely Spam! Wonderful Spam!");
int len = PyUnicode_GET_LENGTH(myString);
Py_UCS4 *buf = PyMem_Malloc(sizeof(Py_UCS4) * (len + 1));
PyUnicode_AsUCS4(myString, buf, 100, 1);
for (int i = 0; buf[i]; i++)
{
printf("%04X ", buf[i]);
}
PyMem_Free(buf);
printf("\n\n");
buf = PyUnicode_AsUCS4Copy(myString);
for (int i = 0; buf[i]; i++)
{
printf("%04X ", buf[i]);
}
printf("\n\n");
PyMem_Free(buf);
Py_DECREF(myString);
Py_RETURN_NONE;
}
static PyObject *string3(PyObject *self, PyObject *args)
{
PyObject *myString = PyUnicode_FromString("Spam, Spam, Spam, Spam… Lovely Spam! Wonderful Spam!");
int len = PyUnicode_GET_LENGTH(myString);
PyObject *substr = PyUnicode_FromString("Lovely");
Py_ssize_t pos = PyUnicode_Find(myString, substr, 0, len, 1);
printf("position of Lovely = %ld\n", pos);
pos = PyUnicode_FindChar(myString, 0x0000006D, 0, len, 1);
printf("position of first m %ld\n", pos);
substr = PyUnicode_FromString("Spam");
Py_ssize_t result = PyUnicode_Tailmatch(myString, substr, 0, len, -1);
printf("prefix match %ld\n", result);
substr = PyUnicode_FromString("Spam");
result = PyUnicode_Contains(myString, substr);
printf("Contains Spam %ld\n", result);
Py_ssize_t count = PyUnicode_Count(myString, substr, 0, len);
printf("Count of how many spam(s) %ld\n", count);
PyObject *test = PyUnicode_FromString("Spam");
result = PyUnicode_Compare(substr, test);
printf("compare to Spam %ld\n", result);
result = PyUnicode_CompareWithASCIIString(substr, "Spam");
printf("compare to Spam %ld\n", result);
PyObject *logic = PyUnicode_RichCompare(substr, test, Py_EQ);
if (Py_IsTrue(logic))
{
printf("Strings are equal\n");
}
else
{
printf("Strings are not equal\n");
}
Py_RETURN_NONE;
}
static PyObject *string4(PyObject *self, PyObject *args)
{
PyObject *myString = PyUnicode_FromString("Spam, Spam, Spam, Spam . . .Lovely Spam! Wonderful Spam!");
PyObject *bytesUTF8 = PyUnicode_AsUTF8String(myString);
PyObject *bytesUTF16 = PyUnicode_AsUTF16String(myString);
PyObject *bytesUTF32 = PyUnicode_AsUTF32String(myString);
Py_ssize_t len = PyBytes_Size(bytesUTF32);
for (int i = 0; i < len; i++)
{
PyObject *item = PySequence_GetItem(bytesUTF32, i);
int c = PyLong_AsLong(item);
printf("%02X ", c);
}
printf("\n");
char *buffer;
PyBytes_AsStringAndSize(bytesUTF32, &buffer, &len);
PyObject *decodeStr = PyUnicode_DecodeUTF32(buffer, len, NULL, NULL);
return decodeStr;
}
static PyObject *string5(PyObject *self, PyObject *args)
{
PyObject *myString = PyUnicode_New(1, 1114111);
PyUnicode_WriteChar(myString, 0, 0x2020);
PyObject *myBytes = PyUnicode_AsEncodedString(myString, "cp1252", NULL);
char *buffer;
Py_ssize_t len;
PyBytes_AsStringAndSize(myBytes, &buffer, &len);
printf("%X \n", buffer[0]);
printf("%s \n", buffer);
Py_DECREF(myBytes);
return myString;
}
static PyObject *string6(PyObject *self, PyObject *args)
{
PyObject *myString = PyUnicode_FromString("† Spam, Spam, Spam, Spam . . .Lovely Spam! Wonderful Spam!");
PyObject *myBytes = PyUnicode_AsEncodedString(myString, "raw_unicode_escape", NULL);
Py_DECREF(myString);
return myBytes;
}
static PyMethodDef AddMethods[] = {
{"bytes1", bytes1, METH_VARARGS, "an example"},
{"bytes2", bytes2, METH_VARARGS, "an example"},
{"string1", string1, METH_VARARGS, "an example"},
{"string2", string2, METH_VARARGS, "an example"},
{"string3", string3, METH_VARARGS, "an example"},
{"string4", string4, METH_VARARGS, "an example"},
{"string5", string5, METH_VARARGS, "an example"},
{"string6", string6, 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
mybytes=example.bytes1()
print(mybytes)
print()mybytes=example.bytes2(b"My long message")
print(mybytes)print(example.string1())
print()
example.string2()
print()
example.string3()
print()
print(example.string4())print()
result=example.string5()
print(result)
print()
result=example.string6()
print(result)