#27818: Use contextlib.suppress to suppress exceptions.
-------------------------------------+-------------------------------------
     Reporter:  Mads Jensen          |                    Owner:  Tim
         Type:                       |  Graham <timograham@…>
  Cleanup/optimization               |                   Status:  closed
    Component:  Core (Other)         |                  Version:  master
     Severity:  Normal               |               Resolution:  fixed
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Adam (Chainz) Johnson):

 I only found out about this in review from Tim the other week. I think it
 should be reverted because it's making things slower unnecessarily. I
 count contextlib.suppress as about 10 times slower than plain try/except,
 not to mention it can trigger garbage collection as it creates and
 destroys an object every time:

 {{{
 In [8]: def foo():
    ...:     try:
    ...:         pass
    ...:     except AttributeError:
    ...:         pass
    ...:

 In [9]: def foo2():
    ...:     with contextlib.suppress(AttributeError):
    ...:         pass
    ...:

 In [10]: %timeit foo()
 The slowest run took 10.24 times longer than the fastest. This could mean
 that an intermediate result is being cached.
 10000000 loops, best of 3: 111 ns per loop

 In [11]: %timeit foo2()
 The slowest run took 6.15 times longer than the fastest. This could mean
 that an intermediate result is being cached.
 1000000 loops, best of 3: 1.13 µs per loop
 }}}

 How would we proceed? Should I take this to the mailing list, or can I
 just submit a revert PR?

-- 
Ticket URL: <https://code.djangoproject.com/ticket/27818#comment:7>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/068.dd31b59456297a71bec1eaabdaff508d%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to