-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add --doctest-ufunc option to doctest Numpy ufuncs
This absorbs the functionality of the pytest-doctest-ufunc package, which was heavily based on pytest-doctestplus to begin with. pytest-doctest-ufunc will be retired. Fixes #123.
- Loading branch information
Showing
10 changed files
with
173 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ install_requires = | |
|
||
[options.extras_require] | ||
test = | ||
numpy | ||
pytest-remotedata>=0.3.2 | ||
sphinx | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# -*- coding: utf-8 -*- | ||
import sys | ||
import glob | ||
|
||
import pytest | ||
|
||
pytest.importorskip('numpy') | ||
|
||
pytest_plugins = ['pytester'] | ||
|
||
|
||
def test_help_message(testdir): | ||
result = testdir.runpytest( | ||
'--help', | ||
) | ||
# fnmatch_lines does an assertion internally | ||
result.stdout.fnmatch_lines([ | ||
'*--doctest-ufunc*Enable running doctests in ' | ||
'docstrings of Numpy ufuncs.', | ||
]) | ||
|
||
|
||
def test_example(testdir): | ||
# Create and build example module | ||
testdir.copy_example('tests/ufunc_example/_module2.c') | ||
testdir.copy_example('tests/ufunc_example/module1.py') | ||
testdir.copy_example('tests/ufunc_example/module2.py') | ||
testdir.copy_example('tests/ufunc_example/setup.py') | ||
testdir.run(sys.executable, 'setup.py', 'build') | ||
build_dir, = glob.glob(str(testdir.tmpdir / 'build/lib.*')) | ||
|
||
# Run pytest without doctests: 0 tests run | ||
result = testdir.runpytest(build_dir) | ||
result.assert_outcomes(passed=0, failed=0) | ||
|
||
# Run pytest with doctests: 1 test run | ||
result = testdir.runpytest(build_dir, '--doctest-modules') | ||
result.assert_outcomes(passed=1, failed=0) | ||
|
||
# Run pytest with doctests including ufuncs: 2 tests run | ||
result = testdir.runpytest(build_dir, '--doctest-plus', '--doctest-modules', '--doctest-ufunc') | ||
result.assert_outcomes(passed=2, failed=0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION | ||
|
||
#include <numpy/arrayobject.h> | ||
#include <numpy/ufuncobject.h> | ||
#include <Python.h> | ||
|
||
|
||
static double foo_inner(double a, double b) | ||
{ | ||
return a + b; | ||
} | ||
|
||
|
||
static void foo_loop( | ||
char **args, | ||
const npy_intp *dimensions, | ||
const npy_intp *steps, | ||
void *NPY_UNUSED(data) | ||
) { | ||
const npy_intp n = dimensions[0]; | ||
for (npy_intp i = 0; i < n; i ++) | ||
{ | ||
*(double *) &args[2][i * steps[2]] = foo_inner( | ||
*(double *) &args[0][i * steps[0]], | ||
*(double *) &args[1][i * steps[1]]); | ||
} | ||
} | ||
|
||
|
||
static PyUFuncGenericFunction foo_loops[] = {foo_loop}; | ||
static char foo_types[] = {NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE}; | ||
static void *foo_data[] = {NULL}; | ||
static const char foo_name[] = "foo"; | ||
static const char foo_docstring[] = ">>> foo(1, 2)\n3.0"; | ||
|
||
static PyModuleDef moduledef = { | ||
.m_base = PyModuleDef_HEAD_INIT, | ||
.m_name = "_module2", | ||
.m_size = -1 | ||
}; | ||
|
||
|
||
PyMODINIT_FUNC PyInit__module2(void) | ||
{ | ||
import_array(); | ||
import_ufunc(); | ||
|
||
PyObject *module = PyModule_Create(&moduledef); | ||
if (!module) | ||
return NULL; | ||
|
||
PyObject *obj = PyUFunc_FromFuncAndData( | ||
foo_loops, foo_data, foo_types, 1, 2, 1, PyUFunc_None, foo_name, | ||
foo_docstring, 0); | ||
if (!obj) | ||
{ | ||
Py_DECREF(module); | ||
return NULL; | ||
} | ||
if (PyModule_AddObject(module, foo_name, obj) < 0) | ||
{ | ||
Py_DECREF(obj); | ||
Py_DECREF(module); | ||
return NULL; | ||
} | ||
|
||
return module; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
def foo(): | ||
'''A doctest... | ||
>>> foo() | ||
1 | ||
''' | ||
return 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from _module2 import foo # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from setuptools import setup, Extension | ||
import numpy as np | ||
|
||
ext = Extension('_module2', ['_module2.c'], | ||
extra_compile_args=['-std=c99'], | ||
include_dirs=[np.get_include()]) | ||
setup(name='example', py_modules=['module1', 'module2'], ext_modules=[ext]) |