From 0fb2cb1bf244b870425474a35164d67811fbec2f Mon Sep 17 00:00:00 2001 From: Nick Budak Date: Sat, 21 Jun 2025 09:30:19 -0700 Subject: [PATCH] Add instructions on os.dupterm support and a test for exec --- .github/workflows/ci.yml | 6 +++--- README.md | 2 +- mqterm/jobs.py | 9 +-------- tests/test_jobs.py | 10 ++++++++++ 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a1fa5ee..97878a8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,12 +24,12 @@ jobs: steps: - uses: actions/checkout@v2 - name: Build micropython + env: + CFLAGS_EXTRA: "-DMICROPY_PY_OS_DUPTERM=2" # os.dupterm support on unix run: | - git clone --depth 1 https://github.com/micropython/micropython.git + git clone -b v1.24-release --depth=1 --single-branch https://github.com/micropython/micropython.git cd micropython git submodule update --init - make -C mpy-cross - cp mpy-cross/build/mpy-cross /usr/local/bin/ make -C ports/unix cp ports/unix/build-standard/micropython /usr/local/bin/ cd .. diff --git a/README.md b/README.md index 4a26ac5..35d382f 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ TODO ## Developing -You need python and a build of micropython with `asyncio` support. Follow the steps in the CI workflow to get a `micropython` binary and add it to your `PATH`. +You need python and a build of micropython with `asyncio` and `os.dupterm` support. Follow the steps in the CI workflow to get a `micropython` binary and add it to your `PATH`. Before making changes, install the development (CPython) dependencies: diff --git a/mqterm/jobs.py b/mqterm/jobs.py index 4acbaf7..5d5228e 100644 --- a/mqterm/jobs.py +++ b/mqterm/jobs.py @@ -3,14 +3,7 @@ from binascii import hexlify from hashlib import sha256 from io import BytesIO - -try: - from os import dupterm -except ImportError: - # unix mpy doesn't have dupterm; define a no-op version - def dupterm(stream_object, index=0): - return stream_object - +from os import dupterm from micropython import const diff --git a/tests/test_jobs.py b/tests/test_jobs.py index eff3048..c00e9c3 100644 --- a/tests/test_jobs.py +++ b/tests/test_jobs.py @@ -272,3 +272,13 @@ def test_exec(self): self.assertEqual(job_output, "") # No output expected self.assertEqual(file_output, "Hello, World!") os.remove("output.txt") + + # NOTE: on unix micropython you need to compile with MICROPY_PY_OS_DUPTERM + # to capture output. If this fails, see: + # https://forum.micropython.org/viewtopic.php?t=7055 + def test_exec_output(self): + """RunPyJob should capture output from executed script""" + cmd = 'import sys; sys.stdout.write("Hello, World!")' + job = RunPyJob("exec", [cmd]) + output = job.output().read().decode("utf-8").strip() + self.assertEqual(output, "Hello, World!")