Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Never compare bytes and str in putheader()#2141

Merged
sethmlarson merged 2 commits into
urllib3:mainfrom
pquentin:bytes-str-comparison
Jan 26, 2021
Merged

Never compare bytes and str in putheader()#2141
sethmlarson merged 2 commits into
urllib3:mainfrom
pquentin:bytes-str-comparison

Conversation

@pquentin
Copy link
Copy Markdown
Member

@pquentin pquentin commented Jan 25, 2021

Fixes #2071. It's ugly, but I don't know how else to fix that.

I can't enable -bb before python-hyper/brotlicffi#177 is merged and released. (edit: disregard, there are other issues to fix before we can enable -bb.)

@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 25, 2021

Codecov Report

Merging #2141 (f0fb095) into main (d0b2076) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              main     #2141   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           25        25           
  Lines         2216      2216           
=========================================
  Hits          2216      2216           
Impacted Files Coverage Δ
src/urllib3/connection.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d0b2076...f0fb095. Read the comment docs.

@mgorny
Copy link
Copy Markdown
Contributor

mgorny commented Jan 25, 2021

I agree it's ugly-ish but nevertheless kudos for finding some way to solve this. I've been pondering on it for some time and couldn't come up with anything even that good.

@pquentin
Copy link
Copy Markdown
Member Author

pquentin commented Jan 25, 2021

Actually with the second commit it's not that bad! I wonder if I should add a comment.

Copy link
Copy Markdown
Member

@sethmlarson sethmlarson left a comment

Choose a reason for hiding this comment

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

This looks good, one thought:

Comment thread src/urllib3/connection.py
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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!

Copy link
Copy Markdown
Member

@sethmlarson sethmlarson left a comment

Choose a reason for hiding this comment

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

LGTM

@sethmlarson sethmlarson merged commit e47d3e7 into urllib3:main Jan 26, 2021
@pquentin pquentin deleted the bytes-str-comparison branch January 27, 2021 06:22
This was referenced Mar 12, 2021
This was referenced Mar 16, 2021
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.

connection.py: BytesWarning: Comparison between bytes and string: if SKIP_HEADER not in values:

4 participants