8000 tools/mpremote: Make eval parse by default. · anight/micropython@1091021 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1091021

Browse files
jimmodpgeorge
authored andcommitted
tools/mpremote: Make eval parse by default.
This is a step towards making the transport expose a Python API rather than functions that mostly print to stdout. Most use cases of `transport.eval()` are to get some state back from the device, so have it return as a value directly by default. Updates uses of `transport.eval()` to remove the parse argument where it now isn't needed, make the `rtc` command use eval/exec, and update the `mip` command to use eval's parsing. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
1 parent 6835743 commit 1091021

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

tools/mpremote/mpremote/commands.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ def do_soft_reset(state, _args=None):
242242

243243

244244
def do_rtc(state, args):
245+
state.ensure_raw_repl()
246+
state.did_action()
247+
248+
state.transport.exec("import machine")
249+
245250
if args.set:
246251
import datetime
247252

@@ -256,6 +261,6 @@ def do_rtc(state, args):
256261
now.second,
257262
now.microsecond,
258263
)
259-
_do_execbuffer(state, "import machine; machine.RTC().datetime({})".format(timetuple), True)
264+
state.transport.exec("machine.RTC().datetime({})".format(timetuple))
260265
else:
261-
_do_execbuffer(state, "import machine; print(machine.RTC().datetime())", True)
266+
print(state.transport.eval("machine.RTC().datetime()"))

tools/mpremote/mpremote/mip.py

Lines changed: 2 additions & 9 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,7 @@ def _install_package(transport, package, index, target, version, mpy):
150150
mpy_version = "py"
151151
if mpy:
152152
transport.exec("import sys")
153-
mpy_version = (
154-
int(transport.eval("getattr(sys.implementation, '_mpy', 0) & 0xFF").decode())
155-
or "py"
156-
)
153+
mpy_version = transport.eval("getattr(sys.implementation, '_mpy', 0) & 0xFF") or "py"
157154

158155
package = f"{index}/package/{mpy_version}/{package}/{version}.json"
159156

@@ -178,11 +175,7 @@ def do_mip(state, args):
178175

179176
if args.target is None:
180177
state.transport.exec("import sys")
181-
lib_paths = (
182-
state.transport.eval("'|'.join(p for p in sys.path if p.endswith('/lib'))")
183-
.decode()
184-
.split("|")
185-
)
178+
lib_paths = [p for p in state.transport.eval("sys.path") if p.endswith("/lib")]
186179
if lib_paths and lib_paths[0]:
187180
args.target = lib_paths[0]
188181
else:

tools/mpremote/mpremote/transport_serial.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def exec_raw(self, command, timeout=10, data_consumer=None):
271271
self.exec_raw_no_follow(command)
272272
return self.follow(timeout, data_consumer)
273273

274-
def eval(self, expression, parse=False):
274+
def eval(self, expression, parse=True):
275275
if parse:
276276
ret = self.exec("print(repr({}))".format(expression))
277277
ret = ret.strip()
@@ -331,7 +331,7 @@ def repr_consumer(b):
331331
def fs_stat(self, src):
332332
try:
333333
self.exec("import os")
334-
return os.stat_result(self.eval("os.stat(%s)" % ("'%s'" % src), parse=True))
334+
return os.stat_result(self.eval("os.stat(%s)" % ("'%s'" % src)))
335335
except TransportError as e:
336336
reraise_filesystem_error(e, src)
337337

@@ -503,7 +503,7 @@ def fname_cp_dest(src, dest):
503503

504504
def mount_local(self, path, unsafe_links=False):
505505
fout = self.serial
506-
if self.eval('"RemoteFS" in globals()') == b"False":
506+
if not self.eval('"RemoteFS" in globals()'):
507507
self.exec(fs_hook_code)
508508
self.exec("__mount()")
509509
self.mounted = True

0 commit comments

Comments
 (0)
0