Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

main: redesign boot_out.txt writing #5536

Merged
merged 2 commits into from
Nov 3, 2021

Conversation

jepler
Copy link
Member

@jepler jepler commented Nov 2, 2021

New design:

  • capture output to a vstr
  • compare the complete vstr to boot_out.txt
  • rewrite if not a complete match

This is resilient against future changes to the automatic text written to boot_out.txt.

This also fixes rewriting boot_out.txt in the case where boot.py prints something.

Perhaps it also saves a bit of code space. Some tricks:

  • no need to close a file in read mode
  • no need to switch on/off USB write access, going down to the oofatfs layer doesn't check it anyway

This is a possible alternative to

New design:
 * capture output to a vstr
 * compare the complete vstr to boot_out.txt
 * rewrite if not a complete match

This is resilient against future changes to the automatic
text written to boot_out.txt.

This also fixes rewriting boot_out.txt in the case where
boot.py prints something.

Perhaps it also saves a bit of code space. Some tricks:
 * no need to close a file in read mode
 * no need to switch on/off USB write access, going down to the
   oofatfs layer doesn't check it anyway
Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

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

What happens if a write goes past the end of the vstr? Does it just discard the overflow?

How did you test? The easiest way I found to test whether a rewrite happened or not was to lengthen the 1 second delay to 10 seconds. I had a code.py that blinked board.LED, so I knew when it started.

@jepler
Copy link
Member Author

jepler commented Nov 2, 2021

What happens if a write goes past the end of the vstr? Does it just discard the overflow?

The allocation is increased, until it fails. This means you could now MemoryError yourself if boot.py produces a lot of output.

How did you test? The easiest way I found to test whether a rewrite happened or not was to lengthen the 1 second delay to 10 seconds. I had a code.py that blinked board.LED, so I knew when it started.

I temporarily had code that turned the LED red during the 1 second delay before writing out the file. After changing boot.py to output something else and hitting reset, I would get the red LED once but not the next time I hit reset.

@dhalbert
Copy link
Collaborator

dhalbert commented Nov 2, 2021

What happens if a write goes past the end of the vstr? Does it just discard the overflow?

The allocation is increased, until it fails. This means you could now MemoryError yourself if boot.py produces a lot of output.

That's a bit of a pathological case, but I think the exception should be caught if possible so you don't get no output, which is what would happen.

@dhalbert
Copy link
Collaborator

dhalbert commented Nov 2, 2021

If it's hard to catch the MemoryError, then you could just truncate the output after some relatively small amount (a few thousand chars), and add an ellipsis or something.

Now this boot.py:
```py
for i in range(1000):
    print(i)
```
creates a 512-byte boot_out.txt that ends
```
88
89
...
```
@jepler
Copy link
Member Author

jepler commented Nov 2, 2021

Now this boot.py:

for i in range(1000):
    print(i)

creates a 512-byte boot_out.txt that ends

88
89
...

@jepler jepler requested a review from dhalbert November 2, 2021 23:25
Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

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

I like it! Thanks for fixing this well.

@dhalbert dhalbert merged commit 4770120 into adafruit:main Nov 3, 2021
jepler added a commit to jepler/circuitpython that referenced this pull request Nov 8, 2021
Back in adafruit#5536 I modified how boot_out.txt got written. However, I broke
USB enumeration in the safe-mode case.  This fixes it so that a
safe-mode board still connects on USB with all defaults. (tested on a
macropad)
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.

Code gets wiped when power-cycling between 2.8 and 3.0 seconds
2 participants