Skip to content

fix(tsp_control): split load_script() by line when total length exceeds threshold - #581

Closed
bestcal wants to merge 3 commits into
tektronix:mainfrom
bestcal:fix-tsp-load-script-len
Closed

fix(tsp_control): split load_script() by line when total length exceeds threshold#581
bestcal wants to merge 3 commits into
tektronix:mainfrom
bestcal:fix-tsp-load-script-len

Conversation

@bestcal

@bestcal bestcal commented May 17, 2026

Copy link
Copy Markdown

Fixes #500

Problem

The load_script() method in TSPControl previously assembled the entire
TSP script command—including the loadscript <name>, body, and endscript
wrapper—into a single string, then sent it via one write() call.

Real TSP devices enforce a maximum write length (commonly 1024 chars). When
the total command exceeds this, the script silently fails or raises a device
error. Issue #500 reports exactly this failure with long user scripts.

Solution

Introduce a hybrid split strategy based on a TSP_MAX_WRITE_LENGTH = 1000
threshold:

Condition Behavior Rationale
len(script_command) >= 1000 Send line-by-line: loadscript name, then each line of the body via self.write(line), finally endscript Avoids exceeding device buffer on long scripts
len(script_command) < 1000 Keep original single-write call Preserves backward compatibility; existing simulation/backplane tests match exact query strings

Key implementation details:

  • The threshold is intentionally conservative (1000) to leave head-room for
    device-specific limits and protocol overhead.
  • Only the assembled command length is checked; empty lines are still sent
    (they are idempotent for TSP).
  • No public API surface is changed—fully backward-compatible.

Testing

A new regression test test_load_script_split_long_lines is added in
tests/test_smu.py that asserts:

  1. Long script (45 lines, ~1200 chars assembled):
    load_script() splits into multiple write() calls, each ≤ 1000 chars.
  2. Short script (print("hello"), ~40 chars assembled):
    load_script() still uses a single write() call, matching existing
    simulator expectations.

Full test run (pytest tests/test_smu.py -k "test_smu or test_load_script_split_long_lines") passes 5/5:

test_smu.py::test_smu ✓
test_smu.py::test_smu2450 ✓
test_smu.py::test_smu2401 ✓
test_smu.py::test_smu6430 ✓
test_smu.py::test_load_script_split_long_lines ✓

Checklist

  • Bug fix (non-breaking change which fixes an issue)
  • Added regression test covering both split and non-split paths
  • All existing tests continue to pass
  • No public API changes

@bestcal
bestcal requested a review from a team as a code owner May 17, 2026 01:27
@CLAassistant

CLAassistant commented May 17, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ bestcal
❌ pre-commit-ci[bot]
You have signed the CLA already but the status is still pending? Let us recheck it.

@tek-githubbot-1010
tek-githubbot-1010 requested a review from a team May 17, 2026 01:27
@bestcal
bestcal force-pushed the fix-tsp-load-script-len branch from 03f6138 to 1e50789 Compare May 17, 2026 01:45
@bestcal

bestcal commented May 17, 2026

Copy link
Copy Markdown
Author

pre-commit.ci autofix

…ds threshold

Fixes tektronix#500

The TSP load_script() method previously sent the entire script command
(including loadscript/endscript wrapper) in a single write() call.
Devices enforce a maximum write length; scripts longer than ~1000 chars
would silently fail or error out.

This change introduces a hybrid strategy:
- If total script command length >= 1000, send it line-by-line:
  loadscript <name>, each line of the body, then endscript.
- Otherwise, keep the original single-write behavior for backward
  compatibility with simulation/backplane tests.

A regression test is added that asserts:
- A 45-line long script is split into multiple write() calls
- A short script uses a single write() call
@bestcal
bestcal force-pushed the fix-tsp-load-script-len branch from cc98674 to f52fac7 Compare May 17, 2026 01:56
@bestcal

bestcal commented May 17, 2026

Copy link
Copy Markdown
Author

pre-commit.ci autofix

# script_body argument is overwritten by file contents
script_body = Path(file_path).read_text(encoding="utf-8").strip()

tsp_max_write_length = 1000 # TSP devices require a write_termination within 1000 chars

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend making this a private, class constant. Then subclasses can overwrite it if needed.

Comment thread pyproject.toml

[dependency-groups]
dev = [
"pytest-order>=1.4.0",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be necessary, please remove this

- Move tsp_max_write_length to a private class constant _TSP_MAX_WRITE_LENGTH
  so subclasses can override it if needed.
- Remove the unnecessary [dependency-groups] section from pyproject.toml.
Comment thread tests/test_smu.py
assert written_calls[1] == "loadscript longscript"
# Every call should be ≤ 1000 characters
for idx, call in enumerate(written_calls):
assert len(call) <= 1000, (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should access the private variable from the device driver

@nfelt14

nfelt14 commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

Closing this PR based on a lack of interaction and updates.

@nfelt14 nfelt14 closed this Jul 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: TSP devices don't check for a write_termination character when loading a script

4 participants