BIP-0327: avoid using sys.path[0] to find current working directory#2158
Open
omipheo wants to merge 2 commits into
Open
BIP-0327: avoid using sys.path[0] to find current working directory#2158omipheo wants to merge 2 commits into
omipheo wants to merge 2 commits into
Conversation
`bip-0327/reference.py` and `bip-0327/gen_vectors_helper.py` both locate the test-vector JSON files via `os.path.join(sys.path[0], 'vectors', '<file>.json')`. As Sebastian Falbesoner noted in 4e18ee6 ("BIP-374: avoid using sys.path[0] to find current working directory"), this pattern is incompatible with any future `sys.path.insert(0, ...)` extension — for example, the `sys.path.insert(0, str(Path(__file__).parent / "secp256k1lab/src"))` shim that bip-0352 and bip-0374 use to locate their vendored `secp256k1lab` copies. Should bip-0327 follow the same vendoring pattern, every test-vector load would silently miss its file. Switch both files to the `Path(__file__).parent / 'vectors' / '...'` pattern so the path is always resolved relative to the script itself, matching the BIP-374 precedent. `import os` and `import sys` are removed from `reference.py` (no remaining uses); `gen_vectors_helper.py` gets an explicit `from pathlib import Path` since it can no longer inherit those names through `from reference import *`. Verified that `python3 reference.py` and `python3 gen_vectors_helper.py` still exit 0 with all 8 (resp. 2) vector files loading correctly. No behavior change beyond the path-resolution robustness.
jonatack
reviewed
May 14, 2026
Member
jonatack
left a comment
There was a problem hiding this comment.
ACK, it's a minor refactor but more idiomatic and robust and would serve as a better reference for developers.
Member
|
@omipheo note that BIPs 89, 328 and 340 also still use the same pattern. Maybe update them all here in one go. |
…rectory Same fix as the previous commit on BIP-0327, applied to the remaining reference implementations that still use the `os.path.join(sys.path[0], ...)` pattern: bip-0089/reference.py (8 sites), bip-0328/reference.py (1 site), and bip-0340/reference.py (1 site). All resolve test-vector paths via `Path(__file__).parent / ...` so future `sys.path.insert(0, ...)` extensions for vendored deps won't silently miss the data files. `import os` and `import sys` are dropped from each file (no remaining uses). bip-0340/test-vectors.py imports `sys` itself for `sys.stdout` so its wildcard `from reference import *` is unaffected by the import cleanup in bip-0340/reference.py. Verified: `python3 reference.py` for each of bip-0089, bip-0328, bip-0340 still exits 0 with all test vectors loading; bip-0340's test-vectors.py importer also still exits 0. Per @jonatack's review note on PR bitcoin#2158.
Contributor
Author
|
Done — pushed Verified each script still runs clean: Happy to squash both commits into one if you'd prefer a single atomic change before merge. |
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.
Summary
bip-0327/reference.pyandbip-0327/gen_vectors_helper.pyboth locate the test-vector JSON files viaos.path.join(sys.path[0], 'vectors', '<file>.json'). As Sebastian Falbesoner noted in 4e18ee6 ("BIP-374: avoid using sys.path[0] to find current working directory"), this pattern is incompatible with any futuresys.path.insert(0, ...)extension — for example, thesys.path.insert(0, str(Path(__file__).parent / "secp256k1lab/src"))shim that bip-0352 and bip-0374 use to locate their vendoredsecp256k1labcopies. Should bip-0327 follow the same vendoring pattern, every test-vector load would silently miss its file.This PR switches both files to the
Path(__file__).parent / 'vectors' / '...'pattern so the path is always resolved relative to the script itself, matching the BIP-374 precedent.import osandimport sysare removed fromreference.py(no remaining uses);gen_vectors_helper.pygets an explicitfrom pathlib import Pathsince it can no longer inherit those names throughfrom reference import *.Diff
os.path.join(sys.path[0], 'vectors', 'X.json')Path(__file__).parent / 'vectors' / 'X.json'reference.py: 8 call sites (lines 499, 508, 532, 559, 577, 660, 717, 764).gen_vectors_helper.py: 2 call sites (lines 21, 49).Imports adjusted to match.
Verification
Both scripts continue to exit 0 with all test vectors loading.
(Note:
bash bip-0327/tests.shexits non-zero on master too — current local mypy versions flag four pre-existingbytearray/bytesmismatches atreference.py:354,355,671,672that are unrelated to this PR. CI is presumably pinned to an older mypy; the runtime tests themselves pass on both branches.)Notes
Same shape, same justification, applied to the next reference implementation in the same family.