6.11. C Modules

6.11.1. Code

Przykład kodu w C wykorzystującego C modules:

#include <Python.h>

/* Implementation */

static PyObject* say_hello(PyObject* self, PyObject* args) {
    const char* name;

    if (!PyArg_ParseTuple(args, "s", &name))
        return NULL;

    printf("Hello %s!\n", name);
    Py_RETURN_NONE;
}

static PyObject* version(PyObject* self) {
    return Py_BuildValue("s", "Version 1.0");
}


/* Python Binding Definitions */

static PyMethodDef HelloMethods[] = {
     {"say_hello", say_hello, METH_VARARGS, "Greet somebody."},
     {"version", (PyCFunction)version, METH_NOARGS, "returns the version"},
     {NULL, NULL, 0, NULL}
};

static struct PyModuleDef hello = {
    PyModuleDef_HEAD_INIT,
    "hello",                        /* name of module */
    "",                                     /* module documentation, may be NULL */
    -1,                                     /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
    HelloMethods
};

PyMODINIT_FUNC PyInit_hello(void) {
    return PyModule_Create(&hello);
}

6.11.2. Compile

setup.py:

>>> import sys
>>> from distutils.core import setup, Extension
>>>
>>> print('Building for Python 3')
Building for Python 3
>>> module = Extension('hello', sources = ['mylib-cmodules.c'])
>>>
>>> setup(
...     name = 'hello',
...     version='1.0',
...     description = 'Ehlo World!',
...     ext_modules = [module])  

Execute:

$ python setup.py build
$ cd build/lib*
$ python

Run:

>>> import hello  
>>>
>>>
>>> hello.say_hello('José Jiménez')  
José Jiménez

6.11.3. Assignments

Code 6.23. Solution
"""
* Assignment: C Modules
* Complexity: medium
* Lines of code: 10 lines
* Time: 13 min

English:
    TODO: English Translation
    X. Run doctests - all must succeed

Polish:
    1. Wykorzystując C Modules wypisz datę i czas, za pomocą funkcji zdefiniowanej w C `<time.h>`
    2. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    TODO: Doctests
"""