If I'm writing a module - lets say I want to write a function that takes two python strings and joins them - ( I know I can do this using str.join - but I'm trying to learn how things work under the hood)
there is already a function to do that in objstr.c called str_join
it looks like I'm supposed to access it somehow using str_join_obj - but I can't find any examples of how these things get called.
It looks like MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join) would set up a struct that has a pointer to the actual function and then
I should call it with something like fun_builtin_2_call from objfun.c - but I can't find any places that actually use this.
Would anyone have a code snippet or a good place to look to find a builtin function that then uses other builtin functions - preferably string functions so I can just "play" with it
Thanks,
Kelly
Using internal functions
Re: Using internal functions
Many of the internal functions are declared static, which means that they might not be accessible directly to other modules.
Generally speaking, any function you can find in a .h file is fair game.
There are really 2 kinds of functions: C-friendly functions and python-friendly functions. If nobody has had a need for a C-friendly function sometimes they're not available or delcared static. Those things change over time.
It looks like str_join is STATIC. To call it from C you'd need to fish the function pointer out of the str_join_obj and call it. The python strings are based on vstr, which does have a C API (look in py/misc.h)
The python friendly function are intended to be used from python, although it is possible to also call them from C, just a bit more work. There aren't many examples floating around because it isn't done all that often.
Here's one example: https://github.com/micropython/micropyt ... #L460-L466
Here's another: https://github.com/micropython/micropyt ... #L144-L155
Generally speaking, any function you can find in a .h file is fair game.
There are really 2 kinds of functions: C-friendly functions and python-friendly functions. If nobody has had a need for a C-friendly function sometimes they're not available or delcared static. Those things change over time.
It looks like str_join is STATIC. To call it from C you'd need to fish the function pointer out of the str_join_obj and call it. The python strings are based on vstr, which does have a C API (look in py/misc.h)
The python friendly function are intended to be used from python, although it is possible to also call them from C, just a bit more work. There aren't many examples floating around because it isn't done all that often.
Here's one example: https://github.com/micropython/micropyt ... #L460-L466
Here's another: https://github.com/micropython/micropyt ... #L144-L155
Re: Using internal functions
Thanks for the reply - I managed to get it a bit more figured out - not sure if this is the 'correct' way or not, but it worked.
#include "py/objstr.h" (and others)
const char hello[200];
static mp_obj_t mod_test_echo(mp_obj_t name) {
mp_obj_t x;
snprintf(hello,200,"Hello ");
x = mp_obj_new_str(hello,strlen(hello),true);
return str_join_obj.fun._2(x,name);
}
Although I forgot what str.join did, it worked exactly as it should
Is there any better way to learn this stuff other than spelunking through the depths of the code? i.e. a repository of wisdom somewhere?
I think my next challenge is to spit back a simple dictionary that I make on the fly or something.
Thanks,
!
#include "py/objstr.h" (and others)
const char hello[200];
static mp_obj_t mod_test_echo(mp_obj_t name) {
mp_obj_t x;
snprintf(hello,200,"Hello ");
x = mp_obj_new_str(hello,strlen(hello),true);
return str_join_obj.fun._2(x,name);
}
Although I forgot what str.join did, it worked exactly as it should
Is there any better way to learn this stuff other than spelunking through the depths of the code? i.e. a repository of wisdom somewhere?
I think my next challenge is to spit back a simple dictionary that I make on the fly or something.
Thanks,
!
Re: Using internal functions
The closest thing to documentation that exists is this:
http://forum.micropython.org/viewtopic.php?f=3&t=2370
I've written several posts on calling C from Python:
http://forum.micropython.org/viewtopic.php?f=2&t=1411
http://forum.micropython.org/viewtopic.php?f=3&t=153
So yeah "use the source".
http://forum.micropython.org/viewtopic.php?f=3&t=2370
I've written several posts on calling C from Python:
http://forum.micropython.org/viewtopic.php?f=2&t=1411
http://forum.micropython.org/viewtopic.php?f=3&t=153
So yeah "use the source".
Re: Using internal functions
You could also do: Which wont' waste 200 bytes of RAM for the global variable.
Code: Select all
static mp_obj_t mod_test_echo(mp_obj_t name) {
mp_obj_t x;
const char *hello = "Hello ";
x = mp_obj_new_str(hello,strlen(hello),true);
return str_join_obj.fun._2(x,name);
}