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

Is fp closed checks closed first #435

Merged
merged 3 commits into from
Jul 31, 2014

Conversation

novocaine
Copy link

for #434

@Lukasa
Copy link
Sponsor Contributor

Lukasa commented Jul 30, 2014

This seems reasonable to me. As to @piotr-dobrogost's suggestion, changing the style seems reasonable but I suggest placing it in the except clause rather than the try clause (which I suggest is what Piotr probably meant. =) )

@shazow
Copy link
Member

shazow commented Jul 30, 2014

+1, they should either both be hasattr/getattr or both be in a try/except clause.

Also please keep comments and strings consistent with the rest of the codebase (full sentences, start with a capital, end with a period, etc).

Thanks for putting this together. :)

@novocaine
Copy link
Author

Amended with suggestions.

I would prefer to have the two checks at the same level of indentation rather than putting the second in the except clause; it reads more like a sequential series of checks, and if there are subsequent checks added later the indentation won't get out of control.

@shazow
Copy link
Member

shazow commented Jul 31, 2014

This is good, thank you. I'll merge it later this week. :)

I think in general we prefer hasattr/getattr instead of exception-catching where possible because raising/catching exceptions in a tight loop tends to be rather slow. Also reduces the risk of accidentally catching an unrelated exception. Maybe I'll tweak that later.

@shazow shazow added the Ready label Jul 31, 2014
@novocaine
Copy link
Author

https://twitter.com/raymondh/status/492554332764520448

On Thu, Jul 31, 2014 at 10:38 AM, Andrey Petrov notifications@github.com
wrote:

This is good, thank you. I'll merge it later this week. :)

I think in general we prefer hasattr/getattr instead of exception-catching
where possible because raising/catching exceptions in a tight loop tends to
be rather slow. Also reduces the risk of accidentally catching an unrelated
exception. Maybe I'll tweak that later.


Reply to this email directly or view it on GitHub
#435 (comment).

@shazow
Copy link
Member

shazow commented Jul 31, 2014

Can't believe I dug this up but...

https://github.com/python-git/python/blob/master/Python/bltinmodule.c#L860

Wonder if C-level exception handling is more efficient than Python-level, though.

In [1]: foo = object()

In [2]: def a():
   ...:     return hasattr(foo, 'bar')
   ...:

In [3]: def b():
   ...:     return getattr(foo, 'bar', None)
   ...:

In [4]: def c():
   ...:     try:
   ...:         return foo.bar
   ...:     except AttributeError:
   ...:         return False
   ...:

In [5]: %timeit a
10000000 loops, best of 3: 38.7 ns per loop

In [6]: %timeit b
10000000 loops, best of 3: 44.6 ns per loop

In [7]: %timeit c
10000000 loops, best of 3: 37.4 ns per loop

¯_(ツ)_/¯

Looks like it doesn't matter much anymore. Will keep it as-is. Thanks @novocaine. :)

shazow added a commit that referenced this pull request Jul 31, 2014
@shazow shazow merged commit 18cf53a into urllib3:master Jul 31, 2014
shazow added a commit that referenced this pull request Jul 31, 2014
@requiredfield
Copy link

Just a heads up that this change caused compatibility issues with Python 3 and PyPy for cachecontrol (see psf/cachecontrol#39). In case the urllib3 / requests maintainers / subscribers of this thread want to investigate further!

@sigmavirus24
Copy link
Contributor

@requiredfield we (requests maintainers) do watch urllib3's issues and threads. We also watch cachecontrol. The issue you link to was fixed and the fix was released. It's being included in pip as well. Thank you for thinking of us.

@requiredfield
Copy link

Didn't realize you watched cachecontrol already, sorry for the unnecessary ping.

@sigmavirus24
Copy link
Contributor

@requiredfield No need to apologize! I appreciate the ping as I'm sure @shazow does. (@Lukasa was in IRC with the rest of us)

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.

None yet

6 participants