brunoerg
commented at 5:43 pm on April 26, 2022:
member
When manipulating a file in Python without using with(), you have to close the file manually, so this PR does it in get_block_hashes (contrib/linearize/linearize-data.py).
Edit: this PR does it for all occurances that previously weren’t using with.
fanquake added the label
Scripts and tools
on Apr 26, 2022
laanwj
commented at 6:01 pm on April 26, 2022:
member
When manipulating a file in Python without using with(),
You’re right, though prefer fixing this by using with …. Or is there a specific reason to avoid it here?
brunoerg
commented at 6:09 pm on April 26, 2022:
member
You’re right, though prefer fixing this by using with …. Or is there a specific reason to avoid it here?
@laanwj, I didn’t do it before because since in other places it closes the file after manipulating it (without using with), it would be simple just add a close()here, but I 100% prefer with, can I modify all the occurances to use with?
theStack
commented at 12:52 pm on April 27, 2022:
member
You’re right, though prefer fixing this by using with …. Or is there a specific reason to avoid it here?
@laanwj, I didn’t do it before because since in other places it closes the file after manipulating it (without using with), it would be simple just add a close()here, but I 100% prefer with, can I modify all the occurances to use with?
Concept ACK on identifying further such cases and fixing them. Especially for small scripts not closing is not a big deal in practice though; here is a nice list of arguments why it it strongly recommended to do it anyway: https://stackoverflow.com/a/25070939
Assuming that the following used regex is reliable, we have 17 occurences of open() calls without the with syntax (didn’t check them in detail, but I guess many of them never call close):
laanwj
commented at 3:06 pm on April 27, 2022:
member
can I modify all the occurances to use with?
Sounds good to me. It removes the burden of having the explicitly think about closing (removes the entire bug class of “forget to close”) so is very much preferred when it’s possible.
brunoerg
commented at 4:51 pm on April 27, 2022:
member
brunoerg renamed this:
contrib: close file after manipulating it in `linearize-data`
test, contrib, refactor: use with when opening a file
on Apr 27, 2022
brunoerg
commented at 7:30 pm on April 27, 2022:
member
Force-pushed addressing @laanwj and @theStack comments. Just changed the title to address it.
brunoerg marked this as a draft
on Apr 27, 2022
brunoerg force-pushed
on Apr 27, 2022
test, contrib, refactor: use `with` when opening a file027aab663a
brunoerg force-pushed
on Apr 27, 2022
Gitleyla approved
brunoerg marked this as ready for review
on Apr 27, 2022
DrahtBot
commented at 2:10 am on April 28, 2022:
member
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
Conflicts
Reviewers, this pull request conflicts with the following ones:
#24985 (doc, test: Compilation for 64-bit Windows with msys2 by rnapoles)
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.
brunoerg renamed this:
test, contrib, refactor: use with when opening a file
test, contrib, refactor: use `with` when opening a file
on Apr 29, 2022
brunoerg
commented at 12:34 pm on April 29, 2022:
member
I think we could put in the docs a recommendation to use when when opening files in Python. What do you think?
laanwj
commented at 5:51 pm on May 4, 2022:
member
I think we could put in the docs a recommendation to use when when opening files in Python. What do you think?
Not sure. Isn’t that like a general Python thing, not specific to our project? I don’t think it’s worthwhile to maintain a document of basic Python programming language best practices in our project.
laanwj merged this
on May 4, 2022
laanwj closed this
on May 4, 2022
sidhujag referenced this in commit
53da4898be
on May 4, 2022
This is a metadata mirror of the GitHub repository
bitcoin/bitcoin.
This site is not affiliated with GitHub.
Content is generated from a GitHub metadata backup.
generated: 2024-12-21 15:12 UTC
This site is hosted by @0xB10C More mirrored repositories can be found on mirror.b10c.me