-
Notifications
You must be signed in to change notification settings - Fork 30
WIP: Add helper for creating modules. #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2e1987c
ENH: Add helper for creating modules.
cf6959c
ENH: drop py2 compat code
b6de78e
scoped_ref => owned_ref
6e1e903
fix up pytypeobject* issues
0bae803
fix makefile to rename so before running tests
ca6c303
change LIBPY_AUTOMODULE for clang?
30ea8ec
no methods list?
2af9e55
Something that works
c08dcb2
PR feedback
6a614e1
Update docstring, fmt
175669d
fmt
0bd1b40
fix commented out code
b35fa51
get
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| #pragma once | ||
|
|
||
| #include <forward_list> | ||
| #include <unordered_map> | ||
| #include <vector> | ||
|
|
||
| #include "libpy/abi.h" | ||
| #include "libpy/borrowed_ref.h" | ||
| #include "libpy/detail/api.h" | ||
| #include "libpy/detail/numpy.h" | ||
| #include "libpy/detail/python.h" | ||
| #include "libpy/owned_ref.h" | ||
|
|
||
| #define _libpy_XSTR(s) #s | ||
| #define _libpy_STR(s) _libpy_XSTR(s) | ||
|
|
||
| #define _libpy_MODULE_PATH(parent, name) _libpy_STR(parent) "." _libpy_STR(name) | ||
|
|
||
| #define _libpy_XCAT(a, b) a##b | ||
| #define _libpy_CAT(a, b) _libpy_XCAT(a, b) | ||
|
|
||
| #define _libpy_MODINIT_NAME(name) _libpy_CAT(PyInit_, name) | ||
| #define _libpy_MODULE_CREATE(path) PyModule_Create(&_libpy_module) | ||
|
|
||
| /** Define a Python module. | ||
|
|
||
| @param parent A symbol indicating the parent module. | ||
| @param name The leaf name of the module. | ||
| @param methods ({...}) list of objects representing the functions to add to the | ||
| module. Note this list must be surrounded by parentheses. | ||
|
|
||
| ## Examples | ||
|
|
||
| Create a module `my_package.submodule.my_module` with two functions `f` and | ||
| `g` and one type `T`. | ||
|
|
||
| \code | ||
| LIBPY_AUTOMODULE(my_package.submodule, | ||
| my_module, | ||
| ({py::autofunction<f>("f"), | ||
| py::autofunction<g>("g")})) | ||
| (py::borrowed_ref<> m) { | ||
| py::borrowed_ref t = py::autoclass<T>("T").new_().type(); | ||
| return PyObject_SetAttrString(m.get(), "T", static_cast<PyObject*>(t)); | ||
| } | ||
| /endcode | ||
| */ | ||
| #define LIBPY_AUTOMODULE(parent, name, methods) \ | ||
| bool _libpy_user_mod_init(py::borrowed_ref<>); \ | ||
| PyMODINIT_FUNC _libpy_MODINIT_NAME(name)() LIBPY_EXPORT; \ | ||
| PyMODINIT_FUNC _libpy_MODINIT_NAME(name)() { \ | ||
| import_array(); \ | ||
| if (py::abi::ensure_compatible_libpy_abi()) { \ | ||
| return nullptr; \ | ||
| } \ | ||
| static std::vector<PyMethodDef> ms methods; \ | ||
| ms.emplace_back(py::end_method_list); \ | ||
| static PyModuleDef _libpy_module{ \ | ||
| PyModuleDef_HEAD_INIT, \ | ||
| _libpy_MODULE_PATH(parent, name), \ | ||
| nullptr, \ | ||
| -1, \ | ||
| ms.data(), \ | ||
| }; \ | ||
| py::owned_ref m(_libpy_MODULE_CREATE(_libpy_MODULE_PATH(parent, name))); \ | ||
| if (!m) { \ | ||
| return nullptr; \ | ||
| } \ | ||
| try { \ | ||
| if (_libpy_user_mod_init(m)) { \ | ||
| return nullptr; \ | ||
| } \ | ||
| } \ | ||
| catch (const std::exception& e) { \ | ||
| py::raise_from_cxx_exception(e); \ | ||
| return nullptr; \ | ||
| } \ | ||
| catch (...) { \ | ||
| if (!PyErr_Occurred()) { \ | ||
| py::raise(PyExc_RuntimeError) << "an unknown C++ exception was raised"; \ | ||
| return nullptr; \ | ||
| } \ | ||
| } \ | ||
| return std::move(m).escape(); \ | ||
| } \ | ||
| bool _libpy_user_mod_init |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| import libpy # noqa |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #include "libpy/autoclass.h" | ||
| #include "libpy/automethod.h" | ||
| #include "libpy/automodule.h" | ||
|
|
||
| bool is_42(int arg) { | ||
| return arg == 42; | ||
| } | ||
|
|
||
| bool is_true(bool arg) { | ||
| return arg; | ||
| } | ||
|
|
||
| using int_float_pair = std::pair<int, float>; | ||
|
|
||
| int first(const int_float_pair& ob) { | ||
| return ob.first; | ||
| } | ||
|
|
||
| float second(const int_float_pair& ob) { | ||
| return ob.second; | ||
| } | ||
|
|
||
| LIBPY_AUTOMODULE(tests, | ||
| _test_automodule, | ||
| ({py::autofunction<is_42>("is_42"), | ||
| py::autofunction<is_true>("is_true")})) | ||
| (py::borrowed_ref<> m) { | ||
| py::owned_ref t = py::autoclass<int_float_pair>("_test_automodule.int_float_pair") | ||
| .new_<int, float>() | ||
| .comparisons<int_float_pair>() | ||
| .def<first>("first") | ||
| .def<second>("second") | ||
| .type(); | ||
| return PyObject_SetAttrString(m.get(), "int_float_pair", static_cast<PyObject*>(t)); | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from . import _test_automodule as mod | ||
|
|
||
|
|
||
| def test_modname(): | ||
| assert mod.__name__ == 'tests._test_automodule' | ||
|
|
||
|
|
||
| def test_function(): | ||
| assert mod.is_42(42) | ||
| assert not mod.is_42(~42) | ||
|
|
||
|
|
||
| def test_type(): | ||
| assert isinstance(mod.int_float_pair, type) | ||
| a = mod.int_float_pair(1, 2.5) | ||
| assert a.first() == 1 | ||
| assert a.second() == 2.5 | ||
| b = mod.int_float_pair(1, 2.5) | ||
| assert a == b | ||
| c = mod.int_float_pair(1, 3.5) | ||
| assert a != c |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.