accessing REPL via pyserial

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
v923z
Posts: 100
Joined: Mon Dec 28, 2015 6:19 pm

Re: accessing REPL via pyserial

Post by v923z » Thu May 10, 2018 7:35 pm

dhylands wrote:
Wed May 09, 2018 4:36 pm
In the REPL, each statement you execute is evaluated and if the return value from the statement is not equal to None, then the result is printed. Consider the following:

Code: Select all

>>> def foo():
...     print('Hey there')
...     return 42
... 
>>> foo()
Hey there
42
>>> def foo2():
...     print('Another print')
...     return None
... 
>>> foo2()
Another print
>>> def foo3():
...     print('foo3')
... 
>>> foo3()
foo3
>>> def foo4():
...     print('Before 3 * 3')
...     3 * 3
...     print('After 3 * 3')
...     return 42
... 
>>> foo4()
Before 3 * 3
After 3 * 3
42
An function which doesn't return anything winds up returning None
But if you try to run this this through pybooard.py, then this no longer holds. Here is an example, called test.py:

Code: Select all

def f():
    print('in f()')
    return 12

f()

def g():
    return 13

g()

print('print g: ', g())
and if I do this:

Code: Select all

python pyboard.py test.py
then I get this:

Code: Select all

$ python pyboard.py test.py 
in f()
print g:  13
Neither 12, nor 13 is printed, while these numbers were in fact returned. I understand what you are saying, and it is clear that something that returns None is not printed. But if the function does return something, this still doesn't guarantee that the result is printed. It depends on the context.

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

Re: accessing REPL via pyserial

Post by dhylands » Thu May 10, 2018 7:52 pm

The reason that you don't see 12 or 13 printed when running through pyboard.py is that you're NOT running through the REPL.

What pyboard.py does is like running a script. You'll see the exact same behavior when running your test.py script under python3:

Code: Select all

794 >python3 test.py
in f()
print g:  13
Using pyboard.py:

Code: Select all

795 >python ../../tools/pyboard.py test.py
in f()
print g:  13
Similarly, if I were to instead paste the contents of test.py into a python3 REPL or a MicroPython REPL, I would see:

Code: Select all

799 >python3
Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
...     print('in f()')
...     return 12
... 
>>> f()
in f()
12
>>> 
>>> def g():
...     return 13
... 
>>> g()
13
>>> 
>>> print('print g: ', g())
print g:  13
>>> 
When running python code from a script, the only thing that's printed are actual print statements (or other things which write data to the same place print does).

REPL is short for "Read Eval Print Loop" and the print portion is what's causing the output of each line to printed.

v923z
Posts: 100
Joined: Mon Dec 28, 2015 6:19 pm

Re: accessing REPL via pyserial

Post by v923z » Sun May 13, 2018 5:36 pm

OK, then it is probably a question of what is "invoking" the REPL in the first place. pyboard does not, but any serial terminal does. I see that this issue is probably not related to the board as such, but to the software on the computer.

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

Re: accessing REPL via pyserial

Post by dhylands » Mon May 14, 2018 4:50 pm

The REPL is run by MicroPython after main.py returns.

For the pyboard, which uses the stm32 port, this is where boot.py is executed:
https://github.com/micropython/micropyt ... #L628-L642

This is where main.py is executed:
https://github.com/micropython/micropyt ... #L687-L705

And this is where the REPL is executed:
https://github.com/micropython/micropyt ... #L707-L719

Post Reply