Permalink
Browse files

Initial version

  • Loading branch information...
0 parents commit c4fa206b9579bb739a1cf9ceb7980b47a9b9c5e2 @williamstein williamstein committed Jul 28, 2007
Oops, something went wrong.
@@ -0,0 +1,11 @@
+The original Pyrex code as of 2006-04 is licensed under the following
+license: "Copyright stuff: Pyrex is free of restrictions. You may use,
+redistribute, modify and distribute modified versions."
+
+------------------
+
+Cython, which derives from Pyrex, is licensed under the Python
+Software Foundation License. More precisely, all modifications
+made to go from Pyrex to Cython are so licensed.
+
+
@@ -0,0 +1,15 @@
+all:
+ python Setup.py build_ext --inplace
+
+test: all
+ python run_primes.py 20
+ python run_numeric_demo.py
+ python run_spam.py
+ cd callback; $(MAKE) test
+
+clean:
+ @echo Cleaning Demos
+ @rm -f *.c *.o *.so *~ core
+ @rm -rf build
+ @cd callback; $(MAKE) clean
+ @cd embed; $(MAKE) clean
@@ -0,0 +1,21 @@
+PYHOME = $(HOME)/pkg/python/version
+PYINCLUDE = \
+ -I$(PYHOME)/include/python2.2 \
+ -I$(PYHOME)/$(ARCH)/include/python2.2
+
+%.c: %.pyx
+ ../bin/pyrexc $<
+
+%.o: %.c
+ gcc -c -fPIC $(PYINCLUDE) $<
+
+%.so: %.o
+ gcc -shared $< -lm -o $@
+
+all: primes.so spam.so numeric_demo.so
+
+clean:
+ @echo Cleaning Demos
+ @rm -f *.c *.o *.so *~ core core.*
+ @cd callback; $(MAKE) clean
+ @cd embed; $(MAKE) clean
@@ -0,0 +1,13 @@
+from distutils.core import setup
+from distutils.extension import Extension
+from Pyrex.Distutils import build_ext
+
+setup(
+ name = 'Demos',
+ ext_modules=[
+ Extension("primes", ["primes.pyx"]),
+ Extension("spam", ["spam.pyx"]),
+ Extension("numeric_demo", ["numeric_demo.pyx"]),
+ ],
+ cmdclass = {'build_ext': build_ext}
+)
@@ -0,0 +1,10 @@
+all:
+ python Setup.py build_ext --inplace
+
+test: all
+ python run_cheese.py
+
+clean:
+ @echo Cleaning Demos/callback
+ @rm -f cheese.c *.o *.so *~ core
+ @rm -rf build
@@ -0,0 +1,19 @@
+PYHOME = $(HOME)/pkg/python/version
+PYINCLUDE = \
+ -I$(PYHOME)/include/python2.2 \
+ -I$(PYHOME)/$(ARCH)/include/python2.2
+
+%.c: %.pyx
+ ../../bin/pyrexc $<
+
+%.o: %.c
+ gcc -c -fPIC $(PYINCLUDE) $<
+
+%.so: %.o
+ gcc -shared $< -lm -o $@
+
+all: cheese.so
+
+clean:
+ @echo Cleaning Demos/callback
+ @rm -f *.c *.o *.so *~ core core.*
@@ -0,0 +1 @@
+This example demonstrates how you can wrap a C API that has a callback interface, so that you can pass Python functions to it as callbacks. The files cheesefinder.h and cheesefinder.c represent the C library to be wrapped. The file cheese.pyx is the Pyrex module which wraps it. The file run_cheese.py demonstrates how to call the wrapper.
@@ -0,0 +1,11 @@
+from distutils.core import setup
+from distutils.extension import Extension
+from Pyrex.Distutils import build_ext
+
+setup(
+ name = 'callback',
+ ext_modules=[
+ Extension("cheese", ["cheese.pyx", "cheesefinder.c"]),
+ ],
+ cmdclass = {'build_ext': build_ext}
+)
@@ -0,0 +1,13 @@
+#
+# Pyrex wrapper for the cheesefinder API
+#
+
+cdef extern from "cheesefinder.h":
+ ctypedef void (*cheesefunc)(char *name, void *user_data)
+ void find_cheeses(cheesefunc user_func, void *user_data)
+
+def find(f):
+ find_cheeses(callback, <void*>f)
+
+cdef void callback(char *name, void *f):
+ (<object>f)(name)
@@ -0,0 +1,21 @@
+/*
+ * An example of a C API that provides a callback mechanism.
+ */
+
+#include "cheesefinder.h"
+
+static char *cheeses[] = {
+ "cheddar",
+ "camembert",
+ "that runny one",
+ 0
+};
+
+void find_cheeses(cheesefunc user_func, void *user_data) {
+ char **p = cheeses;
+ while (*p) {
+ user_func(*p, user_data);
+ ++p;
+ }
+}
+
@@ -0,0 +1 @@
+typedef void (*cheesefunc)(char *name, void *user_data);
@@ -0,0 +1,7 @@
+import cheese
+
+def report_cheese(name):
+ print "Found cheese:", name
+
+cheese.find(report_cheese)
+
@@ -0,0 +1,30 @@
+PYVERSION = 2.2
+PYHOME = $(HOME)/pkg/python/$(PYVERSION)
+PYARCH = $(PYHOME)/$(ARCH)
+PYINCLUDE = \
+ -I$(PYHOME)/include/python$(PYVERSION) \
+ -I$(PYARCH)/include/python$(PYVERSION)
+PYLIB = -L$(PYARCH)/lib/python$(PYVERSION)/config \
+ -lpython$(PYVERSION) \
+ -ldl -lpthread -lutil -lm
+
+%.c: %.pyx
+ ../../bin/pyrexc $<
+
+%.o: %.c
+ gcc -c -fPIC $(PYINCLUDE) $<
+
+#%.so: %.o
+# gcc -shared $< -lm -o $@
+
+all: main
+
+main: main.o embedded.o
+ gcc main.o embedded.o $(PYLIB) -o main
+
+clean:
+ @echo Cleaning Demos/embed
+ @rm -f *~ *.o *.so core core.* embedded.h embedded.c main
+
+embedded.h: embedded.c
+main.o: embedded.h
@@ -0,0 +1,35 @@
+# Makefile for Microsoft C Compiler, building a DLL
+PYVERSION = 2.2
+PYHOME = \Python$(PYVERSION:.=)
+PYINCLUDE = -I$(PYHOME)\include
+PYLIB = /LIBPATH:$(PYHOME)\libs
+
+CFLAGS = $(PYINCLUDE) /Ox /W3 /GX -nologo
+.SUFFIXES: .exe .dll .obj .c .cpp .pyx
+
+.pyx.c:
+ $(PYHOME)\Python.exe ../../pyrexc.py $<
+
+all: main.exe
+
+clean:
+ del /Q/F *.obj embedded.h embedded.c main.exe embedded.dll embedded.lib embedded.exp
+
+# When linking the DLL we must explicitly list all of the exports
+# There doesn't seem to be an easy way to get DL_EXPORT to have the correct definition
+# to do the export for us without breaking the importing of symbols from the core
+# python library.
+embedded.dll: embedded.obj
+ link /nologo /DLL /INCREMENTAL:NO $(PYLIB) $** /IMPLIB:$*.lib /DEF:<< /OUT:$*.dll
+EXPORTS initembedded
+EXPORTS spam
+<<
+
+main.exe: main.obj embedded.lib
+ link /nologo $** $(PYLIB) /OUT:main.exe
+
+embedded.h: embedded.c
+main.obj: embedded.h
+embedded.obj: embedded.c
+ $(CC) /MD $(CFLAGS) -c $**
+embedded.lib: embedded.dll
@@ -0,0 +1 @@
+# Makefile for Microsoft compiler statically linking PYVERSION = 2.2 PYHOME = \Python$(PYVERSION:.=) PYINCLUDE = -I$(PYHOME)\include PYLIB = /LIBPATH:$(PYHOME)\libs python22.lib CFLAGS = $(PYINCLUDE) /Ox /W3 /GX -nologo .SUFFIXES: .exe .dll .obj .c .cpp .pyx .pyx.c: $(PYHOME)\Python.exe ../../pyrexc.py $< all: main.exe clean: -del /Q/F *.obj embedded.h embedded.c main.exe main.exe: main.obj embedded.obj link /nologo $** $(PYLIB) /OUT:main.exe embedded.h: embedded.c main.obj: embedded.h
@@ -0,0 +1,30 @@
+PYVERSION = 2.2
+PYHOME = $(HOME)/pkg/python/$(PYVERSION)
+PYARCH = $(PYHOME)/$(ARCH)
+PYINCLUDE = \
+ -I$(PYHOME)/include/python$(PYVERSION) \
+ -I$(PYARCH)/include/python$(PYVERSION)
+PYLIB = -L$(PYARCH)/lib/python$(PYVERSION)/config \
+ -lpython$(PYVERSION) \
+ -ldl -lpthread -lutil -lm
+
+%.c: %.pyx
+ ../../bin/pyrexc $<
+
+%.o: %.c
+ gcc -c -fPIC $(PYINCLUDE) $<
+
+#%.so: %.o
+# gcc -shared $< -lm -o $@
+
+all: main
+
+main: main.o embedded.o
+ gcc main.o embedded.o $(PYLIB) -o main
+
+clean:
+ @echo Cleaning Demos/embed
+ @rm -f *~ *.o *.so core core.* embedded.h embedded.c main
+
+embedded.h: embedded.c
+main.o: embedded.h
@@ -0,0 +1 @@
+This example demonstrates how Pyrex-generated code can be called directly from a main program written in C. In this example, the module's initialisation function (called "initembedded", since the module is called "embedded") is called explicitly. This is necessary because the module is not being imported using the normal Python import mechanism. The Windows makefiles were contributed by Duncan Booth <Duncan.Booth@SuttonCourtenay.org.uk>.
@@ -0,0 +1,5 @@
+cdef public void spam():
+ praise()
+
+def praise():
+ print "Spam, glorious spam!"
@@ -0,0 +1,9 @@
+#include "Python.h"
+#include "embedded.h"
+
+int main(int argc, char *argv) {
+ Py_Initialize();
+ initembedded();
+ spam();
+ Py_Finalize();
+}
@@ -0,0 +1,39 @@
+#
+# This example demonstrates how to access the internals
+# of a Numeric array object.
+#
+
+cdef extern from "Numeric/arrayobject.h":
+
+ struct PyArray_Descr:
+ int type_num, elsize
+ char type
+
+ ctypedef class Numeric.ArrayType [object PyArrayObject]:
+ cdef char *data
+ cdef int nd
+ cdef int *dimensions, *strides
+ cdef object base
+ cdef PyArray_Descr *descr
+ cdef int flags
+
+def print_2d_array(ArrayType a):
+ print "Type:", chr(a.descr.type)
+ if chr(a.descr.type) <> "f":
+ raise TypeError("Float array required")
+ if a.nd <> 2:
+ raise ValueError("2 dimensional array required")
+ cdef int nrows, ncols
+ cdef float *elems, x
+ nrows = a.dimensions[0]
+ ncols = a.dimensions[1]
+ elems = <float *>a.data
+ hyphen = "-"
+ divider = ("+" + 10 * hyphen) * ncols + "+"
+ print divider
+ for row in range(nrows):
+ for col in range(ncols):
+ x = elems[row * ncols + col]
+ print "| %8f" % x,
+ print "|"
+ print divider
@@ -0,0 +1,18 @@
+def primes(int kmax):
+ cdef int n, k, i
+ cdef int p[1000]
+ result = []
+ if kmax > 1000:
+ kmax = 1000
+ k = 0
+ n = 2
+ while k < kmax:
+ i = 0
+ while i < k and n % p[i] <> 0:
+ i = i + 1
+ if i == k:
+ p[k] = n
+ k = k + 1
+ result.append(n)
+ n = n + 1
+ return result
@@ -0,0 +1,13 @@
+def primes(kmax):
+ p = []
+ k = 0
+ n = 2
+ while k < kmax:
+ i = 0
+ while i < k and n % p[i] <> 0:
+ i = i + 1
+ if i == k:
+ p.append(n)
+ k = k + 1
+ n = n + 1
+ return p
@@ -0,0 +1,5 @@
+import Numeric
+import numeric_demo
+
+a = Numeric.array([[1.0, 3.5, 8.4], [2.3, 6.6, 4.1]], "f")
+numeric_demo.print_2d_array(a)
@@ -0,0 +1,7 @@
+import sys
+from primes import primes
+if len(sys.argv) >= 2:
+ n = int(sys.argv[1])
+else:
+ n = 1000
+print primes(n)
@@ -0,0 +1,8 @@
+from spam import Spam
+
+s = Spam()
+print "Created:", s
+s.set_amount(42)
+print "Amount =", s.get_amount()
+s.describe()
+s = None
@@ -0,0 +1,22 @@
+#
+# Example of an extension type.
+#
+
+cdef class Spam:
+
+ cdef int amount
+
+ def __new__(self):
+ self.amount = 0
+
+ def __dealloc__(self):
+ print self.amount, "tons of spam is history."
+
+ def get_amount(self):
+ return self.amount
+
+ def set_amount(self, new_amount):
+ self.amount = new_amount
+
+ def describe(self):
+ print self.amount, "tons of spam!"
@@ -0,0 +1 @@
+<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
Oops, something went wrong.

0 comments on commit c4fa206

Please sign in to comment.