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-38698: Prevent UnboundLocalError to pop up in parse_message_id #17277

Merged

Conversation

PCManticore
Copy link
Contributor

@PCManticore PCManticore commented Nov 20, 2019

parse_message_id() was improperly using a token defined inside an exception
handler, which was raising UnboundLocalError on parsing an invalid value.

https://bugs.python.org/issue38698

Automerge-Triggered-By: @maxking

@PCManticore
Copy link
Contributor Author

PCManticore commented Nov 20, 2019

I'm not sure if this is NEWS worthy and can't apply "skip news" by myself.

@corona10 corona10 requested a review from maxking Nov 24, 2019
Copy link
Member

@corona10 corona10 left a comment

@maxking

Since you are the core developer, I add you as the reviewer for this PR.
Please take a look at this PR, please.

This issue should be fixed and the solution is very easy to fix.

@@ -2113,7 +2113,8 @@ def parse_message_id(value):
except errors.HeaderParseError:
message_id.defects.append(errors.InvalidHeaderDefect(
"Expected msg-id but found {!r}".format(value)))
message_id.append(token)
else:
Copy link
Member

@corona10 corona10 Nov 24, 2019

Choose a reason for hiding this comment

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

I'd like you to change the code like below.

     message_id = MessageID()
     try:
         token, value = get_msg_id(value)
+        message_id.append(token)
     except errors.HeaderParseError:
         message_id.defects.append(errors.InvalidHeaderDefect(
             "Expected msg-id but found {!r}".format(value)))
-    else:
-        message_id.append(token)
     return message_id

Copy link
Contributor Author

@PCManticore PCManticore Nov 24, 2019

Choose a reason for hiding this comment

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

What is wrong with having the append call inside the else branch? Especially since the body of the try statement contains just the instruction that might fail. This seems to me like an extra nitpicky comment for a personal flavour, not necessarily an issue with the code itself.

Copy link
Member

@corona10 corona10 Nov 24, 2019

Choose a reason for hiding this comment

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

It didn't mean your code was wrong. I apologize if you feel bad.
As I read the code again, the original author's intention seems to be the code you proposed.

@@ -2113,7 +2113,8 @@ def parse_message_id(value):
except errors.HeaderParseError:
message_id.defects.append(errors.InvalidHeaderDefect(
"Expected msg-id but found {!r}".format(value)))
message_id.append(token)
else:
Copy link
Member

@corona10 corona10 Nov 24, 2019

Choose a reason for hiding this comment

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

It didn't mean your code was wrong. I apologize if you feel bad.
As I read the code again, the original author's intention seems to be the code you proposed.

@maxking
Copy link
Contributor

maxking commented Dec 1, 2019

@corona10 We do need a NEWS item for this, it is a bugfix. Skip News label should be used only for trivial doc changes.

The changes look good, thanks for your contribution @PCManticore, can you please add a NEWS entry?

Devguide has explanation on using the blurb tool to generate a NEWS entry. https://devguide.python.org/committing/#what-s-new-and-news-entries

@maxking maxking removed the skip news label Dec 1, 2019
parse_message_id() was improperly using a token defined inside an exception
handler, which was raising `UnboundLocalError` on parsing an invalid value.
@PCManticore PCManticore force-pushed the bpo-38698-unbound-local-error branch from 14ad49b to 1dddda0 Compare Dec 2, 2019
@PCManticore
Copy link
Contributor Author

PCManticore commented Dec 2, 2019

@maxking Makes sense, all done!

maxking
maxking approved these changes Dec 5, 2019
Copy link
Contributor

@maxking maxking left a comment

Fix LGTM!

@maxking maxking added 🤖 automerge PR will be merged once it's been approved and all CI passed type-bug An unexpected behavior, bug, or error labels Dec 5, 2019
@miss-islington
Copy link
Contributor

miss-islington commented Dec 5, 2019

Sorry, I can't merge this PR. Reason: Base branch was modified. Review and try the merge again..

@miss-islington miss-islington merged commit bb81549 into python:master Dec 5, 2019
@maxking maxking removed the 🤖 automerge PR will be merged once it's been approved and all CI passed label Dec 5, 2019
@tirkarthi
Copy link
Member

tirkarthi commented Dec 5, 2019

I think this can be backported to 3.8 too which has the same issue.

@maxking
Copy link
Contributor

maxking commented Dec 5, 2019

You are of course right @tirkarthi :)
I missed that.

@miss-islington
Copy link
Contributor

miss-islington commented Dec 5, 2019

Thanks @PCManticore for the PR 🌮🎉.. I'm working now to backport this PR to: 3.8.
🐍🍒🤖

@bedevere-bot
Copy link

bedevere-bot commented Dec 5, 2019

GH-17476 is a backport of this pull request to the 3.8 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 5, 2019
…ythonGH-17277)

parse_message_id() was improperly using a token defined inside an exception
handler, which was raising `UnboundLocalError` on parsing an invalid value.

https://bugs.python.org/issue38698
(cherry picked from commit bb81549)

Co-authored-by: Claudiu Popa <pcmanticore@gmail.com>
miss-islington added a commit that referenced this pull request Dec 5, 2019
…H-17277)

parse_message_id() was improperly using a token defined inside an exception
handler, which was raising `UnboundLocalError` on parsing an invalid value.

https://bugs.python.org/issue38698
(cherry picked from commit bb81549)

Co-authored-by: Claudiu Popa <pcmanticore@gmail.com>
shihai1991 pushed a commit to shihai1991/cpython that referenced this pull request Jan 31, 2020
…ythonGH-17277)

parse_message_id() was improperly using a token defined inside an exception
handler, which was raising `UnboundLocalError` on parsing an invalid value.




https://bugs.python.org/issue38698
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants