Created
April 9, 2024 18:37
-
-
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__
This file contains hidden or 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
| 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) |
This file contains hidden or 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
| #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); | |
| } |
This file contains hidden or 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
| from setuptools import Extension, setup | |
| setup(ext_modules=[ | |
| Extension('foo', ['foo.c']) | |
| ]) |
This file contains hidden or 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
| 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