So, I have a repo to build a python C extension laid out as follows:
setup.py
demo.c
MANIFEST.in
The contents of the C file are:
#include <Python.h>
static PyObject* print_message(PyObject* self, PyObject* args)
{
const char* str_arg;
if(!PyArg_ParseTuple(args, "s", &str_arg)) {
puts("Could not parse the python arg!");
return NULL;
}
#ifdef USE_PRINTER
printf("printer %s\n", str_arg);
#else
printf("msg %s\n", str_arg);
#endif
// This can also be done with Py_RETURN_NONE
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef myMethods[] = {
{ "print_message", print_message, METH_VARARGS, "Prints a called string" },
{ NULL, NULL, 0, NULL }
};
// Our Module Definition struct
static struct PyModuleDef myModule = {
PyModuleDef_HEAD_INIT,
"DemoPackage",
"A demo module for python c extensions",
-1,
myMethods
};
// Initializes our module using our above struct
PyMODINIT_FUNC PyInit_DemoPackage(void)
{
return PyModule_Create(&myModule);
}
In my setup.py, I have the following code:
from distutils.core import setup, Extension
module1 = Extension('DemoPackage',
define_macros = [('USE_PRINTER', '1')],
include_dirs = ['include'],
sources = ['src/demo.c'])
setup (name = 'DemoPackage',
version = '1.0',
description = 'This is a demo package',
author = '<first> <last>',
author_email = '[email protected]',
url = 'https://docs.python.org/extending/building',
long_description = open('README.md').read(),
ext_modules = [module1])
My question here is, if I can build and install the package with the commands:
$ python setup.py build $ python setup.py install
How do I incorporate or write a unit test in the scenario of a C extension? I am looking for the unit test to be run in conjunction with setup.py, similarly to how a test would work for cmake.