Skip to content

bpo-38698: Prevent UnboundLocalError to pop up in parse_message_id #17277

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

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

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

Copy link
Member

@corona10 corona10 left a comment

Choose a reason for hiding this comment

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

@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

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

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

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

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 December 2, 2019 09:36
@PCManticore
Copy link
Contributor Author

@maxking Makes sense, all done!

Copy link
Contributor

@maxking maxking left a comment

Choose a reason for hiding this comment

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

Fix LGTM!

@maxking maxking added 🤖 automerge type-bug An unexpected behavior, bug, or error labels Dec 5, 2019
@miss-islington
Copy link
Contributor

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
@tirkarthi
Copy link
Member

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

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

@bedevere-bot
Copy link

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 <[email protected]>
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 <[email protected]>
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.

7 participants