[SOLVED] filesystem access from c module

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
m.elkhouly
Posts: 3
Joined: Wed May 22, 2019 12:41 pm

[SOLVED] filesystem access from c module

Post by m.elkhouly » Wed May 22, 2019 12:56 pm

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.
Last edited by m.elkhouly on Thu Sep 12, 2019 9:00 am, edited 1 time in total.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: filesystem access from c module

Post by jimmo » Wed May 22, 2019 4:32 pm

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?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: filesystem access from c module

Post by dhylands » Wed May 22, 2019 4:59 pm

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.

m.elkhouly
Posts: 3
Joined: Wed May 22, 2019 12:41 pm

Re: filesystem access from c module

Post by m.elkhouly » Wed May 22, 2019 6:27 pm

[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.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: filesystem access from c module

Post by jimmo » Thu May 23, 2019 12:15 am

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).

m.elkhouly
Posts: 3
Joined: Wed May 22, 2019 12:41 pm

Re: filesystem access from c module

Post by m.elkhouly » Fri May 24, 2019 9:16 pm

the mpfile module did the trick for me.
thanks for the help.

Post Reply