Skip to content

Commit 37b1894

Browse files
committed
Initial commit
0 parents  commit 37b1894

7 files changed

+138
-0
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*~
2+
3+
*.o
4+
*.so
5+
6+
__pycache__/
7+
*.pyc
8+
*.pyo

Makefile

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Declaration of variables
2+
CC = gcc
3+
CC_FLAGS = -shared -fPIC -I/usr/include/python2.7 \
4+
-Wall -Wextra -Wno-unused-parameter -O2 -g
5+
6+
# File names
7+
SOURCES = $(wildcard *.c)
8+
OBJECTS = $(SOURCES:module.c=.so)
9+
10+
# Main target
11+
all: $(OBJECTS)
12+
13+
# Object files
14+
%.so: %module.c
15+
$(CC) $(CC_FLAGS) $< -o $@
16+
17+
run: $(OBJECTS)
18+
python -c "import fib; print fib.fib(5)"
19+
python -c "import hello; hello.greet('Foobar')"
20+
21+
# To remove generated files
22+
clean:
23+
rm -f *.so
24+
25+
.PHONY: all run clean

Readme.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Python/C API quick start
2+
=========================
3+
4+
A quick example of Python 2.7 modules implemented in C using the Python/C API.
5+
6+
```
7+
make all
8+
python -c "import hello; hello.say_hello('World')"
9+
python -c "import fib; print fib.fib(5)"
10+
make clean
11+
```
12+
13+
Note that the best way to distribute a Python module is to use a `setup.py`
14+
file. The Makefile is here only to debunk Python's magic.
15+
16+
17+
## Steps
18+
19+
1. Write a `foobarmodule.c` C file.
20+
Note the `#include <Python.h>` at the beginning of example files.
21+
2. Compile `foobarmodule.c` and produce a `foobar.so` file.
22+
See the Makefile for details.
23+
3. Put `foobar.so` in a folder listed in the Python path.
24+
4. Load the module from Python with `import foobar`.

equivalent_fib.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
def fib(n):
3+
"Calculate the Fibonacci numbers (in Python)."
4+
if n <= 1:
5+
return n
6+
else
7+
return fib(n-1) + fib(n-2)

equivalent_hello.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
def greet(name):
3+
"Greet somebody (in Python)."
4+
print "Hello", name

fibmodule.c

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <Python.h>
2+
3+
int _fib(int n)
4+
{
5+
printf("Debug: calling _fib(%d)\n", n);
6+
if (n <= 1)
7+
return n;
8+
else
9+
return _fib(n-1) + _fib(n-2);
10+
}
11+
12+
/* Wrapped _fib function */
13+
static PyObject* fib(PyObject* self, PyObject* args)
14+
{
15+
int n;
16+
17+
/* Parse the input, from Python integer to C int */
18+
if (!PyArg_ParseTuple(args, "i", &n))
19+
return NULL;
20+
/* If the above function returns -1, an appropriate Python exception will
21+
* have been set, and the function simply returns NULL
22+
*/
23+
24+
/* Construct the result: a Python integer object */
25+
return Py_BuildValue("i", _fib(n));
26+
}
27+
28+
/* Define functions in module */
29+
static PyMethodDef FibMethods[] = {
30+
{"fib", fib, METH_VARARGS, "Calculate the Fibonacci numbers (in C)."},
31+
{NULL, NULL, 0, NULL} /* Sentinel */
32+
};
33+
34+
/* Module initialization */
35+
PyMODINIT_FUNC
36+
initfib(void)
37+
{
38+
(void) Py_InitModule("fib", FibMethods);
39+
}

hellomodule.c

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <Python.h>
2+
3+
static PyObject* greet(PyObject* self, PyObject* args)
4+
{
5+
const char* name;
6+
7+
/* Parse the input, from Python string to C string */
8+
if (!PyArg_ParseTuple(args, "s", &name))
9+
return NULL;
10+
/* If the above function returns -1, an appropriate Python exception will
11+
* have been set, and the function simply returns NULL
12+
*/
13+
14+
printf("Hello %s\n", name);
15+
16+
/* Returns a None Python object */
17+
Py_RETURN_NONE;
18+
}
19+
20+
/* Define functions in module */
21+
static PyMethodDef HelloMethods[] = {
22+
{"greet", greet, METH_VARARGS, "Greet somebody (in C)."},
23+
{NULL, NULL, 0, NULL} /* Sentinel */
24+
};
25+
26+
/* Module initialization */
27+
PyMODINIT_FUNC
28+
inithello(void)
29+
{
30+
(void) Py_InitModule("hello", HelloMethods);
31+
}

0 commit comments

Comments
 (0)