app: use explicit ValueError instead of assert in projects_dir_validate (fixes #5403)#5448
Open
adityachilka1 wants to merge 1 commit into
Open
Conversation
…te (fixes platformio#5403) `projects_dir_validate()` used `assert os.path.isdir(pr)] to reject invalid paths. Python drops all `assert` statements under `-O` (and the PYTHONOPTIMIZE envvar), so invalid `projects_dir` values were silently accepted in optimised builds and the failure later surfaced in unrelated codepaths. Raise `ValueError` explicitly so the check survives `-O`. The caller (`sanitize_setting()`) catches any exception from a validator and converts it to `InvalidSettingValue`, so the user-facing error path is unchanged. Adds `tests/misc/test_app.py` with three cases: * existing dir returns the absolute path * nonexistent path raises ValueError * file (not-a-dir) raises ValueError
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5403.
The bug
platformio/app.py:Python drops
assertstatements under-O(and when thePYTHONOPTIMIZEenvironment variable is set). On production images and distro packages that run
Python in optimised mode, any string - even a fake path - gets through the
validator untouched. The error then surfaces downstream (e.g. when the projects
directory is later globbed), where it is harder to diagnose as a config issue.
The fix
Replace the
assertwith an explicitraise ValueError. One-line diff:Why ValueError
Caller (
sanitize_setting, platformio/app.py:168) wraps any exception fromvalidators into
exception.InvalidSettingValueviaraise ... from exc. Sothe user-facing error path is identical.
ValueErroris idiomatic for"argument has the correct type but wrong value".
Tests
Adds
tests/misc/test_app.pywith three cases:Verification (local)
Also verified that the pre-fix code (replicated in a sandbox) silently returned
an invalid path under
-O.No behavioural regression
assertraisedAssertionError(a subclass of
Exception) whichsanitize_settingcaught. Now raisesValueErrorwhich it still catches. No user-facing difference.-O) runs: was a silent pass-through. Now correctly raises.Related
The CONTRIBUTING.md requires a CLA. I'm filing this as an individual contributor
(@adityachilka1) and will sign the CLA through CLA-assistant when the bot prompts.