diff options
author | Jim Mussared <jim.mussared@gmail.com> | 2022-12-21 11:43:35 +1100 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2023-01-13 16:38:34 +1100 |
commit | 6013d27dd59dd111041f72106a5a5eddfb02a792 (patch) | |
tree | a83fbde0378eaeaee8ebe3f407ab409c9d713a38 /tools/pyboard.py | |
parent | 77002a92bfcef58e01f20428df25793d43231b15 (diff) | |
download | micropython-6013d27dd59dd111041f72106a5a5eddfb02a792.tar.gz micropython-6013d27dd59dd111041f72106a5a5eddfb02a792.zip |
tools/pyboard.py: Add parse kwarg to eval.
This is useful when using pyboard.py as a library rather than at the
command line.
pyb.eval("1+1") --> b"2"
pyb.eval("{'a': '\x00'}") --> b"{'a': '\\x00'}"
Now you can also do
pyb.eval("1+1", parse=True) --> 2
pyb.eval("{'a': '\x00'}", parse=True) --> {'a': '\x00'}
This work was funded through GitHub Sponsors.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Diffstat (limited to 'tools/pyboard.py')
-rwxr-xr-x | tools/pyboard.py | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/tools/pyboard.py b/tools/pyboard.py index 73250a7c28..b45cc19196 100755 --- a/tools/pyboard.py +++ b/tools/pyboard.py @@ -457,10 +457,15 @@ class Pyboard: self.exec_raw_no_follow(command) return self.follow(timeout, data_consumer) - def eval(self, expression): - ret = self.exec_("print({})".format(expression)) - ret = ret.strip() - return ret + def eval(self, expression, parse=False): + if parse: + ret = self.exec_("print(repr({}))".format(expression)) + ret = ret.strip() + return ast.literal_eval(ret.decode()) + else: + ret = self.exec_("print({})".format(expression)) + ret = ret.strip() + return ret def exec_(self, command, data_consumer=None): ret, ret_err = self.exec_raw(command, data_consumer=data_consumer) |