Skip to content

Commit 3b03a76

Browse files
committed
Add pure Python version for pointers
1 parent 568e00f commit 3b03a76

File tree

6 files changed

+45
-9
lines changed

6 files changed

+45
-9
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pointers_cython.c
2+
pointers_pure.c

source-code/cython/Pointers/Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
VERSION = cpython-311-x86_64-linux-gnu
22
CYTHON_LIB = pointers_cython.$(VERSION).so
3+
PURE_LIB = pointers_pure.$(VERSION).so
34

4-
all: $(CYTHON_LIB)
5+
all: $(CYTHON_LIB) $(PURE_LIB)
56

67
$(CYTHON_LIB): pointers_cython.pyx
78
python setup.py build_ext --inplace
89

10+
$(PURE_LIB): pointers_pure.py
11+
python setup.py build_ext --inplace
12+
913
clean:
1014
python setup.py clean
11-
$(RM) pointers_cython.c $(CYTHON_LIB)
15+
$(RM) pointers_cython.c pointers_pure.c $(CYTHON_LIB) $(PURE_LIB)
16+
$(RM) -r build

source-code/cython/Pointers/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
Simple example of using pointers in Cython code.
33

44
## What is it?
5-
1. `pointers_cython.pyx`: code that computes a list of squares using a cdef
6-
function that increments an integer value referred to by a pointer.
5+
1. `pointers_cython.pyx`: Cython code that computes a list of squares
6+
using a cdef function that increments an integer value referred to
7+
by a pointer.
8+
1. `pointers_pure.pyx`: pure Python code that computes a list of squares
9+
using a cfunc function that increments an integer value referred to by
10+
a pointer.
711
1. `setup.py`: Python build script.
812
1. `Makefile`: make file to build the extension.
9-
1. `squares.py`: script to load the compiled module, and compute the
10-
squares of the first few positive integers.
13+
1. `squares.py`: script to load one of the compiled module, and compute
14+
the squares of the first few positive integers.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import cython
2+
3+
4+
def squares(n: cython.int):
5+
sqrs: list = []
6+
i: cython.int = 0
7+
while i < n:
8+
sqrs.append(i**2)
9+
incr(cython.address(i))
10+
return sqrs
11+
12+
@cython.cfunc
13+
def incr(i: cython.p_int):
14+
i[0] += 1

source-code/cython/Pointers/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
from Cython.Build import cythonize
55

66
setup(
7-
ext_modules=cythonize('pointers_cython.pyx',
7+
ext_modules=cythonize(['pointers_cython.pyx', 'pointers_pure.py'],
88
language_level='3str')
99
)
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
#!/usr/bin/env python
22

3-
import pointers_cython as pointers
43

4+
import argparse
55

6-
print(pointers.squares(5))
6+
arg_parser = argparse.ArgumentParser(description='create a list of squares')
7+
arg_parser.add_argument('--n', type=int, default=5,
8+
help='number of squares to create')
9+
arg_parser.add_argument('module', choices=['pure_python', 'cython'],
10+
help='module to use for creating squares')
11+
options = arg_parser.parse_args()
12+
13+
if options.module == 'pure_python':
14+
import pointers_pure as pointers
15+
elif options.module == 'cython':
16+
import pointers_cython as pointers
17+
18+
print(pointers.squares(options.n))

0 commit comments

Comments
 (0)