summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/source/reference/random/examples/cffi.rst2
-rw-r--r--doc/source/reference/random/examples/numba.rst2
-rw-r--r--doc/source/reference/random/examples/numba_cffi.rst2
-rw-r--r--numpy/random/_examples/cffi/extending.py38
-rw-r--r--numpy/random/_examples/cffi/parse.py46
5 files changed, 55 insertions, 35 deletions
diff --git a/doc/source/reference/random/examples/cffi.rst b/doc/source/reference/random/examples/cffi.rst
index 04d52203b..79cdf6a8f 100644
--- a/doc/source/reference/random/examples/cffi.rst
+++ b/doc/source/reference/random/examples/cffi.rst
@@ -1,5 +1,7 @@
Extending via CFFI
------------------
+The source of ``numpy/random/_examples/cffi/extending.py``
+
.. literalinclude:: ../../../../../numpy/random/_examples/cffi/extending.py
:language: python
diff --git a/doc/source/reference/random/examples/numba.rst b/doc/source/reference/random/examples/numba.rst
index b41a02568..1a6dd6e85 100644
--- a/doc/source/reference/random/examples/numba.rst
+++ b/doc/source/reference/random/examples/numba.rst
@@ -1,5 +1,7 @@
Extending via Numba
-------------------
+The source of ``numpy/random/_examples/numba/extending.py``
+
.. literalinclude:: ../../../../../numpy/random/_examples/numba/extending.py
:language: python
diff --git a/doc/source/reference/random/examples/numba_cffi.rst b/doc/source/reference/random/examples/numba_cffi.rst
index fb2f85cce..c3e38537f 100644
--- a/doc/source/reference/random/examples/numba_cffi.rst
+++ b/doc/source/reference/random/examples/numba_cffi.rst
@@ -1,5 +1,7 @@
Extending via Numba and CFFI
----------------------------
+The source of ``numpy/random/_examples/numba/extending_distributions.py``
+
.. literalinclude:: ../../../../../numpy/random/_examples/numba/extending_distributions.py
:language: python
diff --git a/numpy/random/_examples/cffi/extending.py b/numpy/random/_examples/cffi/extending.py
index 732cbbb1d..8440d400e 100644
--- a/numpy/random/_examples/cffi/extending.py
+++ b/numpy/random/_examples/cffi/extending.py
@@ -1,9 +1,10 @@
"""
-Use cffi to access the underlying C functions from distributions.h
+Use cffi to access any of the underlying C functions from distributions.h
"""
import os
import numpy as np
import cffi
+from .parse import parse_distributions_h
ffi = cffi.FFI()
inc_dir = os.path.join(np.get_include(), 'numpy')
@@ -15,40 +16,7 @@ ffi.cdef('''
''')
-with open(os.path.join(inc_dir, 'random', 'bitgen.h')) as fid:
- s = []
- for line in fid:
- # massage the include file
- if line.strip().startswith('#'):
- continue
- s.append(line)
- ffi.cdef('\n'.join(s))
-
-with open(os.path.join(inc_dir, 'random', 'distributions.h')) as fid:
- s = []
- in_skip = 0
- for line in fid:
- # massage the include file
- if line.strip().startswith('#'):
- continue
-
- # skip any inlined function definition
- # which starts with 'static NPY_INLINE xxx(...) {'
- # and ends with a closing '}'
- if line.strip().startswith('static NPY_INLINE'):
- in_skip += line.count('{')
- continue
- elif in_skip > 0:
- in_skip += line.count('{')
- in_skip -= line.count('}')
- continue
-
- # replace defines with their value or remove them
- line = line.replace('DECLDIR', '')
- line = line.replace('NPY_INLINE', '')
- line = line.replace('RAND_INT_TYPE', 'int64_t')
- s.append(line)
- ffi.cdef('\n'.join(s))
+parse_distributions_h(ffi, inc_dir)
lib = ffi.dlopen(np.random._generator.__file__)
diff --git a/numpy/random/_examples/cffi/parse.py b/numpy/random/_examples/cffi/parse.py
new file mode 100644
index 000000000..73d8646c7
--- /dev/null
+++ b/numpy/random/_examples/cffi/parse.py
@@ -0,0 +1,46 @@
+import os
+
+
+def parse_distributions_h(ffi, inc_dir):
+ """
+ Parse distributions.h located in inc_dir for CFFI, filling in the ffi.cdef
+
+ Read the function declarations without the "#define ..." macros that will
+ be filled in when loading the library.
+ """
+
+ with open(os.path.join(inc_dir, 'random', 'bitgen.h')) as fid:
+ s = []
+ for line in fid:
+ # massage the include file
+ if line.strip().startswith('#'):
+ continue
+ s.append(line)
+ ffi.cdef('\n'.join(s))
+
+ with open(os.path.join(inc_dir, 'random', 'distributions.h')) as fid:
+ s = []
+ in_skip = 0
+ for line in fid:
+ # massage the include file
+ if line.strip().startswith('#'):
+ continue
+
+ # skip any inlined function definition
+ # which starts with 'static NPY_INLINE xxx(...) {'
+ # and ends with a closing '}'
+ if line.strip().startswith('static NPY_INLINE'):
+ in_skip += line.count('{')
+ continue
+ elif in_skip > 0:
+ in_skip += line.count('{')
+ in_skip -= line.count('}')
+ continue
+
+ # replace defines with their value or remove them
+ line = line.replace('DECLDIR', '')
+ line = line.replace('NPY_INLINE', '')
+ line = line.replace('RAND_INT_TYPE', 'int64_t')
+ s.append(line)
+ ffi.cdef('\n'.join(s))
+