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

Never compare bytes and str in putheader() #2141

Merged
merged 2 commits into from Jan 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/urllib3/connection.py
Expand Up @@ -184,7 +184,7 @@ def putrequest(self, method, url, *args, **kwargs):

def putheader(self, header, *values):
""""""
if SKIP_HEADER not in values:
if not any(isinstance(v, str) and v == SKIP_HEADER for v in values):
Copy link
Member

Choose a reason for hiding this comment

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

Thoughts on using type(v) is str instead of isinstance()? Also can we use the -bb option as you've done in brotlicffi to ensure these error in the future?

Copy link
Contributor

Choose a reason for hiding this comment

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

Why type(v) is str?

Copy link
Member Author

Choose a reason for hiding this comment

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

As mentioned in the initial comment, I can't enable -bb because of brotlicffi (and other issues that I haven't investiaged yet).

I think isinstance() is the pythonic way to do this, and it performs just as well as type() is str (consistently less than 100nsec as mesured by timeit on my laptop).

Copy link
Member

Choose a reason for hiding this comment

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

Was looking for a speed win in this often-called function :) It's all good though, let's merge this!

super().putheader(header, *values)
elif to_str(header.lower()) not in SKIPPABLE_HEADERS:
raise ValueError(
Expand Down