Skip to content

Commit 9840bc7

Browse files
committed
added stuff for the Extensions class
1 parent 6ab6989 commit 9840bc7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+3843
-23
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <Python.h>
2+
3+
static PyObject *
4+
add(PyObject *self, PyObject *args)
5+
{
6+
int x;
7+
int y;
8+
int sts;
9+
10+
if (!PyArg_ParseTuple(args, "ii", &x, &y))
11+
return NULL;
12+
sts = x+y;
13+
return Py_BuildValue("i", sts);
14+
}
15+
16+
// Module's method table and initialization function
17+
// see: https://docs.python.org/2/extending/extending.html#the-module-s-method-table-and-initialization-function
18+
static PyMethodDef AddMethods[] = {
19+
{"add", add, METH_VARARGS, "add two numbers"},
20+
{NULL, NULL, 0, NULL} // sentinel
21+
};
22+
23+
24+
PyMODINIT_FUNC
25+
initadd(void) {
26+
// Module's initialization function
27+
// Will be called again if you use Python's reload()
28+
(void) Py_InitModule("add", AddMethods);
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <Python.h>
2+
3+
static PyObject *
4+
divide(PyObject *self, PyObject *args)
5+
{
6+
double x;
7+
double y;
8+
double sts;
9+
10+
if (!PyArg_ParseTuple(args, "dd", &x, &y))
11+
return NULL;
12+
sts = x/y;
13+
return Py_BuildValue("d", sts);
14+
}
15+
16+
// Module's method table and initialization function
17+
// See: http://docs.python.org/extending/extending.html#the-module-s-method-table-and-initialization-function
18+
static PyMethodDef DivideMethods[] = {
19+
{"divide", divide, METH_VARARGS, "divide two numbers"},
20+
{NULL, NULL, 0, NULL}
21+
};
22+
23+
PyMODINIT_FUNC // does the right thing on Windows, Linux, etc.
24+
initdivide(void) {
25+
// Module's initialization function
26+
// Will be called again if you use Python's reload()
27+
28+
Py_InitModule("divide", DivideMethods);
29+
}
30+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python
2+
3+
from distutils.core import setup, Extension
4+
5+
setup(
6+
name='Cdiv',
7+
version='1.0',
8+
description='sample method that does exceptions',
9+
ext_modules=[Extension('divide', sources=['divide.c'])],
10+
)
11+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env python
2+
3+
from setuptools import setup, Extension
4+
5+
setup(
6+
name='Cadd',
7+
version='1.0',
8+
description='simple c extension for an example',
9+
ext_modules=[Extension('add', sources=['add.c'])],
10+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
simple test for the add C extension
5+
"""
6+
7+
import pytest
8+
9+
import add
10+
11+
def test_basic():
12+
assert add.add(3,4) == 7
13+
14+
def test_negative():
15+
assert add.add(-12, 5) == -7
16+
17+
def test_float():
18+
with pytest.raises(TypeError):
19+
add.add(3, 4.0)
20+
21+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
all: add; gcc -shared -o add.so add.c
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <stdio.h>
2+
3+
int add(int x, int y) {
4+
return x+y;
5+
}
6+
int main(void) {
7+
int w = 3;
8+
int q = 2;
9+
printf("%d + %d = %d\n\n", w, q, add(w,q));
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python
2+
3+
from ctypes import *
4+
5+
libc = CDLL("libc.dylib")
6+
7+
i = c_int()
8+
f = c_float()
9+
s = create_string_buffer('\000' * 32)
10+
print i.value, f.value, repr(s.value)
11+
libc.sscanf("1 3.14 Hello", "%d %f %s",byref(i), byref(f), s)
12+
print i.value, f.value, repr(s.value)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
3+
from ctypes import *
4+
5+
libc = CDLL("libc.dylib")
6+
7+
TenInts = c_int * 10
8+
data = TenInts(*range(10,0,-1))
9+
10+
# http://www.gnu.org/software/libc/manual/html_node/Array-Sort-Function.html
11+
# void qsort (void *array, size_t count, size_t size, comparison_fn_t compare)
12+
13+
# http://docs.python.org/2/library/ctypes.html#ctypes.CFUNCTYPE
14+
CMPFUNC = CFUNCTYPE(c_int, POINTER(c_int), POINTER(c_int))
15+
16+
def py_cmp_func(a, b):
17+
# print "py_cmp_func", a[0], b[0]
18+
return a[0] - b[0]
19+
20+
for i in data:
21+
print i
22+
23+
libc.qsort(data, len(data), sizeof(c_int), CMPFUNC(py_cmp_func))
24+
25+
for i in data:
26+
print i
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python
2+
3+
import sys
4+
5+
lib_ext = ('.so' if ('darwin' in sys.platform or
6+
'linux' in sys.platform)
7+
else ".dll")
8+
9+
import ctypes
10+
11+
# now load our own shared library
12+
add = ctypes.cdll.LoadLibrary("add"+lib_ext)
13+
14+
print "This should be 7:",
15+
print add.add(3,4)
16+
17+
# Using an already-loaded shared library:
18+
## loading libc and libm (math)
19+
if 'darwin' in sys.platform:
20+
# OS-X likes full paths...
21+
libc = ctypes.CDLL("/usr/lib/libc.dylib")
22+
libm = ctypes.CDLL("/usr/lib/libm.dylib")
23+
elif 'linux' in sys.platform:
24+
# linux uses the shared lib search path
25+
libc = ctypes.CDLL("libc.so")
26+
libm = ctypes.CDLL("libm.so")
27+
elif 'win' in sys.platform:
28+
# Windows can find already loaded dlls by name
29+
libc = ctypes.cdll.msvcrt # lib c and libm are in msvcrt
30+
libm = ctypes.cdll.msvcrt
31+
32+
libc.printf("printed via libc printf()\n")
33+
34+
print "passing different types to printf:"
35+
#libc.printf("An int %d, a double %f\n", 1234, 3.14)
36+
libc.printf("An int %d, a double %f\n", 1234, ctypes.c_double(3.14))
37+
38+
39+
## Calling libm
40+
## prototype for pow():
41+
## double pow ( double x, double y )
42+
43+
print "This should be 81.0:",
44+
print libm.pow(ctypes.c_double(3), ctypes.c_double(4))
45+
46+
## need to set the return type!
47+
libm.pow.restype = ctypes.c_double
48+
print "This should be 81.0:",
49+
print libm.pow(ctypes.c_double(3), ctypes.c_double(4))
50+
51+
## if you are going to call the same function a lot,
52+
## you can specify the arument types:
53+
54+
libm.pow.restype = ctypes.c_double
55+
libm.pow.argtypes = [ctypes.c_double, ctypes.c_double]
56+
print "This should be 81.0:",
57+
print libm.pow(3, 4.0)
58+
59+
## much easier!
60+
61+
62+
63+
64+
65+
66+
67+

0 commit comments

Comments
 (0)