Skip to content

Improve code snippets in single_source_version.rst - #193

Closed
Changaco wants to merge 10 commits into
python:masterfrom
Changaco:patch-2
Closed

Changaco wants to merge 10 commits into
python:masterfrom
Changaco:patch-2

Conversation

@Changaco

@Changaco Changaco commented Dec 3, 2015

Copy link
Copy Markdown
Contributor

These are all the code improvements that were in #190 before it got scaled back, remade in small commits.

Comment thread source/single_source_version.rst Outdated

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Member

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).

Copy link
Copy Markdown
Contributor Author

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'.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Would this be satisfactory?

# Since version numbers only use a subset of ascii, we can read the file
# as bytes, thus avoiding unicode decode errors.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comment added.

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks @ncoghlan :-)

@Changaco

Copy link
Copy Markdown
Contributor Author

If there are no other concerns then this is ready for merge.

@Changaco

Copy link
Copy Markdown
Contributor Author

Ping.

Comment thread source/single_source_version.rst Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

storing in locals might be better? exec(fp.read(), {}, about)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

storing in locals might be better? exec(fp.read(), {}, about)

Why? By separating globals and locals it's no longer obvious that the variables will end up in the right dictionary.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Because globals gets cluttered with all the various builtins. Locals will just contain what __about__.py defines. It may not matter much for this specific use case, but if we're recommending code we should follow good practice (and using locals is good practice here, IMO).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Okay, that makes sense, I've made the change.

@Changaco

Copy link
Copy Markdown
Contributor Author

Anything else?

@Changaco

Copy link
Copy Markdown
Contributor Author

It looks like we all forgot about this PR. Is someone willing to merge it?

@Changaco

Copy link
Copy Markdown
Contributor Author

Okay, I give up.

@Changaco Changaco closed this Jul 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants