Skip to content

Instantly share code, notes, and snippets.

@lpsinger
Created April 9, 2024 18:37
Show Gist options
  • Select an option

  • Save lpsinger/65e59728555dc2096af88d394e2d4a6b to your computer and use it in GitHub Desktop.

Select an option

Save lpsinger/65e59728555dc2096af88d394e2d4a6b to your computer and use it in GitHub Desktop.
Example of collection doctests from a Python function defined in a C function; has __doc__ but not __code__
from functools import wraps
from foo import hello as _hello
def wrap_it(func):
@wraps(func)
def wrapper(*args, **kwargs):
return result
return wrapper
hello = wrap_it(_hello)
#include <Python.h>
static PyObject *hello(PyObject *module, PyObject *args) {
return PyUnicode_FromString("Hello world");
}
static PyModuleDef moduledef = {
.m_base = PyModuleDef_HEAD_INIT,
.m_name = "foo",
.m_methods = (PyMethodDef []) {
{"hello", hello, METH_NOARGS,
">>> hello()\n"
"'Hello world'\n"},
{/* terminal element, all NULL */}
}
};
PyMODINIT_FUNC
PyInit_foo(void) {
return PyModule_Create(&moduledef);
}
from setuptools import Extension, setup
setup(ext_modules=[
Extension('foo', ['foo.c'])
])
from doctest import DocTestFinder
import foo
import bar
finder = DocTestFinder()
# This works
assert len(finder.find(foo.hello)[0].examples) == 1
# This raises the exception:
# AttributeError: 'builtin_function_or_method' object has no attribute '__code__'. Did you mean: '__call__'?
assert len(finder.find(bar.hello)[0].examples) == 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment