Skip to content
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

bpo-32614: Modify re examples to use a raw string to prevent warning #5265

Merged
merged 4 commits into from
Feb 2, 2018

Conversation

csabella
Copy link
Contributor

@csabella csabella commented Jan 21, 2018

I only modified the examples that would have given a warning.

https://bugs.python.org/issue32614

Copy link
Member

@ericvsmith ericvsmith left a comment

Choose a reason for hiding this comment

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

I agree with the ones that are invalid escape sequences. I can go either way on the '\n' examples, although I would personally not change them.

@@ -1233,7 +1233,7 @@ either side. This takes the job beyond :meth:`!replace`'s abilities.)

Another common task is deleting every occurrence of a single character from a
string or replacing it with another single character. You might do this with
something like ``re.sub('\n', ' ', S)``, but :meth:`~str.translate` is capable of
something like ``re.sub(r'\n', ' ', S)``, but :meth:`~str.translate` is capable of
Copy link
Member

Choose a reason for hiding this comment

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

This change isn't strictly required, since '\n' is valid and won't raise a warning.

Copy link
Member

Choose a reason for hiding this comment

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

This is a bit tricky. '\n' produces a string of length 1. r'\n' produces a string of length 2. But re recognizes the same escapes that Python does, plus the additional ones, so the latter is also cooked down to a singles character, but later by re instead of Python itself. I think I would be inclined to omit 'r' but mention that it can be optionally added even when the literal only has standard escapes.

@@ -1398,7 +1398,7 @@ into a list with each nonempty line having its own entry:
.. doctest::
:options: +NORMALIZE_WHITESPACE

>>> entries = re.split("\n+", text)
>>> entries = re.split(r"\n+", text)
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

@terryjreedy
Copy link
Member

I think you can add to this patch a generic comment about adding an 'r' prefix, as discussed on one of the lists (just today?). For instance, after the r'\d+' example.

"The 'r' prefix, making the literal a raw string literal, is needed because escape sequences in a normal 'cooked' string literal that are not recognized by Python, as opposed to re, now result in a deprecation warning and will eventually become a syntax error."

This should also be someplace in re.rst.

@csabella
Copy link
Contributor Author

@terryjreedy I've made the changes, but there's a separate issue for adding the DeprecationWarning info to the docs - #5255

I had opened this one just to make sure the examples wouldn't produce a warning/error if someone was following them.

@terryjreedy
Copy link
Member

If I understand, you think ready to go except for news entry. Please add.

The failure log just says "There was an error while trying to fetch the log." I clicked Restart button to see if there is a real problem that none of us see.

@terryjreedy
Copy link
Member

The addition of

The `r` prefix, making the literal a raw string literal, is needed in this

causes

$ make check suspicious html SPHINXOPTS="-q -W -j4"
python3 tools/rstlint.py -i tools -i ./venv -i README.rst
[2] howto/regex.rst:472: default role used
1 problem with severity 2 found.
make: *** [check] Error 1

I don't know enough .rst to know the fix.

>>> p.findall('12 drummers drumming, 11 pipers piping, 10 lords a-leaping')
['12', '11', '10']

The `r` prefix, making the literal a raw string literal, is needed in this
Copy link
Member

Choose a reason for hiding this comment

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

Use double backticks:

``r``

Copy link
Member

Choose a reason for hiding this comment

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

Done

@@ -327,6 +329,12 @@ backslashes are not handled in any special way in a string literal prefixed with
while ``"\n"`` is a one-character string containing a newline. Regular
expressions will often be written in Python code using this raw string notation.

In addition, special escape sequences that are valid in ``re``, but not valid
Copy link
Member

Choose a reason for hiding this comment

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

Use RE or regular expressions instead of ``re``.

Copy link
Member

Choose a reason for hiding this comment

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

Done

>>> p.findall('12 drummers drumming, 11 pipers piping, 10 lords a-leaping')
['12', '11', '10']

The `r` prefix, making the literal a raw string literal, is needed in this
example because escape sequences in a normal "cooked" string literal that are
not recognized by Python, as opposed to ``re``, now result in a
Copy link
Member

Choose a reason for hiding this comment

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

Same as above.

Copy link
Member

Choose a reason for hiding this comment

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

Done

@csabella
Copy link
Contributor Author

csabella commented Feb 2, 2018

@terryjreedy and @serhiy-storchaka Thank you! I've made the requested changes.

@terryjreedy
Copy link
Member

Am I correct in thinking this should be backported to 3.7, but not 3.6? Serhiy, do you wish to merge this, or should I?

@serhiy-storchaka
Copy link
Member

Please merge it Terry.

Warnings are emitted starting from 3.6. Examples in 3.6 should be fixed too.

@terryjreedy terryjreedy merged commit 6677142 into python:master Feb 2, 2018
@miss-islington
Copy link
Contributor

Thanks @csabella for the PR, and @terryjreedy for merging it 🌮🎉.. I'm working now to backport this PR to: 3.6, 3.7.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 2, 2018
…ythonGH-5265)

Modify RE examples in documentation to use raw strings to prevent DeprecationWarning.
Add text to REGEX HOWTO to highlight the deprecation.  Approved by Serhiy Storchaka.
(cherry picked from commit 6677142)

Co-authored-by: Cheryl Sabella <cheryl.sabella@gmail.com>
@bedevere-bot
Copy link

GH-5499 is a backport of this pull request to the 3.7 branch.

@miss-islington
Copy link
Contributor

Sorry, @csabella and @terryjreedy, I could not cleanly backport this to 3.6 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 66771422d0541289d0b1287bc3c28e8b5609f6b4 3.6

terryjreedy added a commit to terryjreedy/cpython that referenced this pull request Feb 2, 2018
…rning (pythonGH-5265)

Modify RE examples in documentation to use raw strings to prevent DeprecationWarning.
Add text to REGEX HOWTO to highlight the deprecation.  Approved by Serhiy Storchaka.

(cherry picked from commit 6677142)
terryjreedy added a commit to terryjreedy/cpython that referenced this pull request Feb 2, 2018
…rning (pythonGH-5265)

Modify RE examples in documentation to use raw strings to prevent DeprecationWarning.
Add text to REGEX HOWTO to highlight the deprecation.  Approved by Serhiy Storchaka..
(cherry picked from commit 6677142)
terryjreedy pushed a commit that referenced this pull request Feb 2, 2018
…H-5265) (#5499)

Modify RE examples in documentation to use raw strings to prevent DeprecationWarning.
Add text to REGEX HOWTO to highlight the deprecation.  Approved by Serhiy Storchaka.
(cherry picked from commit 6677142)

Co-authored-by: Cheryl Sabella <cheryl.sabella@gmail.com>
terryjreedy added a commit that referenced this pull request Feb 2, 2018
…a… …rning (GH-5265) (GH-5500)

Modify RE examples in documentation to use raw strings to prevent DeprecationWarning.
Add text to REGEX HOWTO to highlight the deprecation.  Approved by Serhiy Storchaka.

(cherry picked from commit 6677142)
@csabella csabella deleted the redocs branch February 2, 2018 22:42
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.

7 participants