pax_global_header 0000666 0000000 0000000 00000000064 13770401327 0014516 g ustar 00root root 0000000 0000000 52 comment=c329db8fb45dd09b3079eb90c45ac48907e49047
cython-c329db8fb45dd09b3079eb90c45ac48907e49047-Demos-freeze/ 0000775 0000000 0000000 00000000000 13770401327 0022305 5 ustar 00root root 0000000 0000000 cython-c329db8fb45dd09b3079eb90c45ac48907e49047-Demos-freeze/Demos/ 0000775 0000000 0000000 00000000000 13770401327 0023354 5 ustar 00root root 0000000 0000000 cython-c329db8fb45dd09b3079eb90c45ac48907e49047-Demos-freeze/Demos/freeze/ 0000775 0000000 0000000 00000000000 13770401327 0024634 5 ustar 00root root 0000000 0000000 cython-c329db8fb45dd09b3079eb90c45ac48907e49047-Demos-freeze/Demos/freeze/Makefile 0000664 0000000 0000000 00000003117 13770401327 0026276 0 ustar 00root root 0000000 0000000 CC = gcc
CYTHON = ../../bin/cython
CYTHON_FREEZE = ../../bin/cython_freeze
PYTHON = python
RST2HTML = rst2html
PY_LDFLAGS = $(shell $(PYTHON) -c 'from distutils.sysconfig import get_config_var as g; import sys; sys.stdout.write(" ".join([g("LINKFORSHARED"), "-L"+g("LIBPL")]) + "\n")')
PY_CPPFLAGS = $(shell $(PYTHON) -c 'from distutils.sysconfig import *; import sys; sys.stdout.write("-I"+get_python_inc() + "\n")')
LIBDIR1 := $(shell $(PYTHON) -c "from distutils import sysconfig; print(sysconfig.get_config_var('LIBDIR'))")
LIBDIR2 := $(shell $(PYTHON) -c "from distutils import sysconfig; print(sysconfig.get_config_var('LIBPL'))")
PYLIB := $(shell $(PYTHON) -c "from distutils import sysconfig; print(sysconfig.get_config_var('LIBRARY')[3:-2])")
LIBS := $(shell $(PYTHON) -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_var('LIBS'))")
CFLAGS = -fPIC -fno-strict-aliasing -g -O2 -Wall -Wextra
CPPFLAGS = $(PY_CPPFLAGS)
LDFLAGS = $(PY_LDFLAGS)
LDLIBS = -L$(LIBDIR1) -L$(LIBDIR2) -l$(PYLIB)
# Name of executable
TARGETS = nCr python
# List of Cython source files, with main module first.
CYTHON_SOURCE = combinatorics.pyx lcmath.pyx
CYTHON_SECONDARY = $(CYTHON_SOURCE:.pyx=.c) $(TARGETS:=.c)
all : $(TARGETS)
html : README.html
$(TARGETS) : % : %.o $(CYTHON_SOURCE:.pyx=.o)
nCr.c :
$(CYTHON_FREEZE) $(CYTHON_SOURCE:.pyx=) > $@
python.c :
$(CYTHON_FREEZE) --pymain $(CYTHON_SOURCE:.pyx=) > $@
%.c : %.pyx
$(CYTHON) $(CYTHONFLAGS) $^
%.html : %.txt
$(RST2HTML) $^ $@
clean:
$(RM) *.o $(CYTHON_SECONDARY) $(TARGETS) README.html
.PHONY: clean
.SECONDARY: $(CYTHON_SECONDARY)
cython-c329db8fb45dd09b3079eb90c45ac48907e49047-Demos-freeze/Demos/freeze/README.rst 0000664 0000000 0000000 00000007043 13770401327 0026327 0 ustar 00root root 0000000 0000000 NAME
====
**cython_freeze** - create a C file for embedding Cython modules
SYNOPSIS
========
::
cython_freeze [-o outfile] [-p] module [...]
DESCRIPTION
===========
**cython_freeze** generates a C source file to embed a Python interpreter
with one or more Cython modules built in. This allows one to create a single
executable from Cython code, without having to have separate shared objects
for each Cython module. A major advantage of this approach is that it allows
debugging with gprof(1), which does not work with shared objects.
Unless ``-p`` is given, the first module's ``__name__`` is set to
``"__main__"`` and is imported on startup; if ``-p`` is given, a normal Python
interpreter is built, with the given modules built into the binary.
Note that this method differs from ``cython --embed``. The ``--embed`` options
modifies the resulting C source file to include a ``main()`` function, so it
can only be used on a single Cython module. The advantage ``--embed`` is
simplicity. This module, on the other hand, can be used with multiple
modules, but it requires another C source file to be created.
OPTIONS
=======
::
-o FILE, --outfile=FILE write output to FILE instead of standard output
-p, --pymain do not automatically run the first module as __main__
EXAMPLE
=======
In the ``Demos/freeze`` directory, there exist two Cython modules:
* ``lcmath.pyx``: A module that interfaces with the -lm library.
* ``combinatorics.pyx``: A module that implements n-choose-r using lcmath.
Both modules have the Python idiom ``if __name__ == "__main__"``, which only
execute if that module is the "main" module. If run as main, lcmath prints the
factorial of the argument, while combinatorics prints n-choose-r.
The provided Makefile creates an executable, *nCr*, using combinatorics as the
"main" module. It basically performs the following (ignoring the compiler
flags)::
$ cython_freeze combinatorics lcmath > nCr.c
$ cython combinatorics.pyx
$ cython lcmath.pyx
$ gcc -c nCr.c
$ gcc -c combinatorics.c
$ gcc -c lcmath.c
$ gcc nCr.o combinatorics.o lcmath.o -o nCr
Because the combinatorics module was listed first, its ``__name__`` is set
to ``"__main__"``, while lcmath's is set to ``"lcmath"``. The executable now
contains a Python interpreter and both Cython modules. ::
$ ./nCr
USAGE: ./nCr n r
Prints n-choose-r.
$ ./nCr 15812351235 12
5.10028093999e+113
You may wish to build a normal Python interpreter, rather than having one
module as "main". This may happen if you want to use your module from an
interactive shell or from another script, yet you still want it statically
linked so you can profile it with gprof. To do this, add the ``--pymain``
flag to ``cython_freeze``. In the Makefile, the *python* executable is built
like this. ::
$ cython_freeze --pymain combinatorics lcmath -o python.c
$ gcc -c python.c
$ gcc python.o combinatorics.o lcmath.o -o python
Now ``python`` is a normal Python interpreter, but the lcmath and combinatorics
modules will be built into the executable. ::
$ ./python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import lcmath
>>> lcmath.factorial(155)
4.7891429014634364e+273
PREREQUISITES
=============
Cython 0.11.2 (or newer, assuming the API does not change)
SEE ALSO
========
* `Python `_
* `Cython `_
* `freeze.py `_
cython-c329db8fb45dd09b3079eb90c45ac48907e49047-Demos-freeze/Demos/freeze/combinatorics.pyx 0000664 0000000 0000000 00000000736 13770401327 0030240 0 ustar 00root root 0000000 0000000 # cython: language_level=3
import lcmath
def nCr(n, r):
"""Return the number of ways to choose r elements of a set of n."""
return lcmath.exp(
lcmath.lfactorial(n) -
lcmath.lfactorial(r) -
lcmath.lfactorial(n-r)
)
if __name__ == "__main__":
import sys
if len(sys.argv) != 3:
sys.stderr.write("USAGE: %s n r\nPrints n-choose-r.\n" % sys.argv[0])
sys.exit(2)
n, r = map(float, sys.argv[1:])
print(nCr(n, r))
cython-c329db8fb45dd09b3079eb90c45ac48907e49047-Demos-freeze/Demos/freeze/lcmath.pyx 0000664 0000000 0000000 00000001137 13770401327 0026650 0 ustar 00root root 0000000 0000000 # cython: language_level=3
cdef extern from "math.h":
double c_lgamma "lgamma" (double)
double c_exp "exp" (double)
def exp(n):
"""Return e**n."""
return c_exp(n)
def lfactorial(n):
"""Return an estimate of the log factorial of n."""
return c_lgamma(n+1)
def factorial(n):
"""Return an estimate of the factorial of n."""
return c_exp( c_lgamma(n+1) )
if __name__ == "__main__":
import sys
if len(sys.argv) != 2:
sys.stderr.write("USAGE: %s n\nPrints n!.\n" % sys.argv[0])
sys.exit(2)
n, = map(float, sys.argv[1:])
print(factorial(n))