mabuchilab / NiceLib

A Python package for rapidly developing "nice" bindings for C libraries, using cffi
GNU General Public License v3.0
24 stars 8 forks source link

How to handle variable array sizes? #8

Closed Tillsten closed 5 years ago

Tillsten commented 5 years ago

Maybe i am missing something: I try to wrap a library where I sometimes have to pass an array without its size. Since the size changes, I can't use arr[n]. Is there are workaround? Similar issue is where I have something like

int cam_read_loop(int handle, long* arr, int number_of_reads)

The size of the array then should be a multiple of number_of_reads.

Tillsten commented 5 years ago

Hmmm, or is this already handled by len=in?

natezb commented 5 years ago

For cam_read_loop, it sounds like you want len=in. This would give you a method that you'd call like cam.read_loop(num_reads).

In your first case, it might help if you gave an example of the function being wrapped. arr and arr[n] tell nicelib to automatically create an empty array and pass it in. If the length is not specified, nicelib cannot know how big to make this array.

If instead the user is supposed to explicitly pass in the array, you can use in or inout.

Tillsten commented 5 years ago

You are right, my problems are solved by using in and supply the array directly. It may be helpful to make this option clearer in the docs. Btw is there a reason that keyword arguments are not supported by the mid-level bindings?

natezb commented 5 years ago

Feel free add a feature request for keyword argument support. This isn't currently supported because we only recently started extracting function argnames from headers and adding them to the docstrings. Now that we do that, it probably wouldn't take much effort to implement.

We also could consider allowing users to specify new, "nicer" argnames within Sig definitions.

natezb commented 5 years ago

Going over the docs now, it appears we don't have an option to pass in an array and auto-specify its length. We could add arrin and bufin options for this case. I'll open an issue for this particular feature request.

natezb commented 5 years ago

FYI I just added kwarg support for python > 3.3 in 279fe2263e41c068e87e4f7dfa8a12b4aa380c20. Check it out and let me know if you have any issues.