support buffer protocol in Solver.add_clauses #722
Merged
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.
This pull request allows Solver.add_clauses to accept any contiguous buffer according to the Python buffer protocol instead of just
array.array
. This possibility was already mentioned in #519.The main advantage of the buffer protocol is that it can be implemented by third party modules as well. Thus,
add_clauses
will also work with numpy arrays for example. Another advantage is that the C side of the API is much simpler, and thus I was able to remove a bunch of code.One limitation that remains in the current state of this pull request is that only contiguous buffers, i.e., buffers where the element are contiguous in memory, are supported. Non-contiguous buffers can be created in numpy, for example, if someone indexes only a column of a multidimensional array. If that use-case is desired it would not require too many changes to get it running. (Right now,
PyObject_GetBuffer
will automatically raise a ValueError when used on a non-contiguous buffer.)Regarding unit-tests, I did not add any new ones but the existing ones for
array.array
cover the changed code and they all passed for me.