fix: make pslab flash work again after the SerialHandler refactor - #293
thisisanubhav wants to merge 5 commits into
Conversation
enter_bootloader() still used self.device.interface, which the SerialHandler refactor removed, so it raised AttributeError. Set the baudrate through SerialHandler.baudrate.
flash() read psl.interface, which ScienceLab no longer has, and passed the ScienceLab to mcbootflash, which needs an object with read() and write(). Use psl.device, the SerialHandler, for both.
ScienceLab now takes a connection handler, but main() passed it the port string, which failed with AttributeError. Pass the SerialHandler that main() has already opened on that port.
Run enter_bootloader() and flash() against a SerialHandler on pyserial's loop:// port with mcbootflash stubbed, and check that main() hands the opened handler to ScienceLab.
Reviewer's GuideRestores Sequence diagram for the repaired PSLab flash flowsequenceDiagram
participant CLI
participant ScienceLab
participant SerialHandler
participant Device
participant mcbootflash
CLI->>SerialHandler: SerialHandler(port=args.port)
CLI->>ScienceLab: ScienceLab(handler)
CLI->>ScienceLab: flash(psl, hexfile)
ScienceLab->>Device: baudrate
ScienceLab->>ScienceLab: enter_bootloader()
ScienceLab->>Device: baudrate = 460800
ScienceLab->>mcbootflash: get_boot_attrs(device)
mcbootflash->>Device: erase_flash(device, memory_range, erase_size)
mcbootflash->>Device: write_flash(device, chunk)
mcbootflash->>Device: checksum(device, chunk)
mcbootflash->>Device: self_verify(device)
mcbootflash->>Device: reset(device)
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (1)
Included review availability: Your plan provides up to 4 included reviews per hour; 3 remain after this review. 📝 WalkthroughWalkthroughThe flash path now reuses the opened ChangesFlash path repair
Priority: ➖ Normal Estimated code review effort: 2 (Simple) | ~15 minutes Change: Bug fix · Severity of issue fixed: Medium Merge Risk: ⚪ Minimal · up to Flashing now reuses the opened serial connection for bootloader setup and firmware operations, with focused coverage for the repaired flow. No merge-blocking risk is identified. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="tests/test_flash.py" line_range="21" />
<code_context>
+
+@pytest.fixture
+def handler() -> SerialHandler:
+ sh = SerialHandler("loop://")
+ sh._ser = serial.serial_for_url("loop://", baudrate=1000000, timeout=0.1)
+ return sh
+
+
</code_context>
<issue_to_address>
**nitpick (bug_risk):** The `handler` fixture replaces the `SerialHandler`'s underlying serial object with a new `loop://` connection but never closes it, so every test using the fixture leaks an open serial resource.
**Triggers:** When the test suite runs repeatedly or in parallel with many tests using this fixture.
**Suggested fix:** Close `sh._ser` in a fixture finalizer or yield the handler and call `disconnect()` during teardown.
```suggestion
yield sh
sh.disconnect()
```
</issue_to_address>Sourcery assessment
Needs a human reviewer. If the serial connection is handled incorrectly, the device could be erased or receive an invalid firmware image, leaving it unusable until it is reflashed. Reverting the code would not restore firmware already written, but the device state is bounded and normally repairable by rerunning a correct flash.
Fixes #292
The SerialHandler refactor (e70d01d) made
ScienceLabwrap a connection handler (ScienceLab(device),psl.device) and removed theinterfaceattribute. The flashing path still used the old API, sopslab flashfailed with anAttributeErrorbefore reaching the bootloader.Changes (one commit each)
ScienceLab.enter_bootloader()setsself.device.baudrateinstead ofself.device.interface.baudrate.cli.flash()reads and sets the baudrate and timeout onpsl.device, and passespsl.devicetomcbootflash, which needs an object withread/write, instead of theScienceLabitself.cli.main()builds theScienceLabfrom theSerialHandlerit has already opened, instead of passing the port string, which failed with'str' object has no attribute 'get_firmware_version'.tests/test_flash.py, which needs no hardware. It uses a realSerialHandleron pyserial'sloop://port withmcbootflashstubbed.Testing
pytest tests/test_flash.py: 3 passed. Withmain'scli.pyandsciencelab.py, all 3 fail.black --check,flake8,pydocstyleandbanditare clean onpslab/cli.py(in the tox lint set).mcbootflash) is unchanged; this only restores the objects handed to it.ScienceLab.read_log()has the same leftover (device.interface.readline(),self.get_ack()). It isn't on the flashing path, so I've left it out of this PR; it's noted in #292.No UI changes.
Summary by Sourcery
Restore PSLab firmware flashing compatibility with the refactored serial connection API.
Bug Fixes:
Tests:
Summary by CodeRabbit
Bug Fixes
Tests