Page 1 of 1

[SOLVED] filesystem access from c module

Posted: Wed May 22, 2019 12:56 pm
by m.elkhouly
hello,
i'm writing a C module that needs to create and handle file operations, and i got lost trying to find a way to do that.

[code]
char *pth = "text.txt" ;
mp_obj_t dest[2];
mp_obj_t file;
dest[0] = mp_obj_new_str(pth, strlen(pth));
dest[1] = mp_obj_new_str("w", 1);
mp_obj_t file_obj = mp_module_get(MP_QSTR_uio);
mp_obj_t open_fn = mp_load_attr(file_obj, MP_QSTR_open);
file = mp_call_function_2(open_fn, dest[0], dest[1]);
[/code]

what i can do is open the file (and confirm it using os.listdir()) but i'm not able to write to or close it.

Re: filesystem access from c module

Posted: Wed May 22, 2019 4:32 pm
by jimmo
If you include "py/builtin.h" you can use mp_builtin_open_obj directly to get the open function.

But you'll need to use mp_load_attr to get the file methods like read/write.

What goes wrong when you try to write to it? Compile or runtime error?

Re: filesystem access from c module

Posted: Wed May 22, 2019 4:59 pm
by dhylands
I wrote some code a few years ago, which you can find here:
https://github.com/dhylands/micropython ... f13645cR52

It never got merged into mainline, but it may still work.

There is also the mp_stream object (from py/stream.h and py/stream.c) but I haven't used this myself.

Re: filesystem access from c module

Posted: Wed May 22, 2019 6:27 pm
by m.elkhouly
[quote=jimmo post_id=36795 time=1558542758 user_id=3071]
If you include "py/builtin.h" you can use mp_builtin_open_obj directly to get the open function.

But you'll need to use mp_load_attr to get the file methods like read/write.

What goes wrong when you try to write to it? Compile or runtime error?
[/quote]

i kept getting module has no attribute write/close, but that was me not able to identify the right module to use.

[quote=dhylands post_id=36801 time=1558544386 user_id=81]
I wrote some code a few years ago, which you can find here:
https://github.com/dhylands/micropython ... f13645cR52

It never got merged into mainline, but it may still work.

There is also the mp_stream object (from py/stream.h and py/stream.c) but I haven't used this myself.
[/quote]

thanks i'll probably try this route.
thanks for the help.

Re: filesystem access from c module

Posted: Thu May 23, 2019 12:15 am
by jimmo
m.elkhouly wrote:
Wed May 22, 2019 6:27 pm
i kept getting module has no attribute write/close, but that was me not able to identify the right module to use.
read and write are methods on the object returned from open. Not methods on a module.

Does your code look something like this?

Code: Select all

mp_obj_t file_obj = mp_call_function_2(mp_builtin_open_obj, dest[0], dest[1]);
mp_obj_t write_fn = mp_load_attr(file_obj, MP_QSTR_write);
mp_call_function_2(write_fn, ...buffer...);
(As it turns out, this is almost exactly what dhyland's code does).

Re: filesystem access from c module

Posted: Fri May 24, 2019 9:16 pm
by m.elkhouly
the mpfile module did the trick for me.
thanks for the help.