Repository navigation
Improve code snippets in single_source_version.rst #193
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
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
63c7bc4
fix and simplify option 1
Changaco 0aaf755
remove variant that only works with python 2
Changaco 0349bde
fix option 3: open the file in binary mode
Changaco 676374e
handle paths properly
Changaco 8bf114a
explicitly show the `setup()` call like above
Changaco 23e18e0
add imports so the reader can simply copy-paste
Changaco 5b23f1a
use `__about__.py` instead of `version.py`
Changaco 09472c8
add comments to explain why we do it that way
Changaco ad5eff8
add comment explaining why we manipulate bytes
Changaco 90197a6
don't clutter the `about` dict with globals
Changaco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add some explanation of the use of 'rb' and 'br' to convince readers (and me) that this will always work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see this change as an advantage. It's technically correct, I guess, but the files in question are actually text files, and reading them as such seems more logical (even if it does mean you need to think about encodings).
Binary reading a text file just to avoid thinking about encodings feels like a hack (and the sort of thinking that the string changes in Python 3 were explicitly designed to discourage).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What we're trying to extract, i.e. the version number, only uses a subset of ascii characters, so we don't need to deal with unicode. 'rb' tells python to read the file as bytes without trying to decode them, and since we're reading bytes we need the regexp pattern to be bytes (otherwise python 3 will raise
TypeError: cannot use a string pattern on a bytes-like object), thus 'br'.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this be satisfactory?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: I did search the stdlib for a function that properly decodes a python file (i.e. by looking for the
# coding:header), but I couldn't find one, so reading the file as bytes is easier.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These exist in Python 3 (as tokenize.open() and tokenize.detect_encoding()), but for 2/3 compatible code, assuming ASCII compatibility and reading the bytes is the most straightforward option. We assume ASCII compatibility in other aspects of the packaging ecosystem (most notably distribution package names), so it's a reasonable assumption to make here as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @ncoghlan :-)