Skip to content

Avoid __file__ in hashlib example #134540

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

Merged
merged 3 commits into from
May 22, 2025
Merged

Avoid __file__ in hashlib example #134540

merged 3 commits into from
May 22, 2025

Conversation

thejcannon
Copy link
Contributor

@thejcannon thejcannon commented May 22, 2025

I love the cleverness and quirkiness of the hashlib documentation choosing to hash the hashlib module itself, however it is exactly that: "clever" and "quirky". Unfortunately, I've seen Python newbies and intermediates struggling to understand this example when trying to hash files themselves (not the least because the example does hashlib.__file__ and not __file__ or some_other_module.__file__).

So, let's move to something more "obvious" 👍 at the cost of being a slightly less silly place 👎

Open question to the room on whether this should be backported.


📚 Documentation preview 📚: https://cpython-previews--134540.org.readthedocs.build/

@@ -284,7 +284,7 @@ a file or file-like object.
Example:

>>> import io, hashlib, hmac
>>> with open(hashlib.__file__, "rb") as f:
>>> with open("path/to/file", "rb") as f:
Copy link
Member

Choose a reason for hiding this comment

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

the previous code worked because it passes doctest. It probably runs from the repo root, what happens if you use "Doc/library/hashlib.rst" here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, now its even cleverer in that this was hand-picked because we know its gonna pass doctest? 🤦

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
>>> with open("path/to/file", "rb") as f:
>>> with open("Doc/library/hashlib.rst", "rb") as f:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

    FileNotFoundError: [Errno 2] No such file or directory: 'Doc/library/hashlib.rst'

https://github.com/python/cpython/actions/runs/15196498586/job/42741793385?pr=134540#step:8:405

@gpshead gpshead self-assigned this May 22, 2025
@gpshead gpshead added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes skip issue labels May 22, 2025
@gpshead gpshead merged commit 9b292ff into python:main May 22, 2025
28 checks passed
@github-project-automation github-project-automation bot moved this from Todo to Done in Docs PRs May 22, 2025
@miss-islington-app
Copy link

Thanks @thejcannon for the PR, and @gpshead for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 22, 2025
(cherry picked from commit 9b292ff)

Co-authored-by: Josh Cannon <joshdcannon@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 22, 2025
(cherry picked from commit 9b292ff)

Co-authored-by: Josh Cannon <joshdcannon@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented May 22, 2025

GH-134549 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label May 22, 2025
@bedevere-app
Copy link

bedevere-app bot commented May 22, 2025

GH-134550 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label May 22, 2025
@thejcannon thejcannon deleted the patch-2 branch May 22, 2025 21:32
gpshead pushed a commit that referenced this pull request May 22, 2025
Avoid `__file__` in `hashlib` example (GH-134540)
(cherry picked from commit 9b292ff)

Co-authored-by: Josh Cannon <joshdcannon@gmail.com>
gpshead pushed a commit that referenced this pull request May 22, 2025
Avoid `__file__` in `hashlib` example (GH-134540)
(cherry picked from commit 9b292ff)

Co-authored-by: Josh Cannon <joshdcannon@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip issue skip news
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants