Running a multi line python script from memory

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
usjcarkm
Posts: 17
Joined: Fri Jun 05, 2015 9:25 pm

Running a multi line python script from memory

Post by usjcarkm » Mon Jun 22, 2015 7:16 pm

Hello,

Is there a way to run a multi line python script from memory (not from a File, since
my embedded op system doesn't support this and also don't need it) ?

Is there a form that takes a char* of the multi line python script itself
versus a file name ?

I don't have files, just the script in memory (hard coded in a const char*
for now, but could also be received from a socket at some point in the near future)

This seems like a straight forward need, but I haven't found any good examples.

Here is a simple example of the kind of script I need to run:

Code: Select all

count = 0
while count < 5:
    print count
    count += 1
I won't be able to use this form:
int pyexec_file(const char *filename);

Here is what I would like to do, though it does not work
(note, that I have tried calling do_str() by passing in the entire string with carriage returns, as listed in many main.c files in the micropython project, but it doesn't work and get the following error:

Code: Select all

Traceback (most recent call last):
  File "<stdin>"
SyntaxError: invalid syntax
These two scripts when executed individually works via the do_str() call:

Code: Select all

print('hello world')

Code: Select all

print('hello world!', list(x+1 for x in range(10)), end='eol\\n')
I tried instead calling the function pyexec_event_repl_process_char() as follows for each char in string
and it also fails to execute.

Code: Select all

int stack_dummy;
stack_top = (char*)&stack_dummy;

#if MICROPY_ENABLE_GC
gc_init(heap, heap + sizeof(heap));
#endif
mp_init();	

const char* src = "count = 0\n"
		  "while count < 5:\n"
    		  "  print count\n"
    		  "  count += 1\n";

char* pChar = src;
while(*pChar != '\0')
{
  pyexec_event_repl_process_char(*pChar);
  pChar++;
}
	
mp_deinit();

Damien
Site Admin
Posts: 619
Joined: Mon Dec 09, 2013 5:02 pm

Re: Running a multi line python script from memory

Post by Damien » Mon Jun 22, 2015 8:04 pm

usjcarkm wrote: Is there a way to run a multi line python script from memory (not from a File, since
my embedded op system doesn't support this and also don't need it) ?

Is there a form that takes a char* of the multi line python script itself
versus a file name ?
Yes, use the do_str() function but replace MP_PARSE_SINGLE_INPUT with MP_PARSE_FILE_INPUT. This will parse the input string as though it were a file, instead of a single line.

usjcarkm
Posts: 17
Joined: Fri Jun 05, 2015 9:25 pm

Re: Running a multi line python script from memory

Post by usjcarkm » Mon Jun 22, 2015 9:57 pm

Thanks. Worked great.
I also mistyped the the print line. Should enclose the param as shown below:

Code: Select all

count = 0
while count < 5:
 print(count)
 count += 1

Post Reply