-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
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
Export arg parse functions for native modules. #14335
Export arg parse functions for native modules. #14335
Conversation
c234db6
to
6fa7b26
Compare
Code size report:
|
Luckly the native .mpy sub version has been increased for the upcoming 1.23.0 release, which means we can make changes like this before the release (because this changes the native ABI).
You should be able to access this already via
For this, you could call |
py/nativeglue.h
Outdated
@@ -83,6 +83,7 @@ typedef enum { | |||
MP_F_NATIVE_YIELD_FROM, | |||
MP_F_SETJMP, | |||
MP_F_NUMBER_OF, | |||
MP_F_ARG_PARSE_ALL_KW_ARRAY, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not needed.
py/nativeglue.c
Outdated
@@ -348,6 +348,7 @@ const mp_fun_table_t mp_fun_table = { | |||
&mp_stream_readinto_obj, | |||
&mp_stream_unbuffered_readline_obj, | |||
&mp_stream_write_obj, | |||
&mp_arg_parse_all_kw_array, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this up, say between mp_get_stream_raise
and mp_binary_get_size
. And then increase MP_FUN_TABLE_MP_TYPE_TYPE_OFFSET
by 1 (in tools/mpy_ld.py
).
6fa7b26
to
8e6f0d5
Compare
Thanks for the review @dpgeorge ! I believe I have addressed your comments. I'll try and get a MVP of my native module working this weekend. Would we like to add all the exported symbols I need to this PR? Or would we like to tackle them one by one? I think was having issues trying to use |
If there are only a handful of them, then, yes, please add them here in this PR. |
I see you merged BrianPugh/tamp#128. Did that require any more additions to the native exports? |
I ended up with a few workarounds by executing the things I needed on the python-side of things. These are the concepts I ran up against (some already discussed in this PR) when writing native modules in C:
I think it just has to be that way.
Is there an easy macro for creating custom exceptions in C?
however, the file could have been totally private to the C side, so I would have liked to been able to open/write/read/close from C. What are the main file interaction functions?
Conceptual hurdles I had to overcome: using I can create more examples in |
@dpgeorge any additional thoughts on the above? |
That's a good approach, to do as much as possible on the Python side. The C side should be for things that need to be fast, or things/libraries that are already written in C.
Yes. There's really not any way around this. If you want to create an array of allowed arguments, you'd need to populate the qstr values at runtime (probably only once in the init function).
IMO it makes sense to expose
There's We would need to either expose all the needed exception functions (like
If you want to do this on the C side you can, just implement the relevant Python calls in C. Eg I don't think it's necessary to expose any functions like
We can definitely add the
Yes, it would be good to add an example of this. Summary: for this PR I would suggest adding the following:
I'll see if I can add something to help create exception types. |
See #14497 for that. |
@BrianPugh would you like me to pick this PR up and make the above changes that I suggested? |
Yes please! Sorry I haven't gotten to this, I've been super busy moving for the past 2 weeks. |
8e6f0d5
to
e253495
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #14335 +/- ##
=======================================
Coverage 98.39% 98.39%
=======================================
Files 161 161
Lines 21204 21204
=======================================
Hits 20864 20864
Misses 340 340 ☔ View full report in Codecov by Sentry. |
Also define `mp_type_bytearray`. These all help to write native modules. Signed-off-by: Brian Pugh <bnp117@gmail.com> Signed-off-by: Damien George <damien@micropython.org>
Python code is no longer needed to implement keyword arguments in `btree.open()`, it can now be done in C. Signed-off-by: Damien George <damien@micropython.org>
e253495
to
df41913
Compare
I made the following changes to this PR:
And it's now merged. |
Exports the very useful
mp_arg_parse_all_kw_array
for native modules.I imagine we would also want to export
mp_arg_parse_all
, but I wanted to check in first to see if this is the correct path.EDIT: Building a list of symbols that I wish I had:
mp_load_method_maybe
- I'm trying to check if an object has an attribute without raising an exception.mp_obj_str_binary_op
- I'm trying to usemp_obj_is_str_or_bytes
and this is missing.